-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(devices): add API to query for senML import logs
- Loading branch information
1 parent
170a1df
commit f7dad8a
Showing
13 changed files
with
390 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { DynamoDBClient } from '@aws-sdk/client-dynamodb' | ||
import { models } from '@hello.nrfcloud.com/proto-map' | ||
import chalk from 'chalk' | ||
import { publicDevicesRepo } from '../../sharing/publicDevicesRepo.js' | ||
import type { CommandDefinition } from './CommandDefinition.js' | ||
|
||
const modelIDs = Object.keys(models) | ||
|
||
export const shareDevice = ({ | ||
db, | ||
publicDevicesTableName, | ||
}: { | ||
db: DynamoDBClient | ||
publicDevicesTableName: string | ||
}): CommandDefinition => ({ | ||
command: `share-device <deviceId> <model> <email>`, | ||
action: async (deviceId, model, email) => { | ||
console.log(publicDevicesTableName) | ||
if (!modelIDs.includes(model)) | ||
throw new Error( | ||
`Unknown model ${model}. Known models are ${modelIDs.join(', ')}.`, | ||
) | ||
if (!/.+@.+/.test(email)) { | ||
throw new Error(`Must provide valid email.`) | ||
} | ||
console.debug(chalk.yellow('Device ID:'), chalk.blue(deviceId)) | ||
const publicDevice = publicDevicesRepo({ | ||
db, | ||
TableName: publicDevicesTableName, | ||
}) | ||
const maybePublished = await publicDevice.share({ | ||
deviceId, | ||
model, | ||
email, | ||
confirmed: true, | ||
}) | ||
if ('error' in maybePublished) { | ||
console.error(maybePublished.error) | ||
throw new Error(`Failed to share device.`) | ||
} | ||
}, | ||
help: 'Shares an existing device to be shown on the map', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { | ||
CloudWatchLogsClient, | ||
GetQueryResultsCommand, | ||
QueryStatus, | ||
StartQueryCommand, | ||
type ResultField, | ||
} from '@aws-sdk/client-cloudwatch-logs' | ||
import { aProblem } from '@hello.nrfcloud.com/lambda-helpers/aProblem' | ||
import { aResponse } from '@hello.nrfcloud.com/lambda-helpers/aResponse' | ||
import { addVersionHeader } from '@hello.nrfcloud.com/lambda-helpers/addVersionHeader' | ||
import { corsOPTIONS } from '@hello.nrfcloud.com/lambda-helpers/corsOPTIONS' | ||
import { | ||
formatTypeBoxErrors, | ||
validateWithTypeBox, | ||
} from '@hello.nrfcloud.com/proto' | ||
import { Context, DeviceId } from '@hello.nrfcloud.com/proto-map/api' | ||
import middy from '@middy/core' | ||
import { fromEnv } from '@nordicsemiconductor/from-env' | ||
import { Type } from '@sinclair/typebox' | ||
import type { | ||
APIGatewayProxyEventV2, | ||
APIGatewayProxyResultV2, | ||
} from 'aws-lambda' | ||
import pRetry from 'p-retry' | ||
|
||
const { importLogGroupName, version } = fromEnv({ | ||
importLogGroupName: 'IMPORT_LOGGROUP_NAME', | ||
version: 'VERSION', | ||
})(process.env) | ||
|
||
const logs = new CloudWatchLogsClient({}) | ||
|
||
const validateInput = validateWithTypeBox( | ||
Type.Object({ | ||
id: DeviceId, | ||
}), | ||
) | ||
|
||
const h = async ( | ||
event: APIGatewayProxyEventV2, | ||
): Promise<APIGatewayProxyResultV2> => { | ||
const maybeValidQuery = validateInput(event.pathParameters) | ||
|
||
if ('errors' in maybeValidQuery) { | ||
return aProblem({ | ||
title: 'Validation failed', | ||
status: 400, | ||
detail: formatTypeBoxErrors(maybeValidQuery.errors), | ||
}) | ||
} | ||
|
||
const queryString = `filter @logStream LIKE '${maybeValidQuery.value.id}-' | ||
| fields @timestamp, @message | ||
| sort @timestamp desc | ||
| limit 100` | ||
const { queryId } = await logs.send( | ||
new StartQueryCommand({ | ||
logGroupName: importLogGroupName, | ||
queryString, | ||
startTime: Date.now() - 24 * 60 * 60 * 1000, | ||
endTime: Date.now(), | ||
}), | ||
) | ||
console.debug({ queryId, queryString }) | ||
|
||
const results = await pRetry( | ||
async () => { | ||
const result = await logs.send( | ||
new GetQueryResultsCommand({ | ||
queryId, | ||
}), | ||
) | ||
switch (result.status) { | ||
case QueryStatus.Cancelled: | ||
return [] | ||
case QueryStatus.Complete: | ||
return result.results | ||
case QueryStatus.Failed: | ||
console.error(`Query failed!`) | ||
return [] | ||
case QueryStatus.Timeout: | ||
console.error(`Query timed out!`) | ||
return [] | ||
case QueryStatus.Running: | ||
case QueryStatus.Scheduled: | ||
throw new Error(`Running!`) | ||
case QueryStatus.Unknown: | ||
default: | ||
console.debug('Unknown query status.') | ||
return [] | ||
} | ||
}, | ||
{ | ||
factor: 1, | ||
minTimeout: 1000, | ||
retries: 10, | ||
}, | ||
) | ||
|
||
return aResponse( | ||
200, | ||
{ | ||
'@context': Context.named('senml-import-logs'), | ||
results: (results ?? []).map((fields) => { | ||
const result = JSON.parse((fields[1] as ResultField).value as string) | ||
return { | ||
...result, | ||
ts: new Date( | ||
(fields[0] as ResultField).value as string, | ||
).toISOString(), | ||
} | ||
}), | ||
}, | ||
60, | ||
) | ||
} | ||
|
||
export const handler = middy() | ||
.use(addVersionHeader(version)) | ||
.use(corsOPTIONS('GET')) | ||
.handler(h) |
Oops, something went wrong.