mirror of
https://github.com/ansible/awx.git
synced 2026-05-15 21:37:42 -02:30
HostMetricSummaryMonthly API and Migrations
This commit is contained in:
committed by
John Westcott IV
parent
05f918e666
commit
f919178734
@@ -385,6 +385,10 @@ class FieldLookupBackend(BaseFilterBackend):
|
||||
raise ParseError(json.dumps(e.messages, ensure_ascii=False))
|
||||
|
||||
|
||||
class HostMetricSummaryMonthlyFieldLookupBackend(FieldLookupBackend):
|
||||
RESERVED_NAMES = ('page', 'page_size', 'format', 'order', 'order_by', 'search', 'type', 'past_months', 'count_disabled', 'no_truncate', 'limit')
|
||||
|
||||
|
||||
class OrderByBackend(BaseFilterBackend):
|
||||
"""
|
||||
Filter to apply ordering based on query string parameters.
|
||||
|
||||
@@ -57,6 +57,7 @@ from awx.main.models import (
|
||||
Group,
|
||||
Host,
|
||||
HostMetric,
|
||||
HostMetricSummaryMonthly,
|
||||
Instance,
|
||||
InstanceGroup,
|
||||
InstanceLink,
|
||||
@@ -5406,6 +5407,13 @@ class HostMetricSerializer(BaseSerializer):
|
||||
)
|
||||
|
||||
|
||||
class HostMetricSummaryMonthlySerializer(BaseSerializer):
|
||||
class Meta:
|
||||
model = HostMetricSummaryMonthly
|
||||
read_only_fields = ("id", "date", "license_consumed", "license_capacity", "hosts_added", "hosts_deleted", "indirectly_managed_hosts")
|
||||
fields = read_only_fields
|
||||
|
||||
|
||||
class InstanceGroupSerializer(BaseSerializer):
|
||||
show_capabilities = ['edit', 'delete']
|
||||
capacity = serializers.SerializerMethodField()
|
||||
|
||||
@@ -30,6 +30,7 @@ from awx.api.views import (
|
||||
OAuth2TokenList,
|
||||
ApplicationOAuth2TokenList,
|
||||
OAuth2ApplicationDetail,
|
||||
HostMetricSummaryMonthlyList,
|
||||
)
|
||||
|
||||
from awx.api.views.bulk import (
|
||||
@@ -120,6 +121,7 @@ v2_urls = [
|
||||
re_path(r'^inventories/', include(inventory_urls)),
|
||||
re_path(r'^hosts/', include(host_urls)),
|
||||
re_path(r'^host_metrics/', include(host_metric_urls)),
|
||||
re_path(r'^host_metric_summary_monthly/$', HostMetricSummaryMonthlyList.as_view(), name='host_metric_summary_monthly_list'),
|
||||
re_path(r'^groups/', include(group_urls)),
|
||||
re_path(r'^inventory_sources/', include(inventory_source_urls)),
|
||||
re_path(r'^inventory_updates/', include(inventory_update_urls)),
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
# Python
|
||||
import dateutil
|
||||
import datetime
|
||||
import functools
|
||||
import html
|
||||
import itertools
|
||||
@@ -17,7 +18,6 @@ from collections import OrderedDict
|
||||
|
||||
from urllib3.exceptions import ConnectTimeoutError
|
||||
|
||||
|
||||
# Django
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import FieldError, ObjectDoesNotExist
|
||||
@@ -122,6 +122,7 @@ from awx.api.views.mixin import (
|
||||
UnifiedJobDeletionMixin,
|
||||
NoTruncateMixin,
|
||||
)
|
||||
from awx.api.filters import HostMetricSummaryMonthlyFieldLookupBackend
|
||||
from awx.api.pagination import UnifiedJobEventPagination
|
||||
from awx.main.utils import set_environ
|
||||
|
||||
@@ -1568,6 +1569,37 @@ class HostMetricDetail(RetrieveDestroyAPIView):
|
||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
class HostMetricSummaryMonthlyList(ListAPIView):
|
||||
name = _("Host Metrics Summary Monthly")
|
||||
model = models.HostMetricSummaryMonthly
|
||||
permission_classes = (IsSystemAdminOrAuditor,)
|
||||
serializer_class = serializers.HostMetricSummaryMonthlySerializer
|
||||
search_fields = ('date',)
|
||||
filter_backends = [HostMetricSummaryMonthlyFieldLookupBackend]
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
past_months = self.request.query_params.get('past_months', None)
|
||||
date_from = self._get_date_from(past_months)
|
||||
|
||||
queryset = queryset.filter(date__gte=date_from)
|
||||
return queryset
|
||||
|
||||
@staticmethod
|
||||
def _get_date_from(past_months, default=12, maximum=36):
|
||||
try:
|
||||
months_ago = int(past_months or default)
|
||||
except ValueError:
|
||||
months_ago = default
|
||||
months_ago = min(months_ago, maximum)
|
||||
months_ago = max(months_ago, 1)
|
||||
|
||||
date_from = datetime.date.today() - dateutil.relativedelta.relativedelta(months=months_ago)
|
||||
# Set to beginning of the month
|
||||
date_from = date_from.replace(day=1).isoformat()
|
||||
return date_from
|
||||
|
||||
|
||||
class HostList(HostRelatedSearchMixin, ListCreateAPIView):
|
||||
always_allow_superuser = False
|
||||
model = models.Host
|
||||
|
||||
Reference in New Issue
Block a user