Skip to content

Commit

Permalink
update metrics counts handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Mar 20, 2024
1 parent 9b35a06 commit 400c606
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions wis2-gdc-management/wis2_gdc/registrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def __init__(self):

self.broker = None
self.metadata = None
self.backend = BACKENDS[BACKEND_TYPE](
{'connection': BACKEND_CONNECTION})

if PUBLISH_REPORTS:
self.broker = MQTTPubSubClient(BROKER_URL)
Expand Down Expand Up @@ -105,24 +107,24 @@ def register(self, metadata: dict) -> None:
if ets_results['ets-report']['summary']['FAILED'] > 0:
LOGGER.warning('ETS errors; metadata not published')
return
except KeyError: # validation error
except KeyError:
LOGGER.debug('Validation errors; metadata not published')
self.broker.pub('wis2-gdc/metrics/failed_total',
json.dumps(centre_id_labels))
self._process_record_metric(
self.metadata['id'], 'failed_total', centre_id_labels)
return

self.broker.pub('wis2-gdc/metrics/passed_total',
json.dumps(centre_id_labels))
self._process_record_metric(
self.metadata['id'], 'passed_total', centre_id_labels)

data_policy = self.metadata['properties']['wmo:dataPolicy']

self.broker.pub(f'wis2-gdc/metrics/{data_policy}_total',
json.dumps(centre_id_labels))
self._process_record_metric(
self.metadata['id'], f'{data_policy}_total', centre_id_labels)

LOGGER.info('Updating links')
self.update_record_links()

LOGGER.info(f'Publishing metadata to {BACKEND_TYPE} ({BACKEND_CONNECTION})') # noqa
LOGGER.info('Publishing metadata to backend')
self._publish()

if RUN_KPI:
Expand All @@ -135,6 +137,25 @@ def register(self, metadata: dict) -> None:
LOGGER.info('Publishing KPI report to broker')
self.broker.pub(topic, json.dumps(kpi_results))

def _process_record_metric(self, identifier: str, metric_name: str,
labels: list) -> None:
"""
Helper function to process record metric
:param identifier: identifier of metadata record
:param metric_name: `str` of name of metric
:param labels: `list` of labels to apply
:returns: `None`
"""

if self.backend.exists(identifier):
LOGGER.debug('Record exists; publishing metric')
else:
LOGGER.debug('Record does not exist; not publishing metric')
self.broker.pub(f'wis2-gdc/metrics/{metric_name}',
json.dumps(labels))

def _run_ets(self) -> dict:
"""
Helper function to run ETS
Expand Down Expand Up @@ -169,9 +190,8 @@ def _publish(self):
:returns: `None`
"""

backend = BACKENDS[BACKEND_TYPE]({'connection': BACKEND_CONNECTION})
LOGGER.info('Saving metadata to backend')
backend.save(self.metadata)
LOGGER.info(f'Saving to {BACKEND_TYPE} ({BACKEND_CONNECTION})')
self.backend.save(self.metadata)

def update_record_links(self) -> None:
"""
Expand Down

0 comments on commit 400c606

Please sign in to comment.