Skip to content

Commit 05159a1

Browse files
committed
Add global secondary index to live activities payload table
* primary key match id and sort key by ttl to allow us to easily find the most recent payload sent for a given match id.
1 parent dae3765 commit 05159a1

2 files changed

Lines changed: 68 additions & 5 deletions

File tree

cdk/lib/__snapshots__/footballnotificationslambda.test.ts.snap

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,34 @@ exports[`The FootballNotificationsLambda stack matches the snapshot for CODE 1`]
151151
"AttributeName": "id",
152152
"AttributeType": "S",
153153
},
154+
{
155+
"AttributeName": "liveActivityID",
156+
"AttributeType": "S",
157+
},
158+
{
159+
"AttributeName": "ttl",
160+
"AttributeType": "N",
161+
},
154162
],
155163
"BillingMode": "PAY_PER_REQUEST",
164+
"GlobalSecondaryIndexes": [
165+
{
166+
"IndexName": "lastPayload-index",
167+
"KeySchema": [
168+
{
169+
"AttributeName": "liveActivityID",
170+
"KeyType": "HASH",
171+
},
172+
{
173+
"AttributeName": "ttl",
174+
"KeyType": "RANGE",
175+
},
176+
],
177+
"Projection": {
178+
"ProjectionType": "ALL",
179+
},
180+
},
181+
],
156182
"KeySchema": [
157183
{
158184
"AttributeName": "id",
@@ -967,8 +993,34 @@ exports[`The FootballNotificationsLambda stack matches the snapshot for PROD 1`]
967993
"AttributeName": "id",
968994
"AttributeType": "S",
969995
},
996+
{
997+
"AttributeName": "liveActivityID",
998+
"AttributeType": "S",
999+
},
1000+
{
1001+
"AttributeName": "ttl",
1002+
"AttributeType": "N",
1003+
},
9701004
],
9711005
"BillingMode": "PAY_PER_REQUEST",
1006+
"GlobalSecondaryIndexes": [
1007+
{
1008+
"IndexName": "lastPayload-index",
1009+
"KeySchema": [
1010+
{
1011+
"AttributeName": "liveActivityID",
1012+
"KeyType": "HASH",
1013+
},
1014+
{
1015+
"AttributeName": "ttl",
1016+
"KeyType": "RANGE",
1017+
},
1018+
],
1019+
"Projection": {
1020+
"ProjectionType": "ALL",
1021+
},
1022+
},
1023+
],
9721024
"KeySchema": [
9731025
{
9741026
"AttributeName": "id",

cdk/lib/footballnotificationslambda.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import {
1010
TreatMissingData,
1111
Unit,
1212
} from 'aws-cdk-lib/aws-cloudwatch';
13-
import { AttributeType, BillingMode, Table } from 'aws-cdk-lib/aws-dynamodb';
13+
import {
14+
AttributeType,
15+
BillingMode,
16+
ProjectionType,
17+
Table,
18+
} from 'aws-cdk-lib/aws-dynamodb';
1419
import { Schedule } from 'aws-cdk-lib/aws-events';
1520
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
1621
import { LoggingFormat, Runtime } from 'aws-cdk-lib/aws-lambda';
@@ -71,8 +76,8 @@ export class FootballNotificationsLambda extends GuStack {
7176
}),
7277
);
7378

79+
// DynamoDB Push Notifications Table
7480
const dynamoTableName = `${stack}-football-notifications-${stage}`;
75-
7681
const dynamoTable = new Table(this, 'DynamoTable', {
7782
tableName: dynamoTableName,
7883
partitionKey: { name: 'notificationId', type: AttributeType.STRING },
@@ -85,13 +90,11 @@ export class FootballNotificationsLambda extends GuStack {
8590
logicalId: 'DynamoTable',
8691
reason: 'Retaining a stateful resource previously defined in YAML',
8792
});
88-
8993
Tags.of(dynamoTable).add('devx-backup-enabled', 'true');
9094

91-
// Live Activities DynamoDB Table
95+
// DynamoDB Live Activities Payloads Table
9296
const liveActivitiesAppName = 'liveactivities';
9397
const liveActivitiesDynamoTableName = `${stack}-${liveActivitiesAppName}-payload-${stage}`;
94-
9598
const liveActivitiesDynamoTable = new Table(
9699
this,
97100
'LiveActivitiesDynamoTable',
@@ -104,6 +107,14 @@ export class FootballNotificationsLambda extends GuStack {
104107
);
105108
Tags.of(liveActivitiesDynamoTable).add('devx-backup-enabled', 'true');
106109

110+
// GSI is used to determine if an untracked state change event has occurred, ie. goal was overruled.
111+
liveActivitiesDynamoTable.addGlobalSecondaryIndex({
112+
indexName: 'lastPayload-index',
113+
partitionKey: { name: 'liveActivityID', type: AttributeType.STRING },
114+
sortKey: { name: 'ttl', type: AttributeType.NUMBER },
115+
projectionType: ProjectionType.ALL,
116+
});
117+
107118
footballnotificationslambda.addToRolePolicy(
108119
new PolicyStatement({
109120
actions: [

0 commit comments

Comments
 (0)