Skip to content

Commit

Permalink
feat: query dynamodb for statistics in report
Browse files Browse the repository at this point in the history
  • Loading branch information
marnixdessing committed Oct 31, 2022
1 parent 87d528e commit bdf6533
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/report/reportRequestHandler.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -19,6 +34,21 @@ export async function handleReporting(sesClient: SESClient, dynamoDBClient: Dyna

}

function createCommand(ytd: string, lastEvaluatedKey?: Record<string, AttributeValue>) {
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);
}
Expand Down

0 comments on commit bdf6533

Please sign in to comment.