Skip to content

Commit

Permalink
Task progress (#264)
Browse files Browse the repository at this point in the history
* Add TaskProgress model and endpoints

* Prevent duplicate data with each file type

* Lint fix

* Update progress during task execution

* Fetch task progress and display linear progress bar

* Type and lint fixes

* Parse progress from executable xml output

* Lint fix

* First and last 10% to represent file download and upload

* Fix optimize and analyze

* Refactor and fix task progress fetching

* Lint fix

* Delete old taskprogress objects when new ones are created
  • Loading branch information
annehaley authored Feb 10, 2023
1 parent 8aa5f38 commit c7ab8d1
Show file tree
Hide file tree
Showing 15 changed files with 389 additions and 260 deletions.
50 changes: 50 additions & 0 deletions shapeworks_cloud/core/migrations/0023_task_progress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Generated by Django 3.2.17 on 2023-02-10 15:15

import uuid

from django.db import migrations, models
import django.db.models.deletion
import django_extensions.db.fields


class Migration(migrations.Migration):
dependencies = [
('core', '0022_private_datasets'),
]

operations = [
migrations.CreateModel(
name='TaskProgress',
fields=[
(
'created',
django_extensions.db.fields.CreationDateTimeField(
auto_now_add=True, verbose_name='created'
),
),
(
'modified',
django_extensions.db.fields.ModificationDateTimeField(
auto_now=True, verbose_name='modified'
),
),
(
'task_id',
models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False),
),
('name', models.CharField(max_length=255)),
('error', models.CharField(max_length=255)),
('percent_complete', models.IntegerField(default=0)),
(
'project',
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to='core.project'
),
),
],
options={
'get_latest_by': 'modified',
'abstract': False,
},
),
]
17 changes: 17 additions & 0 deletions shapeworks_cloud/core/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import uuid

from django.contrib.auth.models import User
from django.core.files.base import ContentFile
Expand Down Expand Up @@ -214,3 +215,19 @@ class ReconstructedSample(TimeStampedModel, models.Model):
particles = models.ForeignKey(
OptimizedParticles, on_delete=models.CASCADE, related_name='reconstructed_samples'
)


class TaskProgress(TimeStampedModel, models.Model):
task_id = models.UUIDField(default=uuid.uuid4, primary_key=True)
name = models.CharField(max_length=255)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
error = models.CharField(max_length=255)
percent_complete = models.IntegerField(default=0)

def update_percentage(self, percentage):
self.percent_complete = percentage
self.save()

def update_error(self, error):
self.error = error[:255]
self.save()
42 changes: 38 additions & 4 deletions shapeworks_cloud/core/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,14 @@ def groom(self, request, **kwargs):
project = self.get_object()
form_data = request.data
form_data = {k: str(v) for k, v in form_data.items()}
groom.delay(request.user.id, project.id, form_data)
return Response(status=status.HTTP_204_NO_CONTENT)

models.TaskProgress.objects.filter(name='groom', project=project).delete()
progress = models.TaskProgress.objects.create(name='groom')
groom.delay(request.user.id, project.id, form_data, progress.task_id)
return Response(
data={'groom_task': serializers.TaskProgressSerializer(progress).data},
status=status.HTTP_200_OK,
)

@action(
detail=True,
Expand All @@ -222,8 +228,26 @@ def optimize(self, request, **kwargs):
project = self.get_object()
form_data = request.data
form_data = {k: str(v) for k, v in form_data.items()}
optimize.delay(request.user.id, project.id, form_data)
return Response(status=status.HTTP_204_NO_CONTENT)

models.TaskProgress.objects.filter(name='optimize', project=project).delete()
models.TaskProgress.objects.filter(name='analyze', project=project).delete()

progress = models.TaskProgress.objects.create(name='optimize')
analysis_progress = models.TaskProgress.objects.create(name='analyze')
optimize.delay(
request.user.id,
project.id,
form_data,
progress.task_id,
analysis_progress.task_id,
)
return Response(
data={
'optimize_task': serializers.TaskProgressSerializer(progress).data,
'analyze_task': serializers.TaskProgressSerializer(analysis_progress).data,
},
status=status.HTTP_200_OK,
)


class GroomedSegmentationViewSet(BaseViewSet):
Expand Down Expand Up @@ -277,3 +301,13 @@ class ReconstructedSampleViewSet(
queryset = models.ReconstructedSample.objects.all()
serializer_class = serializers.ReconstructedSampleSerializer
filterset_class = filters.ReconstructedSampleFilter


class TaskProgressViewSet(
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
GenericViewSet,
):
permission_classes = [IsAuthenticatedOrReadOnly]
queryset = models.TaskProgress.objects.all()
serializer_class = serializers.TaskProgressSerializer
6 changes: 6 additions & 0 deletions shapeworks_cloud/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,9 @@ class ReconstructedSampleSerializer(serializers.ModelSerializer):
class Meta:
model = models.ReconstructedSample
fields = '__all__'


class TaskProgressSerializer(serializers.ModelSerializer):
class Meta:
model = models.TaskProgress
fields = '__all__'
Loading

0 comments on commit c7ab8d1

Please sign in to comment.