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

1040 Add lambda insights to MR generation lambda #1716

Merged
merged 3 commits into from
Jul 4, 2024
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
1 change: 1 addition & 0 deletions packages/infra/lib/api-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,7 @@ export class APIStack extends Stack {
layers: [lambdaLayers.shared, lambdaLayers.chromium],
memory: 4096,
timeout: lambdaTimeout,
isEnableInsights: true,
vpc,
alarmSnsAction: alarmAction,
});
Expand Down
15 changes: 15 additions & 0 deletions packages/infra/lib/shared/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Code,
Function as Lambda,
ILayerVersion,
LambdaInsightsVersion,
Runtime,
RuntimeManagementMode,
SingletonFunction,
Expand Down Expand Up @@ -59,6 +60,17 @@ export interface LambdaProps extends StackProps {
readonly architecture?: Architecture;
readonly layers: ILayerVersion[];
readonly version?: string | undefined;
/**
* Whether to enable Lambda insights.
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights.html
*/
readonly isEnableInsights?: boolean | undefined;
/**
* Which lambda insights version to use.
* Defaults to the latest version available in the CDK version.
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html
*/
readonly insightsVersion?: LambdaInsightsVersion | undefined;
}

export function createLambda(props: LambdaProps): Lambda {
Expand All @@ -74,6 +86,9 @@ export function createLambda(props: LambdaProps): Lambda {
...(props.layers && props.layers.length > 0 ? { layers: props.layers } : {}),
vpc: props.vpc,
vpcSubnets: props.subnets ? { subnets: props.subnets } : undefined,
insightsVersion: props.isEnableInsights
? props.insightsVersion ?? LambdaInsightsVersion.VERSION_1_0_229_0 // latest version on CDK 2.122.0
: undefined,
/**
* Watch out if this is more than 60s while using SQS we likely need to update the
* queue's VisibilityTimeout so the message is not processed more than once.
Expand Down
64 changes: 42 additions & 22 deletions packages/lambdas/src/cda-to-visualization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const cdaToVisTimeoutInMillis = getEnvOrFail("CDA_TO_VIS_TIMEOUT_MS");
const GRACEFUL_SHUTDOWN_ALLOWANCE_MS = 3_000;
const SIGNED_URL_DURATION_SECONDS = 60;

let cda10: unknown;
let narrative: unknown;
const styleSheetTextStringified = JSON.stringify(styleSheetText);

const s3client = new AWS.S3({
signatureVersion: "v4",
});
Expand Down Expand Up @@ -149,6 +153,7 @@ const convertStoreAndReturnPdfDocUrl = async ({
await sleep(2_500);

// Generate PDF from page in puppeteer
const before = Date.now();
await page.pdf({
path: pdfFilepath,
printBackground: true,
Expand All @@ -161,6 +166,7 @@ const convertStoreAndReturnPdfDocUrl = async ({
bottom: "20px",
},
});
console.log(`Finished generating the PDF, took ${Date.now() - before}ms`);

// Upload generated PDF to S3 bucket
await s3client
Expand Down Expand Up @@ -192,29 +198,14 @@ const convertStoreAndReturnPdfDocUrl = async ({
return urlPdf;
};

const convertToHtml = async (document: string): Promise<string> => {
async function convertToHtml(document: string): Promise<string> {
try {
const cda10 = await SaxonJS.getResource(
{
location:
"https://raw.githubusercontent.com/metriport/metriport/master/packages/lambdas/static/cda_l10n.xml",
type: "xml",
},
"async"
);

const narrative = await SaxonJS.getResource(
{
location:
"https://raw.githubusercontent.com/metriport/metriport/master/packages/lambdas/static/cda_narrativeblock.xml",
type: "xml",
},
"async"
);
const cda10 = await getCda10();
const narrative = await getNarrative();

const result = await SaxonJS.transform(
{
stylesheetText: JSON.stringify(styleSheetText),
stylesheetText: styleSheetTextStringified,
stylesheetParams: {
vocFile: cda10,
narrative: narrative,
Expand All @@ -233,13 +224,42 @@ const convertToHtml = async (document: string): Promise<string> => {
});
throw error;
}
};
}

// TODO could we do the same we do w/ stylesheet? require("./cda-to-visualization/stylesheet.js");
async function getCda10() {
if (!cda10) {
cda10 = await SaxonJS.getResource(
{
location:
"https://raw.githubusercontent.com/metriport/metriport/master/packages/lambdas/static/cda_l10n.xml",
type: "xml",
},
"async"
);
}
return cda10;
}

async function getNarrative() {
if (!narrative) {
narrative = await SaxonJS.getResource(
{
location:
"https://raw.githubusercontent.com/metriport/metriport/master/packages/lambdas/static/cda_narrativeblock.xml",
type: "xml",
},
"async"
);
}
return narrative;
}

const getSignedUrl = async ({ fileName, bucketName }: { fileName: string; bucketName: string }) => {
async function getSignedUrl({ fileName, bucketName }: { fileName: string; bucketName: string }) {
const url = s3client.getSignedUrl("getObject", {
Bucket: bucketName,
Key: fileName,
Expires: SIGNED_URL_DURATION_SECONDS,
});
return url;
};
}
Loading