From f381184f21f03ab9a6cada07a26f84f3d2ab241b Mon Sep 17 00:00:00 2001 From: Hironsan Date: Tue, 2 Nov 2021 15:52:20 +0900 Subject: [PATCH] Change example id from auto field to uuid field --- .../api/migrations/0017_alter_example_id.py | 19 +++++++++++++++++++ backend/api/models.py | 2 ++ backend/api/tasks.py | 11 +++++++---- backend/api/urls.py | 18 +++++++++--------- backend/api/views/download/writer.py | 6 +++--- 5 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 backend/api/migrations/0017_alter_example_id.py diff --git a/backend/api/migrations/0017_alter_example_id.py b/backend/api/migrations/0017_alter_example_id.py new file mode 100644 index 00000000..a9e6a6ea --- /dev/null +++ b/backend/api/migrations/0017_alter_example_id.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.8 on 2021-11-02 05:47 + +from django.db import migrations, models +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0016_auto_20211018_0556'), + ] + + operations = [ + migrations.AlterField( + model_name='example', + name='id', + field=models.UUIDField(db_index=True, default=uuid.uuid4, editable=False, primary_key=True, serialize=False), + ), + ] diff --git a/backend/api/models.py b/backend/api/models.py index 96287d2d..1677164a 100644 --- a/backend/api/models.py +++ b/backend/api/models.py @@ -1,4 +1,5 @@ import string +import uuid from typing import Literal from auto_labeling_pipeline.models import RequestModelFactory @@ -149,6 +150,7 @@ class Label(models.Model): class Example(models.Model): + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, db_index=True) meta = models.JSONField(default=dict) filename = models.FileField(default='.', max_length=1024) project = models.ForeignKey( diff --git a/backend/api/tasks.py b/backend/api/tasks.py index a44f3c5a..26d197a0 100644 --- a/backend/api/tasks.py +++ b/backend/api/tasks.py @@ -1,4 +1,5 @@ import itertools +import uuid from celery import shared_task from celery.utils.log import get_task_logger @@ -60,12 +61,14 @@ class DataFactory: self.label_class.objects.bulk_create(labels) def create_data(self, examples, project): + uuids = sorted(uuid.uuid4() for _ in range(len(examples))) dataset = [ - self.data_class(project=project, **example.data) - for example in examples + self.data_class(id=uid, project=project, **example.data) + for uid, example in zip(uuids, examples) ] - results = self.data_class.objects.bulk_create(dataset) - return results + data = self.data_class.objects.bulk_create(dataset) + data.sort(key=lambda example: example.id) + return data def create_annotation(self, examples, ids, user, project): mapping = {label.text: label.id for label in project.labels.all()} diff --git a/backend/api/urls.py b/backend/api/urls.py index a0443cd3..32730ab3 100644 --- a/backend/api/urls.py +++ b/backend/api/urls.py @@ -48,7 +48,7 @@ urlpatterns_project = [ name='example_list' ), path( - route='examples/', + route='examples/', view=views.ExampleDetail.as_view(), name='example_detail' ), @@ -89,23 +89,23 @@ urlpatterns_project = [ name='doc_list' ), path( - route='docs/', + route='docs/', view=views.DocumentDetail.as_view(), name='doc_detail' ), path( - route='approval/', + route='approval/', view=views.ApprovalAPI.as_view(), name='approve_labels' ), # Todo: change. path( - route='docs//annotations', + route='docs//annotations', view=views.AnnotationList.as_view(), name='annotation_list' ), path( - route='docs//annotations/', + route='docs//annotations/', view=views.AnnotationDetail.as_view(), name='annotation_detail' ), @@ -120,7 +120,7 @@ urlpatterns_project = [ name='tag_detail' ), path( - route='examples//comments', + route='examples//comments', view=views.CommentListDoc.as_view(), name='comment_list_doc' ), @@ -130,12 +130,12 @@ urlpatterns_project = [ name='comment_list_project' ), path( - route='examples//comments/', + route='examples//comments/', view=views.CommentDetail.as_view(), name='comment_detail' ), path( - route='examples//states', + route='examples//states', view=views.ExampleStateList.as_view(), name='example_state_list' ), @@ -175,7 +175,7 @@ urlpatterns_project = [ name='auto_labeling_config_test' ), path( - route='examples//auto-labeling', + route='examples//auto-labeling', view=views.AutoLabelingAnnotation.as_view(), name='auto_labeling_annotation' ), diff --git a/backend/api/views/download/writer.py b/backend/api/views/download/writer.py index dd0e88b4..c42bfa7a 100644 --- a/backend/api/views/download/writer.py +++ b/backend/api/views/download/writer.py @@ -82,7 +82,7 @@ class CsvWriter(BaseWriter): def create_line(self, record) -> Dict: return { - 'id': record.id, + 'id': str(record.id), 'data': record.data, 'label': '#'.join(record.label), **record.metadata @@ -120,7 +120,7 @@ class JSONWriter(BaseWriter): def create_line(self, record) -> Dict: return { - 'id': record.id, + 'id': str(record.id), 'data': record.data, 'label': record.label, **record.metadata @@ -132,7 +132,7 @@ class JSONLWriter(LineWriter): def create_line(self, record): return json.dumps({ - 'id': record.id, + 'id': str(record.id), 'data': record.data, 'label': record.label, **record.metadata