diff --git a/awx/api/serializers.py b/awx/api/serializers.py index c76c75839f..6772aa89e0 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -3443,6 +3443,25 @@ class AdHocCommandSerializer(UnifiedJobSerializer): return vars_validate_or_raise(value) +class AdHocCommandDetailSerializer(AdHocCommandSerializer): + + host_status_counts = serializers.SerializerMethodField( + help_text=_('A count of hosts uniquely assigned to each status.'), + ) + + class Meta: + model = AdHocCommand + fields = ('*', 'host_status_counts',) + + def get_host_status_counts(self, obj): + try: + counts = obj.ad_hoc_command_events.only('event_data').get(event='playbook_on_stats').get_host_status_counts() + except AdHocCommandEvent.DoesNotExist: + counts = {} + + return counts + + class AdHocCommandCancelSerializer(AdHocCommandSerializer): can_cancel = serializers.BooleanField(read_only=True) diff --git a/awx/api/views.py b/awx/api/views.py index bc6a785002..379f8c6926 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -4553,7 +4553,7 @@ class HostAdHocCommandsList(AdHocCommandList, SubListCreateAPIView): class AdHocCommandDetail(UnifiedJobDeletionMixin, RetrieveDestroyAPIView): model = AdHocCommand - serializer_class = AdHocCommandSerializer + serializer_class = AdHocCommandDetailSerializer class AdHocCommandCancel(RetrieveAPIView): diff --git a/awx/main/models/events.py b/awx/main/models/events.py index 8b8637d8f2..4361f01853 100644 --- a/awx/main/models/events.py +++ b/awx/main/models/events.py @@ -607,6 +607,9 @@ class BaseCommandEvent(CreatedModifiedModel): ''' return self.event + def get_host_status_counts(self): + return create_host_status_counts(getattr(self, 'event_data', {})) + class AdHocCommandEvent(BaseCommandEvent):