2017-03-22 16:15:25 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
from rest_framework import serializers
|
2018-12-10 02:11:54 +00:00
|
|
|
from django.shortcuts import reverse
|
2017-03-22 16:15:25 +00:00
|
|
|
|
2022-10-08 08:55:14 +00:00
|
|
|
from ..models import AdHoc, AdHocExecution
|
2017-03-22 16:15:25 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2019-12-18 07:37:53 +00:00
|
|
|
class AdHocSerializer(serializers.ModelSerializer):
|
2020-06-15 11:42:36 +00:00
|
|
|
tasks = serializers.ListField()
|
2019-12-18 07:37:53 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AdHoc
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
2022-10-18 02:43:51 +00:00
|
|
|
'tasks', "pattern", "args", "date_created",
|
2019-12-18 07:37:53 +00:00
|
|
|
]
|
2022-10-18 02:43:51 +00:00
|
|
|
fields_fk = ["last_execution"]
|
|
|
|
fields_m2m = ["assets"]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields = fields_small + fields_fk + fields_m2m
|
2019-12-18 07:37:53 +00:00
|
|
|
read_only_fields = [
|
2020-03-12 08:24:38 +00:00
|
|
|
'date_created'
|
2019-12-18 07:37:53 +00:00
|
|
|
]
|
|
|
|
extra_kwargs = {
|
|
|
|
"become": {'write_only': True}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class AdHocDetailSerializer(AdHocSerializer):
|
|
|
|
latest_execution = AdHocExecutionNestSerializer(allow_null=True)
|
|
|
|
task_name = serializers.CharField(source='task.name')
|
|
|
|
|
|
|
|
class Meta(AdHocSerializer.Meta):
|
|
|
|
fields = AdHocSerializer.Meta.fields + [
|
2022-10-18 02:43:51 +00:00
|
|
|
'latest_execution', 'created_by', 'task_name'
|
2020-06-15 11:42:36 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-10-08 08:55:14 +00:00
|
|
|
# class CommandExecutionSerializer(serializers.ModelSerializer):
|
|
|
|
# result = serializers.JSONField(read_only=True)
|
|
|
|
# log_url = serializers.SerializerMethodField()
|
|
|
|
#
|
|
|
|
# class Meta:
|
|
|
|
# model = CommandExecution
|
|
|
|
# fields_mini = ['id']
|
|
|
|
# fields_small = fields_mini + [
|
|
|
|
# 'command', 'result', 'log_url',
|
|
|
|
# 'is_finished', 'date_created', 'date_finished'
|
|
|
|
# ]
|
|
|
|
# fields_m2m = ['hosts']
|
|
|
|
# fields = fields_small + fields_m2m
|
|
|
|
# read_only_fields = [
|
|
|
|
# 'result', 'is_finished', 'log_url', 'date_created',
|
|
|
|
# 'date_finished'
|
|
|
|
# ]
|
|
|
|
# ref_name = 'OpsCommandExecution'
|
|
|
|
#
|
|
|
|
# @staticmethod
|
|
|
|
# def get_log_url(obj):
|
|
|
|
# return reverse('api-ops:celery-task-log', kwargs={'pk': obj.id})
|
2019-12-05 07:09:25 +00:00
|
|
|
|