Skip to content

Commit ae64088

Browse files
committed
Fixing UTC metadata not showing for other endpoints
1 parent 5ce46d2 commit ae64088

2 files changed

Lines changed: 40 additions & 15 deletions

File tree

api/datalake_api/fetcher.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,6 @@ def get_file(self, file_id):
7777
key = self._get_s3_key(file_id)
7878
fd = key['Body']
7979
j = json.loads(key['Metadata']['datalake'])
80-
start_utc = j['start']
81-
end_utc = j['end']
82-
83-
if start_utc:
84-
start_utc = datetime.fromtimestamp(
85-
start_utc / 1000.0, tz=timezone.utc
86-
).isoformat()
87-
if end_utc:
88-
end_utc = datetime.fromtimestamp(
89-
end_utc / 1000.0, tz=timezone.utc
90-
).isoformat()
91-
j['start_UTC'] = start_utc
92-
j['end_UTC'] = end_utc
93-
9480
metadata = Metadata(j)
9581
return ArchiveFile(fd, metadata)
9682

api/datalake_api/v0.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from flask import current_app as app
1919
import os
2020
import simplejson as json
21+
from datetime import datetime, timezone
22+
import decimal
2123
from .querier import ArchiveQuerier, Cursor, InvalidCursor, \
2224
DEFAULT_LOOKBACK_DAYS
2325
from .fetcher import ArchiveFileFetcher
@@ -29,6 +31,36 @@
2931

3032
_archive_querier = None
3133

34+
def add_utc_metadata(metadata):
35+
"""Add UTC timestamp fields to metadata
36+
37+
This function takes a metadata dict and adds start_UTC and end_UTC fields
38+
containing ISO-formatted UTC timestamps corresponding to the millisecond
39+
timestamps in the start and end fields.
40+
Can be expanded to add any needed metadata to apis
41+
"""
42+
if not metadata:
43+
return metadata
44+
45+
start_utc = metadata['start']
46+
end_utc = metadata['end']
47+
if start_utc:
48+
if isinstance(start_utc, decimal.Decimal):
49+
start_utc = float(start_utc)
50+
start_utc = datetime.fromtimestamp(
51+
start_utc / 1000.0, tz=timezone.utc
52+
).isoformat()
53+
if end_utc:
54+
if isinstance(end_utc, decimal.Decimal):
55+
end_utc = float(end_utc)
56+
end_utc = datetime.fromtimestamp(
57+
end_utc / 1000.0, tz=timezone.utc
58+
).isoformat()
59+
60+
metadata['start_UTC'] = start_utc
61+
metadata['end_UTC'] = end_utc
62+
return metadata
63+
3264
def _get_aws_kwargs():
3365
kwargs = dict(
3466
region_name=app.config.get('AWS_REGION'),
@@ -349,7 +381,11 @@ def files_get():
349381
where=params.get('where'),
350382
cursor=params.get('cursor'))
351383

352-
[r.update(http_url=_get_canonical_http_url(r)) for r in results]
384+
for r in results:
385+
r.update(http_url=_get_canonical_http_url(r))
386+
# Add UTC timestamps to each record's metadata
387+
r['metadata'] = add_utc_metadata(r['metadata'])
388+
353389
response = {
354390
'records': results,
355391
'next': _get_next_url(flask.request, results),
@@ -476,6 +512,7 @@ def file_get_metadata(file_id):
476512
id: DatalakeAPIError
477513
'''
478514
f = _get_file(file_id)
515+
f.metadata = add_utc_metadata(f.metadata)
479516
return Response(json.dumps(f.metadata), content_type='application/json')
480517

481518

@@ -542,6 +579,8 @@ def latest_get(what, where):
542579
params = _validate_latest_params(params)
543580
f = _get_latest(what, where, params.get('lookback', DEFAULT_LOOKBACK_DAYS))
544581
f.update(http_url=_get_canonical_http_url(f))
582+
# Add UTC timestamps to metadata
583+
f['metadata'] = add_utc_metadata(f['metadata'])
545584
return Response(json.dumps(f), content_type='application/json')
546585

547586

0 commit comments

Comments
 (0)