Skip to content
Snippets Groups Projects
Commit 514acf7a authored by Mattia Mancini's avatar Mattia Mancini
Browse files

OSB-28: add ordering by date and change in the summary parameters to include a...

OSB-28: add ordering by date and change in the summary parameters to include a lookback time instead of a datetime
parent 1271e4a3
No related branches found
No related tags found
2 merge requests!89Monitoring maintenance Epic branch merge,!1Resolve OSB-13 "Monitoringmaintenance "
......@@ -150,12 +150,12 @@ class ControllerStationOverview(APIView):
class ControllerStationTestsSummary(APIView):
"""
Overview of the latest station tests performed on the stations
Overview of the latest station tests performed on the stations # lookback days before now
"""
DEFAULT_STATION_GROUP = 'A'
DEFAULT_ONLY_ERRORS = True
DEFAULT_N_STATION_TESTS = 4
DEFAULT_LOOKBACK_TIME_IN_DAYS = 7
queryset = StationTest.objects.all()
schema = ManualSchema(fields=[
......@@ -163,8 +163,8 @@ class ControllerStationTestsSummary(APIView):
"station_group",
required=False,
location='query',
schema=coreschema.Enum(['C', 'R', 'I', 'A']),
description='Station group to select for choices are [C|R|I|ALL]'
schema=coreschema.Enum(['C', 'R', 'I', 'A'],
description='Station group to select for choices are [C|R|I|ALL]')
),
coreapi.Field(
"errors_only",
......@@ -174,10 +174,10 @@ class ControllerStationTestsSummary(APIView):
description='displays or not only the station with more than one error')
),
coreapi.Field(
"n_station_tests",
"lookback_time",
required=False,
location='query',
schema=coreschema.Integer(description='number of station tests to select',
schema=coreschema.Integer(description='number of days from now (default 7)',
minimum=1)
)
]
......@@ -196,8 +196,8 @@ class ControllerStationTestsSummary(APIView):
self.errors_only = request.query_params.get('errors_only', self.DEFAULT_ONLY_ERRORS)
self.station_group = request.query_params.get('station_group', self.DEFAULT_STATION_GROUP)
self.n_station_tests = int(request.query_params.get('n_station_tests',
self.DEFAULT_N_STATION_TESTS))
self.lookback_time = datetime.timedelta(int(request.query_params.get('lookback_time',
self.DEFAULT_LOOKBACK_TIME_IN_DAYS)))
def get(self, request, format=None):
try:
......@@ -209,56 +209,50 @@ class ControllerStationTestsSummary(APIView):
return Response(status=status.HTTP_406_NOT_ACCEPTABLE,
data='Please specify all the required parameters: %s' % (e,))
station_entities = Station.objects.all()
station_test_list = StationTest.objects\
.filter(start_datetime__gte=datetime.date.today()-self.lookback_time)\
.order_by('-start_datetime', 'station__name')
for group in self.station_group:
if group is not 'A':
station_entities = station_entities.filter(type=group)
station_entities = station_test_list.filter(type=group)
# Since django preferes a ordered dict over a dict we make it happy... for now
response_payload = list()
for station_entity in station_entities:
station_test_list = StationTest.objects.\
filter(station__name=station_entity.name).order_by('-end_datetime')[:self.n_station_tests]
for station_test in station_test_list:
station_test_payload = OrderedDict()
component_errors = station_test.component_errors
station_test_payload['station_name'] = station_entity.name
station_test_payload[
'total_component_errors'] = station_test.component_errors.count()
station_test_payload['date'] = station_test.start_datetime.strftime('%Y-%m-%d')
station_test_payload['start_datetime'] = station_test.start_datetime
station_test_payload['end_datetime'] = station_test.end_datetime
station_test_payload['checks'] = station_test.checks
component_errors_summary = component_errors. \
values('component__type', 'type').annotate(
total=Count('type')).order_by('-total')
component_errors_summary_dict = OrderedDict()
for item in component_errors_summary:
item_component_type = item['component__type']
item_error_type = item['type']
item_error_total = item['total']
if item_component_type not in component_errors_summary_dict:
component_errors_summary_dict[item_component_type] = OrderedDict()
component_errors_summary_dict[item_component_type][item_error_type] = \
item_error_total
station_test_payload['component_error_summary'] = component_errors_summary_dict
response_payload.append(station_test_payload)
for station_test in station_test_list:
station_test_payload = OrderedDict()
component_errors = station_test.component_errors
station_test_payload['station_name'] = station_test.station.name
station_test_payload[
'total_component_errors'] = station_test.component_errors.count()
station_test_payload['date'] = station_test.start_datetime.strftime('%Y-%m-%d')
station_test_payload['start_datetime'] = station_test.start_datetime
station_test_payload['end_datetime'] = station_test.end_datetime
station_test_payload['checks'] = station_test.checks
component_errors_summary = component_errors. \
values('component__type', 'type').annotate(
total=Count('type')).order_by('-total')
component_errors_summary_dict = OrderedDict()
for item in component_errors_summary:
item_component_type = item['component__type']
item_error_type = item['type']
item_error_total = item['total']
if item_component_type not in component_errors_summary_dict:
component_errors_summary_dict[item_component_type] = OrderedDict()
component_errors_summary_dict[item_component_type][item_error_type] = \
item_error_total
station_test_payload['component_error_summary'] = component_errors_summary_dict
response_payload.append(station_test_payload)
if self.errors_only and self.errors_only is not 'false':
response_payload = filter(
lambda station_test_entry:
station_test_entry['total_component_errors'] > 0,
response_payload)
response_payload = sorted(response_payload, key=lambda item: item['station_name'])
response_payload = sorted(response_payload, key=lambda item: item['date'], reverse=True)
return Response(status=status.HTTP_200_OK, data=response_payload)
......@@ -337,7 +331,7 @@ class ControllerLatestObservations(APIView):
return Response(status=status.HTTP_406_NOT_ACCEPTABLE,
data='Please specify both the start and the end date: %s' % (e,))
rtsm_observation_entities = RTSMObservation.objects.filter(
rtsm_observation_entities = RTSMObservation.objects.order_by('-start_datetime').filter(
start_datetime__gte=self.from_date)
for group in self.station_group:
if group is not 'A':
......@@ -358,7 +352,7 @@ class ControllerLatestObservations(APIView):
rtsm_list = RTSMObservation.objects.filter(
start_datetime__gte=self.from_date,
observation_id=rtsm_observation_entity['observation_id']). \
order_by('-end_datetime')
order_by('-start_datetime')
unique_modes = [item['errors__mode'] for item in rtsm_list.values('errors__mode').distinct()]
observation_payload['mode'] = unique_modes
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment