generated from NHSDigital/nhs-notify-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 3
CCM-13304: Generate reports #203
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
Open
simonlabarere
wants to merge
31
commits into
main
Choose a base branch
from
feature/CCM-13304_generate_reports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
4906c7d
CCM-13303: Update Generate Report schema to require a reportDate
simonlabarere e6d2b58
CCM-13304: Initial commit
simonlabarere a07bc6c
Merge branch 'main' into feature/CCM-13304_generate_reports
simonlabarere be5fef3
CCM-13304: Fix existing test
simonlabarere 5c2c8e8
CCM-13304: Fix existing test
simonlabarere cb4822a
CCM-13304: Added a helper to trigger Glue table refresh
gareth-allan 5b64e91
Merge branch 'main' into feature/CCM-13304_generate_reports
simonlabarere d753b4e
CCM-13304: Unit tests
simonlabarere b3cd51d
CCM-13304: Unit tests
simonlabarere 83fc83e
CCM-13304: Fix terraform
simonlabarere 531bd8d
CCM-13304: Added a component test for report generation
gareth-allan 7820414
Merge branch 'main' into feature/CCM-13304_component_test_helpers
gareth-allan 6ab35e2
CCM-13304: Bundle SQL in lambda
simonlabarere 4c987cb
CCM-13304: Bundle SQL in lambda
simonlabarere 0a9383d
CCM-13304: Add permission to run Athen queries
simonlabarere 52dd47a
CCM-13304: Add letter status to athena table
simonlabarere af74a2a
CCM-13304: Fix Athena Workgroup arn
simonlabarere 9f0938f
CCM-13304: Update report bucket permission for Athena
simonlabarere e6f00cb
CCM-13304: Add letter status to athena table
simonlabarere 94ba7f7
CCM-13304: Update query
simonlabarere 0277b56
CCM-13304: Update query
simonlabarere b8bb5bc
CCM-13304: Query fix attempt
simonlabarere ee60d9e
CCM-13304: Query fix attempt
simonlabarere 8a521d2
CCM-13304: update permissions to run athena query
simonlabarere 33a3a41
CCM-13304: Tweaks following review
gareth-allan 09c9963
Merge branch 'main' into feature/CCM-13304_generate_reports
gareth-allan 4ed6c84
CCM-13304: Linting/test tweaks
gareth-allan 5eb32eb
Merge branch 'main' into feature/CCM-13304_component_test_helpers
gareth-allan 2d1e07b
Merge branch 'feature/CCM-13304_component_test_helpers' into feature/…
gareth-allan f3af3ef
CCM-13304: First attempt at component test
gareth-allan bfdfd1a
CCM-13304: Remove test.only
gareth-allan 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 hidden or 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 hidden or 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
18 changes: 18 additions & 0 deletions
18
infrastructure/terraform/components/dl/cloudwatch_event_rule_generate_report.tf
This file contains hidden or 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,18 @@ | ||
| resource "aws_cloudwatch_event_rule" "generate_report" { | ||
| name = "${local.csi}-generate-report" | ||
| description = "Generate Report event rule" | ||
| event_bus_name = aws_cloudwatch_event_bus.main.name | ||
| event_pattern = jsonencode({ | ||
| "detail" : { | ||
| "type" : [ | ||
| "uk.nhs.notify.digital.letters.reporting.generate.report.v1" | ||
| ], | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| resource "aws_cloudwatch_event_target" "generate_report_report_generator" { | ||
| rule = aws_cloudwatch_event_rule.generate_report.name | ||
| arn = module.sqs_report_generator.sqs_queue_arn | ||
| event_bus_name = aws_cloudwatch_event_bus.main.name | ||
| } |
This file contains hidden or 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
10 changes: 10 additions & 0 deletions
10
infrastructure/terraform/components/dl/lambda_event_source_mapping_report_generator.tf
This file contains hidden or 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,10 @@ | ||
| resource "aws_lambda_event_source_mapping" "report_generator" { | ||
| event_source_arn = module.sqs_report_generator.sqs_queue_arn | ||
| function_name = module.report_generator.function_name | ||
| batch_size = var.queue_batch_size | ||
| maximum_batching_window_in_seconds = var.queue_batch_window_seconds | ||
|
|
||
| function_response_types = [ | ||
| "ReportBatchItemFailures" | ||
| ] | ||
| } |
156 changes: 156 additions & 0 deletions
156
infrastructure/terraform/components/dl/module_lambda_report_generator.tf
This file contains hidden or 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,156 @@ | ||
| module "report_generator" { | ||
| source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.30/terraform-lambda.zip" | ||
|
|
||
| function_name = "report-generator" | ||
| description = "A function to generate reports from an event" | ||
|
|
||
| aws_account_id = var.aws_account_id | ||
| component = local.component | ||
| environment = var.environment | ||
| project = var.project | ||
| region = var.region | ||
| group = var.group | ||
|
|
||
| log_retention_in_days = var.log_retention_in_days | ||
| kms_key_arn = module.kms.key_arn | ||
|
|
||
| iam_policy_document = { | ||
| body = data.aws_iam_policy_document.report_generator_lambda.json | ||
| } | ||
|
|
||
| function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"] | ||
| function_code_base_path = local.aws_lambda_functions_dir_path | ||
| function_code_dir = "report-generator/dist" | ||
| function_include_common = true | ||
| handler_function_name = "handler" | ||
| runtime = "nodejs22.x" | ||
| memory = 128 | ||
| timeout = 60 | ||
| log_level = var.log_level | ||
|
|
||
| force_lambda_code_deploy = var.force_lambda_code_deploy | ||
| enable_lambda_insights = false | ||
|
|
||
| log_destination_arn = local.log_destination_arn | ||
| log_subscription_role_arn = local.acct.log_subscription_role_arn | ||
|
|
||
| lambda_env_vars = { | ||
| "ATHENA_WORKGROUP" = aws_athena_workgroup.reporting.name | ||
| "ATHENA_DATABASE" = aws_glue_catalog_database.reporting.name | ||
| "EVENT_PUBLISHER_EVENT_BUS_ARN" = aws_cloudwatch_event_bus.main.arn | ||
| "EVENT_PUBLISHER_DLQ_URL" = module.sqs_event_publisher_errors.sqs_queue_url | ||
| "MAX_POLL_LIMIT" = var.athena_query_max_polling_attempts | ||
| "REPORTING_BUCKET" = module.s3bucket_reporting.bucket | ||
| "REPORT_NAME" = "completed_communications" | ||
| "WAIT_FOR_IN_SECONDS" = var.athena_query_polling_time_seconds | ||
| } | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "report_generator_lambda" { | ||
| statement { | ||
| sid = "AllowS3Get" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "s3:PutObject", | ||
| "s3:GetObject", | ||
| "s3:GetBucketLocation", | ||
| "s3:ListBucket" | ||
| ] | ||
|
|
||
| resources = [ | ||
| "${module.s3bucket_reporting.arn}/*", | ||
| "${module.s3bucket_reporting.arn}" | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "KMSPermissions" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "kms:Decrypt", | ||
| "kms:GenerateDataKey", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.kms.key_arn, | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "AllowAthenaAccess" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "athena:StartQueryExecution", | ||
| "athena:GetQueryResults", | ||
| "athena:GetQueryExecution" | ||
| ] | ||
|
|
||
| resources = [ | ||
| "arn:aws:athena:${var.region}:${var.aws_account_id}:workgroup/${aws_athena_workgroup.reporting.name}" | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "AllowGlueAccess" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "glue:GetTable", | ||
| "glue:GetDatabase", | ||
| "glue:GetPartition", | ||
| "glue:GetPartitions", | ||
| ] | ||
|
|
||
| resources = [ | ||
| "arn:aws:glue:${var.region}:${var.aws_account_id}:catalog", | ||
| "arn:aws:glue:${var.region}:${var.aws_account_id}:database/${aws_glue_catalog_database.reporting.name}", | ||
| "arn:aws:glue:${var.region}:${var.aws_account_id}:table/${aws_glue_catalog_database.reporting.name}/*" | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "SQSPermissionsReportGeneratorQueue" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "sqs:ReceiveMessage", | ||
| "sqs:DeleteMessage", | ||
| "sqs:GetQueueAttributes", | ||
| "sqs:GetQueueUrl", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.sqs_report_generator.sqs_queue_arn, | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "PutEvents" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "events:PutEvents", | ||
| ] | ||
|
|
||
| resources = [ | ||
| aws_cloudwatch_event_bus.main.arn, | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "SQSPermissionsEventPublisherDLQ" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "sqs:SendMessage", | ||
| "sqs:SendMessageBatch", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.sqs_event_publisher_errors.sqs_queue_arn, | ||
| ] | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
infrastructure/terraform/components/dl/module_sqs_report_generator.tf
This file contains hidden or 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,44 @@ | ||
| module "sqs_report_generator" { | ||
| source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.30/terraform-sqs.zip" | ||
|
|
||
| aws_account_id = var.aws_account_id | ||
| component = local.component | ||
| environment = var.environment | ||
| project = var.project | ||
| region = var.region | ||
| name = "report-generator" | ||
|
|
||
| sqs_kms_key_arn = module.kms.key_arn | ||
|
|
||
| visibility_timeout_seconds = 60 | ||
|
|
||
| create_dlq = true | ||
|
|
||
| sqs_policy_overload = data.aws_iam_policy_document.sqs_report_generator.json | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "sqs_report_generator" { | ||
| statement { | ||
| sid = "AllowEventBridgeToSendMessage" | ||
| effect = "Allow" | ||
|
|
||
| principals { | ||
| type = "Service" | ||
| identifiers = ["events.amazonaws.com"] | ||
| } | ||
|
|
||
| actions = [ | ||
| "sqs:SendMessage" | ||
| ] | ||
|
|
||
| resources = [ | ||
| "arn:aws:sqs:${var.region}:${var.aws_account_id}:${local.csi}-report-generator-queue" | ||
| ] | ||
|
|
||
| condition { | ||
| test = "ArnEquals" | ||
| variable = "aws:SourceArn" | ||
| values = [aws_cloudwatch_event_rule.generate_report.arn] | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,14 @@ | ||
| import { baseJestConfig } from '../../jest.config.base'; | ||
|
|
||
| const config = baseJestConfig; | ||
|
|
||
| config.coverageThreshold = { | ||
| global: { | ||
| branches: 88, | ||
| functions: 100, | ||
| lines: 97, | ||
| statements: 97, | ||
| }, | ||
| }; | ||
|
|
||
| export default config; |
This file contains hidden or 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,24 @@ | ||
| { | ||
| "dependencies": { | ||
| "@aws-sdk/client-athena": "^3.984.0", | ||
| "digital-letters-events": "^0.0.1", | ||
| "utils": "^0.0.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@tsconfig/node22": "^22.0.2", | ||
| "@types/aws-lambda": "^8.10.155", | ||
| "@types/jest": "^29.5.14", | ||
| "jest": "^29.7.0", | ||
| "typescript": "^5.9.3" | ||
| }, | ||
| "name": "nhs-notify-digital-letters-report-generator", | ||
| "private": true, | ||
| "scripts": { | ||
| "lambda-build": "rm -rf dist && npx esbuild --bundle --minify --sourcemap --target=es2020 --platform=node --loader:.node=file --entry-names=[name] --outdir=dist src/index.ts && cp -r src/queries dist/", | ||
| "lint": "eslint .", | ||
| "lint:fix": "eslint . --fix", | ||
| "test:unit": "jest", | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "version": "0.0.1" | ||
| } |
Oops, something went wrong.
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.
Is 50 attempts not a bit high?
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.
I copied this from core.