server-proxy.mock.js 15.0 KB
Newer Older
D
Dmitry Kalinin 已提交
1
// Copyright (C) 2020-2021 Intel Corporation
V
Vitaliy Nishukov 已提交
2 3
//
// SPDX-License-Identifier: MIT
B
Boris Sekachev 已提交
4 5 6

const {
    tasksDummyData,
D
Dmitry Kalinin 已提交
7
    projectsDummyData,
B
Boris Sekachev 已提交
8
    aboutDummyData,
9
    formatsDummyData,
B
Boris Sekachev 已提交
10 11
    shareDummyData,
    usersDummyData,
B
Boris Sekachev 已提交
12 13 14
    taskAnnotationsDummyData,
    jobAnnotationsDummyData,
    frameMetaDummyData,
15
    cloudStoragesDummyData,
B
Boris Sekachev 已提交
16 17
} = require('./dummy-data.mock');

18
function QueryStringToJSON(query, ignoreList = []) {
D
Dmitry Kalinin 已提交
19 20 21 22 23
    const pairs = [...new URLSearchParams(query).entries()];

    const result = {};
    for (const pair of pairs) {
        const [key, value] = pair;
24 25 26 27 28 29
        if (!ignoreList.includes(key)) {
            if (['id'].includes(key)) {
                result[key] = +value;
            } else {
                result[key] = value;
            }
D
Dmitry Kalinin 已提交
30 31 32 33 34 35
        }
    }

    return JSON.parse(JSON.stringify(result));
}

B
Boris Sekachev 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49
class ServerProxy {
    constructor() {
        async function about() {
            return JSON.parse(JSON.stringify(aboutDummyData));
        }

        async function share(directory) {
            let position = shareDummyData;

            // Emulation of internal directories
            if (directory.length > 1) {
                const components = directory.split('/');

                for (const component of components) {
V
Vitaliy Nishukov 已提交
50
                    const idx = position.map((x) => x.name).indexOf(component);
B
Boris Sekachev 已提交
51 52 53
                    if (idx !== -1 && 'children' in position[idx]) {
                        position = position[idx].children;
                    } else {
V
Vitaliy Nishukov 已提交
54
                        throw new window.cvat.exceptions.ServerError(`${component} is not a valid directory`, 400);
B
Boris Sekachev 已提交
55 56 57 58 59 60 61
                    }
                }
            }

            return JSON.parse(JSON.stringify(position));
        }

62 63 64 65
        async function formats() {
            return JSON.parse(JSON.stringify(formatsDummyData));
        }

B
Boris Sekachev 已提交
66 67 68 69 70 71 72 73
        async function exception() {
            return null;
        }

        async function login() {
            return null;
        }

74 75 76 77
        async function logout() {
            return null;
        }

D
Dmitry Kalinin 已提交
78
        async function getProjects(filter = '') {
79
            const queries = QueryStringToJSON(filter, ['without_tasks']);
D
Dmitry Kalinin 已提交
80 81 82 83 84 85 86
            const result = projectsDummyData.results.filter((x) => {
                for (const key in queries) {
                    if (Object.prototype.hasOwnProperty.call(queries, key)) {
                        // TODO: Particular match for some fields is not checked
                        if (queries[key] !== x[key]) {
                            return false;
                        }
B
Boris Sekachev 已提交
87 88 89
                    }
                }

D
Dmitry Kalinin 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102
                return true;
            });

            return result;
        }

        async function saveProject(id, projectData) {
            const object = projectsDummyData.results.filter((project) => project.id === id)[0];
            for (const prop in projectData) {
                if (
                    Object.prototype.hasOwnProperty.call(projectData, prop)
                    && Object.prototype.hasOwnProperty.call(object, prop)
                ) {
D
Dmitry Kalinin 已提交
103 104 105 106 107
                    if (prop === 'labels') {
                        object[prop] = projectData[prop].filter((label) => !label.deleted);
                    } else {
                        object[prop] = projectData[prop];
                    }
D
Dmitry Kalinin 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
                }
            }
        }

        async function createProject(projectData) {
            const id = Math.max(...projectsDummyData.results.map((el) => el.id)) + 1;
            projectsDummyData.results.push({
                id,
                url: `http://localhost:7000/api/v1/projects/${id}`,
                name: projectData.name,
                owner: 1,
                assignee: null,
                bug_tracker: projectData.bug_tracker,
                created_date: '2019-05-16T13:08:00.621747+03:00',
                updated_date: '2019-05-16T13:08:00.621797+03:00',
                status: 'annotation',
                tasks: [],
                labels: JSON.parse(JSON.stringify(projectData.labels)),
            });

            const createdProject = await getProjects(`?id=${id}`);
            return createdProject[0];
        }

        async function deleteProject(id) {
            const projects = projectsDummyData.results;
            const project = projects.filter((el) => el.id === id)[0];
            if (project) {
                projects.splice(projects.indexOf(project), 1);
B
Boris Sekachev 已提交
137
            }
D
Dmitry Kalinin 已提交
138
        }
B
Boris Sekachev 已提交
139

D
Dmitry Kalinin 已提交
140
        async function getTasks(filter = '') {
B
Boris Sekachev 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
            // Emulation of a query filter
            const queries = QueryStringToJSON(filter);
            const result = tasksDummyData.results.filter((x) => {
                for (const key in queries) {
                    if (Object.prototype.hasOwnProperty.call(queries, key)) {
                        // TODO: Particular match for some fields is not checked
                        if (queries[key] !== x[key]) {
                            return false;
                        }
                    }
                }

                return true;
            });

            return result;
        }

        async function saveTask(id, taskData) {
V
Vitaliy Nishukov 已提交
160
            const object = tasksDummyData.results.filter((task) => task.id === id)[0];
B
Boris Sekachev 已提交
161
            for (const prop in taskData) {
V
Vitaliy Nishukov 已提交
162
                if (
D
Dmitry Kalinin 已提交
163 164
                    Object.prototype.hasOwnProperty.call(taskData, prop)
                    && Object.prototype.hasOwnProperty.call(object, prop)
V
Vitaliy Nishukov 已提交
165
                ) {
D
Dmitry Kalinin 已提交
166 167 168 169 170
                    if (prop === 'labels') {
                        object[prop] = taskData[prop].filter((label) => !label.deleted);
                    } else {
                        object[prop] = taskData[prop];
                    }
B
Boris Sekachev 已提交
171 172 173 174 175
                }
            }
        }

        async function createTask(taskData) {
V
Vitaliy Nishukov 已提交
176
            const id = Math.max(...tasksDummyData.results.map((el) => el.id)) + 1;
B
Boris Sekachev 已提交
177 178 179 180
            tasksDummyData.results.push({
                id,
                url: `http://localhost:7000/api/v1/tasks/${id}`,
                name: taskData.name,
D
Dmitry Kalinin 已提交
181
                project_id: taskData.project_id || null,
B
Boris Sekachev 已提交
182 183
                size: 5000,
                mode: 'interpolation',
D
Dmitry Kalinin 已提交
184 185 186 187
                owner: {
                    id: 2,
                    username: 'bsekache',
                },
B
Boris Sekachev 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
                assignee: null,
                bug_tracker: taskData.bug_tracker,
                created_date: '2019-05-16T13:08:00.621747+03:00',
                updated_date: '2019-05-16T13:08:00.621797+03:00',
                overlap: taskData.overlap ? taskData.overlap : 5,
                segment_size: taskData.segment_size ? taskData.segment_size : 5000,
                flipped: false,
                status: 'annotation',
                image_quality: taskData.image_quality,
                labels: JSON.parse(JSON.stringify(taskData.labels)),
            });

            const createdTask = await getTasks(`?id=${id}`);
            return createdTask[0];
        }

        async function deleteTask(id) {
            const tasks = tasksDummyData.results;
V
Vitaliy Nishukov 已提交
206
            const task = tasks.filter((el) => el.id === id)[0];
B
Boris Sekachev 已提交
207 208 209 210 211 212
            if (task) {
                tasks.splice(tasks.indexOf(task), 1);
            }
        }

        async function getJob(jobID) {
V
Vitaliy Nishukov 已提交
213 214 215 216 217 218 219 220 221 222 223
            const jobs = tasksDummyData.results
                .reduce((acc, task) => {
                    for (const segment of task.segments) {
                        for (const job of segment.jobs) {
                            const copy = JSON.parse(JSON.stringify(job));
                            copy.start_frame = segment.start_frame;
                            copy.stop_frame = segment.stop_frame;
                            copy.task_id = task.id;

                            acc.push(copy);
                        }
B
Boris Sekachev 已提交
224 225
                    }

V
Vitaliy Nishukov 已提交
226 227 228
                    return acc;
                }, [])
                .filter((job) => job.id === jobID);
229

V
Vitaliy Nishukov 已提交
230 231 232 233 234
            return (
                jobs[0] || {
                    detail: 'Not found.',
                }
            );
B
Boris Sekachev 已提交
235 236 237
        }

        async function saveJob(id, jobData) {
V
Vitaliy Nishukov 已提交
238 239 240 241 242 243
            const object = tasksDummyData.results
                .reduce((acc, task) => {
                    for (const segment of task.segments) {
                        for (const job of segment.jobs) {
                            acc.push(job);
                        }
B
Boris Sekachev 已提交
244 245
                    }

V
Vitaliy Nishukov 已提交
246 247 248
                    return acc;
                }, [])
                .filter((job) => job.id === id)[0];
B
Boris Sekachev 已提交
249 250

            for (const prop in jobData) {
V
Vitaliy Nishukov 已提交
251
                if (
D
Dmitry Kalinin 已提交
252 253
                    Object.prototype.hasOwnProperty.call(jobData, prop)
                    && Object.prototype.hasOwnProperty.call(object, prop)
V
Vitaliy Nishukov 已提交
254
                ) {
B
Boris Sekachev 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267
                    object[prop] = jobData[prop];
                }
            }
        }

        async function getUsers() {
            return JSON.parse(JSON.stringify(usersDummyData)).results;
        }

        async function getSelf() {
            return JSON.parse(JSON.stringify(usersDummyData)).results[0];
        }

268 269 270 271
        async function getPreview() {
            return 'DUMMY_IMAGE';
        }

B
Boris Sekachev 已提交
272 273
        async function getData() {
            return 'DUMMY_IMAGE';
B
Boris Sekachev 已提交
274 275
        }

B
Boris Sekachev 已提交
276 277
        async function getMeta(tid) {
            return JSON.parse(JSON.stringify(frameMetaDummyData[tid]));
B
Boris Sekachev 已提交
278 279
        }

B
Boris Sekachev 已提交
280 281 282 283 284 285 286 287 288
        async function getAnnotations(session, id) {
            if (session === 'task') {
                return JSON.parse(JSON.stringify(taskAnnotationsDummyData[id]));
            }

            if (session === 'job') {
                return JSON.parse(JSON.stringify(jobAnnotationsDummyData[id]));
            }

289 290 291
            return null;
        }

B
Boris Sekachev 已提交
292 293 294 295 296 297 298 299
        async function updateAnnotations(session, id, data, action) {
            // Actually we do not change our dummy data
            // We just update the argument in some way and return it

            data.version += 1;

            if (action === 'create') {
                let idGenerator = 1000;
V
Vitaliy Nishukov 已提交
300 301 302 303 304 305 306
                data.tracks
                    .concat(data.tags)
                    .concat(data.shapes)
                    .map((el) => {
                        el.id = ++idGenerator;
                        return el;
                    });
B
Boris Sekachev 已提交
307 308 309 310 311 312 313 314 315 316 317 318

                return data;
            }

            if (action === 'update') {
                return data;
            }

            if (action === 'delete') {
                return data;
            }

319 320 321
            return null;
        }

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
        async function getCloudStorages(filter = '') {
            const queries = QueryStringToJSON(filter);
            const result = cloudStoragesDummyData.results.filter((item) => {
                for (const key in queries) {
                    if (Object.prototype.hasOwnProperty.call(queries, key)) {
                        if (queries[key] !== item[key]) {
                            return false;
                        }
                    }
                }
                return true;
            });
            return result;
        }

        async function updateCloudStorage(id, cloudStorageData) {
            const cloudStorage = cloudStoragesDummyData.results.find((item) => item.id === id);
            if (cloudStorage) {
                for (const prop in cloudStorageData) {
                    if (
                        Object.prototype.hasOwnProperty.call(cloudStorageData, prop)
                            && Object.prototype.hasOwnProperty.call(cloudStorage, prop)
                    ) {
                        cloudStorage[prop] = cloudStorageData[prop];
                    }
                }
            }
        }

        async function createCloudStorage(cloudStorageData) {
            const id = Math.max(...cloudStoragesDummyData.results.map((item) => item.id)) + 1;
            cloudStoragesDummyData.results.push({
                id,
                provider_type: cloudStorageData.provider_type,
                resource: cloudStorageData.resource,
                display_name: cloudStorageData.display_name,
                credentials_type: cloudStorageData.credentials_type,
                specific_attributes: cloudStorageData.specific_attributes,
                description: cloudStorageData.description,
                owner: 1,
                created_date: '2021-09-01T09:29:47.094244+03:00',
                updated_date: '2021-09-01T09:29:47.103264+03:00',
            });

            const result = await getCloudStorages(`?id=${id}`);
            return result[0];
        }

        async function deleteCloudStorage(id) {
            const cloudStorages = cloudStoragesDummyData.results;
            const cloudStorageId = cloudStorages.findIndex((item) => item.id === id);
            if (cloudStorageId !== -1) {
                cloudStorages.splice(cloudStorageId);
            }
        }


V
Vitaliy Nishukov 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
        Object.defineProperties(
            this,
            Object.freeze({
                server: {
                    value: Object.freeze({
                        about,
                        share,
                        formats,
                        exception,
                        login,
                        logout,
                    }),
                    writable: false,
                },

D
Dmitry Kalinin 已提交
394 395 396 397 398 399 400 401 402 403
                projects: {
                    value: Object.freeze({
                        get: getProjects,
                        save: saveProject,
                        create: createProject,
                        delete: deleteProject,
                    }),
                    writable: false,
                },

V
Vitaliy Nishukov 已提交
404 405 406 407 408 409 410 411 412 413 414 415
                tasks: {
                    value: Object.freeze({
                        getTasks,
                        saveTask,
                        createTask,
                        deleteTask,
                    }),
                    writable: false,
                },

                jobs: {
                    value: Object.freeze({
416 417
                        get: getJob,
                        save: saveJob,
V
Vitaliy Nishukov 已提交
418 419 420 421 422 423
                    }),
                    writable: false,
                },

                users: {
                    value: Object.freeze({
424 425
                        get: getUsers,
                        self: getSelf,
V
Vitaliy Nishukov 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
                    }),
                    writable: false,
                },

                frames: {
                    value: Object.freeze({
                        getData,
                        getMeta,
                        getPreview,
                    }),
                    writable: false,
                },

                annotations: {
                    value: {
                        updateAnnotations,
                        getAnnotations,
                    },
444
                },
445 446 447 448 449 450 451 452 453 454

                cloudStorages: {
                    value: Object.freeze({
                        get: getCloudStorages,
                        update: updateCloudStorage,
                        create: createCloudStorage,
                        delete: deleteCloudStorage,
                    }),
                    writable: false,
                },
V
Vitaliy Nishukov 已提交
455 456
            }),
        );
B
Boris Sekachev 已提交
457 458 459 460 461
    }
}

const serverProxy = new ServerProxy();
module.exports = serverProxy;