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

PRMP-642: Create Bulk ODS Update Lambda #12

Merged
merged 52 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
ec7e960
[PRMP-629] - new dynamo tables and lambda
NogaNHS Jul 23, 2024
fbbab7a
[PRMP-629] - formatting
NogaNHS Jul 23, 2024
ef34064
[PRMP-629] - new s3 bucket
NogaNHS Jul 24, 2024
2e2a108
[PRMP-639] - new resources
NogaNHS Jul 25, 2024
0303238
[PRMP-639] - trud service and script
NogaNHS Jul 26, 2024
0a2fcf7
[PRMP-639] - Github actions added
NogaNHS Jul 29, 2024
fc1452a
[PRMP-639] - add reusable workflows
NogaNHS Jul 30, 2024
48d7062
[PRMP-639] - changes to workflow
NogaNHS Aug 1, 2024
b906d68
[PRMP-639] - add deletion_protection to dynamodb
NogaNHS Aug 1, 2024
6f2c7e5
[PRMP-639] - add deployment github actions
NogaNHS Aug 1, 2024
5720752
[PRMP-639] - add comment to tasks
NogaNHS Aug 1, 2024
0e039de
[PRMP-639] - removing matrix from dispatch
NogaNHS Aug 1, 2024
5d4c6f7
[PRMP-639] - format
NogaNHS Aug 1, 2024
04a33c4
[PRMP-639] - PR changes
NogaNHS Aug 2, 2024
a7ef859
[PRMP-639] - debugging jq error
NogaNHS Aug 2, 2024
deba7fc
[PRMP-639] - new tasks file
NogaNHS Aug 2, 2024
17635a8
[PRMP-639] - grant permission to run new tasks file
NogaNHS Aug 2, 2024
3f96a04
[PRMP-639] - fix syntax
NogaNHS Aug 2, 2024
1b5bdbc
[PRMP-639] - fix error
NogaNHS Aug 2, 2024
a769f36
[PRMP-640] - upgrade lambdas to 3.12
NogaNHS Aug 6, 2024
2bce625
[PRMP-640] - add lambda layers
NogaNHS Aug 6, 2024
b897708
[PRMP-640] - add lambda layer task
NogaNHS Aug 6, 2024
6aefaae
[PRMP-639] - changes path
NogaNHS Aug 6, 2024
45a9e4f
[PRMP-639] - remove not needed depends_on
NogaNHS Aug 6, 2024
fe13cfd
[PRMP-639] - change cron job time
NogaNHS Aug 6, 2024
7022c09
[PRMP-639] - add read only aws role
NogaNHS Aug 8, 2024
0035a49
[PRMP-639] - add workflows for other stacks
NogaNHS Aug 9, 2024
06e1070
[PRMP-639] - add workflows for other stacks
NogaNHS Aug 9, 2024
e8298bd
[PRMP-639] - add workflows for other stacks
NogaNHS Aug 9, 2024
e5f7ffd
[PRMP-639] - format
NogaNHS Aug 9, 2024
9522cba
[PRMP-639] - set max parallel
NogaNHS Aug 9, 2024
ef2bcd7
[PRMP-639] - change working-directory
NogaNHS Aug 9, 2024
d30aaf2
[PRMP-639] - change working-directory to stack
NogaNHS Aug 9, 2024
e1e8d52
[PRMP-639] - fix typo
NogaNHS Aug 9, 2024
2a31224
[PRMP-639] - fix typo
NogaNHS Aug 9, 2024
b2044f8
[PRMP-639] - add account to vars
NogaNHS Aug 9, 2024
e7e6eeb
[PRMP-639] - add account to vars
NogaNHS Aug 9, 2024
a7f1ece
[PRMP-639] - add account to vars
NogaNHS Aug 9, 2024
6cb8ee4
[PRMP-639] - bug fixes
NogaNHS Aug 9, 2024
758fccc
[PRMP-639] - remove prod account from vars
NogaNHS Aug 12, 2024
2d17218
[PRMP-639] - remove gocd pipeline
NogaNHS Aug 12, 2024
67deb96
[PRMP-639] - reuse image tag from docker job for prod
NogaNHS Aug 13, 2024
b2fdef7
[PRMP-639] - separate prod into prod and prod-plan
NogaNHS Aug 13, 2024
09eb77e
[PRMP-639] - separate prod into prod and prod-pla2 d 2
NogaNHS Aug 13, 2024
158909a
Add bulk ods update lambda
abbas-khan10 Aug 9, 2024
d7e07e9
cleanup files and add shared utils
abbas-khan10 Aug 12, 2024
b815b7b
fix icb spacing bug
abbas-khan10 Aug 14, 2024
63db4d5
[PRMP-642] - change unzip to unzip into path
NogaNHS Aug 14, 2024
e285d3f
PR changes and layer optimisation
abbas-khan10 Aug 15, 2024
c31ff4f
fix layer paths and cleanup
abbas-khan10 Aug 15, 2024
63bc488
refactor
abbas-khan10 Aug 15, 2024
c0b9c4e
Merge branch 'refs/heads/PRMP-594' into PRMP-642
NogaNHS Aug 16, 2024
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
50 changes: 29 additions & 21 deletions lambda/bulk-ods-update/bulk_ods_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import csv

import boto3
from pynamodb.exceptions import DoesNotExist

from utils.enums.trud import OdsDownloadType, TrudItem
from utils.models.ods_models import PracticeOds, IcbOds
Expand All @@ -20,6 +19,8 @@
ICB_QUARTERLY_FILE_PATH,
ICB_MONTHLY_FILE_NAME,
ICB_QUARTERLY_FILE_NAME,
GP_WEEKLY_FILE_NAME,
GP_WEEKLY_ZIP_FILE_PATH,
)

logger = logging.getLogger()
Expand Down Expand Up @@ -50,8 +51,8 @@ def determine_ods_manifest_download_type() -> OdsDownloadType:
logger.info("Determining download type")
today = date.today()

last_day_of_month = calendar.monthrange(today.year, today.month)[1]
last_date_of_month = date(today.year, today.month, last_day_of_month)
total_days_in_month = calendar.monthrange(today.year, today.month)[1]
last_date_of_month = date(today.year, today.month, total_days_in_month)

last_sunday_of_month = last_date_of_month

Expand Down Expand Up @@ -81,28 +82,32 @@ def extract_and_process_ods_gp_data(trud_service: TrudApiService):
gp_ods_releases[0].get("archiveFileUrl")
)

eppracur_csv_path = os.path.join(TEMP_DIR, "epraccur.csv")
eppracur_csv_path = os.path.join(TEMP_DIR, GP_WEEKLY_FILE_NAME)

epraccur_zip_file = trud_service.unzipping_files(
download_file_bytes, "Data/epraccur.zip", TEMP_DIR, True
download_file_bytes, GP_WEEKLY_ZIP_FILE_PATH, TEMP_DIR, True
)
trud_service.unzipping_files(epraccur_zip_file,"epraccur.csv", TEMP_DIR)

logger.info(os.listdir(TEMP_DIR))
trud_service.unzipping_files(epraccur_zip_file, GP_WEEKLY_FILE_NAME, TEMP_DIR)

gp_ods_data = trud_csv_to_dict(eppracur_csv_path, GP_FILE_HEADERS)
gp_ods_data_amended_data = get_amended_records(gp_ods_data)

if gp_ods_data_amended_data:
logger.info(
f"Found {len(gp_ods_data_amended_data)} amended GP data records to update"
)
compare_and_overwrite(OdsDownloadType.GP, gp_ods_data_amended_data)
return

logger.info("No amended GP data found")


def extract_and_process_ods_icb_data(trud_service: TrudApiService):
logger.info("Extracting and processing ODS ICB data")

icb_ods_releases = trud_service.get_release_list(TrudItem.ORG_REF_DATA_MONTHLY, True)
icb_ods_releases = trud_service.get_release_list(
TrudItem.ORG_REF_DATA_MONTHLY, True
)

is_quarterly_release = icb_ods_releases[0].get("name").endswith(".0.0")
download_file = trud_service.get_download_file(
Expand All @@ -127,6 +132,9 @@ def extract_and_process_ods_icb_data(trud_service: TrudApiService):
icb_ods_data_amended_data = get_amended_records(icb_ods_data)

if icb_ods_data_amended_data:
logger.info(
f"Found {len(icb_ods_data_amended_data)} amended ICB data records to update"
)
compare_and_overwrite(OdsDownloadType.ICB, icb_ods_data_amended_data)
return

Expand All @@ -152,29 +160,29 @@ def trud_csv_to_dict(file_path: str, headers: list[str]) -> list[dict]:


def compare_and_overwrite(download_type: OdsDownloadType, data: list[dict]):
logger.info(data)
if download_type == OdsDownloadType.GP:
logger.info("Comparing GP Practice data")
for amended_record in data:
try:
practice = PracticeOds(amended_record.get("PracticeOdsCode"))
practice.update(
actions=[
PracticeOds.practice_name.set(amended_record.get("PracticeName")),
PracticeOds.icb_ods_code.set(amended_record.get("IcbOdsCode"))
PracticeOds.practice_name.set(
amended_record.get("PracticeName")
),
PracticeOds.icb_ods_code.set(amended_record.get("IcbOdsCode")),
]
)
except DoesNotExist as e:
logger.info(f"Failed to retrieve record by Practice ODS code: {str(e)}")
except Exception as e:
logger.info(
f"Failed to create/update record by Practice ODS code: {str(e)}"
)

if download_type == OdsDownloadType.ICB:
logger.info("Comparing ICB data")
for amended_record in data:
abbas-khan10 marked this conversation as resolved.
Show resolved Hide resolved
try:
icb = IcbOds(amended_record.get("IcbOdsCode"))
icb.update(
actions=[
IcbOds.icb_name.set(amended_record.get("IcbName"))
]
)
except DoesNotExist as e:
logger.info(f"Failed to retrieve record by ICB ODS code: {str(e)}")
icb.update(actions=[IcbOds.icb_name.set(amended_record.get("IcbName"))])
except Exception as e:
logger.info(f"Failed to create/update record by ICB ODS code: {str(e)}")
1 change: 1 addition & 0 deletions lambda/mi-enrichment-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pynamodb==6.0.1
6 changes: 4 additions & 2 deletions stacks/gp-registrations-mi/scripts/get_latest_ods_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
ICB_QUARTERLY_FILE_NAME,
ICB_FILE_HEADERS,
GP_FILE_HEADERS,
GP_WEEKLY_FILE_NAME,
GP_WEEKLY_ZIP_FILE_PATH,
)


Expand Down Expand Up @@ -48,9 +50,9 @@ def get_gp_latest_ods_csv(service):
release_list_response[0].get("archiveFileUrl")
)
epraccur_zip_file = service.unzipping_files(
download_file, "Data/epraccur.zip", byte=True
download_file, GP_WEEKLY_ZIP_FILE_PATH, byte=True
)
epraccur_csv_file = service.unzipping_files(epraccur_zip_file, "epraccur.csv")
epraccur_csv_file = service.unzipping_files(epraccur_zip_file, GP_WEEKLY_FILE_NAME)
create_modify_csv(
epraccur_csv_file,
"initial_full_gps_ods.csv",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ resource "aws_lambda_function" "event_enrichment_lambda" {
ApplicationRole = "AwsLambdaFunction"
}
)
layers = [aws_lambda_layer_version.event_enrichment_lambda.arn]
layers = [aws_lambda_layer_version.mi_enrichment_lambda_layer.arn]
environment {
variables = {
SPLUNK_CLOUD_EVENT_UPLOADER_SQS_QUEUE_URL = aws_sqs_queue.incoming_mi_events_for_splunk_cloud_event_uploader.url,
Expand Down Expand Up @@ -54,11 +54,4 @@ resource "aws_cloudwatch_log_group" "event_enrichment_lambda" {
}
)
retention_in_days = 60
}

resource "aws_lambda_layer_version" "event_enrichment_lambda" {
filename = var.event_enrichment_lambda_layer_zip
layer_name = "${var.environment}-${var.event_enrichment_lambda_name}_layer"
compatible_runtimes = ["python3.12"]
compatible_architectures = ["x86_64"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resource "aws_lambda_function" "ods_bulk_update" {
source_code_hash = filebase64sha256(var.bulk_ods_update_lambda_zip)
runtime = "python3.12"
timeout = 300
layers = [aws_lambda_layer_version.ods_bulk_update_lambda.arn]
layers = [aws_lambda_layer_version.mi_enrichment_lambda_layer.arn]
environment {
variables = {
TRUD_API_KEY_PARAM_NAME = data.aws_ssm_parameter.trud_api_key.name,
Expand Down Expand Up @@ -73,9 +73,3 @@ resource "aws_lambda_permission" "bulk_upload_metadata_schedule_permission" {
]
}

resource "aws_lambda_layer_version" "ods_bulk_update_lambda" {
filename = var.event_enrichment_lambda_layer_zip
layer_name = "${var.environment}-${var.ods_bulk_update_lambda_name}_layer"
compatible_runtimes = ["python3.12"]
compatible_architectures = ["x86_64"]
}
6 changes: 6 additions & 0 deletions stacks/gp-registrations-mi/terraform/lambda_layer.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "aws_lambda_layer_version" "mi_enrichment_lambda_layer" {
Copy link
Contributor

Choose a reason for hiding this comment

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

remove layer from resource name

filename = var.mi_enrichment_lambda_layer_zip
layer_name = "${var.environment}_mi_enrichment_layer"
compatible_runtimes = ["python3.12"]
compatible_architectures = ["x86_64"]
}
20 changes: 7 additions & 13 deletions stacks/gp-registrations-mi/terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ variable "splunk_cloud_api_token_param_name" {
description = "SSM param containing splunk cloud api token to send MI events to"
}

variable "mi_enrichment_lambda_layer_zip" {
type = string
description = "Path to zipfile containing relevant packages for MI lambdas"
default = "../../../lambda/build/layers/mi-enrichment.zip"
}

variable "splunk_cloud_event_uploader_lambda_zip" {
type = string
description = "Path to zipfile containing lambda code for uploading events to splunk cloud"
Expand All @@ -96,22 +102,10 @@ variable "event_enrichment_lambda_zip" {
default = "../../../lambda/build/event-enrichment.zip"
}

variable "event_enrichment_lambda_layer_zip" {
type = string
description = "Path to zipfile containing lambda layer code for enriching MI events"
default = "../../../lambda/build/layer/event-enrichment.zip"
}

variable "bulk_ods_update_lambda_zip" {
type = string
description = "Path to zipfile containing lambda code for ODS update"
default = "placeholder_lambda_payload.zip"
}

variable "bulk_ods_update_lambda_layer_zip" {
type = string
description = "Path to zipfile containing lambda layer code for ODS update"
default = "placeholder_lambda_payload.zip"
default = "../../../lambda/build/bulk-ods-update.zip"
}

variable "s3_event_uploader_lambda_zip" {
Expand Down
14 changes: 5 additions & 9 deletions tasks_github_actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ function build_lambda {
rm -rf $build_dir
mkdir -p $build_dir

requirements_file=lambda/$lambda_name/requirements.txt
if test -f "$requirements_file"; then
build_lambda_layer $lambda_name
fi

if test "$lambda_services"; then
cp -r ./$lambda_services $build_dir
fi
Expand All @@ -28,13 +23,13 @@ function build_lambda {
}

function build_lambda_layer {
lambda_name=$1
build_dir=lambda/build/layer/$lambda_name
layer_name=$1
build_dir=lambda/build/layers/$layer_name

rm -rf $build_dir/python
mkdir -p $build_dir/python

requirements_file=lambda/$lambda_name/requirements.txt
requirements_file=lambda/$layer_name-requirements.txt
if test -f "$requirements_file"; then
python3 -m venv create_layer
source create_layer/bin/activate
Expand All @@ -43,13 +38,14 @@ function build_lambda_layer {

cp -r create_layer/lib $build_dir/python
pushd $build_dir
zip -r -X ../$lambda_name.zip .
zip -r -X ../$layer_name.zip .
popd
}

echo "--- ${task} ---"
case "${task}" in
build-lambdas)
build_lambda_layer mi-enrichment
build_lambda bulk-ods-update utils
abbas-khan10 marked this conversation as resolved.
Show resolved Hide resolved
build_lambda error-alarm-alert
build_lambda splunk-cloud-event-uploader
Expand Down
3 changes: 3 additions & 0 deletions utils/trud_files.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
GP_WEEKLY_ZIP_FILE_PATH = "Data/epraccur.zip"
GP_WEEKLY_FILE_NAME = "epraccur.csv"

ICB_MONTHLY_FILE_PATH = "eamendam.zip"
ICB_MONTHLY_FILE_NAME = "eccgam.csv"

Expand Down
Loading