Skip to content

Couple of fixes in "save annotations" #1000

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

Merged
merged 1 commit into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 8 additions & 2 deletions cvat-core/src/annotations-saver.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,18 @@
},
};

const keys = ['id', 'label_id', 'group', 'frame',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create a function to covert an object into "hash". Thus you don't need to repeat keys in several files.

Copy link
Member Author

@bsekachev bsekachev Dec 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nmanovic
The first file from legacy UI,
The second from cvat-core.

They aren't related anyhow.

'occluded', 'z_order', 'points', 'type', 'shapes',
'attributes', 'value', 'spec_id', 'outside'];

// Find created and updated objects
for (const type of Object.keys(exported)) {
for (const object of exported[type]) {
if (object.id in this.initialObjects[type]) {
const exportedHash = JSON.stringify(object);
const initialHash = JSON.stringify(this.initialObjects[type][object.id]);
const exportedHash = JSON.stringify(object, keys);
const initialHash = JSON.stringify(
this.initialObjects[type][object.id], keys,
);
if (exportedHash !== initialHash) {
splitted.updated[type].push(object);
}
Expand Down
30 changes: 26 additions & 4 deletions cvat/apps/engine/static/engine/js/annotationSaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,18 @@ class AnnotationSaverModel extends Listener {
tags: [],
};

const keys = ['id', 'label_id', 'group', 'frame',
'occluded', 'z_order', 'points', 'type', 'shapes',
'attributes', 'value', 'spec_id', 'outside'];

// Compare initial state objects and export state objects
// in order to get updated and created objects
for (const type of Object.keys(this._initialObjects)) {
for (const obj of exported[type]) {
if (obj.id in this._initialObjects[type]) {
const exportedHash = JSON.stringify(obj);
const initialSash = JSON.stringify(this._initialObjects[type][obj.id]);
if (exportedHash !== initialSash) {
const exportedHash = JSON.stringify(obj, keys);
const initialHash = JSON.stringify(this._initialObjects[type][obj.id], keys);
if (exportedHash !== initialHash) {
updated[type].push(obj);
}
} else if (typeof obj.id === 'undefined') {
Expand Down Expand Up @@ -241,20 +245,33 @@ class AnnotationSaverModel extends Listener {
this._shapeCollection.flush = false;
this._version = savedObjects.version;
this._resetState();

for (const type of Object.keys(this._initialObjects)) {
for (const object of savedObjects[type]) {
if (object.shapes) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment why it is important to delete shape.id here?

for (const shape of object.shapes) {
delete shape.id;
}
}
this._initialObjects[type][object.id] = object;
}
}

this._version = savedObjects.version;
} else {
const [created, updated, deleted] = this._split(exported);
this.notify('saveCreated');
const savedCreated = await this._create(created);
this._updateCreatedObjects(created, savedCreated, mapping);
this._version = savedCreated.version;

for (const type of Object.keys(this._initialObjects)) {
for (const object of savedCreated[type]) {
if (object.shapes) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to create a special function here and use it 3 times?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, it does

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that it is from legacy UI. Let's keep it as is for now.

for (const shape of object.shapes) {
delete shape.id;
}
}
this._initialObjects[type][object.id] = object;
}
}
Expand All @@ -264,6 +281,11 @@ class AnnotationSaverModel extends Listener {
this._version = savedUpdated.version;
for (const type of Object.keys(this._initialObjects)) {
for (const object of savedUpdated[type]) {
if (object.shapes) {
for (const shape of object.shapes) {
delete shape.id;
}
}
this._initialObjects[type][object.id] = object;
}
}
Expand Down Expand Up @@ -375,7 +397,7 @@ class AnnotationSaverView {
});

window.onbeforeunload = (e) => {
if (this._controller.hasUnsavedChanges()) { // eslint-disable-line react/no-this-in-sfc
if (this._controller.hasUnsavedChanges()) {
const message = 'You have unsaved changes. Leave this page?';
e.returnValue = message;
return message;
Expand Down