Skip to content

Commit

Permalink
feat: add toRecord() and toTimestreamType()
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Feb 11, 2022
1 parent c70bd2c commit 0d3c845
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 3 deletions.
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"license": "BSD-3-Clause",
"peerDependencies": {
"@aws-sdk/client-timestream-query": ">= 3 < 4",
"@aws-sdk/client-timestream-write": ">= 3 < 4"
"@aws-sdk/client-timestream-write": ">= 3 < 4",
"@types/uuid": ">= 8 < 9",
"uuid": ">= 8 < 9"
},
"devDependencies": {
"@nordicsemiconductor/asset-tracker-cloud-code-style": "11.0.21",
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './parseResult'
export * from './client'
export * from './index'
export * from './parseResult'
export * from './toRecord'
export * from './toTimestreamType'
37 changes: 37 additions & 0 deletions src/toRecord.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { MeasureValueType } from '@aws-sdk/client-timestream-write'
import { toRecord } from '.'

describe('toRecord()', () => {
it('should convert sensor data to a timestream record', () => {
const ts = Date.now()
expect(
toRecord({
name: 'bat',
v: 3.3,
ts,
dimensions: { measureGroup: 'someGroup' },
}),
).toEqual({
Dimensions: [
{
Name: 'measureGroup',
Value: 'someGroup',
},
],
MeasureName: 'bat',
MeasureValue: `3.3`,
MeasureValueType: MeasureValueType.DOUBLE,
Time: ts.toString(),
})
})
it('should ignore sensor data if the value is not defined', () => {
expect(
toRecord({
name: 'bat',
v: undefined,
ts: Date.now(),
dimensions: { measureGroup: 'someGroup' },
}),
).toEqual(undefined)
})
})
26 changes: 26 additions & 0 deletions src/toRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { _Record } from '@aws-sdk/client-timestream-write'
import { toTimestreamType } from './toTimestreamType'

export const toRecord = ({
name,
ts,
v,
dimensions,
}: {
name: string
ts: number
v?: { toString: () => string }
dimensions?: Record<string, string>
}): _Record | undefined => {
if (v === undefined) return
return {
Dimensions: Object.entries(dimensions ?? {}).map(([Name, Value]) => ({
Name,
Value,
})),
MeasureName: name,
MeasureValue: v.toString(),
MeasureValueType: toTimestreamType(v),
Time: ts.toString(),
}
}
13 changes: 13 additions & 0 deletions src/toTimestreamType.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { toTimestreamType } from './toTimestreamType'

describe('toTimestreamType', () => {
it.each([
[true, 'BOOLEAN'],
[1.1, 'DOUBLE'],
[1, 'DOUBLE'],
['foo', 'VARCHAR'],
['12345678901234567890', 'VARCHAR'],
])('should determind %s as %s', (v, expected) => {
expect(toTimestreamType(v)).toEqual(expected)
})
})
8 changes: 8 additions & 0 deletions src/toTimestreamType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { MeasureValueType } from '@aws-sdk/client-timestream-write'

export const toTimestreamType = (v: unknown): MeasureValueType => {
if (typeof v === 'string') return MeasureValueType.VARCHAR
if (typeof v === 'boolean') return MeasureValueType.BOOLEAN
if (Number.isInteger(v)) return MeasureValueType.DOUBLE
return MeasureValueType.DOUBLE
}

0 comments on commit 0d3c845

Please sign in to comment.