Skip to content

Commit

Permalink
feat: count issue attempts and log errors
Browse files Browse the repository at this point in the history
  • Loading branch information
marnixdessing committed Apr 4, 2023
1 parent bccfc86 commit 260f05e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/ApiStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ export class ApiStack extends Stack {
YIVI_API_ACCESS_KEY_ID_ARN: secretYiviApiAccessKeyId.secretArn,
YIVI_API_SECRET_KEY_ARN: secretYiviApiSecretKey.secretArn,
YIVI_API_KEY_ARN: secretYiviApiKey.secretArn,
STATISTICS_LOG_GROUP_ARN: statisticsLogGroup.logGroupArn,
STATISTICS_LOG_STREAM: statisticsLogStream.logStreamName,
STATISTICS_LOG_GROUP_NAME: statisticsLogGroup.logGroupName,
STATISTICS_LOG_STREAM_NAME: statisticsLogStream.logStreamName,
DIVERSIFYER: diversifiyer,
},
lambdaInsightsExtensionArn: insightsArn,
Expand Down
37 changes: 25 additions & 12 deletions src/app/issue/issueRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ async function handleLoggedinRequest(session: Session, brpApi: BrpApi, yiviApi:
}

// Log the issue event
if (!error) {
logIssueEvent(logsClient, brpData, session)
.then(() => console.debug('Logged issue event') )
.catch(err => console.error('Could not log issue event', err));
await storeIssueEventInSession(brpData, session);
}
logIssueEvent(logsClient, session, brpData, error) // Logs do not show in the console as we do not await
.then(() => console.debug('Logged issue event') )
.catch(err => console.error('Could not log issue event', err));

await storeIssueEventInSession(brpData, session);

// Render the page
const data = {
Expand All @@ -90,39 +89,53 @@ async function handleLoggedinRequest(session: Session, brpApi: BrpApi, yiviApi:
* @param session the uses session to store data in
*/
async function storeIssueEventInSession(brpData: any, session: Session) {
const gemeente = brpData.Persoon.Adres.Gemeente;
const gemeente = brpData?.Persoon?.Adres?.Gemeente;
const loggedin = session.getValue('loggedin', 'BOOL') ?? false;
const loa = session.getValue('loa');
const issueAttempt = session.getValue('attempt', 'N') ?? 0;

try {
await session.updateSession({
loggedin: { BOOL: loggedin },
bsn: { S: brpData.Persoon.BSN.BSN },
gemeente: { S: gemeente },
loa: { S: loa },
issueAttempt: { N: issueAttempt + 1 }, // Increment
});
} catch (err) {
console.log('Could not add issue statistics to session', err);
}
}


async function logIssueEvent(client: CloudWatchLogsClient, brpData: any, session: Session) {
/**
* Log the issue event to a separate log group
* @param client
* @param session
* @param brpData
* @param error
*/
async function logIssueEvent(client: CloudWatchLogsClient, session: Session, brpData: any, error?: string) {

// Setup statistics data
const bsn = session.getValue('bsn', 'S');
const loa = session.getValue('loa');
const issueAttempt = session.getValue('attempt', 'N') ?? 0;
const gemeente = brpData.Persoon.Adres.Gemeente;
const timestamp = Date.now();
const diversify = `${bsn}/${gemeente}/${process.env.DIVERSIFYER}`;
const subject = crypto.createHash('sha256').update(diversify).digest('hex');

let message = JSON.stringify({ timestamp, gemeente, subject, loa, issueAttempt });
if (error) {
message = JSON.stringify({ timestamp, loa, issueAttempt, error });
}

const input = {
logGroupName: process.env.STATISTICS_LOG_GROUP_ARN,
logStreamName: process.env.STATISTICS_LOG_STREAM,
logGroupName: process.env.STATISTICS_LOG_GROUP_NAME,
logStreamName: process.env.STATISTICS_LOG_STREAM_NAME,
logEvents: [{
timestamp: timestamp,
message: JSON.stringify({ timestamp, gemeente, subject, loa }),
message: message,
}],
};
const command = new PutLogEventsCommand(input);
Expand Down

0 comments on commit 260f05e

Please sign in to comment.