Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove collections in endpoints #5662

Merged
merged 176 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
176 commits
Select commit Hold shift + click to select a range
bbaa891
Add missing pagination in some endpoints
zhiltsov-max Jan 9, 2023
9542c8d
Update SDK
zhiltsov-max Jan 9, 2023
c498fa5
Update tests
zhiltsov-max Jan 9, 2023
fbcc345
Update changelog
zhiltsov-max Jan 9, 2023
26473e8
Fix formatting
zhiltsov-max Jan 9, 2023
c20991a
Fix changelog
zhiltsov-max Jan 9, 2023
fb2516c
t
zhiltsov-max Jan 10, 2023
d3c4ca7
Merge branch 'zm/add-missing-pagination' into zm/redirect-duplicated-…
zhiltsov-max Jan 10, 2023
3dcf328
Merge branch 'develop' into zm/improve-some-endpoints
nmanovic Jan 10, 2023
3e507bf
Added fetchAll aggregator
bsekachev Jan 11, 2023
c65ac32
Add simple redirects
zhiltsov-max Jan 10, 2023
895aa16
Merge branch 'develop' into zm/improve-some-endpoints
zhiltsov-max Jan 11, 2023
3ca2026
Update test assets
zhiltsov-max Jan 11, 2023
e73cbf6
Merge remote-tracking branch 'origin/zm/improve-some-endpoints' into …
zhiltsov-max Jan 11, 2023
ca7c732
Update UI tests
zhiltsov-max Jan 11, 2023
97bfb9f
t
zhiltsov-max Jan 11, 2023
6d314f6
Merge branch 'develop' into zm/redirect-duplicated-endpoints
zhiltsov-max Jan 11, 2023
4f6f365
Merge branch 'zm/improve-some-endpoints' into zm/redirect-duplicated-…
zhiltsov-max Jan 11, 2023
af23b06
Implement simple filters support
zhiltsov-max Jan 11, 2023
6689ca2
Simplify implementation
zhiltsov-max Jan 11, 2023
44944e7
Update filtering fields in views
zhiltsov-max Jan 12, 2023
3f0f5d1
Remove extra fields from schema
zhiltsov-max Jan 12, 2023
afba94c
Fix prefetch use
zhiltsov-max Jan 13, 2023
ecbb364
Change string filter to inclusion
zhiltsov-max Jan 13, 2023
7ebab92
Update search fields and add tests
zhiltsov-max Jan 17, 2023
c322136
Refactor some code
zhiltsov-max Jan 17, 2023
92e6a69
Update webhook filters
zhiltsov-max Jan 17, 2023
0815eca
Polish some code
zhiltsov-max Jan 17, 2023
23eabd3
Fix schema errors
zhiltsov-max Jan 17, 2023
fbc52cc
Remove extra parameters from endpoints
zhiltsov-max Jan 17, 2023
0e235b4
Fix redirects and restore extra parameters
zhiltsov-max Jan 18, 2023
49e4b61
Restore permission check behavior
zhiltsov-max Jan 18, 2023
f645f40
Update issue tests
zhiltsov-max Jan 18, 2023
b19e581
Merge branch 'develop' into zm/redirect-duplicated-endpoints
zhiltsov-max Jan 18, 2023
b58f121
Fix linter problems
zhiltsov-max Jan 18, 2023
0bec884
Update license headers
zhiltsov-max Jan 18, 2023
ecf8dcb
Fix linter warning
zhiltsov-max Jan 18, 2023
ee31c85
Update changelog
zhiltsov-max Jan 18, 2023
f72024d
Fix issue tests
zhiltsov-max Jan 19, 2023
2a24b80
Merge branch 'develop' into zm/redirect-duplicated-endpoints
zhiltsov-max Jan 19, 2023
073037d
Refactor some code
zhiltsov-max Jan 19, 2023
7fd4e56
Fix merge
zhiltsov-max Jan 19, 2023
f12884e
Fix linter
zhiltsov-max Jan 19, 2023
056e8cc
Implement filters using JsonFilter
zhiltsov-max Jan 20, 2023
403b547
Remove extra import
zhiltsov-max Jan 20, 2023
a2792f2
Fix check
zhiltsov-max Jan 20, 2023
9009179
Fix type description
zhiltsov-max Jan 20, 2023
6b343cf
Merge branch 'develop' into zm/redirect-duplicated-endpoints
zhiltsov-max Jan 20, 2023
4b79078
Merge branch 'develop' into zm/redirect-duplicated-endpoints
zhiltsov-max Jan 23, 2023
b014016
Merge remote-tracking branch 'origin/zm/redirect-duplicated-endpoints…
zhiltsov-max Jan 23, 2023
e3aa275
Fix api tests
zhiltsov-max Jan 23, 2023
67a0a54
Add project.labels endpoint
zhiltsov-max Jan 23, 2023
71f5b20
Merge branch 'develop' into zm/redirect-duplicated-endpoints
zhiltsov-max Jan 23, 2023
b59e6ea
Revert switching to json filter
zhiltsov-max Jan 23, 2023
03325cb
Fix tests
zhiltsov-max Jan 23, 2023
fa790bb
t
zhiltsov-max Jan 24, 2023
1474de4
t
zhiltsov-max Jan 26, 2023
43795a0
Fix license headers
zhiltsov-max Jan 26, 2023
101a8de
Implement extra fields in serializers, update test data
zhiltsov-max Jan 26, 2023
2484bf9
Refactored previews & projects
bsekachev Jan 27, 2023
31686a7
Fetching jobs workarounded
bsekachev Jan 27, 2023
bf7e420
Fixed code
bsekachev Jan 27, 2023
db797c4
Fixed issue comments
bsekachev Jan 27, 2023
1b806e0
update some tests
zhiltsov-max Jan 27, 2023
94516c3
Fixed comments
bsekachev Jan 27, 2023
ab7e92c
Merge branch 'zm/redirect-duplicated-endpoints' of github.com:opencv/…
bsekachev Jan 27, 2023
9eb136f
Fixed page size
bsekachev Jan 27, 2023
81508b0
Reworked fetching issue comments
bsekachev Jan 27, 2023
f69d708
Simplified sorting
bsekachev Jan 27, 2023
afdf454
Fix tests
zhiltsov-max Jan 30, 2023
fd2311d
Merge remote-tracking branch 'origin/zm/redirect-duplicated-endpoints…
zhiltsov-max Jan 30, 2023
3ba32f3
Merge branch 'develop' into zm/redirect-duplicated-endpoints
zhiltsov-max Jan 30, 2023
cfcfec5
Fix merge
zhiltsov-max Jan 30, 2023
b489edc
Fix linter issues
zhiltsov-max Jan 30, 2023
2539dc4
Fix test
zhiltsov-max Jan 30, 2023
f7c1cdc
Fix pytorch adapter
zhiltsov-max Jan 30, 2023
c7c6cf7
Update changelog
zhiltsov-max Jan 30, 2023
9efe98d
Merge branch 'develop' into zm/redirect-duplicated-endpoints
zhiltsov-max Jan 30, 2023
e49b578
Fix test
zhiltsov-max Jan 31, 2023
65d0c0d
Merge remote-tracking branch 'origin/zm/redirect-duplicated-endpoints…
zhiltsov-max Jan 31, 2023
327fc9f
Fix ui problem with jobs
zhiltsov-max Jan 31, 2023
a379b2b
Fix some tests
zhiltsov-max Feb 1, 2023
dfea00d
Fixed jest tests
klakhov Feb 1, 2023
8448c98
fixed getJobs method
klakhov Feb 1, 2023
cfae321
Fixed remaining tests
klakhov Feb 1, 2023
27c5e16
progress count by segments, removed jobs fetching
klakhov Feb 1, 2023
7920f29
added waiting for task load
klakhov Feb 1, 2023
c5d0032
Add new fields to the server schema
zhiltsov-max Feb 1, 2023
fd00c57
Merge branch 'zm/redirect-duplicated-endpoints' into zm/remove-duplic…
zhiltsov-max Feb 1, 2023
2f80481
Add label endpoints and fields
zhiltsov-max Feb 2, 2023
6a55cd9
Add label viewset, update collection fields
zhiltsov-max Feb 2, 2023
6f932d0
Add endpoint for labels
zhiltsov-max Feb 3, 2023
5247ace
Merge branch 'develop' into zm/remove-duplicating-collections-in-endp…
zhiltsov-max Feb 3, 2023
640c884
t
zhiltsov-max Feb 6, 2023
afcc8c4
Fix tests
zhiltsov-max Feb 6, 2023
f446f27
Fix merge
zhiltsov-max Feb 7, 2023
f988d8c
Implement job_id filter
zhiltsov-max Feb 7, 2023
d63a319
Revert metadata changes
zhiltsov-max Feb 8, 2023
9678400
Fix output for collection summaries
zhiltsov-max Feb 8, 2023
9fd9869
Fix few errors
zhiltsov-max Feb 8, 2023
1d0314d
Fixes with labels & jobs for new API (projects & tasks)
bsekachev Feb 9, 2023
cc40f7a
Merged develop
bsekachev Feb 9, 2023
36f8998
Added more types
bsekachev Feb 9, 2023
a237d2f
Removed unused proxy
bsekachev Feb 9, 2023
784f43b
More types
bsekachev Feb 9, 2023
6023e2c
Fix task id and org filtering
zhiltsov-max Feb 9, 2023
3a149f6
Merge remote-tracking branch 'origin/zm/remove-duplicating-collection…
zhiltsov-max Feb 9, 2023
1323e26
Removed extra piece of code
bsekachev Feb 9, 2023
d839676
Merge branch 'zm/remove-duplicating-collections-in-endpoints' of gith…
bsekachev Feb 9, 2023
a5875d4
Project page & adding, removing, patching labels for a project
bsekachev Feb 9, 2023
438a26b
Small fix
bsekachev Feb 9, 2023
07f1fc2
Task page, updating, creating and deleting labels, updating jobs
bsekachev Feb 10, 2023
8475968
Job can be opened now
bsekachev Feb 10, 2023
96f2cf3
Added type for a job
bsekachev Feb 10, 2023
46c66ee
Fixed updating jobs
bsekachev Feb 10, 2023
3ca8439
Add more tests
zhiltsov-max Feb 10, 2023
3c26779
Merge remote-tracking branch 'origin/zm/remove-duplicating-collection…
zhiltsov-max Feb 10, 2023
fc71273
Fix the patch method
zhiltsov-max Feb 10, 2023
5f1c1df
Add more tests
zhiltsov-max Feb 11, 2023
40db42c
Fix feching job after creating a task with the job
bsekachev Feb 13, 2023
1c64e9e
Fixed array destructurization
bsekachev Feb 13, 2023
3487319
Fix job_id list filter test
zhiltsov-max Feb 13, 2023
0f112ce
Merge remote-tracking branch 'origin/zm/remove-duplicating-collection…
zhiltsov-max Feb 13, 2023
7166d22
Merge branch 'develop' into zm/remove-duplicating-collections-in-endp…
bsekachev Feb 13, 2023
3dc7249
Fixed couple of tests
bsekachev Feb 13, 2023
e46910c
Fix label patch cases
zhiltsov-max Feb 13, 2023
2d676ce
Fix tests, add delete tests
zhiltsov-max Feb 13, 2023
5793bb6
Fix test
zhiltsov-max Feb 13, 2023
7d0c6c2
Merge remote-tracking branch 'origin/zm/remove-duplicating-collection…
zhiltsov-max Feb 13, 2023
e7e1b35
Fixed moving to a project / between projects
bsekachev Feb 14, 2023
5368815
Updated license headers
bsekachev Feb 14, 2023
9a12de9
Fix merge
zhiltsov-max Feb 14, 2023
b46196b
Merge remote-tracking branch 'origin/zm/remove-duplicating-collection…
zhiltsov-max Feb 14, 2023
c2aab74
Fixed test case 2900
bsekachev Feb 14, 2023
0dc794e
Fixed two cypress tests
bsekachev Feb 14, 2023
fab599a
Fix project patch cases
zhiltsov-max Feb 14, 2023
df0158a
Fix remaining project cases
zhiltsov-max Feb 14, 2023
965e9cc
Fix task tests
zhiltsov-max Feb 14, 2023
55cb0df
Fix file name
zhiltsov-max Feb 14, 2023
c5eca87
Remove extra changes
zhiltsov-max Feb 14, 2023
1692c5c
Fix webhook tests
zhiltsov-max Feb 14, 2023
bf356ff
Fix webhook creation/filtering with invalid org
zhiltsov-max Feb 14, 2023
36730a8
Merge remote-tracking branch 'origin/zm/remove-duplicating-collection…
zhiltsov-max Feb 14, 2023
6203d3d
Fix linter problems
zhiltsov-max Feb 14, 2023
a43772e
Merge branch 'develop' into zm/remove-duplicating-collections-in-endp…
zhiltsov-max Feb 14, 2023
18928ce
Partially fixed jest tests
bsekachev Feb 15, 2023
e0a7bc0
Almost all jest tests fixed
bsekachev Feb 15, 2023
9d3420a
Fixed other jest tests
bsekachev Feb 15, 2023
bf2da14
Fix permission check for worker assignees in orgs
zhiltsov-max Feb 15, 2023
7eaf236
Merge remote-tracking branch 'origin/zm/remove-duplicating-collection…
zhiltsov-max Feb 15, 2023
df5b4ef
Fix unit tests
zhiltsov-max Feb 15, 2023
8c114ef
Merged develop
bsekachev Feb 17, 2023
818d59d
Improve collection serializers naming in the schema
zhiltsov-max Feb 17, 2023
f145bff
Add usage info for the deleted field
zhiltsov-max Feb 17, 2023
764726a
Simplify condition
zhiltsov-max Feb 17, 2023
85f1436
Merge remote-tracking branch 'origin/zm/remove-duplicating-collection…
zhiltsov-max Feb 17, 2023
4cc5311
Fixed link
bsekachev Feb 20, 2023
f3755b5
Fixed extra patch request
bsekachev Feb 20, 2023
9193b6c
Simplified some filters
bsekachev Feb 20, 2023
489693d
Remove sublabels from the 1st level of output
zhiltsov-max Feb 20, 2023
9e1b3f4
Polish schema, add docs
zhiltsov-max Feb 20, 2023
77b153e
Workaround name duplicates checks in patching separate entity labels
zhiltsov-max Feb 20, 2023
29f9802
Deny sublabel editing without the parent label
zhiltsov-max Feb 20, 2023
3efba9a
Fix tests, add tasks tests for labels
zhiltsov-max Feb 20, 2023
5dc67fa
Merge remote-tracking branch 'origin/zm/remove-duplicating-collection…
zhiltsov-max Feb 20, 2023
9f36ebb
Merge remote-tracking branch 'origin/develop' into zm/remove-duplicat…
zhiltsov-max Feb 20, 2023
954af3a
Fix linter
zhiltsov-max Feb 20, 2023
ea98129
Fixed eslint
bsekachev Feb 20, 2023
f7f3f2f
Remove extra opa tests
zhiltsov-max Feb 20, 2023
3624a58
Merge remote-tracking branch 'origin/zm/remove-duplicating-collection…
zhiltsov-max Feb 20, 2023
47efea4
Workaround checks for label duplicates
zhiltsov-max Feb 20, 2023
6c422e5
Fixed tests & updating a label
bsekachev Feb 21, 2023
0e52437
Fixed unique check for label names
bsekachev Feb 21, 2023
bba5c6d
Apply suggestions from code review - fix schema docs
zhiltsov-max Feb 21, 2023
74257ec
Fixed a problem with PIP (conflict of verisons for click package).
nmanovic Feb 21, 2023
53769ef
Merge branch 'zm/remove-duplicating-collections-in-endpoints' of http…
nmanovic Feb 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cvat-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"ts-jest": "26"
},
"dependencies": {
"@types/lodash": "^4.14.191",
"axios": "^0.27.2",
"browser-or-node": "^2.0.0",
"cvat-data": "link:./../cvat-data",
Expand All @@ -34,6 +35,7 @@
"jest-config": "^29.0.3",
"js-cookie": "^3.0.1",
"json-logic-js": "^2.0.1",
"lodash": "^4.17.21",
"platform": "^1.3.5",
"quickhull": "^1.0.3",
"store": "^2.0.12",
Expand Down
12 changes: 6 additions & 6 deletions cvat-core/src/annotation-formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import { DimensionType } from 'enums';
import {
AnnotationExporterResponseBody,
AnnotationFormatsResponseBody,
AnnotationImporterResponseBody,
SerializedAnnotationExporter,
SerializedAnnotationFormats,
SerializedAnnotationImporter,
} from 'server-response-types';

export class Loader {
Expand All @@ -17,7 +17,7 @@ export class Loader {
public enabled: boolean;
public dimension: DimensionType;

constructor(initialData: AnnotationImporterResponseBody) {
constructor(initialData: SerializedAnnotationImporter) {
const data = {
name: initialData.name,
format: initialData.ext,
Expand Down Expand Up @@ -53,7 +53,7 @@ export class Dumper {
public enabled: boolean;
public dimension: DimensionType;

constructor(initialData: AnnotationExporterResponseBody) {
constructor(initialData: SerializedAnnotationExporter) {
const data = {
name: initialData.name,
format: initialData.ext,
Expand Down Expand Up @@ -86,7 +86,7 @@ export class AnnotationFormats {
public loaders: Loader[];
public dumpers: Dumper[];

constructor(initialData: AnnotationFormatsResponseBody) {
constructor(initialData: SerializedAnnotationFormats) {
const data = {
exporters: initialData.exporters.map((el) => new Dumper(el)),
importers: initialData.importers.map((el) => new Loader(el)),
Expand Down
1 change: 1 addition & 0 deletions cvat-core/src/annotations-history.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2019-2022 Intel Corporation
// Copyright (C) 2023 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

Expand Down
52 changes: 32 additions & 20 deletions cvat-core/src/api-implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,27 +172,25 @@ export default function implementAPI(cvat) {
filter: isString,
sort: isString,
search: isString,
taskID: isInteger,
jobID: isInteger,
});

checkExclusiveFields(query, ['jobID', 'taskID', 'filter', 'search'], ['page', 'sort']);
checkExclusiveFields(query, ['jobID', 'filter', 'search'], ['page', 'sort']);
if ('jobID' in query) {
const job = await serverProxy.jobs.get({ id: query.jobID });
const { results } = await serverProxy.jobs.get({ id: query.jobID });
const [job] = results;
if (job) {
return [new Job(job)];
// When request job by ID we also need to add labels to work with them
const labels = await serverProxy.labels.get({ job_id: job.id });
return [new Job({ ...job, labels: labels.results })];
}

return [];
}

if ('taskID' in query) {
query.filter = JSON.stringify({ and: [{ '==': [{ var: 'task_id' }, query.taskID] }] });
}

const searchParams = {};
for (const key of Object.keys(query)) {
if (['page', 'sort', 'search', 'filter'].includes(key)) {
if (['page', 'sort', 'search', 'filter', 'task_id'].includes(key)) {
searchParams[key] = query[key];
}
}
Expand All @@ -214,7 +212,7 @@ export default function implementAPI(cvat) {
ordering: isString,
});

checkExclusiveFields(filter, ['id', 'projectId'], ['page']);
checkExclusiveFields(filter, ['id'], ['page']);
const searchParams = {};
for (const key of Object.keys(filter)) {
if (['page', 'id', 'sort', 'search', 'filter', 'ordering'].includes(key)) {
Expand All @@ -233,15 +231,19 @@ export default function implementAPI(cvat) {

const tasksData = await serverProxy.tasks.get(searchParams);
const tasks = await Promise.all(tasksData.map(async (taskItem) => {
// Temporary workaround for UI
// Fixme: too much requests on tasks page
let jobs = { results: [] };
if ('id' in filter) {
jobs = await serverProxy.jobs.get({
filter: JSON.stringify({ and: [{ '==': [{ var: 'task_id' }, taskItem.id] }] }),
}, true);
// When request task by ID we also need to add labels and jobs to work with them
const labels = await serverProxy.labels.get({ task_id: taskItem.id });
const jobs = await serverProxy.jobs.get({ task_id: taskItem.id }, true);
return new Task({
...taskItem, progress: taskItem.jobs, jobs: jobs.results, labels: labels.results,
});
}
return new Task({ ...taskItem, jobs: jobs.results });

return new Task({
...taskItem,
progress: taskItem.jobs,
});
}));

tasks.count = tasksData.count;
Expand All @@ -260,15 +262,25 @@ export default function implementAPI(cvat) {
checkExclusiveFields(filter, ['id'], ['page']);
const searchParams = {};
for (const key of Object.keys(filter)) {
if (['id', 'page', 'search', 'sort', 'page', 'filter'].includes(key)) {
if (['page', 'id', 'sort', 'search', 'filter'].includes(key)) {
searchParams[key] = filter[key];
}
}

const projectsData = await serverProxy.projects.get(searchParams);
const projects = projectsData.map((project) => new Project(project));
projects.count = projectsData.count;
const projects = await Promise.all(projectsData.map(async (projectItem) => {
if ('id' in filter) {
// When request a project by ID we also need to add labels to work with them
const labels = await serverProxy.labels.get({ project_id: projectItem.id });
return new Project({ ...projectItem, labels: labels.results });
}

return new Project({
...projectItem,
});
}));

projects.count = projectsData.count;
return projects;
};

Expand Down
45 changes: 17 additions & 28 deletions cvat-core/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2019-2022 Intel Corporation
// Copyright (C) 2022 CVAT.ai Corporation
// Copyright (C) 2022-2023s CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -92,33 +92,22 @@ export function checkObjectType(name, value, type, instance?): boolean {
}

export class FieldUpdateTrigger {
constructor() {
let updatedFlags = {};

Object.defineProperties(
this,
Object.freeze({
reset: {
value: () => {
updatedFlags = {};
},
},
update: {
value: (name) => {
updatedFlags[name] = true;
},
},
getUpdated: {
value: (data, propMap = {}) => {
const result = {};
for (const updatedField of Object.keys(updatedFlags)) {
result[propMap[updatedField] || updatedField] = data[updatedField];
}
return result;
},
},
}),
);
#updatedFlags: Record<string, boolean> = {};

reset(): void {
this.#updatedFlags = {};
}

update(name: string): void {
this.#updatedFlags[name] = true;
}

getUpdated(data: object, propMap: Record<string, string> = {}): Record<string, unknown> {
const result = {};
for (const updatedField of Object.keys(this.#updatedFlags)) {
result[propMap[updatedField] || updatedField] = data[updatedField];
}
return result;
}
}

Expand Down
1 change: 0 additions & 1 deletion cvat-core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

const config = {
backendAPI: '/api',
proxy: false,
organizationID: null,
origin: '',
uploadChunkSize: 100,
Expand Down
7 changes: 7 additions & 0 deletions cvat-core/src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ export enum ShareFileType {
REG = 'REG',
}

export enum ChunkType {
IMAGESET = 'imageset',
VIDEO = 'video',
}

export enum TaskStatus {
ANNOTATION = 'annotation',
VALIDATION = 'validation',
COMPLETED = 'completed',
}

export type ProjectStatus = TaskStatus;

export enum JobStage {
ANNOTATION = 'annotation',
VALIDATION = 'validation',
Expand Down
58 changes: 24 additions & 34 deletions cvat-core/src/labels.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
// Copyright (C) 2019-2022 Intel Corporation
// Copyright (C) 2023 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

import {
AttrInputType, LabelType, SerializedAttribute, SerializedLabel,
} from 'server-response-types';
import { ShapeType, AttributeType } from './enums';
import { ArgumentError } from './exceptions';

type AttrInputType = 'select' | 'radio' | 'checkbox' | 'number' | 'text';

export interface RawAttribute {
name: string;
mutable: boolean;
input_type: AttrInputType;
default_value: string;
values: string[];
id?: number;
}

export class Attribute {
public id?: number;
public defaultValue: string;
Expand All @@ -24,7 +17,7 @@ export class Attribute {
public name: string;
public values: string[];

constructor(initialData: RawAttribute) {
constructor(initialData: SerializedAttribute) {
const data = {
id: undefined,
default_value: undefined,
Expand Down Expand Up @@ -75,8 +68,8 @@ export class Attribute {
);
}

toJSON(): RawAttribute {
const object: RawAttribute = {
toJSON(): SerializedAttribute {
const object: SerializedAttribute = {
name: this.name,
mutable: this.mutable,
input_type: this.inputType,
Expand All @@ -92,19 +85,6 @@ export class Attribute {
}
}

type LabelType = 'rectangle' | 'polygon' | 'polyline' | 'points' | 'ellipse' | 'cuboid' | 'skeleton' | 'mask' | 'tag' | 'any';
export interface RawLabel {
id?: number;
name: string;
color?: string;
type: LabelType;
svg?: string;
sublabels?: RawLabel[];
has_parent?: boolean;
deleted?: boolean;
attributes: RawAttribute[];
}

export class Label {
public name: string;
public readonly id?: number;
Expand All @@ -116,9 +96,10 @@ export class Label {
svg: string;
} | null;
public deleted: boolean;
public patched: boolean;
public readonly hasParent?: boolean;

constructor(initialData: RawLabel) {
constructor(initialData: SerializedLabel) {
const data = {
id: undefined,
name: undefined,
Expand All @@ -127,6 +108,7 @@ export class Label {
structure: undefined,
has_parent: false,
deleted: false,
patched: false,
svg: undefined,
elements: undefined,
sublabels: undefined,
Expand Down Expand Up @@ -167,13 +149,19 @@ export class Label {
throw new ArgumentError(`Name must be a string, but ${typeof name} was given`);
}
data.name = name;
if (Number.isInteger(data.id)) {
data.patched = true;
}
},
},
color: {
get: () => data.color,
set: (color) => {
if (typeof color === 'string' && color.match(/^#[0-9a-f]{6}$|^$/)) {
data.color = color;
if (Number.isInteger(data.id)) {
data.patched = true;
}
} else {
throw new ArgumentError('Trying to set wrong color format');
}
Expand Down Expand Up @@ -203,15 +191,21 @@ export class Label {
data.deleted = value;
},
},
patched: {
get: () => data.patched,
set: (value) => {
data.patched = value;
},
},
hasParent: {
get: () => data.has_parent,
},
}),
);
}

toJSON(): RawLabel {
const object: RawLabel = {
toJSON(): SerializedLabel {
const object: SerializedLabel = {
name: this.name,
attributes: [...this.attributes.map((el) => el.toJSON())],
type: this.type,
Expand All @@ -225,10 +219,6 @@ export class Label {
object.id = this.id;
}

if (this.deleted) {
object.deleted = this.deleted;
}

if (this.type) {
object.type = this.type;
}
Expand Down
Loading