|
18 | 18 | from flask import current_app as app |
19 | 19 | import os |
20 | 20 | import simplejson as json |
| 21 | +from datetime import datetime, timezone |
| 22 | +import decimal |
21 | 23 | from .querier import ArchiveQuerier, Cursor, InvalidCursor, \ |
22 | 24 | DEFAULT_LOOKBACK_DAYS |
23 | 25 | from .fetcher import ArchiveFileFetcher |
|
29 | 31 |
|
30 | 32 | _archive_querier = None |
31 | 33 |
|
| 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 | + |
32 | 64 | def _get_aws_kwargs(): |
33 | 65 | kwargs = dict( |
34 | 66 | region_name=app.config.get('AWS_REGION'), |
@@ -349,7 +381,11 @@ def files_get(): |
349 | 381 | where=params.get('where'), |
350 | 382 | cursor=params.get('cursor')) |
351 | 383 |
|
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 | + |
353 | 389 | response = { |
354 | 390 | 'records': results, |
355 | 391 | 'next': _get_next_url(flask.request, results), |
@@ -476,6 +512,7 @@ def file_get_metadata(file_id): |
476 | 512 | id: DatalakeAPIError |
477 | 513 | ''' |
478 | 514 | f = _get_file(file_id) |
| 515 | + f.metadata = add_utc_metadata(f.metadata) |
479 | 516 | return Response(json.dumps(f.metadata), content_type='application/json') |
480 | 517 |
|
481 | 518 |
|
@@ -542,6 +579,8 @@ def latest_get(what, where): |
542 | 579 | params = _validate_latest_params(params) |
543 | 580 | f = _get_latest(what, where, params.get('lookback', DEFAULT_LOOKBACK_DAYS)) |
544 | 581 | f.update(http_url=_get_canonical_http_url(f)) |
| 582 | + # Add UTC timestamps to metadata |
| 583 | + f['metadata'] = add_utc_metadata(f['metadata']) |
545 | 584 | return Response(json.dumps(f), content_type='application/json') |
546 | 585 |
|
547 | 586 |
|
|
0 commit comments