Skip to content

Commit

Permalink
refactor registration to take WCMP2 from CLI and WNM from Pub/Sub
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Feb 27, 2024
1 parent c029005 commit 4d3b4b1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
click
pywcmp
pywis-pubsub
requests
unicodecsv
2 changes: 1 addition & 1 deletion wis2-gdc.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export WIS2_GDC_BACKEND_CONNECTION=http://localhost:9200/wis2-discovery-metadata
export WIS2_GDC_BROKER_URL=mqtt://wis2-gdc:wis2-gdc@localhost:1883
export WIS2_GDC_CENTRE_ID=ca-eccc-msc-gdc
export WIS2_GDC_GB=mqtt://everyone:everyone@localhost:1883
export WIS2_GDC_GB_TOPIC=origin/a/wis2/+/+/metadata/#
export WIS2_GDC_GB_TOPIC=origin/a/wis2/+/metadata/#
export WIS2_GDC_OPENMETRICS_FILE=/tmp/wis2-gdc-openmetrics.txt
export WIS2_GDC_PUBLISH_REPORTS=true

Expand Down
3 changes: 2 additions & 1 deletion wis2_gdc/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class DiscoveryMetadataHook(Hook):
def execute(self, topic: str, msg_dict: dict) -> None:
LOGGER.debug('Discovery metadata hook execution begin')
r = Registrar()
r.register(msg_dict)
wcmp2_dict = r.get_wcmp2(msg_dict)
r.register(wcmp2_dict)
LOGGER.debug('Discovery metadata hook execution end')

def __repr__(self):
Expand Down
5 changes: 4 additions & 1 deletion wis2_gdc/monitor/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
###############################################################################

from datetime import datetime
import logging
from typing import Union

import prometheus_client
Expand All @@ -31,6 +32,8 @@
prometheus_client.REGISTRY.unregister(prometheus_client.PLATFORM_COLLECTOR)
prometheus_client.REGISTRY.unregister(prometheus_client.PROCESS_COLLECTOR)

LOGGER = logging.getLogger(__name__)

# sets metrics as per https://github.com/wmo-im/wis2-metric-hierarchy/blob/main/metric-hierarchy/gdc.csv # noqa


Expand Down Expand Up @@ -155,7 +158,7 @@ def write(self) -> None:
write_to_textfile(filename, self.registry)

def __exit__(self, exc_type, exc_value, traceback):
print("EXITING")
LOGGER.debug('Exiting')

def __repr__(self):
return '<Metrics>'
21 changes: 21 additions & 0 deletions wis2_gdc/registrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pathlib import Path

import click
import requests

from pywcmp.wcmp2.ets import WMOCoreMetadataProfileTestSuite2
from pywcmp.wcmp2.kpi import WMOCoreMetadataProfileKeyPerformanceIndicators
Expand Down Expand Up @@ -54,6 +55,25 @@ def __init__(self):
if PUBLISH_REPORTS:
self.broker = MQTTPubSubClient(BROKER_URL)

def get_wcmp2(self, wnm: dict) -> dict:
"""
Helper function to fetch WCMP2 document from a WNM
:param wnm: `dict` of WNM
:returns: `dict` of WCMP2
"""

try:
LOGGER.debug('Fetching canonical URL')
wcmp2_url = list(filter(lambda d: d['rel'] == 'canonical',
wnm['links']))[0]
except (IndexError, KeyError):
LOGGER.error('No canonical link found')
raise

return requests.get(wcmp2_url).json()

def register(self, metadata: dict) -> None:
"""
Register a metadata document
Expand All @@ -64,6 +84,7 @@ def register(self, metadata: dict) -> None:
"""

self.metadata = metadata

self.centre_id = self.metadata['id'].split(':')[3]
topic = f"report/a/wis2/{self.centre_id}"
centre_id_labels = [self.centre_id, CENTRE_ID]
Expand Down

0 comments on commit 4d3b4b1

Please sign in to comment.