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

OSB-28: increase speed in quering the latestObservations

parent 10f8be5a
No related branches found
No related tags found
2 merge requests!89Monitoring maintenance Epic branch merge,!1Resolve OSB-13 "Monitoringmaintenance "
...@@ -263,14 +263,13 @@ class ControllerLatestObservations(APIView): ...@@ -263,14 +263,13 @@ class ControllerLatestObservations(APIView):
DEFAULT_STATION_GROUP = 'A' DEFAULT_STATION_GROUP = 'A'
DEFAULT_ONLY_ERRORS = True DEFAULT_ONLY_ERRORS = True
queryset = StationTest.objects.all()
schema = ManualSchema(fields=[ schema = ManualSchema(fields=[
coreapi.Field( coreapi.Field(
"station_group", "station_group",
required=False, required=False,
location='query', location='query',
schema=coreschema.Enum(['C', 'R', 'I', 'A'], description= schema=coreschema.Enum(['C', 'R', 'I', 'A'], description=
'Station group to select for choices are [C|R|I|ALL]', 'Station group to select for choices are [C|R|I|A]',
) )
), ),
coreapi.Field( coreapi.Field(
...@@ -299,19 +298,6 @@ class ControllerLatestObservations(APIView): ...@@ -299,19 +298,6 @@ class ControllerLatestObservations(APIView):
except Exception as e: except Exception as e:
raise ValueError('cannot parse %s with format %s - %s' % (date, expected_format, e)) raise ValueError('cannot parse %s with format %s - %s' % (date, expected_format, e))
def compute_rtsm_observation_summary(self, rtsm_errors):
errors_summary = OrderedDict()
errors_summary_query = rtsm_errors.annotate(total=
Window(expression=Count('rcu'),
partition_by=[F(
'error_type')])).values(
'error_type', 'total').distinct()
for error_summary in errors_summary_query:
errors_summary[error_summary['error_type']] = error_summary['total']
return errors_summary
def validate_query_parameters(self, request): def validate_query_parameters(self, request):
self.errors_only = request.query_params.get('errors_only', self.DEFAULT_ONLY_ERRORS) 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.station_group = request.query_params.get('station_group', self.DEFAULT_STATION_GROUP)
...@@ -328,62 +314,66 @@ class ControllerLatestObservations(APIView): ...@@ -328,62 +314,66 @@ class ControllerLatestObservations(APIView):
except KeyError as e: except KeyError as e:
return Response(status=status.HTTP_406_NOT_ACCEPTABLE, return Response(status=status.HTTP_406_NOT_ACCEPTABLE,
data='Please specify both the start and the end date: %s' % (e,)) data='Please specify both the start and the end date: %s' % (e,))
filtered_entities = RTSMObservation.objects\
rtsm_observation_entities = RTSMObservation.objects.defer('errors').filter( .filter(start_datetime__gte=self.from_date)
start_datetime__gte=self.from_date) if self.station_group != 'A':
for group in self.station_group: filtered_entities = filtered_entities\
if group is not 'A': .filter(station__type=self.station_group)
rtsm_observation_entities = rtsm_observation_entities.filter(station__type=group) if self.errors_only:
filtered_entities = filtered_entities.exclude(errors_summary__isnull=True)
# Since django preferes a ordered dict over a dict we make it happy... for now
response_payload = list() errors_summary = filtered_entities\
for rtsm_observation_entity in rtsm_observation_entities.values('observation_id', .values('observation_id',
'start_datetime', 'station__name',
'end_datetime'). \ 'start_datetime',
distinct().order_by('-start_datetime'): 'end_datetime',
observation_payload = OrderedDict() 'errors_summary__error_type',
'errors_summary__mode')\
observation_payload['observation_id'] = rtsm_observation_entity['observation_id'] .annotate(total=Count('errors_summary__error_type'))\
observation_payload['start_datetime'] = rtsm_observation_entity['start_datetime'] .order_by('observation_id', 'station__name')
observation_payload['end_datetime'] = rtsm_observation_entity['end_datetime'] response = dict()
rtsm_list = RTSMObservation.objects.defer('errors').filter( for error_summary in errors_summary:
start_datetime__gte=self.from_date, observation_id = error_summary['observation_id']
observation_id=rtsm_observation_entity['observation_id']) station_name = error_summary['station__name']
unique_modes = [item['errors_summary__mode'] for item in start_datetime = error_summary['start_datetime']
rtsm_list.values('errors_summary__mode').distinct()] end_datetime = error_summary['end_datetime']
mode = error_summary['errors_summary__mode']
observation_payload['mode'] = unique_modes error_type = error_summary['errors_summary__error_type']
observation_payload['total_component_errors'] = 0 total = error_summary['total']
station_involved_list = list() if observation_id not in response:
response[observation_id] = OrderedDict()
for rtsm_entry_per_station in rtsm_list.order_by('station__name'): response[observation_id]['observation_id'] = observation_id
station_summary = OrderedDict() response[observation_id]['start_datetime'] = start_datetime
station_summary['station_name'] = rtsm_entry_per_station.station.name response[observation_id]['end_datetime'] = end_datetime
station_summary['n_errors'] = \ response[observation_id]['total_component_errors'] = 0
rtsm_entry_per_station.errors.values('rcu').distinct().count() response[observation_id]['mode'] = list()
station_summary['component_error_summary'] = self.compute_rtsm_observation_summary( response[observation_id]['station_involved'] = dict()
rtsm_entry_per_station.errors_summary)
if total == 0:
station_involved_list.append(station_summary) continue
observation_payload['total_component_errors'] += station_summary['n_errors'] response[observation_id]['total_component_errors'] += total
station_involved_summary = response[observation_id]['station_involved']
observation_payload['station_involved'] = station_involved_list response[observation_id]['mode'] += [mode]\
if mode not in response[observation_id]['mode'] else []
response_payload.append(observation_payload) if station_name not in station_involved_summary:
if self.errors_only and self.errors_only != 'false': station_involved_summary[station_name] = OrderedDict()
station_involved_summary[station_name]['station_name'] = station_name
response_payload = filter( station_involved_summary[station_name]['n_errors'] = 0
lambda station_entry: station_involved_summary[station_name]['component_error_summary'] = OrderedDict()
len(station_entry['total_component_errors']) > 0,
response_payload) station_involved_summary[station_name]['n_errors'] += total
station_involved_summary[station_name]['component_error_summary'][error_type] = total
response_payload = sorted(response.values(),
key= lambda item: item['start_datetime'],
reverse=True)
return Response(status=status.HTTP_200_OK, data=response_payload) return Response(status=status.HTTP_200_OK, data=response_payload)
class ControllerStationTestStatistics(APIView): class ControllerStationTestStatistics(APIView):
""" """
......
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