Skip to content

Commit

Permalink
BRS-815: Improve metrics endoint API call. (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
marklise authored Oct 31, 2022
1 parent e5c6392 commit 6072942
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions lambda/metric/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const AWS = require('aws-sdk');
const { runScan, TABLE_NAME } = require('../dynamoUtil');
const { getPassesByStatus, TABLE_NAME } = require('../dynamoUtil');
const { sendResponse } = require('../responseUtil');
const { decodeJWT, resolvePermissions } = require('../permissionUtil');
const { logger } = require('../logger');
Expand All @@ -25,21 +25,24 @@ exports.handler = async (event, context) => {

if (event.queryStringParameters.type == 'passTotals') {
// Get all the passes for a specific facility
const cancelled = await getPassNumbers(queryObj, 'cancelled');

const cancelled = await getPassesByStatus('cancelled');
logger.debug("cancelled:", cancelled.length)
const active = await getPassNumbers(queryObj, 'active');

const active = await getPassesByStatus('active');
logger.debug("active:", active.length)
const reserved = await getPassNumbers(queryObj, 'reserved');

const reserved = await getPassesByStatus('reserved');
logger.debug("reserved:", reserved.length)
const expired = await getPassNumbers(queryObj, 'expired');

const expired = await getPassesByStatus('expired');
logger.debug("expired:", expired.length)

return sendResponse(200, {
cancelled: cancelled.length,
active: active.length,
reserved: reserved.length,
expired: expired.length,
expired: expired.length
});
} else {
logger.error('Invalid Request');
Expand All @@ -50,20 +53,3 @@ exports.handler = async (event, context) => {
return sendResponse(400, err, context);
}
};

async function getPassNumbers(queryObj, status) {
queryObj.ExpressionAttributeValues = {};
queryObj.ExpressionAttributeValues[':ending'] = { S: 'pass::' };
queryObj.ExpressionAttributeValues[':passStatus'] = { S: status };
queryObj.FilterExpression = 'begins_with(pk, :ending) and passStatus=:passStatus';

logger.debug('queryObj:', queryObj);
let scanResults = [];
do {
passData = await runScan(queryObj, true);
passData.data.forEach((item) => scanResults.push(item));
queryObj.ExclusiveStartKey = passData.LastEvaluatedKey;
} while(typeof passData.LastEvaluatedKey !== "undefined");

return scanResults;
}

0 comments on commit 6072942

Please sign in to comment.