diff --git a/src/report/reportRequestHandler.ts b/src/report/reportRequestHandler.ts index 46eb9698..26e2182d 100644 --- a/src/report/reportRequestHandler.ts +++ b/src/report/reportRequestHandler.ts @@ -1,11 +1,26 @@ -import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; +import { AttributeValue, DynamoDBClient, ScanCommand } from '@aws-sdk/client-dynamodb'; import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses'; import Mustache from 'mustache'; import template from './report.mustache'; export async function handleReporting(sesClient: SESClient, dynamoDBClient: DynamoDBClient, recipients: string[]) { - console.log(dynamoDBClient.config.apiVersion); + const ytd = new Date().setFullYear(new Date().getFullYear() - 1).toString(); + + let query = createCommand(ytd); + let result = await dynamoDBClient.send(query); + + console.debug('Updating statistics with new data', JSON.stringify(result, null, 4)); + + while (result.LastEvaluatedKey) { + // Next query calculate further statistics + query = createCommand(ytd, result.LastEvaluatedKey); + result = await dynamoDBClient.send(query); + + console.debug('Updating statistics with new data', JSON.stringify(result, null, 4)); + } + + // Do dynamodb query (yesterday) // Do dynamodb query (this month) // Do dynamodb query (this year) @@ -19,6 +34,21 @@ export async function handleReporting(sesClient: SESClient, dynamoDBClient: Dyna } +function createCommand(ytd: string, lastEvaluatedKey?: Record) { + return new ScanCommand({ + TableName: process.env.STATISTICS_TABLE, + ExclusiveStartKey: lastEvaluatedKey, + ScanFilter: { + timestamp: { + ComparisonOperator: 'GT', + AttributeValueList: [ + { N: ytd.toString() }, + ], + }, + }, + }); +} + function renderReport(data: any) { return Mustache.render(template, data); }