From 40178f6d9e29fba255ad6eee62db17f7201d517f Mon Sep 17 00:00:00 2001 From: Markus Tacker Date: Tue, 19 Mar 2024 16:37:35 +0100 Subject: [PATCH] fix(memfault): fetch list of devices recursively --- lambda/listThingsInGroup.ts | 19 +++++++++++++++++++ lambda/memfault.ts | 13 ++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 lambda/listThingsInGroup.ts diff --git a/lambda/listThingsInGroup.ts b/lambda/listThingsInGroup.ts new file mode 100644 index 0000000..99830ee --- /dev/null +++ b/lambda/listThingsInGroup.ts @@ -0,0 +1,19 @@ +import { IoTClient, ListThingsInThingGroupCommand } from '@aws-sdk/client-iot' + +export const listThingsInGroup = + (iot: IoTClient) => + async ( + groupName: string, + nextToken?: string, + allThings?: Array, + ): Promise> => { + const { things, nextToken: n } = await iot.send( + new ListThingsInThingGroupCommand({ + thingGroupName: groupName, + nextToken, + }), + ) + const t = [...(allThings ?? []), ...(things ?? [])] + if (n === undefined) return t + return listThingsInGroup(iot)(groupName, n, t) + } diff --git a/lambda/memfault.ts b/lambda/memfault.ts index d426ff5..2fa7d0a 100644 --- a/lambda/memfault.ts +++ b/lambda/memfault.ts @@ -1,9 +1,10 @@ -import { IoTClient, ListThingsInThingGroupCommand } from '@aws-sdk/client-iot' +import { IoTClient } from '@aws-sdk/client-iot' import { GetParametersByPathCommand, SSMClient } from '@aws-sdk/client-ssm' import { fromEnv } from '@nordicsemiconductor/from-env' import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3' import { getActiveConnections } from './notifyClients.js' import { DynamoDBClient } from '@aws-sdk/client-dynamodb' +import { listThingsInGroup } from './listThingsInGroup.js' const ssm = new SSMClient({}) const iot = new IoTClient({}) @@ -83,6 +84,8 @@ const api = { }, } +const listThings = listThingsInGroup(iot) + /** * Pull data from Memfault about all devices */ @@ -91,13 +94,9 @@ export const handler = async (): Promise => { console.debug('No active connections.') return } - const { things } = await iot.send( - new ListThingsInThingGroupCommand({ - thingGroupName: nrfAssetTrackerStackName, - }), - ) + const deviceReboots: Record> = {} - for (const thing of things ?? []) { + for (const thing of await listThings(nrfAssetTrackerStackName)) { const reboots = await api.getLastReboots(thing) if (reboots === null) { console.debug(thing, `No data found.`)