Skip to content

Commit

Permalink
feat: DEV-1205: Add task.updated_at column (HumanSignal#1784)
Browse files Browse the repository at this point in the history
* Update task.updated_at on annotation update (DEV-1205)

* Fix set updated_at on annotation delete (DEV-1205)

* Set update_at for every dm action (DEV-1205)

* Stop changing updated_at on actions (DEV-1205)

* Update experimental.py

Co-authored-by: Max Tkachenko <[email protected]>
Co-authored-by: niklub <[email protected]>
  • Loading branch information
3 people authored Jan 24, 2022
1 parent ccfd95d commit 1c4328c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions label_studio/data_manager/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def perform_action(action_id, project, queryset, user, **kwargs):
if not check_permissions(user, action):
raise DRFPermissionDenied(f'Action is not allowed for the current user: {action["id"]}')


try:
result = action['entry_point'](project, queryset, **kwargs)
except Exception as e:
Expand Down
11 changes: 10 additions & 1 deletion label_studio/data_manager/actions/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import logging

from django.db.models import signals
from datetime import datetime

from core.permissions import AllPermissions
from core.utils.common import temporary_disconnect_signal, temporary_disconnect_all_signals
from tasks.models import Annotation, Prediction, update_is_labeled_after_removing_annotation, \
from tasks.models import (
Annotation, Prediction, Task, update_is_labeled_after_removing_annotation,
bulk_update_stats_project_tasks
)
from webhooks.utils import emit_webhooks_for_instance
from webhooks.models import WebhookAction
from data_manager.functions import evaluate_predictions
Expand Down Expand Up @@ -74,10 +77,15 @@ def delete_tasks_annotations(project, queryset, **kwargs):
task_ids = queryset.values_list('id', flat=True)
annotations = Annotation.objects.filter(task__id__in=task_ids)
count = annotations.count()

# take only tasks where annotations were deleted
real_task_ids = set(list(annotations.values_list('task__id', flat=True)))
annotations_ids = list(annotations.values('id'))
annotations.delete()

emit_webhooks_for_instance(project.organization, project, WebhookAction.ANNOTATIONS_DELETED, annotations_ids)
bulk_update_stats_project_tasks(queryset)
Task.objects.filter(id__in=real_task_ids).update(updated_at=datetime.now())
return {'processed_items': count,
'detail': 'Deleted ' + str(count) + ' annotations'}

Expand All @@ -92,6 +100,7 @@ def delete_tasks_predictions(project, queryset, **kwargs):
predictions = Prediction.objects.filter(task__id__in=task_ids)
count = predictions.count()
predictions.delete()
queryset.update(updated_at=datetime.now())
return {'processed_items': count, 'detail': 'Deleted ' + str(count) + ' predictions'}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"""
import logging

from django.utils.timezone import now

from core.permissions import AllPermissions
from tasks.models import Prediction, Annotation
from tasks.models import Prediction, Annotation, Task
from tasks.serializers import TaskSerializerBulk

all_permissions = AllPermissions()
Expand All @@ -30,7 +32,9 @@ def predictions_to_annotations(project, queryset, **kwargs):

# prepare annotations
annotations = []
tasks_ids = []
for result, model_version, task_id, prediction_id in predictions_values:
tasks_ids.append(task_id)
annotations.append({
'result': result,
'completed_by_id': user.pk,
Expand All @@ -42,6 +46,7 @@ def predictions_to_annotations(project, queryset, **kwargs):
logger.debug(f'{count} predictions will be converter to annotations')
db_annotations = [Annotation(**annotation) for annotation in annotations]
db_annotations = Annotation.objects.bulk_create(db_annotations)
Task.objects.filter(id__in=tasks_ids).update(updated_at=now())

TaskSerializerBulk.post_process_annotations(db_annotations)
return {'response_code': 200, 'detail': f'Created {count} annotations'}
Expand Down
11 changes: 11 additions & 0 deletions label_studio/data_manager/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ def get_all_columns(project, *_):
'explore': False,
'labeling': False
}
},
{
'id': 'updated_at',
'title': 'Updated at',
'type': 'Datetime',
'target': 'tasks',
'help': 'Task update time',
'visibility_defaults': {
'explore': False,
'labeling': False
}
}
]

Expand Down
1 change: 1 addition & 0 deletions label_studio/data_manager/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class Meta:
"cancelled_annotations",
"completed_at",
"created_at",
"updated_at",
"annotations_results",
"data",
"id",
Expand Down
20 changes: 20 additions & 0 deletions label_studio/tasks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,18 @@ def decrease_project_summary_counters(self):
summary = self.task.project.summary
summary.remove_created_annotations_and_labels([self])

def save(self, *args, **kwargs):
result = super().save(*args, **kwargs)
# set updated_at field of task to now()
self.task.save(update_fields=['updated_at'])
return result

def delete(self, *args, **kwargs):
result = super().delete(*args, **kwargs)
# set updated_at field of task to now()
self.task.save(update_fields=['updated_at'])
return result


class TaskLock(models.Model):
task = models.ForeignKey(
Expand Down Expand Up @@ -454,8 +466,16 @@ def prepare_prediction_result(cls, result, project):
def save(self, *args, **kwargs):
# "result" data can come in different forms - normalize them to JSON
self.result = self.prepare_prediction_result(self.result, self.task.project)
# set updated_at field of task to now()
self.task.save(update_fields=['updated_at'])
return super(Prediction, self).save(*args, **kwargs)

def delete(self, *args, **kwargs):
result = super().delete(*args, **kwargs)
# set updated_at field of task to now()
self.task.save(update_fields=['updated_at'])
return result

class Meta:
db_table = 'prediction'

Expand Down

0 comments on commit 1c4328c

Please sign in to comment.