Skip to content

Commit

Permalink
fix(memfault): fetch list of devices recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Mar 19, 2024
1 parent 870a234 commit 40178f6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
19 changes: 19 additions & 0 deletions lambda/listThingsInGroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IoTClient, ListThingsInThingGroupCommand } from '@aws-sdk/client-iot'

export const listThingsInGroup =
(iot: IoTClient) =>
async (
groupName: string,
nextToken?: string,
allThings?: Array<string>,
): Promise<Array<string>> => {
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)
}
13 changes: 6 additions & 7 deletions lambda/memfault.ts
Original file line number Diff line number Diff line change
@@ -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({})
Expand Down Expand Up @@ -83,6 +84,8 @@ const api = {
},
}

const listThings = listThingsInGroup(iot)

/**
* Pull data from Memfault about all devices
*/
Expand All @@ -91,13 +94,9 @@ export const handler = async (): Promise<void> => {
console.debug('No active connections.')
return
}
const { things } = await iot.send(
new ListThingsInThingGroupCommand({
thingGroupName: nrfAssetTrackerStackName,
}),
)

const deviceReboots: Record<string, Array<Reboot>> = {}
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.`)
Expand Down

0 comments on commit 40178f6

Please sign in to comment.