Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
vggonzal authored and vggonzal committed Aug 17, 2023
1 parent 198b238 commit 7e926fb
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 564 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ jobs:
path: dist/*.zip

- name: Deploy Terraform
if: |
github.ref == 'refs/heads/develop' ||
github.ref == 'refs/heads/main' ||
startsWith(github.ref, 'refs/heads/release') ||
github.event.head_commit.message == '/deploy sit' ||
github.event.head_commit.message == '/deploy uat'
#if: |
# github.ref == 'refs/heads/develop' ||
# github.ref == 'refs/heads/main' ||
# startsWith(github.ref, 'refs/heads/release') ||
# github.event.head_commit.message == '/deploy sit' ||
# github.event.head_commit.message == '/deploy uat'
working-directory: terraform/
env:
AWS_ACCESS_KEY_ID: ${{ secrets[format('AWS_ACCESS_KEY_ID_SERVICES_{0}', env.TARGET_ENV_UPPERCASE)] }}
Expand Down
68 changes: 50 additions & 18 deletions hydrocronapi/controllers/db/db.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,97 @@
# pylint: disable=W0613
"""Module to interface with DynamoDB database."""


import logging
from datetime import datetime
from typing import Generator

import boto3
from .hydrocron_database import Hydrocron_DB
from boto3.dynamodb.conditions import Key

dynamodb = boto3.client('dynamodb')

logger = logging.getLogger()


def get_reach_series(start_time: datetime, end_time: datetime) -> Generator:
table_name = 'hydrocron_swot_reaches_test'
"""Get Timeseries for a particular Reach filtering by time range
:param start_time: Start time of the timeseries
:type start_time: str
:param end_time: End time of the timeseries
:type end_time: str
:rtype: None
"""

response = dynamodb.get_item(
TableName=table_name,
TableName='hydrocron_swot_reaches_test',
Key={
'reach_id': {'S': '71224100223'}
}
)
print("get_item")
print(response)
return response


def get_node_series(start_time: datetime, end_time: datetime) -> Generator:
table_name = 'hydrocron_swot_reaches_test'
"""Get Timeseries for a particular Node filtering by time range
:param start_time: Start time of the timeseries
:type start_time: str
:param end_time: End time of the timeseries
:type end_time: str
:rtype: None
"""

response = dynamodb.get_item(
TableName=table_name,
TableName='hydrocron_swot_reaches_test',
Key={
'node_id': {'S': '71224100223'}
}
)
print("get_item")
print(response)
return response


def get_reach_series_by_feature_id(feature_id: str, start_time: datetime, end_time: datetime) -> Generator:
"""Get Timeseries for a particular Reach filtering by ID and time range
#st = float(time.mktime(start_time.timetuple()) - 946710000)
#et = float(time.mktime(end_time.timetuple()) - 946710000)
:param feature_id: Identifier of the feature
:type feature_id: str
:param start_time: Start time of the timeseries
:type start_time: str
:param end_time: End time of the timeseries
:type end_time: str
:rtype: None
"""

#select * from reach where reach_id like %(feature_id)s and cast(time as float) >= %(start_time)s and cast(time as float) <= %(end_time)s""",
table_name = 'hydrocron_swot_reaches_test'
response = dynamodb.get_item(
TableName=table_name,
TableName='hydrocron_swot_reaches_test',
Key={
'reach_id': {'S': feature_id}
}
)
print("get_item")
print(response)
return response


def get_node_series_by_feature_id(feature_id, start_time, end_time):
"""Get Timeseries for a particular Node filtering by ID and time range
:param feature_id: Identifier of the feature
:type feature_id: str
:param start_time: Start time of the timeseries
:type start_time: str
:param end_time: End time of the timeseries
:type end_time: str
:rtype: None
"""
table_name = 'hydrocron_swot_reaches_test'
response = dynamodb.get_item(
TableName=table_name,
Key={
'node_id': {'S': feature_id}
}
)
print(response)
return response
8 changes: 8 additions & 0 deletions tests/db.py → hydrocronapi/controllers/db/db_local.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# pylint: disable=W0613
# pylint: disable=C0116
# pylint: disable=C0411
# pylint: disable=W0511
"""Module to interface with DynamoDB database."""


import logging
from datetime import datetime
from typing import Generator
Expand Down Expand Up @@ -26,6 +33,7 @@

dynamo_instance = Hydrocron_DB(dyn_resource=dyndb_resource)


def get_reach_series(start_time: datetime, end_time: datetime) -> Generator:
hydrocron_table = dynamo_instance.load_table('hydrocron_swot_reaches_test')
items = hydrocron_table.query(KeyConditionExpression=Key('reach_id').eq('71224100223'))
Expand Down
9 changes: 7 additions & 2 deletions hydrocronapi/controllers/db/hydrocron_database.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# pylint: disable=R1720
# pylint: disable=C0121
# pylint: disable=W0612
# pylint: disable=C0103
# pylint: disable=C0115
# pylint: disable=C0114
import logging
from botocore.exceptions import ClientError
from boto3.dynamodb.conditions import Key
Expand Down Expand Up @@ -220,7 +226,6 @@ def add_data(self, **kwargs):
for key, value in kwargs.items():
item_dict[key] = value

item_id = item_dict[self.partition_key_name]
try:
self.table.put_item(
Item=item_dict
Expand Down Expand Up @@ -249,7 +254,7 @@ def run_query(self, partition_key, sort_key=None):
The item.
"""
if sort_key == None:
if sort_key is None:

try:
response = self.table.query(
Expand Down
30 changes: 10 additions & 20 deletions hydrocronapi/controllers/subset.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# pylint: disable=duplicate-code
# pylint: disable=R1702
# pylint: disable=C0114
# pylint: disable=E0401
# pylint: disable=W0613
"""Module defining Lambda workflow for subset endpoint."""

import json
import logging
Expand All @@ -12,7 +10,7 @@

from shapely import Polygon, Point

import hydrocronapi.controllers.db.db as db
from hydrocronapi.controllers.db import db

logger = logging.getLogger()

Expand Down Expand Up @@ -152,7 +150,7 @@ def format_subset_csv(results: Generator, polygon, fields):
"""
# Fetch all results from query
results = results['Items']
results = results['Item']

data = {}

Expand Down Expand Up @@ -185,16 +183,10 @@ def format_subset_csv(results: Generator, polygon, fields):
return csv



def lambda_handler(event, context):
def lambda_handler(event):
"""
This function queries the database for relevant results
"""
print("test timeseries 3")
print("body")
print(event['body'])
print("feature")
print(event['body']['feature'])

feature = event['body']['feature']
subsetpolygon = event['body']['subsetpolygon']
Expand All @@ -213,15 +205,13 @@ def lambda_handler(event, context):
data['time'] = str(10) + " ms."
data['hits'] = 10

data['search on'] = dict(
parameter="identifier",
exact="exact",
page_number=0,
page_size=20
)
data['search on'] = {
"parameter": "identifier",
"exact": "exact",
"page_number": 0,
"page_size": 20
}

data['results'] = results

return data


36 changes: 10 additions & 26 deletions hydrocronapi/controllers/timeseries.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# pylint: disable=duplicate-code
# pylint: disable=C0114
# pylint: disable=W0613
"""Module defining Lambda workflow for subset endpoint."""

import logging
import time
from datetime import datetime
from typing import Generator

import hydrocronapi.controllers.db.db as db
from hydrocronapi.controllers.db import db

logger = logging.getLogger()

Expand Down Expand Up @@ -39,8 +38,6 @@ def gettimeseries_get(feature, feature_id, start_time, end_time, output, fields)
end_time = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")

start = time.time()
print("before db")
print(feature.lower())
if feature.lower() == 'reach':
results = db.get_reach_series_by_feature_id(feature_id, start_time, end_time)
elif feature.lower() == 'node':
Expand Down Expand Up @@ -72,10 +69,7 @@ def format_json(results: Generator, feature_id, elapsed_time):
"""
# Fetch all results
print("RESULTS")
res = results['Item']
print(results)
print(res)

data = {}

Expand All @@ -93,7 +87,6 @@ def format_json(results: Generator, feature_id, elapsed_time):
i = 0

if res['time'] != '-999999999999': # and (res['width'] != '-999999999999')):
print(feature_id)
feature = {'properties': {}, 'geometry': {}, 'type': "Feature"}
feature['geometry']['coordinates'] = []
feature_type = ''
Expand Down Expand Up @@ -122,7 +115,6 @@ def format_json(results: Generator, feature_id, elapsed_time):
feature['properties']['slope'] = float(res['slope']['S'])
data['features'].append(feature)

print(data)
data['hits'] = i

return data
Expand All @@ -142,7 +134,7 @@ def format_csv(results: Generator, feature_id, fields):
"""
# Fetch all results
results = results['Items']
results = results['Item']

data = {}

Expand All @@ -152,7 +144,6 @@ def format_csv(results: Generator, feature_id, fields):
data['error'] = f'413: Query exceeds 6MB with {len(results)} hits.'

else:
# csv = "feature_id, time_str, wse, geometry\n"
csv = fields + '\n'
fields_set = fields.split(", ")
for res in results:
Expand All @@ -174,15 +165,10 @@ def format_csv(results: Generator, feature_id, fields):
return csv


def lambda_handler(event, context):
def lambda_handler(event):
"""
This function queries the database for relevant results
"""
print("test timeseries 3")
print("body")
print(event['body'])
print("feature")
print(event['body']['feature'])

feature = event['body']['feature']
feature_id = event['body']['reach_id']
Expand All @@ -201,15 +187,13 @@ def lambda_handler(event, context):
data['time'] = str(10) + " ms."
data['hits'] = 10

data['search on'] = dict(
parameter="identifier",
exact="exact",
page_number=0,
page_size=20
)
data['search on'] = {
"parameter": "identifier",
"exact": "exact",
"page_number": 0,
"page_size": 20
}

data['results'] = results

return data


Loading

0 comments on commit 7e926fb

Please sign in to comment.