Skip to content
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

Tech Debt: Use modules, safety enums, docs updates #105

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added __init__.py
Empty file.
8 changes: 4 additions & 4 deletions src/handlers/dashboard/get_chart_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import boto3
import pandas

from src.handlers.dashboard.filter_config import get_filter_string
from src.handlers.shared.decorators import generic_error_handler
from src.handlers.shared.enums import BucketPath
from src.handlers.shared.functions import get_latest_data_package_version, http_response
from ..dashboard.filter_config import get_filter_string
from ..shared.decorators import generic_error_handler
from ..shared.enums import BucketPath
from ..shared.functions import get_latest_data_package_version, http_response


def _get_table_cols(table_name: str, version: str = None) -> list:
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/dashboard/get_data_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import os

from src.handlers.shared.decorators import generic_error_handler
from src.handlers.shared.enums import BucketPath, JsonFilename
from src.handlers.shared.functions import get_s3_json_as_dict, http_response
from ..shared.decorators import generic_error_handler
from ..shared.enums import BucketPath, JsonFilename
from ..shared.functions import get_s3_json_as_dict, http_response


@generic_error_handler(msg="Error retrieving data packages")
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/dashboard/get_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import boto3

from src.handlers.shared.decorators import generic_error_handler
from src.handlers.shared.functions import http_response, read_metadata
from ..shared.decorators import generic_error_handler
from ..shared.functions import http_response, read_metadata


@generic_error_handler(msg="Error retrieving metadata")
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/dashboard/get_study_periods.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import boto3

from src.handlers.shared.decorators import generic_error_handler
from src.handlers.shared.enums import JsonFilename
from src.handlers.shared.functions import http_response, read_metadata
from ..shared.decorators import generic_error_handler
from ..shared.enums import JsonFilename
from ..shared.functions import http_response, read_metadata


@generic_error_handler(msg="Error retrieving study period")
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/shared/awswrangler_functions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" functions specifically requiring AWSWranger, which requires a lambda layer"""
import awswrangler

from src.handlers.shared.enums import BucketPath
from .enums import BucketPath


def get_s3_data_package_list(
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/shared/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import functools
import logging

from src.handlers.shared.functions import http_response
from .functions import http_response


def generic_error_handler(msg="Internal server error"):
Expand Down
20 changes: 20 additions & 0 deletions src/handlers/shared/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,23 @@ class JsonFilename(Enum):
TRANSACTIONS = "transactions"
DATA_PACKAGES = "data_packages"
STUDY_PERIODS = "study_periods"


class TransactionKeys(Enum):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we target py3.11, we can just use StrEnum and enum.auto() values to get the lowercase version of the key. And I believe to use the enum directly in place of strings (i.e. no .value needed)

:wistful stare into the distance:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok that is supported and i've already got a ticket for it (#104), and that enough is a motivator to do that. Seperate PR, though

"""stores names of expected keys in the transaction dictionary"""

TRANSACTION_FORMAT_VERSION = "transaction_format_version"
LAST_UPLOAD = "last_upload"
LAST_DATA_UPDATE = "last_data_update"
LAST_AGGREGATION = "last_aggregation"
LAST_ERROR = "last_error"
DELETED = "deleted"


class StudyPeriodMetadataKeys(Enum):
"""stores names of expected keys in the study period metadata dictionary"""

STUDY_PERIOD_FORMAT_VERSION = "study_period_format_version"
EARLIEST_DATE = "earliest_date"
LATEST_DATE = "latest_date"
LAST_DATA_UPDATE = "last_data_update"
23 changes: 12 additions & 11 deletions src/handlers/shared/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@

import boto3

from src.handlers.shared.enums import BucketPath, JsonFilename
from .enums import BucketPath, JsonFilename, StudyPeriodMetadataKeys, TransactionKeys

TRANSACTION_METADATA_TEMPLATE = {
"transaction_format_version": "2",
"last_upload": None,
"last_data_update": None,
"last_aggregation": None,
"last_error": None,
"deleted": None,
TransactionKeys.TRANSACTION_FORMAT_VERSION.value: "2",
TransactionKeys.LAST_UPLOAD.value: None,
TransactionKeys.LAST_DATA_UPDATE.value: None,
TransactionKeys.LAST_AGGREGATION.value: None,
TransactionKeys.LAST_ERROR.value: None,
TransactionKeys.DELETED.value: None,
}

STUDY_PERIOD_METADATA_TEMPLATE = {
"study_period_format_version": "2",
"earliest_date": None,
"latest_date": None,
"last_data_update": None,
StudyPeriodMetadataKeys.STUDY_PERIOD_FORMAT_VERSION.value: "2",
StudyPeriodMetadataKeys.EARLIEST_DATE.value: None,
StudyPeriodMetadataKeys.LATEST_DATE.value: None,
StudyPeriodMetadataKeys.LAST_DATA_UPDATE.value: None,
}


Expand Down
4 changes: 2 additions & 2 deletions src/handlers/site_upload/api_gateway_authorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import os
import re

from src.handlers.shared.enums import BucketPath
from src.handlers.shared.functions import get_s3_json_as_dict
from ..shared.enums import BucketPath
from ..shared.functions import get_s3_json_as_dict


class AuthError(Exception):
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/site_upload/cache_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import awswrangler
import boto3

from src.handlers.shared.decorators import generic_error_handler
from src.handlers.shared.enums import BucketPath, JsonFilename
from src.handlers.shared.functions import http_response
from ..shared.decorators import generic_error_handler
from ..shared.enums import BucketPath, JsonFilename
from ..shared.functions import http_response


def cache_api_data(s3_client, s3_bucket_name: str, db: str, target: str) -> None:
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/site_upload/fetch_upload_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import boto3
import botocore.exceptions

from src.handlers.shared.decorators import generic_error_handler
from src.handlers.shared.enums import BucketPath
from src.handlers.shared.functions import get_s3_json_as_dict, http_response
from ..shared.decorators import generic_error_handler
from ..shared.enums import BucketPath
from ..shared.functions import get_s3_json_as_dict, http_response


def create_presigned_post(
Expand Down
24 changes: 15 additions & 9 deletions src/handlers/site_upload/powerset_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
from numpy import nan
from pandas.core.indexes.range import RangeIndex

from src.handlers.shared.awswrangler_functions import get_s3_data_package_list
from src.handlers.shared.decorators import generic_error_handler
from src.handlers.shared.enums import BucketPath
from src.handlers.shared.functions import (
from ..shared.awswrangler_functions import get_s3_data_package_list
from ..shared.decorators import generic_error_handler
from ..shared.enums import BucketPath, TransactionKeys
from ..shared.functions import (
get_s3_site_filename_suffix,
http_response,
move_s3_file,
Expand Down Expand Up @@ -130,7 +130,7 @@ def merge_error_handler(
s3_path.replace(f"s3://{self.s3_bucket_name}/", ""),
f"{BucketPath.ERROR.value}/{subbucket_path}",
)
self.update_local_metadata("last_error")
self.update_local_metadata(TransactionKeys.LAST_ERROR.value)


def get_static_string_series(static_str: str, index: RangeIndex) -> pandas.Series:
Expand Down Expand Up @@ -215,7 +215,9 @@ def merge_powersets(manager: S3Manager) -> None:
try:
if not any(x.endswith(site_specific_name) for x in latest_file_list):
df = expand_and_concat_sets(df, last_valid_path, last_valid_site)
manager.update_local_metadata("last_aggregation", site=last_valid_site)
manager.update_local_metadata(
TransactionKeys.LAST_AGGREGATION.value, site=last_valid_site
)
except MergeError as e:
# This is expected to trigger if there's an issue in expand_and_concat_sets;
# this usually means there's a data problem.
Expand Down Expand Up @@ -257,8 +259,12 @@ def merge_powersets(manager: S3Manager) -> None:
f"{BucketPath.LAST_VALID.value}/{subbucket_path}",
)
latest_site = site_specific_name.split("/", maxsplit=1)[0]
manager.update_local_metadata("last_data_update", site=latest_site)
manager.update_local_metadata("last_aggregation", site=latest_site)
manager.update_local_metadata(
TransactionKeys.LAST_DATA_UPDATE.value, site=latest_site
)
manager.update_local_metadata(
TransactionKeys.LAST_AGGREGATION.value, site=latest_site
)
except Exception as e: # pylint: disable=broad-except
manager.merge_error_handler(
latest_path,
Expand All @@ -274,7 +280,7 @@ def merge_powersets(manager: S3Manager) -> None:
f"/{subbucket_path}",
manager.site,
)
manager.update_local_metadata("last_aggregation")
manager.update_local_metadata(TransactionKeys.LAST_AGGREGATION.value)
manager.write_local_metadata()

# In this section, we are trying to accomplish two things:
Expand Down
12 changes: 6 additions & 6 deletions src/handlers/site_upload/process_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import boto3

from src.handlers.shared.decorators import generic_error_handler
from src.handlers.shared.enums import BucketPath
from src.handlers.shared.functions import (
from ..shared.decorators import generic_error_handler
from ..shared.enums import BucketPath, TransactionKeys
from ..shared.functions import (
http_response,
move_s3_file,
read_metadata,
Expand Down Expand Up @@ -51,7 +51,7 @@ def process_upload(s3_client, sns_client, s3_bucket_name: str, s3_key: str) -> N
study,
data_package,
version,
"last_upload",
TransactionKeys.LAST_UPLOAD.value,
last_uploaded_date,
)
sns_client.publish(TopicArn=topic_sns_arn, Message=new_key, Subject=sns_subject)
Expand All @@ -65,7 +65,7 @@ def process_upload(s3_client, sns_client, s3_bucket_name: str, s3_key: str) -> N
study,
data_package,
version,
"last_upload",
TransactionKeys.LAST_UPLOAD.value,
last_uploaded_date,
)
metadata = update_metadata(
Expand All @@ -74,7 +74,7 @@ def process_upload(s3_client, sns_client, s3_bucket_name: str, s3_key: str) -> N
study,
data_package,
version,
"last_error",
TransactionKeys.LAST_ERROR.value,
last_uploaded_date,
)
write_metadata(s3_client, s3_bucket_name, metadata)
Expand Down
14 changes: 7 additions & 7 deletions src/handlers/site_upload/study_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import awswrangler
import boto3

from src.handlers.shared.awswrangler_functions import get_s3_study_meta_list
from src.handlers.shared.decorators import generic_error_handler
from src.handlers.shared.enums import JsonFilename
from src.handlers.shared.functions import (
from ..shared.awswrangler_functions import get_s3_study_meta_list
from ..shared.decorators import generic_error_handler
from ..shared.enums import JsonFilename, StudyPeriodMetadataKeys
from ..shared.functions import (
http_response,
read_metadata,
update_metadata,
Expand All @@ -33,7 +33,7 @@ def update_study_period(s3_client, s3_bucket, site, study, data_package, version
study,
data_package,
version,
"earliest_date",
StudyPeriodMetadataKeys.EARLIEST_DATE.value,
df["min_date"][0],
meta_type=JsonFilename.STUDY_PERIODS.value,
)
Expand All @@ -43,7 +43,7 @@ def update_study_period(s3_client, s3_bucket, site, study, data_package, version
study,
data_package,
version,
"latest_date",
StudyPeriodMetadataKeys.LATEST_DATE.value,
df["max_date"][0],
meta_type=JsonFilename.STUDY_PERIODS.value,
)
Expand All @@ -53,7 +53,7 @@ def update_study_period(s3_client, s3_bucket, site, study, data_package, version
study,
data_package,
version,
"last_data_update",
StudyPeriodMetadataKeys.LAST_DATA_UPDATE.value,
datetime.now(timezone.utc),
meta_type=JsonFilename.STUDY_PERIODS.value,
)
Expand Down
43 changes: 4 additions & 39 deletions template.hostedzone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,7 @@ Resources:
ValidationMethod: DNS


# Dashboard
AcmDashCertificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Sub "dashboard.${Domain}"
DomainValidationOptions:
- DomainName: !Sub "dashboard.${Domain}"
HostedZoneId: !Ref CumulusHostedZone
ValidationMethod: DNS

AcmStagingDashCertificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Sub "staging.dashboard.${Domain}"
DomainValidationOptions:
- DomainName: !Sub "staging.dashboard.${Domain}"
HostedZoneId: !Ref CumulusHostedZone
ValidationMethod: DNS

AcmDevDashCertificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Sub "dev.dashboard.${Domain}"
DomainValidationOptions:
- DomainName: !Sub "dev.dashboard.${Domain}"
HostedZoneId: !Ref CumulusHostedZone
ValidationMethod: DNS
# Aggregator

AcmAggCertificate:
Type: AWS::CertificateManager::Certificate
Expand Down Expand Up @@ -89,6 +63,9 @@ Resources:
HostedZoneId: !Ref CumulusHostedZone
ValidationMethod: DNS


#Upload API

AcmApiCertificate:
Type: AWS::CertificateManager::Certificate
Properties:
Expand Down Expand Up @@ -125,18 +102,6 @@ Outputs:
Description: "ACM Www Certificate ARN"
Value: !Ref AcmWwwCertificate

DashCertificateArn:
Description: "ACM Dashboard Certificate ARN"
Value: !Ref AcmDashCertificate

DashStagingCertificateArn:
Description: "ACM Staging Dashboard Certificate ARN"
Value: !Ref AcmStagingDashCertificate

DashDevCertificateArn:
Description: "ACM Dev Dashboard Certificate ARN"
Value: !Ref AcmDevDashCertificate

AggCertificateArn:
Description: "ACM Aggregator Certificate ARN"
Value: !Ref AcmAggCertificate
Expand Down