diff --git a/beat/web/experiments/serializers.py b/beat/web/experiments/serializers.py index fb6e31ef238339d88b454e15c2178a38ba25aae9..af135eac88f9e2957bd77b40ae3ca4b3d7610186 100755 --- a/beat/web/experiments/serializers.py +++ b/beat/web/experiments/serializers.py @@ -39,7 +39,7 @@ from datetime import datetime import simplejson as json -#---------------------------------------------------------- +# ---------------------------------------------------------- class ExperimentSerializer(ShareableSerializer): @@ -59,33 +59,37 @@ class ExperimentSerializer(ShareableSerializer): class Meta: model = Experiment - fields = '__all__' - default_fields = ShareableSerializer.Meta.default_fields + ['name', 'toolchain', - 'datasets', 'short_description', - 'creation_date', 'start_date', 'end_date', - 'duration', 'status', - 'accessibility', - 'attestation_number', 'attestation_locked', - 'cpu_time', 'data_read', 'data_written', - 'is_owner'] - + fields = "__all__" + default_fields = ShareableSerializer.Meta.default_fields + [ + "name", + "toolchain", + "datasets", + "short_description", + "creation_date", + "start_date", + "end_date", + "duration", + "status", + "accessibility", + "attestation_number", + "attestation_locked", + "cpu_time", + "data_read", + "data_written", + "is_owner", + ] def __init__(self, *args, **kwargs): super(ExperimentSerializer, self).__init__(*args, **kwargs) self.statistics = {} def __get_statistics(self, obj, key): - if not obj in self.statistics: - self.statistics[obj] = { - 'cpu_time': 0, - 'data_read': 0, - 'data_written': 0 - } + if obj not in self.statistics: + self.statistics[obj] = {"cpu_time": 0, "data_read": 0, "data_written": 0} for block in obj.blocks.all(): - self.statistics[obj]['cpu_time'] += block.cpu_time() - self.statistics[obj]['data_read'] += block.data_read_size() - self.statistics[obj]['data_written'] += \ - block.data_written_size() + self.statistics[obj]["cpu_time"] += block.cpu_time() + self.statistics[obj]["data_read"] += block.data_read_size() + self.statistics[obj]["data_written"] += block.data_written_size() return self.statistics[obj][key] def get_datasets(self, obj): @@ -112,11 +116,11 @@ class ExperimentSerializer(ShareableSerializer): end_date = self.get_end_date(obj) if (start_date is None) or (end_date is None): - return '-' + return "-" duration = end_date - start_date - duration_seconds = max(duration.seconds, 1) # At least one second + duration_seconds = max(duration.seconds, 1) # At least one second seconds = duration_seconds % 60 minutes = ((duration_seconds - seconds) % 3600) / 60 @@ -129,19 +133,19 @@ class ExperimentSerializer(ShareableSerializer): minutes = 0 hours += 1 - return '%dh%02d' % (hours, minutes) + return "%dh%02d" % (hours, minutes) def get_cpu_time(self, obj): - return self.__get_statistics(obj, 'cpu_time') + return self.__get_statistics(obj, "cpu_time") def get_data_read(self, obj): - return self.__get_statistics(obj, 'data_read') + return self.__get_statistics(obj, "data_read") def get_data_written(self, obj): - return self.__get_statistics(obj, 'data_written') + return self.__get_statistics(obj, "data_written") -#---------------------------------------------------------- +# ---------------------------------------------------------- class AnalyzerSerializer(serializers.ModelSerializer): @@ -149,7 +153,7 @@ class AnalyzerSerializer(serializers.ModelSerializer): class Meta: model = Block - fields = '__all__' + fields = "__all__" def get_results(self, obj): results = {} @@ -158,48 +162,48 @@ class AnalyzerSerializer(serializers.ModelSerializer): if db_results.count() > 0: for result_entry in db_results: results[result_entry.name] = { - 'type': result_entry.type, - 'primary': result_entry.primary, - 'value': result_entry.value(), + "type": result_entry.type, + "primary": result_entry.primary, + "value": result_entry.value(), } else: dataformat_declaration = json.loads(obj.algorithm.result_dataformat) for field, type in dataformat_declaration.items(): - primary = (field[0] == '+') + primary = field[0] == "+" results[field[1:] if primary else field] = { - 'type': type, - 'primary': primary, - 'value': None, + "type": type, + "primary": primary, + "value": None, } return results -#---------------------------------------------------------- +# ---------------------------------------------------------- class BlockErrorSerializer(serializers.ModelSerializer): - block = serializers.CharField(source='name') - algorithm = serializers.CharField(source='algorithm.fullname') + block = serializers.CharField(source="name") + algorithm = serializers.CharField(source="algorithm.fullname") stdout = serializers.CharField() stderr = serializers.CharField() - details = serializers.CharField(source='error_report') + details = serializers.CharField(source="error_report") class Meta: model = Block - fields = '__all__' + fields = "__all__" -#---------------------------------------------------------- +# ---------------------------------------------------------- class ExperimentResultsSerializer(ShareableSerializer): started = serializers.SerializerMethodField() done = serializers.SerializerMethodField() failed = serializers.SerializerMethodField() - attestation = serializers.IntegerField(source='attestation.number') + attestation = serializers.IntegerField(source="attestation.number") blocks_status = serializers.SerializerMethodField() execution_info = serializers.SerializerMethodField() execution_order = serializers.SerializerMethodField() @@ -214,14 +218,29 @@ class ExperimentResultsSerializer(ShareableSerializer): class Meta(ShareableSerializer.Meta): model = Experiment - fields = '__all__' - default_fields = ['started', 'done', 'failed', 'status', 'blocks_status', - 'results', 'attestation', 'declaration', - 'errors', 'sharing', 'accessibility', - 'execution_info', 'execution_order'] + fields = "__all__" + default_fields = [ + "started", + "done", + "failed", + "status", + "blocks_status", + "results", + "attestation", + "declaration", + "errors", + "sharing", + "accessibility", + "execution_info", + "execution_order", + ] def get_started(self, obj): - return obj.status not in [Experiment.PENDING, Experiment.SCHEDULED, Experiment.CANCELLING] + return obj.status not in [ + Experiment.PENDING, + Experiment.SCHEDULED, + Experiment.CANCELLING, + ] def get_done(self, obj): return obj.status in [Experiment.DONE, Experiment.FAILED] @@ -233,33 +252,33 @@ class ExperimentResultsSerializer(ShareableSerializer): results = {} for block in obj.blocks.iterator(): if block.status == Block.DONE: - results[block.name] = 'generated' + results[block.name] = "generated" elif block.status == Block.PENDING: - results[block.name] = 'pending' + results[block.name] = "pending" elif block.status == Block.FAILED: - results[block.name] = 'failed' + results[block.name] = "failed" elif block.status == Block.CANCELLED: - results[block.name] = 'cancelled' + results[block.name] = "cancelled" elif obj.status == Experiment.CANCELLING: - results[block.name] = 'cancelling' + results[block.name] = "cancelling" else: - results[block.name] = 'processing' + results[block.name] = "processing" return results def get_execution_info(self, obj): results = {} for block in obj.blocks.iterator(): results[block.name] = { - 'linear_execution_time': block.linear_execution_time(), - 'speed_up_real': block.speed_up_real(), - 'speed_up_maximal': block.speed_up_maximal(), - 'queuing_time': block.queuing_time(), + "linear_execution_time": block.linear_execution_time(), + "speed_up_real": block.speed_up_real(), + "speed_up_maximal": block.speed_up_maximal(), + "queuing_time": block.queuing_time(), } return results def get_execution_order(self, obj): results = [] - for block in obj.blocks.order_by('execution_order').iterator(): + for block in obj.blocks.order_by("execution_order").iterator(): results.append(block.name) return results @@ -267,18 +286,20 @@ class ExperimentResultsSerializer(ShareableSerializer): results = {} for k in obj.blocks.filter(analyzer=True).all(): serializer = AnalyzerSerializer(k) - results[k.name] = serializer.data['results'] + results[k.name] = serializer.data["results"] return results def get_errors(self, obj): - serializer = BlockErrorSerializer(obj.blocks.filter(outputs__error_report__isnull=False), many=True) + serializer = BlockErrorSerializer( + obj.blocks.filter(outputs__error_report__isnull=False), many=True + ) return serializer.data def get_html_description(self, obj): d = obj.description if len(d) > 0: return restructuredtext(d) - return '' + return "" def get_description(self, obj): return obj.description @@ -288,8 +309,8 @@ class ExperimentResultsSerializer(ShareableSerializer): return None return { - 'date': obj.start_date.strftime("%b %d, %Y, %-H:%M"), - 'natural': naturaltime(obj.start_date), + "date": obj.start_date.strftime("%b %d, %Y, %-H:%M"), + "natural": naturaltime(obj.start_date), } def get_display_end_date(self, obj): @@ -297,6 +318,6 @@ class ExperimentResultsSerializer(ShareableSerializer): return None return { - 'date': obj.end_date.strftime("%b %d, %Y, %-H:%M"), - 'natural': naturaltime(obj.end_date), + "date": obj.end_date.strftime("%b %d, %Y, %-H:%M"), + "natural": naturaltime(obj.end_date), }