Skip to content

Commit

Permalink
make Pub/Sub report publishing configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Nov 26, 2023
1 parent ba65a3e commit 9f24659
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions docker/wis2-gdc.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export WIS2_GDC_BROKER_URL=mqtt://wis2-gdc:wis2-gdc@mosquitto:1883
export WIS2_GDC_CENTRE_ID=ca-eccc-msc-gdc
export WIS2_GDC_GB=mqtt://everyone:everyone@mosquitto:1883
export WIS2_GDC_GB_TOPIC=origin/a/wis2/+/+/metadata/#
export WIS2_GDC_PUBLISH_REPORTS=true

# global broker links
export WIS2_GDC_GB_LINK_METEOFRANCE="Météo-France,mqtts://everyone:[email protected]:8883"
Expand Down
1 change: 1 addition & 0 deletions wis2-gdc.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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_PUBLISH_REPORTS=true

# global broker links
export WIS2_GDC_GB_LINK_METEOFRANCE="Météo-France,mqtts://everyone:[email protected]:8883"
Expand Down
29 changes: 26 additions & 3 deletions wis2_gdc/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,43 @@
###############################################################################

import os
from typing import Any


def str2bool(value: Any) -> bool:
"""
helper function to return Python boolean
type (source: https://stackoverflow.com/a/715468)
:param value: value to be evaluated
:returns: `bool` of whether the value is boolean-ish
"""

value2 = False

if isinstance(value, bool):
value2 = value
else:
value2 = value.lower() in ('yes', 'true', 't', '1', 'on')

return value2


API_URL = os.environ.get('WIS2_GDC_API_URL')
API_URL_DOCKER = os.environ.get('WIS2_GDC_API_URL_DOCKER')
BACKEND_TYPE = os.environ.get('WIS2_GDC_BACKEND_TYPE')
BACKEND_CONNECTION = os.environ.get('WIS2_GDC_BACKEND_CONNECTION')
BROKER_URL = os.environ.get('WIS2_GDC_BROKER_URL')
CENTRE_ID = os.environ.get('WIS2_GDC_CENTRE_ID')
PUBLISH_REPORTS = str2bool(os.environ.get('WIS2_GDC_PUBLISH_REPORTS', 'false'))

GB_LINKS = []

if None in [API_URL, API_URL_DOCKER, BACKEND_TYPE,
BACKEND_CONNECTION, BROKER_URL, CENTRE_ID]:
BACKEND_CONNECTION, BROKER_URL, CENTRE_ID, PUBLISH_REPORTS]:
raise EnvironmentError('Environment variables not set!')

GB_LINKS = []

for key, value in os.environ.items():
if key.startswith('WIS2_GDC_GB_LINK'):
GB_LINKS.append(value.split(','))
20 changes: 13 additions & 7 deletions wis2_gdc/registrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from wis2_gdc.backend import BACKENDS
from wis2_gdc.env import (BACKEND_TYPE, BACKEND_CONNECTION, BROKER_URL,
GB_LINKS)
GB_LINKS, PUBLISH_REPORTS)

LOGGER = logging.getLogger(__name__)

Expand All @@ -46,8 +46,11 @@ def __init__(self):
:returns: `wis2_gdc.registrar.Registrar`
"""

self.broker = None
self.metadata = None
self.broker = MQTTPubSubClient(BROKER_URL)

if PUBLISH_REPORTS:
self.broker = MQTTPubSubClient(BROKER_URL)

def register(self, metadata: dict) -> None:
"""
Expand All @@ -67,8 +70,9 @@ def register(self, metadata: dict) -> None:
LOGGER.info('Running ETS')
ets_results = self._run_ets()

LOGGER.info('Publishing ETS report to broker')
self.broker.pub(topic, json.dumps(ets_results))
if self.broker is not None:
LOGGER.info('Publishing ETS report to broker')
self.broker.pub(topic, json.dumps(ets_results))

try:
if ets_results['ets-report']['summary']['FAILED'] > 0:
Expand All @@ -85,10 +89,12 @@ def register(self, metadata: dict) -> None:
LOGGER.info(f'Publishing metadata to {BACKEND_TYPE} ({BACKEND_CONNECTION})') # noqa
self._publish()

LOGGER.info('Running ETS')
LOGGER.info('Running KPI')
kpi_results = self._run_kpi()
LOGGER.info('Publishing KPI report to broker')
self.broker.pub(topic, json.dumps(kpi_results))

if self.broker is not None:
LOGGER.info('Publishing KPI report to broker')
self.broker.pub(topic, json.dumps(kpi_results))

def _run_ets(self) -> dict:
"""
Expand Down

0 comments on commit 9f24659

Please sign in to comment.