Skip to content

Commit 58ee3d7

Browse files
committed
-Change naming from UTC to iso
-Make precision to ms and add Z -Ading pytest -n auto plugin -Better devshell name -Fix Tests to run on new python.
1 parent f61854d commit 58ee3d7

File tree

6 files changed

+84
-20
lines changed

6 files changed

+84
-20
lines changed

Dockerfile

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,33 @@ ENV LC_ALL C.UTF-8
77

88
# TODO: keep requirements in one place
99
RUN pip install \
10+
'boto==2.49.0' \
11+
'boto3==1.35.41' \
12+
'botocore==1.35.64' \
13+
'datalake<2' \
14+
'flake8>=2.5.0,<4.1' \
15+
'freezegun<1' \
16+
'moto<3' \
17+
'pytest<8' \
18+
'pytest-xdist>=3.0.0' \
19+
'responses<0.22.0' \
20+
'tox>4,<5' \
21+
# test requirements
1022
blinker>=1.4 \
11-
boto3>=1.1.3 \
1223
click>=5.1 \
13-
Flask>=0.10.1 \
1424
flask-swagger>=0.2.14 \
25+
Flask>=0.10.1 \
1526
memoized_property>=1.0.1 \
27+
pyinotify>=0.9.4, \
1628
python-dateutil>=2.4.2 \
1729
python-dotenv>=0.1.3 \
1830
pytz>=2015.4 \
19-
sentry-sdk[flask]>=0.19.5 \
31+
raven>=5.0.0 \
2032
requests>=2.5 \
33+
sentry-sdk[flask]>=0.19.5 \
2134
simplejson>=3.3.1 \
22-
six>=1.10.0 \
23-
# test requirements
24-
'flake8>=2.5.0,<4.1' \
25-
'freezegun<1' \
26-
'moto<3' \
27-
'pytest<8' \
28-
'responses<0.22.0' \
29-
pyinotify>=0.9.4, \
30-
raven>=5.0.0 \
31-
'tox>4,<5' \
32-
'datalake<2'
35+
six>=1.10.0
36+
3337

3438
RUN mkdir -p /opt/
3539
COPY . /opt/

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ IMAGE="$(REPO)/$(REPO_PATH):$(VERSION)"
77
docker: version
88
docker build --build-arg VERSION=$(VERSION) -t $(IMAGE) .
99

10-
.PHONY: devshell # Open a developer shell in the docker env
11-
devshell: docker
10+
.PHONY: dev # Open a developer shell in the docker env
11+
dev: docker
1212
docker run --rm -it -v $$PWD:/opt --entrypoint /bin/bash $(IMAGE)
1313

1414
test-client: docker

api/datalake_api/v0.py

Lines changed: 48 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,38 @@
2931

3032
_archive_querier = None
3133

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+
3266
def _get_aws_kwargs():
3367
kwargs = dict(
3468
region_name=app.config.get('AWS_REGION'),
@@ -305,6 +339,14 @@ def files_get():
305339
type: string
306340
description: 16-byte blake2 hash of the file
307341
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
308350
309351
next:
310352
type: string
@@ -349,7 +391,10 @@ def files_get():
349391
where=params.get('where'),
350392
cursor=params.get('cursor'))
351393

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+
353398
response = {
354399
'records': results,
355400
'next': _get_next_url(flask.request, results),
@@ -476,6 +521,7 @@ def file_get_metadata(file_id):
476521
id: DatalakeAPIError
477522
'''
478523
f = _get_file(file_id)
524+
f.metadata = add_utc_metadata(f.metadata)
479525
return Response(json.dumps(f.metadata), content_type='application/json')
480526

481527

@@ -542,6 +588,7 @@ def latest_get(what, where):
542588
params = _validate_latest_params(params)
543589
f = _get_latest(what, where, params.get('lookback', DEFAULT_LOOKBACK_DAYS))
544590
f.update(http_url=_get_canonical_http_url(f))
591+
f['metadata'] = add_utc_metadata(f['metadata'])
545592
return Response(json.dumps(f), content_type='application/json')
546593

547594

api/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def get_version_from_pyver():
3030
if 'sdist' in sys.argv or 'bdist_wheel' in sys.argv:
3131
raise ImportError('You must install pyver to create a package')
3232
else:
33-
return 'noversion'
33+
return '0.0.0'
3434
version, version_info = pyver.get_version(pkg="datalake_api",
3535
public=True)
3636
return version

api/tests/test_metadata.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1212
# License for the specific language governing permissions and limitations under
1313
# the License.
14+
from datetime import datetime, timezone
15+
from decimal import Decimal
1416
import pytest
1517
import simplejson as json
1618

@@ -32,7 +34,18 @@ def test_get_metadata(metadata_getter, s3_file_maker, random_metadata):
3234
res = metadata_getter('12345')
3335
assert res.status_code == 200
3436
assert res.content_type == 'application/json'
35-
assert json.loads(res.data) == random_metadata
37+
res_data = json.loads(res.data)
38+
for k, v in res_data.items():
39+
if k == 'start_iso' or k == 'end_iso':
40+
if v: # Null case of end time
41+
k_epoch = k.replace('_iso','')
42+
v_epoch = res_data[k_epoch]
43+
expected_v_iso = datetime.fromtimestamp(
44+
v_epoch / 1000.0, tz=timezone.utc
45+
).isoformat(timespec='milliseconds').replace('+00:00', 'Z')
46+
assert expected_v_iso == res_data[k]
47+
else:
48+
assert v == res_data[k]
3649

3750

3851
def test_no_such_metadata(s3_bucket_maker, metadata_getter):

ingester/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def get_version_from_pyver():
3030
if 'sdist' in sys.argv or 'bdist_wheel' in sys.argv:
3131
raise ImportError('You must install pyver to create a package')
3232
else:
33-
return 'noversion'
33+
return '0.0.0'
3434
version, version_info = pyver.get_version(pkg="datalake_ingester",
3535
public=True)
3636
return version

0 commit comments

Comments
 (0)