|
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 unix_ms_to_utc_iso(unix_ms): |
| 36 | + if unix_ms is None: |
| 37 | + return unix_ms |
| 38 | + unix_ms_to_iso = unix_ms |
| 39 | + if isinstance(unix_ms_to_iso, decimal.Decimal): |
| 40 | + unix_ms_to_iso = float(unix_ms_to_iso) |
| 41 | + iso = datetime.fromtimestamp( |
| 42 | + unix_ms_to_iso / 1000.0, tz=timezone.utc |
| 43 | + ).isoformat(timespec='milliseconds').replace('+00:00', 'Z') |
| 44 | + return iso |
| 45 | + |
| 46 | + |
| 47 | +def add_utc_metadata(metadata): |
| 48 | + """Add ISO-8601 UTC timestamp fields to metadata dict |
| 49 | +
|
| 50 | + This function takes a metadata dict and adds start_iso and end_iso fields |
| 51 | + based on existing start and end epoch timestamps |
| 52 | + iso precision is set to milliseconds |
| 53 | + Can be expanded to add any api-level metadata |
| 54 | + """ |
| 55 | + if not metadata: |
| 56 | + return metadata |
| 57 | + |
| 58 | + start_iso = unix_ms_to_utc_iso(metadata['start']) |
| 59 | + end_iso = unix_ms_to_utc_iso(metadata['end']) |
| 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,10 @@ 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 | + r['metadata'] = add_utc_metadata(r['metadata']) |
| 397 | + |
353 | 398 | response = { |
354 | 399 | 'records': results, |
355 | 400 | 'next': _get_next_url(flask.request, results), |
@@ -476,6 +521,7 @@ def file_get_metadata(file_id): |
476 | 521 | id: DatalakeAPIError |
477 | 522 | ''' |
478 | 523 | f = _get_file(file_id) |
| 524 | + f.metadata = add_utc_metadata(f.metadata) |
479 | 525 | return Response(json.dumps(f.metadata), content_type='application/json') |
480 | 526 |
|
481 | 527 |
|
@@ -542,6 +588,7 @@ def latest_get(what, where): |
542 | 588 | params = _validate_latest_params(params) |
543 | 589 | f = _get_latest(what, where, params.get('lookback', DEFAULT_LOOKBACK_DAYS)) |
544 | 590 | f.update(http_url=_get_canonical_http_url(f)) |
| 591 | + f['metadata'] = add_utc_metadata(f['metadata']) |
545 | 592 | return Response(json.dumps(f), content_type='application/json') |
546 | 593 |
|
547 | 594 |
|
|
0 commit comments