-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add toRecord() and toTimestreamType()
- Loading branch information
1 parent
c70bd2c
commit 0d3c845
Showing
7 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export * from './parseResult' | ||
export * from './client' | ||
export * from './index' | ||
export * from './parseResult' | ||
export * from './toRecord' | ||
export * from './toTimestreamType' |
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,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) | ||
}) | ||
}) |
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,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(), | ||
} | ||
} |
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,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) | ||
}) | ||
}) |
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,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 | ||
} |