Skip to content

Commit

Permalink
add option to publish specific stations to a certain topic (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Aug 13, 2024
1 parent 4713303 commit fe9a83a
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions wis2box-management/wis2box/metadata/station.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ def check_station_datasets(wigos_station_identifier: str) -> Iterator[dict]:
yield topic


def add_topic_hierarchy(new_topic: str, territory_name: str, wsi: str) -> None:
def add_topic_hierarchy(new_topic: str, territory_name: str = None,
wsi: str = None) -> None:
"""
Adds topic hierarchy to topic
Expand All @@ -264,9 +265,9 @@ def add_topic_hierarchy(new_topic: str, territory_name: str, wsi: str) -> None:

valid_topics = [topic for link, topic in load_datasets()]
if new_topic not in valid_topics:
msg = f'ERROR: Invalid topic: {new_topic}\n'
msg += 'Valid topics:\n'
msg += '\n'.join(valid_topics)
msg = (f'ERROR: Invalid topic: {new_topic}\n'
'Valid topics:\n'
'\n'.join(valid_topics))
LOGGER.error(msg)
return

Expand All @@ -293,23 +294,36 @@ def add_topic_hierarchy(new_topic: str, territory_name: str, wsi: str) -> None:
upsert_collection_item('stations', feature)


def publish_from_csv() -> None:
def publish_from_csv(path: Path = None, topic: str = None) -> None:
"""
Publishes station collection to API config and backend from csv
:param path: `Path` to station list CSV (default is `None`)
:param topic: `str` of topic hierarchy (default is `None`)
:returns: `None`
"""

if not STATIONS.exists():
msg = f'Please create a station metadata file in {STATION_METADATA}'
LOGGER.error(msg)
raise RuntimeError(msg)
stations_csv_to_publish = STATIONS

if path is not None:
if not path.exists():
msg = f'Station file {path} does not exist'
LOGGER.error(msg)
raise RuntimeError(msg)

stations_csv_to_publish = path
else:
if not STATIONS.exists():
msg = f'Please create a station metadata file in {STATION_METADATA}' # noqa
LOGGER.error(msg)
raise RuntimeError(msg)

oscar_baseurl = 'https://oscar.wmo.int/surface/#/search/station/stationReportDetails' # noqa

LOGGER.debug(f'Publishing station list from {STATIONS}')
LOGGER.debug(f'Publishing station list from {stations_csv_to_publish}')
station_list = []
with STATIONS.open() as fh:
with stations_csv_to_publish.open() as fh:
reader = csv.DictReader(fh)

for row in reader:
Expand Down Expand Up @@ -378,6 +392,13 @@ def publish_from_csv() -> None:
LOGGER.debug(f'Station does not exist: {err}')
upsert_collection_item('stations', feature)

if None not in [path, topic]:
msg = f"Adding topic {topic} to station {row['wigos_station_identifier']}" # noqa
LOGGER.debug(msg)

add_topic_hierarchy(topic, row['territory_name'],
row['wigos_station_identifier'])

LOGGER.info(f'Updated station list: {station_list}')
# inform mqtt-metrics-collector
notify_msg = {
Expand Down Expand Up @@ -495,11 +516,14 @@ def setup(ctx, verbosity):

@click.command()
@click.pass_context
@cli_helpers.OPTION_PATH
@cli_helpers.OPTION_TOPIC_HIERARCHY
@cli_helpers.OPTION_VERBOSITY
def publish_collection(ctx, verbosity):
def publish_collection(ctx, path, topic, verbosity):
"""Publish from station_list.csv"""

publish_from_csv()
publish_from_csv(path, topic)

click.echo('Done')


Expand Down

0 comments on commit fe9a83a

Please sign in to comment.