From 6072942def2566821d6fc05f6a355f788535bcfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mark=20Lis=C3=A9?= Date: Mon, 31 Oct 2022 13:29:28 -0700 Subject: [PATCH] BRS-815: Improve metrics endoint API call. (#184) --- lambda/metric/index.js | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/lambda/metric/index.js b/lambda/metric/index.js index 27466a51..72453abd 100644 --- a/lambda/metric/index.js +++ b/lambda/metric/index.js @@ -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'); @@ -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'); @@ -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; -} \ No newline at end of file