Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 42 Use time range in db query #47

Merged
merged 9 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Issue 18 - Remove Flask
- Issue 52 - Remove partial_f from data columns to obscure
### Fixed
- Issue 42 - Change database query to use time range
- Issue 36 - Request mapping template was not transforming request parameters correctly resulting in 500 internal server errors
- Issue 33 - Obscure data sometimes fails when 1 is chosen multiplier in randomization
### Security
5 changes: 0 additions & 5 deletions hydrocron/api/controllers/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ def gettimeseries_get(feature, feature_id, start_time, end_time, output, fields)
:rtype: None
"""

start_time = start_time.replace("T", " ")[0:19]
end_time = end_time.replace("T", " ")[0:19]
start_time = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
end_time = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")

start = time.time()
if feature.lower() == 'reach':
results = hydrocron.data_repository.get_reach_series_by_feature_id(feature_id, start_time, end_time)
Expand Down
15 changes: 9 additions & 6 deletions hydrocron/api/data_access/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

import logging
from datetime import datetime
from typing import Generator

from boto3.resources.base import ServiceResource
Expand All @@ -21,7 +20,7 @@ def __init__(self, dynamo_resource: ServiceResource):
self._dynamo_instance = dynamo_resource
self._logger = logging.getLogger('hydrocron.api.data_access.db.DynamoDataRepository')

def get_reach_series_by_feature_id(self, feature_id: str, start_time: datetime, end_time: datetime) -> Generator: # noqa: E501 # pylint: disable=W0613
def get_reach_series_by_feature_id(self, feature_id: str, start_time: str, end_time: str) -> Generator: # noqa: E501 # pylint: disable=W0613
"""

@param feature_id:
Expand All @@ -34,8 +33,10 @@ def get_reach_series_by_feature_id(self, feature_id: str, start_time: datetime,
hydrocron_table = self._dynamo_instance.Table(table_name)
hydrocron_table.load()

items = hydrocron_table.query(KeyConditionExpression=Key(
constants.SWOT_REACH_PARTITION_KEY).eq(feature_id))
items = hydrocron_table.query(KeyConditionExpression=(
Key(constants.SWOT_REACH_PARTITION_KEY).eq(feature_id) &
Key(constants.SWOT_REACH_SORT_KEY).between(start_time, end_time))
)
return items

def get_node_series_by_feature_id(self, feature_id, start_time, end_time): # noqa: E501 # pylint: disable=W0613
Expand All @@ -51,6 +52,8 @@ def get_node_series_by_feature_id(self, feature_id, start_time, end_time): # no
hydrocron_table = self._dynamo_instance.Table(table_name)
hydrocron_table.load()

items = hydrocron_table.query(KeyConditionExpression=Key(
constants.SWOT_NODE_PARTITION_KEY).eq(feature_id))
items = hydrocron_table.query(KeyConditionExpression=(
Key(constants.SWOT_NODE_PARTITION_KEY).eq(feature_id) &
Key(constants.SWOT_NODE_SORT_KEY).between(start_time, end_time))
)
return items
4 changes: 2 additions & 2 deletions hydrocron/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@

SWOT_REACH_PARTITION_KEY = "reach_id"
SWOT_NODE_PARTITION_KEY = "node_id"
SWOT_REACH_SORT_KEY = ""
SWOT_NODE_SORT_KEY = ""
SWOT_REACH_SORT_KEY = "range_start_time"
SWOT_NODE_SORT_KEY = "range_start_time"

FIELDNAME_REACH_ID = 'reach_id'
FIELDNAME_TIME = 'time'
Expand Down
9 changes: 6 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from hydrocron.utils.constants import TEST_REACH_ID_VALUE
"""
Tests for API queries
"""


def test_timeseries_lambda_handler(hydrocron_api):
Expand All @@ -8,14 +10,15 @@ def test_timeseries_lambda_handler(hydrocron_api):
----------
hydrocron_api: Fixture ensuring the database is configured for the api
"""

import hydrocron.api.controllers.timeseries

event = {
"body": {
"feature": "Reach",
"reach_id": "71224100223",
"start_time": "2022-08-04T00:00:00+00:00",
"end_time": "2022-08-23T00:00:00+00:00",
"start_time": "2023-06-04T00:00:00Z",
"end_time": "2023-06-23T00:00:00Z",
"output": "csv",
"fields": "reach_id,time_str,wse"
}
Expand Down