Skip to content

Commit 888e6a2

Browse files
committed
fix(devices): use new TTL index
1 parent 9bd1f12 commit 888e6a2

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

cdk/resources/DevicesAPI.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export class DevicesAPI extends Construct {
3232
environment: {
3333
PUBLIC_DEVICES_TABLE_NAME: publicDevices.publicDevicesTable.tableName,
3434
PUBLIC_DEVICES_ID_INDEX_NAME: publicDevices.idIndex,
35-
PUBLIC_DEVICES_TABLE_MODEL_OWNER_CONFIRMED_INDEX_NAME:
36-
publicDevices.publicDevicesTableModelOwnerConfirmedIndex,
35+
PUBLIC_DEVICES_TABLE_MODEL_TTL_INDEX_NAME:
36+
publicDevices.publicDevicesTableModelTTLIndex,
3737
},
3838
initialPolicy: [
3939
new IAM.PolicyStatement({

cdk/resources/PublicDevices.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { Construct } from 'constructs'
66
*/
77
export class PublicDevices extends Construct {
88
public readonly publicDevicesTable: DynamoDB.Table
9-
public readonly publicDevicesTableModelOwnerConfirmedIndex =
10-
'modelOwnerConfirmedIndex'
9+
public readonly publicDevicesTableModelTTLIndex = 'modelTTLIndex'
1110
public readonly idIndex = 'idIndex'
1211
constructor(parent: Construct) {
1312
super(parent, 'public-devices')
@@ -38,5 +37,19 @@ export class PublicDevices extends Construct {
3837
},
3938
projectionType: DynamoDB.ProjectionType.KEYS_ONLY,
4039
})
40+
41+
this.publicDevicesTable.addGlobalSecondaryIndex({
42+
indexName: this.publicDevicesTableModelTTLIndex,
43+
partitionKey: {
44+
name: 'model',
45+
type: DynamoDB.AttributeType.STRING,
46+
},
47+
sortKey: {
48+
name: 'ttl',
49+
type: DynamoDB.AttributeType.NUMBER,
50+
},
51+
projectionType: DynamoDB.ProjectionType.INCLUDE,
52+
nonKeyAttributes: ['id', 'deviceId'],
53+
})
4154
}
4255
}

lambda/devicesData.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,14 @@ import type {
2929
APIGatewayProxyResultV2,
3030
Context as LambdaContext,
3131
} from 'aws-lambda'
32-
import { consentDurationMS } from '../devices/consentDuration.js'
3332

34-
const {
35-
publicDevicesTableName,
36-
publicDevicesTableModelOwnerConfirmedIndex,
37-
version,
38-
} = fromEnv({
39-
version: 'VERSION',
40-
publicDevicesTableName: 'PUBLIC_DEVICES_TABLE_NAME',
41-
publicDevicesTableModelOwnerConfirmedIndex:
42-
'PUBLIC_DEVICES_TABLE_MODEL_OWNER_CONFIRMED_INDEX_NAME',
43-
})(process.env)
33+
const { publicDevicesTableName, publicDevicesTablemodelTTLIndex, version } =
34+
fromEnv({
35+
version: 'VERSION',
36+
publicDevicesTableName: 'PUBLIC_DEVICES_TABLE_NAME',
37+
publicDevicesTablemodelTTLIndex:
38+
'PUBLIC_DEVICES_TABLE_MODEL_TTL_INDEX_NAME',
39+
})(process.env)
4440

4541
const db = new DynamoDBClient({})
4642
const iotData = new IoTDataPlaneClient({})
@@ -56,13 +52,12 @@ const h = async (
5652
context: ValidInput<typeof InputSchema> & LambdaContext,
5753
): Promise<APIGatewayProxyResultV2> => {
5854
const devicesToFetch: { id: string; deviceId: string; model: string }[] = []
59-
const minConfirmTime = Date.now() - consentDurationMS
6055

6156
for (const model of Object.keys(models)) {
6257
const queryInput: QueryCommandInput = {
6358
TableName: publicDevicesTableName,
64-
IndexName: publicDevicesTableModelOwnerConfirmedIndex,
65-
KeyConditionExpression: '#model = :model AND #ttl > :minConfirmTime',
59+
IndexName: publicDevicesTablemodelTTLIndex,
60+
KeyConditionExpression: '#model = :model AND #ttl > :now',
6661
ExpressionAttributeNames: {
6762
'#id': 'id',
6863
'#deviceId': 'deviceId',
@@ -71,8 +66,8 @@ const h = async (
7166
},
7267
ExpressionAttributeValues: {
7368
':model': { S: model },
74-
':minConfirmTime': {
75-
N: Math.round(new Date(minConfirmTime).getTime() / 1000).toString(),
69+
':now': {
70+
N: Math.round(new Date().getTime() / 1000).toString(),
7671
},
7772
},
7873
ProjectionExpression: '#id, #deviceId',

0 commit comments

Comments
 (0)