Skip to content

Commit

Permalink
Move some utliity and update parameter-name generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Teddy-1000 committed Oct 11, 2024
1 parent 02709b1 commit effefc1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
13 changes: 12 additions & 1 deletion ingest/api/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
27 changes: 5 additions & 22 deletions ingest/api/ingest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from typing import Union

import isodate
import grpc
import json

Expand All @@ -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__)


Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions ingest/api/utilities.py
Original file line number Diff line number Diff line change
@@ -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

1 comment on commit effefc1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API Unit Test Coverage Report
FileStmtsMissCoverMissing
\_\_init\_\_.py00100% 
datastore_pb2.py614821%34–81
datastore_pb2_grpc.py542750%15–16, 19, 65–80, 121–123, 128–130, 135–137, 142–144, 148–173, 219, 246, 273, 300
export_metrics.py100100% 
grpc_getter.py201145%15–19, 23–26, 30–32, 36–38
locustfile.py15150%1–31
main.py43784%45, 50, 60, 70–71, 81–82
metadata_endpoints.py653152%45–54, 58, 85, 100–219, 223
response_classes.py50100% 
utilities.py1744674%20, 38, 45, 67–70, 78–89, 94–101, 121, 125, 127, 155, 161, 179, 193–194, 198, 214–218, 222–228, 232–234, 264, 268, 290, 295
custom_geo_json
   edr_feature_collection.py60100% 
formatters
   \_\_init\_\_.py110100% 
   covjson.py59198%91
   geojson.py21290%27, 52
openapi
   custom_dimension_examples.py40100% 
   edr_query_parameter_descriptions.py110100% 
   openapi_examples.py130100% 
routers
   \_\_init\_\_.py00100% 
   edr.py101496%348–349, 438–439
   feature.py471960%99–132, 148–153, 159–181
TOTAL72021171% 

API Unit Test Coverage Summary

Tests Skipped Failures Errors Time
30 0 💤 0 ❌ 0 🔥 1.994s ⏱️

Please sign in to comment.