diff --git a/ingest/api/datastore.py b/ingest/api/datastore.py index 1c876d90..8e617af1 100644 --- a/ingest/api/datastore.py +++ b/ingest/api/datastore.py @@ -3,6 +3,10 @@ from datetime import datetime from dateutil import parser +from api.utilities import convert_to_meter +from api.utilities import seconds_to_iso_8601_duration + + import datastore_pb2 as dstore from google.protobuf.timestamp_pb2 import Timestamp @@ -33,7 +37,14 @@ def build_grpc_messages(msg: str) -> None: period = str(ts_metadata.period) function = ts_metadata.function standard_name = ts_metadata.standard_name - parameter_name = ":".join([standard_name, level, function, period]) + parameter_name = ":".join( + [ + standard_name, + convert_to_meter(level), + function, + seconds_to_iso_8601_duration(period), + ] + ) setattr(ts_metadata, "parameter_name", parameter_name) observation_data = dstore.ObsMetadata() diff --git a/ingest/api/ingest.py b/ingest/api/ingest.py index e76f20ce..e1780f13 100644 --- a/ingest/api/ingest.py +++ b/ingest/api/ingest.py @@ -1,7 +1,6 @@ import logging from typing import Union -import isodate import grpc import json @@ -14,6 +13,9 @@ import datastore_pb2 as dstore +from api.utilities import seconds_to_iso_8601_duration +from api.utilities import convert_to_meter + logger = logging.getLogger(__name__) @@ -36,25 +38,6 @@ def __init__( logger.error("Failed to establish connection to mqtt, " + "\n" + str(e)) raise e - def seconds_to_iso_8601_duration(self, seconds: int) -> str: - duration = isodate.Duration(seconds=seconds) - iso_duration = isodate.duration_isoformat(duration) - - # TODO: find a better way to format these - # Use PT24H instead of P1D - if iso_duration == "P1D": - iso_duration = "PT24H" - - # iso_duration defaults to P0D when seconds is 0 - if iso_duration == "P0D": - iso_duration = "PT0S" - - return iso_duration - - def convert_to_meter(self, level: int) -> str: - level = str(float(level) / 100) - return level - async def ingest(self, message: Union[str, object]): """ This method will interpret call all methods for deciding input type, build the mqtt messages, and @@ -88,8 +71,8 @@ async def publish_messages(self, messages: list): ) # modify the period back to iso format and level back to meter - period_iso = self.seconds_to_iso_8601_duration(msg["properties"]["period"]) - level_string = self.convert_to_meter(msg["properties"]["level"]) + period_iso = seconds_to_iso_8601_duration(msg["properties"]["period"]) + level_string = convert_to_meter(msg["properties"]["level"]) msg["properties"]["level"] = level_string msg["properties"]["period"] = period_iso try: diff --git a/ingest/api/utilities.py b/ingest/api/utilities.py new file mode 100644 index 00000000..ac3dd9df --- /dev/null +++ b/ingest/api/utilities.py @@ -0,0 +1,22 @@ +import isodate + + +def seconds_to_iso_8601_duration(seconds: int) -> str: + duration = isodate.Duration(seconds=seconds) + iso_duration = isodate.duration_isoformat(duration) + + # TODO: find a better way to format these + # Use PT24H instead of P1D + if iso_duration == "P1D": + iso_duration = "PT24H" + + # iso_duration defaults to P0D when seconds is 0 + if iso_duration == "P0D": + iso_duration = "PT0S" + + return iso_duration + + +def convert_to_meter(level: int) -> str: + level = str(float(level) / 100) + return level