-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c163f7
commit 542c6fd
Showing
8 changed files
with
180 additions
and
96 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import activity.processor | ||
|
||
from utils.utils import ParameterStoreAdapter, get_current_timestamp | ||
|
||
|
||
def update_activity() -> None: | ||
parameter_store_adapter = ParameterStoreAdapter() | ||
last_updated_timestamp = parameter_store_adapter.get_last_updated_timestamp() | ||
current_timestamp = get_current_timestamp() | ||
activity.processor.update_install_activity( | ||
last_updated_timestamp, current_timestamp | ||
) | ||
parameter_store_adapter.set_last_updated_timestamp(current_timestamp) |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
snowflake-connector-python==3.0.0 | ||
boto3==1.26.77 | ||
pynamodb==5.4.1 | ||
python-slugify==8.0.1 |
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 |
---|---|---|
@@ -1,39 +1,59 @@ | ||
""" | ||
Module containing functionality for running workflows. This can be run either as | ||
a standalone CLI script or by importing the function `run_workflow()` and | ||
passing the correct event payload information to the function. This allows us to | ||
be able to run the workflow in a consistently in different environments like | ||
local CLI, GitHub actions, or in an AWS lambda. | ||
""" | ||
|
||
import argparse | ||
import os | ||
|
||
from activity.update_activity import update_activity | ||
from categories import run_seed_s3_categories_workflow | ||
from typing import Dict | ||
|
||
|
||
def run_workflow(event: Dict): | ||
type = event['type'] | ||
""" | ||
Function for running a particular data workflow based on the event type. The | ||
event dictionary should contain all of the necessary data to run the | ||
specified data workflow. | ||
""" | ||
|
||
type = event["type"] | ||
|
||
if type == 'seed-s3-categories': | ||
if type == "activity": | ||
update_activity() | ||
|
||
if type == "seed-s3-categories": | ||
run_seed_s3_categories_workflow( | ||
event['table'], | ||
event['bucket'], | ||
event['edam_version'], | ||
event["edam_version"], | ||
event["s3_path"], | ||
) | ||
|
||
|
||
def get_arg_parser(): | ||
def _get_arg_parser(): | ||
parser = argparse.ArgumentParser( | ||
prog='run-workflow', | ||
description='CLI for running hub data workflows', | ||
prog="run-workflow", | ||
description="CLI for running hub data workflows", | ||
) | ||
subparsers = parser.add_subparsers(required=True, dest='type') | ||
subparsers = parser.add_subparsers(required=True, dest="type") | ||
|
||
seed_s3_categories_parser = subparsers.add_parser('seed-s3-categories', help='categories help') | ||
seed_s3_categories_parser.add_argument('--table', required=True) | ||
seed_s3_categories_parser.add_argument('--bucket', required=True) | ||
seed_s3_categories_parser.add_argument('--edam-version', required=True) | ||
seed_s3_categories_parser = subparsers.add_parser( | ||
"seed-s3-categories", help="categories help" | ||
) | ||
seed_s3_categories_parser.add_argument("--edam-version", required=True) | ||
seed_s3_categories_parser.add_argument("--s3-path", required=True) | ||
|
||
subparsers.add_parser("activity", help="activity help") | ||
|
||
return parser | ||
|
||
def main(): | ||
parser = get_arg_parser() | ||
|
||
def _main(): | ||
parser = _get_arg_parser() | ||
run_workflow(vars(parser.parse_args())) | ||
run_workflow | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|
||
if __name__ == "__main__": | ||
_main() |
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,16 @@ | ||
import os | ||
|
||
|
||
def get_required_env(key: str) -> str: | ||
""" | ||
Utility for getting the value of an environment variable with the additional | ||
constraint that it must be defined, otherwise the application will be unable | ||
to run without it. | ||
""" | ||
|
||
value = os.environ.get(key) | ||
|
||
if not value: | ||
raise ValueError(f"Required environment variable '{key}' is not defined") | ||
|
||
return value |
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,42 @@ | ||
import boto3 | ||
import json | ||
import logging | ||
|
||
from os import path | ||
from typing import Any, Dict | ||
|
||
|
||
class S3Client: | ||
""" | ||
Client for accessing S3 resources. | ||
""" | ||
|
||
_bucket: str | ||
_prefix: str | ||
_client: Any | ||
|
||
def __init__(self, bucket: str, prefix=""): | ||
self._bucket = bucket | ||
self._prefix = prefix | ||
self._client = boto3.client("s3") | ||
|
||
def _get_complete_path(self, s3_path: str): | ||
return path.join(self._prefix, s3_path) | ||
|
||
def _get_from_s3(self, s3_path): | ||
obj = self._client.get_object( | ||
Bucket=self._bucket, Key=self._get_complete_path(s3_path) | ||
) | ||
|
||
return obj["Body"].read().decode("utf-8") | ||
|
||
def load_json_from_s3(self, s3_path: str) -> Dict: | ||
""" | ||
Load JSON file from S3 path and convert to a Python dictionary. | ||
""" | ||
|
||
try: | ||
return json.loads(self._get_from_s3(s3_path)) | ||
except Exception as e: | ||
logging.error(e) | ||
return {} |
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