Skip to content

Commit

Permalink
Merge pull request #24 from Virtual-Finland-Development/perf/use-inte…
Browse files Browse the repository at this point in the history
…rnal-resource-in-esco-api

Stabilize EscoAPI cache performance
  • Loading branch information
lsipii authored Jun 22, 2023
2 parents cb0f7cf + c1950ca commit 6524099
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
36 changes: 23 additions & 13 deletions src/escoApi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2 } from 'aws-lambda';
import { UriRedirects, resolveError } from './utils/api';
import { resolveError } from './utils/api';
import { transformOccupations } from './utils/data/transformers';
import { ValidationError } from './utils/exceptions';

const CODESETS_API_ENDPOINT = process.env.CODESETS_API_ENDPOINT;

// Store for the duration of the lambda instance
const internalMemory: any = {
occupations: null,
};

// AWS Lambda function handler for the ESCO API
export async function handler(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyStructuredResultV2> {
// Lambda warmup request handling
Expand All @@ -17,21 +23,25 @@ export async function handler(event: APIGatewayProxyEventV2): Promise<APIGateway
// Normal request handling
try {
const request = parseRequest(event);

const paramsAsQuery = new URLSearchParams(request.params).toString();
const uri = `${CODESETS_API_ENDPOINT}${request.path}`;
const uriAsQuery = uri + (paramsAsQuery ? '?' + paramsAsQuery : '');
const response = await fetch(uriAsQuery);
const responseBody = await response.text();

const responseHeaders = Object.entries(response.headers).reduce((headers, [key, value]) => {
headers[key] = value;
return headers;
}, {} as Record<string, string>);
if (internalMemory.occupations === null) {
const response = await fetch(uri);
const responseData = await response.json();
if (!(responseData.occupations instanceof Array)) {
throw new Error('Invalid response from codesets API');
}
internalMemory.occupations = responseData.occupations;
}

const transformedData = await transformOccupations(internalMemory.occupations, request.params);
const responseBody = JSON.stringify(transformedData);

return {
statusCode: response.status,
headers: responseHeaders,
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: responseBody,
};
} catch (error: any) {
Expand Down Expand Up @@ -59,7 +69,7 @@ function parseRequest(event: APIGatewayProxyEventV2): { method: string; path: st
throw new ValidationError('Missing request body');
}

const knownPaths = Object.keys(UriRedirects);
const knownPaths = ['/productizer/draft/Employment/EscoOccupations'];
if (!knownPaths.includes(rawPath)) {
throw new ValidationError('Unknown request path');
}
Expand Down
15 changes: 2 additions & 13 deletions src/resources/internal/BusinessFinlandEscoOccupations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import InternalResource from '../../utils/data/models/InternalResource';
import { getOutput } from '../../utils/data/parsers';
import { getPaginationParams } from '../../utils/filters';
import { transformOccupations as transform } from '../../utils/data/transformers';

import BusinessFinlandDataSet from './business-finland-esco-v1_1_1-occupations.json';

Expand All @@ -21,18 +21,7 @@ export default new InternalResource({
name: 'BusinessFinlandEscoOccupations',
uri: 'business-finland-esco-v1_1_1-occupations.json',
parsers: {
async transform(occupations: any, params: Record<string, string>) {
const totalCount = occupations.length;
const pagination = getPaginationParams(params);
if (pagination.isPaginated) {
occupations = occupations.slice(pagination.offset, pagination.offset + pagination.limit);
}

return {
totalCount: totalCount,
occupations: occupations,
};
},
transform,
output(data: any) {
return getOutput()<OccupationsResponse>(data);
},
Expand Down
21 changes: 21 additions & 0 deletions src/utils/data/transformers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getPaginationParams } from '../filters';

/**
* Separate occupations transformer from the resource to avoid resource-related dependencies import where not needed
*
* @param occupations
* @param params
* @returns
*/
export async function transformOccupations(occupations: any, params: Record<string, string>) {
const totalCount = occupations.length;
const pagination = getPaginationParams(params);
if (pagination.isPaginated) {
occupations = occupations.slice(pagination.offset, pagination.offset + pagination.limit);
}

return {
totalCount: totalCount,
occupations: occupations,
};
}

0 comments on commit 6524099

Please sign in to comment.