Skip to content

Commit

Permalink
ARPA audit report task fixes (#2005)
Browse files Browse the repository at this point in the history
* Adjust SG attachment for arpa_audit_report ECS service

* Tweaking debug logs

* Tweak message handling

* Suppress call to useTenant()
  • Loading branch information
TylerHendrickson authored Sep 29, 2023
1 parent bf0f418 commit f07d462
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
24 changes: 14 additions & 10 deletions packages/server/src/arpa_reporter/lib/audit-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ async function createObligationSheet(periodId, domain, tenantId) {
// select active reporting periods and sort by date
const reportingPeriods = await getPreviousReportingPeriods(periodId, undefined, tenantId);
log('retrieved previous reporting periods', {
periodId, domain, reportingPeriods, fn: 'createObligationSheet',
periodId, domain, fn: 'createObligationSheet', count: reportingPeriods.length,
});

const rows = await Promise.all(
reportingPeriods.map(async (period) => {
log('creating row for reporting period', { period, fn: 'createObligationSheet' });
log('creating row for reporting period', { period: { id: period.id }, periodId, fn: 'createObligationSheet' });
const uploads = await usedForTreasuryExport(period.id, tenantId);
log('retrived uploads for period', { period, fn: 'createObligationSheet' });
log('retrived uploads for period', { period: { id: period.id }, periodId, fn: 'createObligationSheet' });
const records = await recordsForReportingPeriod(period.id, tenantId);
log('retrieved records', { period, fn: 'createObligationSheet' });
log('retrieved records', { period: { id: period.id }, periodId, fn: 'createObligationSheet' });

log('mapping uploads', { period, fn: 'createObligationSheet' });
log('mapping uploads', { period: { id: period.id }, periodId, fn: 'createObligationSheet' });
return Promise.all(uploads.map((upload) => {
log('initializing empty row', {
period: {
Expand Down Expand Up @@ -162,7 +162,7 @@ async function createObligationSheet(periodId, domain, tenantId) {
}

async function createProjectSummaries(periodId, domain, tenantId) {
log('called createProjectSummaries()', { periodId, domain });
log('called createProjectSummaries()', { periodId, domain, fn: 'createProjectSummaries' });
const records = await mostRecentProjectRecords(periodId, tenantId);
log('retrieved most recent project records', {
fn: 'createProjectSummaries', periodId, domain, record_count: records.length,
Expand Down Expand Up @@ -237,7 +237,7 @@ function getRecordsByProject(records) {
}

async function createReportsGroupedByProject(periodId, tenantId) {
log('called createProjectSummaries()', { periodId });
log('called createReportsGroupedByProject()', { periodId, fn: 'createReportsGroupedByProject' });
const records = await recordsForProject(periodId, tenantId);
log('retrieved records for project', { fn: 'createReportsGroupedByProject', count: records.length });
const recordsByProject = getRecordsByProject(records);
Expand Down Expand Up @@ -307,7 +307,7 @@ async function createReportsGroupedByProject(periodId, tenantId) {
}

async function createKpiDataGroupedByProject(periodId, tenantId) {
log('called createKpiDataGroupedByProject()', { periodId });
log('called createKpiDataGroupedByProject()', { periodId, fn: 'createKpiDataGroupedByProject' });
const records = await recordsForProject(periodId, tenantId);
log('retrieved records for project', { fn: 'createKpiDataGroupedByProject', count: records.length });
const recordsByProject = getRecordsByProject(records);
Expand Down Expand Up @@ -436,7 +436,8 @@ async function generateAndSendEmail(requestHost, recipientEmail, tenantId = useT
};
try {
console.log(uploadParams);
log('uploading report', { uploadParams });
console.log(uploadParams);
log('uploading report', { bucket: uploadParams.Bucket, key: uploadParams.Key });
await s3.send(new PutObjectCommand(uploadParams));
await module.exports.sendEmailWithLink(reportKey, recipientEmail);
} catch (err) {
Expand All @@ -448,14 +449,17 @@ async function generateAndSendEmail(requestHost, recipientEmail, tenantId = useT
async function processSQSMessageRequest(message) {
let requestData;
try {
requestData = JSON.parse(message.Body).detail;
requestData = JSON.parse(message.Body);
} catch (e) {
console.error('Error parsing request data from SQS message:', e);
return false;
}

try {
const user = await getUser(requestData.userId);
if (!user) {
throw new Error(`user not found: ${requestData.userId}`);
}
generateAndSendEmail(ARPA_REPORTER_BASE_URL, user.email, user.tenant_id);
} catch (e) {
console.error('Failed to generate and send audit report', e);
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/arpa_reporter/services/records.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ async function recordsForProject(periodId, tenantId) {
const reportingPeriods = await getPreviousReportingPeriods(periodId, undefined, tenantId);

const allRecords = await Promise.all(
reportingPeriods.map(({ id }) => recordsForReportingPeriod(id)),
reportingPeriods.map(({ id }) => recordsForReportingPeriod(id, tenantId)),
);

const projectRecords = allRecords
Expand Down
10 changes: 5 additions & 5 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ module "consume_grants_to_postgres_security_group" {
allow_all_egress = true
}

module "arpa_audit_report_to_postgres_security_group" {
module "arpa_audit_report_security_group" {
source = "cloudposse/security-group/aws"
version = "2.2.0"

namespace = var.namespace
vpc_id = data.aws_ssm_parameter.vpc_id.value
attributes = ["arpa_audit_report", "postgres"]
attributes = ["arpa_audit_report"]
allow_all_egress = true
}

Expand Down Expand Up @@ -135,7 +135,7 @@ module "api" {
subnet_ids = local.private_subnet_ids
security_group_ids = [
module.consume_grants_to_postgres_security_group.id,
module.arpa_audit_report_to_postgres_security_group.id,
module.arpa_audit_report_security_group.id,
]

# Cluster
Expand Down Expand Up @@ -227,7 +227,7 @@ module "arpa_audit_report" {

# Networking
subnet_ids = local.private_subnet_ids
security_group_ids = [module.api_to_postgres_security_group.id]
security_group_ids = [module.arpa_audit_report_security_group.id]

# Task configuration
ecs_cluster_name = join("", aws_ecs_cluster.default.*.name)
Expand Down Expand Up @@ -310,7 +310,7 @@ module "postgres" {
ingress_security_groups = {
from_api = module.api_to_postgres_security_group.id
from_consume_grants = module.consume_grants_to_postgres_security_group.id
from_arpa_audit_report = module.arpa_audit_report_to_postgres_security_group.id
from_arpa_audit_report = module.arpa_audit_report_security_group.id
}

prevent_destroy = var.postgres_prevent_destroy
Expand Down

0 comments on commit f07d462

Please sign in to comment.