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 54 #55

Merged
merged 10 commits into from
Dec 6, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Issue 33 - Obscure data sometimes fails when 1 is chosen multiplier in randomization
- Issue 45 - Fixed columns filtering bug
- Issue 38 - Fix bug: specification does not match actual returned content (csv)
- Issue 45 - Fixed columns filtering bug
- Issue 54 - Fixed output format
### Security
44 changes: 18 additions & 26 deletions hydrocron/api/controllers/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,39 @@ def gettimeseries_get(feature, feature_id, start_time, end_time, output, fields)
:type start_time: str
:param end_time: End time of the timeseries
:type end_time: str
:param cycleavg: Perform cycle average on the time series
:type cycleavg: bool
:param output: Format of the data returned
:type output: str
:param fields: List of requested columns
:type output: str

:rtype: None
"""

start = time.time()
if feature.lower() == 'reach':
results = hydrocron.data_repository.get_reach_series_by_feature_id(feature_id, start_time, end_time)
elif feature.lower() == 'node':
results = hydrocron.data_repository.get_node_series_by_feature_id(feature_id, start_time, end_time)
else:
return {}
end = time.time()

data = ""
if output == 'geojson':
data = format_json(feature.lower(), results, feature_id, round((end - start) * 1000, 3), fields)
data, hits = format_json(feature.lower(), results, feature_id, fields)
if output == 'csv':
data = format_csv(feature.lower(), results, feature_id, round((end - start) * 1000, 3), fields)
data, hits = format_csv(feature.lower(), results, feature_id, fields)

return data
return data, hits


def format_json(feature_lower, results: Generator, feature_id, dataTime, fields): # noqa: E501 # pylint: disable=W0613
def format_json(feature_lower, results: Generator, feature_id, fields): # noqa: E501 # pylint: disable=W0613
"""

Parameters
----------
feature_lower
results
swot_id
exact
time
fields

Returns
-------
Expand All @@ -78,12 +75,11 @@ def format_json(feature_lower, results: Generator, feature_id, dataTime, fields)
data['error'] = f'413: Query exceeds 6MB with {len(results)} hits.'

else:
data['status'] = "200 OK"
data['time'] = str(dataTime) + " ms."
data['type'] = "FeatureCollection"
data['features'] = []
i = 0
fields_set = fields.split(",")

# st = float(time.mktime(start_time.timetuple()) - 946710000)
# et = float(time.mktime(end_time.timetuple()) - 946710000)
# TODO: process type of feature_id (i.e. reach_id or node_id)
Expand Down Expand Up @@ -127,21 +123,18 @@ def format_json(feature_lower, results: Generator, feature_id, dataTime, fields)
data['features'].append(feature)
i += 1

data['hits'] = i

return data
return data, i


def format_csv(feature_lower, results: Generator, feature_id, dataTime, fields): # noqa: E501 # pylint: disable=W0613
def format_csv(feature_lower, results: Generator, feature_id, fields): # noqa: E501 # pylint: disable=W0613
"""

Parameters
----------
feature_lower
results
feature_id
exact
time
fields

Returns
-------
Expand All @@ -158,9 +151,6 @@ def format_csv(feature_lower, results: Generator, feature_id, dataTime, fields):
data['error'] = f'413: Query exceeds 6MB with {len(results)} hits.'

else:
data['status'] = "200 OK"
data['time'] = str(dataTime) + " ms."
# data['search on'] = {"feature_id": feature_id}
data['type'] = "FeatureCollection"
data['features'] = []
i = 0
Expand All @@ -185,8 +175,7 @@ def format_csv(feature_lower, results: Generator, feature_id, dataTime, fields):
csv += t[j]
csv += ','
csv += '\n'
data['hits'] = i
return csv
return csv, i


def lambda_handler(event, context): # noqa: E501 # pylint: disable=W0613
Expand All @@ -201,8 +190,11 @@ def lambda_handler(event, context): # noqa: E501 # pylint: disable=W0613
output = event['body']['output']
fields = event['body']['fields']

results = gettimeseries_get(feature, feature_id, start_time, end_time, output, fields)

data = {'status': "200 OK", 'results': results}
start = time.time()
results, hits = gettimeseries_get(feature, feature_id, start_time, end_time, output, fields)
end = time.time()
elapsed_time = round((end - start) * 1000, 3)
data = {'status': "200 OK", 'time': elapsed_time, 'hits': hits, 'results': {'csv': "", 'geojson': {}}}
data['results'][event['body']['output']] = results

return data
Loading