Skip to content
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
15 changes: 14 additions & 1 deletion chris_backend/plugininstances/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
("finishedWithError", "Finished with error"),
("cancelled", "Cancelled")]

ACTIVE_STATUSES = ['created', 'waiting', 'scheduled', 'started', 'registeringFiles']

INACTIVE_STATUSES = ['finishedSuccessfully', 'finishedWithError', 'cancelled']


class PluginInstance(models.Model):
title = models.CharField(max_length=100, blank=True)
Expand Down Expand Up @@ -232,11 +236,12 @@ class PluginInstanceFilter(FilterSet):
lookup_expr='exact')
plugin_type = django_filters.CharFilter(field_name='plugin__meta__type',
lookup_expr='exact')
active = django_filters.BooleanFilter(method='filter_by_active_status')

class Meta:
model = PluginInstance
fields = ['id', 'min_start_date', 'max_start_date', 'min_end_date',
'max_end_date', 'root_id', 'previous_id', 'title', 'status',
'max_end_date', 'root_id', 'previous_id', 'title', 'status', 'active',
'owner_username', 'feed_id', 'plugin_id', 'plugin_name',
'plugin_name_exact', 'plugin_version', 'plugin_type', 'workflow_id']

Expand Down Expand Up @@ -269,6 +274,14 @@ def filter_by_previous_id(self, queryset, name, value):
previous = previous_queryset.first()
return previous.next.all()

def filter_by_active_status(self, queryset, name, value):
"""
Custom method to return the plugin instances in a queryset with an
"active" status.
"""
statuses = ACTIVE_STATUSES if value else INACTIVE_STATUSES
return queryset.filter(status__in=statuses)


class PluginInstanceLock(models.Model):
plugin_inst = models.OneToOneField(PluginInstance, on_delete=models.CASCADE,
Expand Down
14 changes: 11 additions & 3 deletions chris_backend/plugininstances/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
from plugins.enums import TYPES
from plugins.models import Plugin

from .models import ACTIVE_STATUSES
from .models import PluginInstance, PluginInstanceSplit
from .models import FloatParameter, IntParameter, BoolParameter
from .models import PathParameter, UnextpathParameter, StrParameter
from .models import (FloatParameter, IntParameter, BoolParameter, PathParameter,
UnextpathParameter, StrParameter)


class PluginInstanceSerializer(serializers.HyperlinkedModelSerializer):
Expand All @@ -33,6 +34,7 @@ class PluginInstanceSerializer(serializers.HyperlinkedModelSerializer):
raw = serializers.ReadOnlyField()
owner_username = serializers.ReadOnlyField(source='owner.username')
size = serializers.ReadOnlyField()
active = serializers.SerializerMethodField()
error_code = serializers.ReadOnlyField()
previous = serializers.HyperlinkedRelatedField(
view_name='plugininstance-detail', read_only=True, allow_null=True
Expand Down Expand Up @@ -61,7 +63,7 @@ class Meta:
model = PluginInstance
fields = ('url', 'id', 'title', 'previous_id', 'compute_resource_name',
'plugin_id', 'plugin_name', 'plugin_version', 'plugin_type',
'feed_id', 'start_date', 'end_date', 'output_path', 'status',
'feed_id', 'start_date', 'end_date', 'output_path', 'status', 'active',
'pipeline_id', 'pipeline_name', 'workflow_id', 'summary', 'raw',
'owner_username', 'cpu_limit', 'memory_limit', 'number_of_workers',
'gpu_limit', 'size', 'error_code', 'output_folder', 'previous',
Expand All @@ -79,6 +81,12 @@ def __init__(self, *args, **kwargs):
self.fields['cpu_limit'].read_only = True
self.fields['memory_limit'].read_only = True

def get_active(self, obj) -> bool:
"""
Overriden to get the "active" status of the plugin instance.
"""
return obj.status in ACTIVE_STATUSES

def validate_previous(self, previous_id):
"""
Custom method to check that an id is provided for previous instance when
Expand Down
19 changes: 18 additions & 1 deletion chris_backend/plugininstances/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from rest_framework import serializers

from plugins.models import PluginMeta, Plugin, PluginParameter, ComputeResource
from plugininstances.models import PluginInstance
from plugininstances.models import PluginInstance, ACTIVE_STATUSES, INACTIVE_STATUSES
from core.models import ChrisFolder
from core.storage.helpers import mock_storage
from plugininstances.serializers import PluginInstanceSerializer
Expand Down Expand Up @@ -256,6 +256,23 @@ def test_validate_value_within_interval(self):
with self.assertRaises(serializers.ValidationError):
plg_inst_serializer.validate_value_within_interval(5, 2, 4)

def test_get_active(self):
"""
Test whether overriden get_active method correctly returns the "active"
status of the plugin instance.
"""
data = self.data
plg_inst_serializer = PluginInstanceSerializer(data=data)
plg_inst_mock = mock.Mock()

for status in ACTIVE_STATUSES:
plg_inst_mock.status = status
self.assertTrue(plg_inst_serializer.get_active(plg_inst_mock))

for status in INACTIVE_STATUSES:
plg_inst_mock.status = status
self.assertFalse(plg_inst_serializer.get_active(plg_inst_mock))


class PathParameterSerializerTests(SerializerTests):

Expand Down
31 changes: 28 additions & 3 deletions chris_backend/plugininstances/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,12 +867,17 @@ def setUp(self):
super(PluginInstanceListQuerySearchViewTests, self).setUp()

user = User.objects.get(username=self.username)


(pl_meta, tf) = PluginMeta.objects.get_or_create(name='pacsquery', type='fs')
(plugin_fs, tf) = Plugin.objects.get_or_create(meta=pl_meta, version='0.1')
plugin_fs.compute_resources.set([self.compute_resource])
plugin_fs.save()

# create three plugin instances
plugin = Plugin.objects.get(meta__name="pacspull")
PluginInstance.objects.get_or_create(
plugin=plugin, owner=user, compute_resource=plugin.compute_resources.all()[0])
plugin=plugin_fs, owner=user, compute_resource=plugin_fs.compute_resources.all()[0])

plugin = Plugin.objects.get(meta__name="pacspull")
(inst, tf) = PluginInstance.objects.get_or_create(
plugin=plugin, owner=user, compute_resource=plugin.compute_resources.all()[0])

Expand All @@ -894,11 +899,30 @@ def setUp(self):

def test_plugin_instance_query_search_list_success(self):
self.client.login(username=self.username, password=self.password)

response = self.client.get(self.list_url)
# response should only contain the instances that match the query
self.assertContains(response, 'created')
self.assertEqual(len(response.data['results']), 1)
self.assertNotContains(response,'finishedSuccessfully')

def test_plugin_instance_query_search_list_success_active(self):
self.client.login(username=self.username, password=self.password)

list_url = reverse("allplugininstance-list-query-search") + '?active=true'
response = self.client.get(list_url)
# response should only contain the instances that are active
self.assertContains(response, 'created')
self.assertEqual(len(response.data['results']), 1)
self.assertNotContains(response,'finishedSuccessfully')

list_url = reverse("allplugininstance-list-query-search") + '?active=false'
response = self.client.get(list_url)
# response should only contain the instances that are not active
self.assertContains(response, 'finishedSuccessfully')
self.assertEqual(len(response.data['results']), 2)
self.assertNotContains(response, 'created')

def test_plugin_instance_query_search_list_success_but_empty_unauthenticated(self):
response = self.client.get(self.list_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand All @@ -909,6 +933,7 @@ def test_plugin_instance_query_search_list_success_unauthenticated(self):
response = self.client.get(list_url)
# response should only contain the instances that match the query
self.assertContains(response, 'finishedSuccessfully')
self.assertEqual(len(response.data['results']), 2)
self.assertNotContains(response, 'created')


Expand Down
Loading