2017-03-22 16:15:25 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
from __future__ import unicode_literals
|
2022-11-11 11:20:17 +00:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
2017-03-22 16:15:25 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2022-11-11 11:20:17 +00:00
|
|
|
from common.drf.fields import ReadableHiddenField
|
2022-10-08 08:55:14 +00:00
|
|
|
from ..models import AdHoc, AdHocExecution
|
2017-03-22 16:15:25 +00:00
|
|
|
|
|
|
|
|
2022-11-11 11:20:17 +00:00
|
|
|
class AdHocSerializer(serializers.ModelSerializer):
|
|
|
|
owner = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
|
|
|
row_count = serializers.IntegerField(read_only=True)
|
|
|
|
size = serializers.IntegerField(read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AdHoc
|
|
|
|
fields = ["id", "name", "module", "owner", "row_count", "size", "date_created", "date_updated"]
|
|
|
|
|
|
|
|
|
|
|
|
class AdhocListSerializer(AdHocSerializer):
|
|
|
|
row_count = serializers.IntegerField(read_only=True)
|
|
|
|
size = serializers.IntegerField(read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AdHoc
|
|
|
|
fields = ["id", "name", "module", "row_count", "size", "args", "owner", "date_created", "date_updated"]
|
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class AdHocExecutionSerializer(serializers.ModelSerializer):
|
2017-12-11 09:08:43 +00:00
|
|
|
stat = serializers.SerializerMethodField()
|
2020-06-15 11:42:36 +00:00
|
|
|
last_success = serializers.ListField(source='success_hosts')
|
|
|
|
last_failure = serializers.DictField(source='failed_hosts')
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
class Meta:
|
2020-03-12 08:24:38 +00:00
|
|
|
model = AdHocExecution
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
2022-10-18 02:43:51 +00:00
|
|
|
'timedelta', 'result', 'summary', 'short_id',
|
2021-04-29 11:10:45 +00:00
|
|
|
'is_finished', 'is_success',
|
|
|
|
'date_start', 'date_finished',
|
2020-06-15 11:42:36 +00:00
|
|
|
]
|
2022-10-18 02:43:51 +00:00
|
|
|
fields_fk = ['task', 'task_display']
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_custom = ['stat', 'last_success', 'last_failure']
|
|
|
|
fields = fields_small + fields_fk + fields_custom
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_task(obj):
|
2019-12-18 07:37:53 +00:00
|
|
|
return obj.task.id
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2017-12-11 09:08:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_stat(obj):
|
2020-05-19 07:15:17 +00:00
|
|
|
count_failed_hosts = len(obj.failed_hosts)
|
|
|
|
count_success_hosts = len(obj.success_hosts)
|
|
|
|
count_total = count_success_hosts + count_failed_hosts
|
2017-12-11 09:08:43 +00:00
|
|
|
return {
|
2020-05-19 07:15:17 +00:00
|
|
|
"total": count_total,
|
|
|
|
"success": count_success_hosts,
|
|
|
|
"failed": count_failed_hosts
|
2017-12-11 09:08:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 07:37:53 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class AdHocExecutionExcludeResultSerializer(AdHocExecutionSerializer):
|
2020-06-15 11:42:36 +00:00
|
|
|
class Meta:
|
|
|
|
model = AdHocExecution
|
|
|
|
fields = [
|
|
|
|
'id', 'task', 'task_display', 'hosts_amount', 'adhoc', 'date_start', 'stat',
|
|
|
|
'date_finished', 'timedelta', 'is_finished', 'is_success',
|
|
|
|
'short_id', 'adhoc_short_id', 'last_success', 'last_failure'
|
|
|
|
]
|
2018-12-10 02:11:54 +00:00
|
|
|
|
|
|
|
|
2020-06-15 11:42:36 +00:00
|
|
|
class AdHocExecutionNestSerializer(serializers.ModelSerializer):
|
|
|
|
last_success = serializers.ListField(source='success_hosts')
|
|
|
|
last_failure = serializers.DictField(source='failed_hosts')
|
|
|
|
last_run = serializers.CharField(source='short_id')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AdHocExecution
|
|
|
|
fields = (
|
2021-04-29 11:10:45 +00:00
|
|
|
'last_success', 'last_failure', 'last_run', 'timedelta',
|
|
|
|
'is_finished', 'is_success'
|
2020-06-15 11:42:36 +00:00
|
|
|
)
|