-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Data versioning support #99
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f8b88d1
Data versioning
dogversioning 140ef4d
transactions/study updates, PR cleanup
dogversioning a92c402
line length
dogversioning 476aed0
upload error handling
dogversioning 675051d
bugfix bonanza
dogversioning 3c332ce
lint
dogversioning 126b9b4
script now executable
dogversioning 2e259f9
touched site upload URL
dogversioning 2caa445
updated metadata version mocks
dogversioning File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
import sys | ||
|
||
import boto3 | ||
|
||
from requests.auth import _basic_auth_str | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
import argparse | ||
import os | ||
import sys | ||
|
||
from pathlib import Path | ||
|
||
import boto3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#!/usr/bin/env python3 | ||
""" Utility for adding versioning to an existing aggregator data store | ||
|
||
This is a one time thing for us, so the CLI/Boto creds are not robust. | ||
""" | ||
import argparse | ||
import io | ||
import json | ||
|
||
import boto3 | ||
|
||
UPLOAD_ROOT_BUCKETS = [ | ||
"archive", | ||
"error", | ||
"last_valid", | ||
"latest", | ||
"site_upload", | ||
"study_metadata", | ||
] | ||
|
||
|
||
def _get_s3_data(key: str, bucket_name: str, client) -> dict: | ||
"""Convenience class for retrieving a dict from S3""" | ||
try: | ||
bytes_buffer = io.BytesIO() | ||
client.download_fileobj(Bucket=bucket_name, Key=key, Fileobj=bytes_buffer) | ||
return json.loads(bytes_buffer.getvalue().decode()) | ||
except Exception: # pylint: disable=broad-except | ||
return {} | ||
|
||
|
||
def _put_s3_data(key: str, bucket_name: str, client, data: dict) -> None: | ||
"""Convenience class for writing a dict to S3""" | ||
b_data = io.BytesIO(json.dumps(data).encode()) | ||
client.upload_fileobj(Bucket=bucket_name, Key=key, Fileobj=b_data) | ||
|
||
|
||
def _get_depth(d): | ||
if isinstance(d, dict): | ||
return 1 + (max(map(_get_depth, d.values())) if d else 0) | ||
return 0 | ||
|
||
|
||
def migrate_bucket_versioning(bucket: str): | ||
client = boto3.client("s3") | ||
res = client.list_objects_v2(Bucket=bucket) | ||
contents = res["Contents"] | ||
moved_files = 0 | ||
for s3_file in contents: | ||
if s3_file["Key"].split("/")[0] in UPLOAD_ROOT_BUCKETS: | ||
key = s3_file["Key"] | ||
key_array = key.split("/") | ||
if len(key_array) == 5: | ||
key_array.insert(4, "000") | ||
new_key = "/".join(key_array) | ||
client.copy({"Bucket": bucket, "Key": key}, bucket, new_key) | ||
client.delete_object(Bucket=bucket, Key=key) | ||
moved_files += 1 | ||
print(f"Moved {moved_files} uploads") | ||
study_periods = _get_s3_data("metadata/study_periods.json", bucket, client) | ||
|
||
if _get_depth(study_periods) == 3: | ||
new_sp = {} | ||
for site in study_periods: | ||
new_sp[site] = {} | ||
for study in study_periods[site]: | ||
new_sp[site][study] = {} | ||
new_sp[site][study]["000"] = study_periods[site][study] | ||
new_sp[site][study]["000"].pop("version") | ||
new_sp[site][study]["000"]["study_period_format_version"] = 2 | ||
# print(json.dumps(new_sp, indent=2)) | ||
_put_s3_data("metadata/study_periods.json", bucket, client, new_sp) | ||
print("study_periods.json updated") | ||
else: | ||
print("study_periods.json does not need update") | ||
|
||
transactions = _get_s3_data("metadata/transactions.json", bucket, client) | ||
if _get_depth(transactions) == 4: | ||
new_t = {} | ||
for site in transactions: | ||
new_t[site] = {} | ||
for study in transactions[site]: | ||
new_t[site][study] = {} | ||
for dp in transactions[site][study]: | ||
new_t[site][study][dp] = {} | ||
new_t[site][study][dp]["000"] = transactions[site][study][dp] | ||
new_t[site][study][dp]["000"].pop("version") | ||
new_t[site][study][dp]["000"]["transacton_format_version"] = 2 | ||
# print(json.dumps(new_t, indent=2)) | ||
_put_s3_data("metadata/transactions.json", bucket, client, new_t) | ||
print("transactions.json updated") | ||
else: | ||
print("transactions.json does not need update") | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description="""Util for migrating aggregator data""" | ||
) | ||
parser.add_argument("-b", "--bucket", help="bucket name") | ||
args = parser.parse_args() | ||
migrate_bucket_versioning(args.bucket) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a little unrelated, but since isort pulled up a couple instances of typing reorgs, i pinned this version and removed all Dict/List typing in favor of 3.9+ dict/list.