|
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 | + |
| 35 | +def add_utc_metadata(metadata): |
| 36 | + """Add UTC timestamp fields to metadata |
| 37 | +
|
| 38 | + This function takes a metadata dict and adds start_iso and end_iso fields |
| 39 | + containing ISO-formatted UTC timestamps corresponding to the millisecond |
| 40 | + timestamps in the start and end fields. |
| 41 | + Can be expanded to add any needed metadata to apis |
| 42 | + """ |
| 43 | + if not metadata: |
| 44 | + return metadata |
| 45 | + |
| 46 | + start_iso = metadata['start'] |
| 47 | + end_iso = metadata['end'] |
| 48 | + if start_iso: |
| 49 | + if isinstance(start_iso, decimal.Decimal): |
| 50 | + start_iso = float(start_iso) |
| 51 | + start_iso = datetime.fromtimestamp( |
| 52 | + start_iso / 1000.0, tz=timezone.utc |
| 53 | + ).isoformat(timespec='milliseconds').replace('+00:00', 'Z') |
| 54 | + if end_iso: |
| 55 | + if isinstance(end_iso, decimal.Decimal): |
| 56 | + end_iso = float(end_iso) |
| 57 | + end_iso = datetime.fromtimestamp( |
| 58 | + end_iso / 1000.0, tz=timezone.utc |
| 59 | + ).isoformat(timespec='milliseconds').replace('+00:00', 'Z') |
| 60 | + |
| 61 | + metadata['start_iso'] = start_iso |
| 62 | + metadata['end_iso'] = end_iso |
| 63 | + return metadata |
| 64 | + |
| 65 | + |
32 | 66 | def _get_aws_kwargs(): |
33 | 67 | kwargs = dict( |
34 | 68 | region_name=app.config.get('AWS_REGION'), |
@@ -305,6 +339,14 @@ def files_get(): |
305 | 339 | type: string |
306 | 340 | description: 16-byte blake2 hash of the file |
307 | 341 | content |
| 342 | + start_iso: |
| 343 | + type: string |
| 344 | + description: the start time of the file in ISO |
| 345 | + format UTC iso timezone |
| 346 | + end_iso: |
| 347 | + type: string |
| 348 | + description: the end time of the file in ISO |
| 349 | + format UTC iso timezone |
308 | 350 |
|
309 | 351 | next: |
310 | 352 | type: string |
@@ -349,7 +391,11 @@ def files_get(): |
349 | 391 | where=params.get('where'), |
350 | 392 | cursor=params.get('cursor')) |
351 | 393 |
|
352 | | - [r.update(http_url=_get_canonical_http_url(r)) for r in results] |
| 394 | + for r in results: |
| 395 | + r.update(http_url=_get_canonical_http_url(r)) |
| 396 | + # Add UTC timestamps to each record's metadata |
| 397 | + r['metadata'] = add_utc_metadata(r['metadata']) |
| 398 | + |
353 | 399 | response = { |
354 | 400 | 'records': results, |
355 | 401 | 'next': _get_next_url(flask.request, results), |
@@ -476,6 +522,7 @@ def file_get_metadata(file_id): |
476 | 522 | id: DatalakeAPIError |
477 | 523 | ''' |
478 | 524 | f = _get_file(file_id) |
| 525 | + f.metadata = add_utc_metadata(f.metadata) |
479 | 526 | return Response(json.dumps(f.metadata), content_type='application/json') |
480 | 527 |
|
481 | 528 |
|
@@ -542,6 +589,8 @@ def latest_get(what, where): |
542 | 589 | params = _validate_latest_params(params) |
543 | 590 | f = _get_latest(what, where, params.get('lookback', DEFAULT_LOOKBACK_DAYS)) |
544 | 591 | f.update(http_url=_get_canonical_http_url(f)) |
| 592 | + # Add UTC timestamps to metadata |
| 593 | + f['metadata'] = add_utc_metadata(f['metadata']) |
545 | 594 | return Response(json.dumps(f), content_type='application/json') |
546 | 595 |
|
547 | 596 |
|
|
0 commit comments