Skip to content

Commit

Permalink
feat: parse timeseries into an array of time, value pairs (#33)
Browse files Browse the repository at this point in the history
Co-authored-by: Friedrich Maeckle <maeckle@nantis.de>
  • Loading branch information
coderbyheart and pfried committed Mar 1, 2021
1 parent d4a4689 commit 2cbeac7
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 5 deletions.
113 changes: 113 additions & 0 deletions src/parseResult.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,117 @@ describe('parseResult', () => {
},
])
})
it('parses a Timestream timeseries result into an array of timeseries values', () => {
const timestreamQueryResult = {
ColumnInfo: [
{ Name: 'device', Type: { ScalarType: 'VARCHAR' } },
{ Name: 'measure_name', Type: { ScalarType: 'VARCHAR' } },
{
Name: 'timeseries',
Type: {
TimeSeriesMeasureValueColumnInfo: {
Type: { ScalarType: 'DOUBLE' },
},
},
},
],
QueryId:
'AEDQCAM2RU6BAPTJ5XWYG4DPUBD7MV7CMCQ6OFJO5MJLI5YPWA334EL64ZQM4MA',
Rows: [
{
Data: [
{
ScalarValue: 'Device1',
},
{
ScalarValue: 'speed',
},
{
TimeSeriesValue: [
{
Time: '2021-02-25 09:47:40.000000000',
Value: { ScalarValue: '1.0' },
},
{
Time: '2021-02-25 09:47:50.000000000',
Value: { ScalarValue: '2.0' },
},
{
Time: '2021-02-25 09:48:00.000000000',
Value: { ScalarValue: '3.0' },
},
],
},
],
},
{
Data: [
{
ScalarValue: 'Device2',
},
{
ScalarValue: 'speed',
},
{
TimeSeriesValue: [
{
Time: '2021-02-25 09:47:40.000000000',
Value: { ScalarValue: '4.0' },
},
{
Time: '2021-02-25 09:47:50.000000000',
Value: { ScalarValue: '5.0' },
},
{
Time: '2021-02-25 09:48:00.000000000',
Value: { ScalarValue: '6.0' },
},
],
},
],
},
],
}

const queryResult = parseResult(timestreamQueryResult)

expect(queryResult).toEqual([
{
device: 'Device1',
measure_name: 'speed',
timeseries: [
{
time: new Date('2021-02-25 09:47:40.000000000Z'),
value: 1.0,
},
{
time: new Date('2021-02-25 09:47:50.000000000Z'),
value: 2.0,
},
{
time: new Date('2021-02-25 09:48:00.000000000Z'),
value: 3.0,
},
],
},
{
device: 'Device2',
measure_name: 'speed',
timeseries: [
{
time: new Date('2021-02-25 09:47:40.000000000Z'),
value: 4.0,
},
{
time: new Date('2021-02-25 09:47:50.000000000Z'),
value: 5.0,
},
{
time: new Date('2021-02-25 09:48:00.000000000Z'),
value: 6.0,
},
],
},
])
})
})
50 changes: 45 additions & 5 deletions src/parseResult.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
ScalarType,
Datum,
ColumnInfo,
Datum,
QueryResponse,
ScalarType,
TimeSeriesDataPoint,
} from '@aws-sdk/client-timestream-query'

type ScalarTypes = boolean | Date | number | string | undefined

type TimeSeriesDatum = { time: Date; value: ScalarTypes }

const parseValue = (value: string, type: ScalarType): ScalarTypes => {
switch (type) {
case 'BOOLEAN':
Expand All @@ -32,22 +35,59 @@ const parseValue = (value: string, type: ScalarType): ScalarTypes => {
const parseDatum = (
datum: Datum,
columnInfo: ColumnInfo,
): ScalarTypes | ScalarTypes[] => {
): ScalarTypes | ScalarTypes[] | TimeSeriesDatum[] => {
if (datum.NullValue === true) return undefined
if (datum.ScalarValue !== undefined)
return parseValue(
datum.ScalarValue,
columnInfo.Type?.ScalarType as ScalarType,
)
if (datum.ArrayValue !== undefined) {
const a = datum.ArrayValue.map((d) => {
return datum.ArrayValue.map((d: Datum) => {
if (d.NullValue === true) return undefined
return parseValue(
d.ScalarValue as string,
columnInfo.Type?.ArrayColumnInfo?.Type?.ScalarType as ScalarType,
)
})
return a
}
if (datum.TimeSeriesValue !== undefined) {
return datum.TimeSeriesValue.reduce(
(
result: { time: Date; value: ScalarTypes }[],
timeSeriesDataPoint: TimeSeriesDataPoint,
) => {
if (
timeSeriesDataPoint.Time !== undefined &&
timeSeriesDataPoint.Value !== undefined
) {
const time = parseValue(
timeSeriesDataPoint.Time,
ScalarType.TIMESTAMP,
) as Date

if (columnInfo.Type?.TimeSeriesMeasureValueColumnInfo != undefined) {
result.push({
time,
value: parseDatum(
timeSeriesDataPoint.Value,
columnInfo.Type.TimeSeriesMeasureValueColumnInfo,
) as ScalarType,
})
} else {
if (timeSeriesDataPoint.Value.ScalarValue !== undefined) {
const value = parseValue(
timeSeriesDataPoint.Value.ScalarValue,
ScalarType.DOUBLE,
)
result.push({ time, value })
}
}
}
return result
},
[],
)
}
throw new Error(
`[@nordicsemiconductor/timestream-helper:parseDatum] Unexpected datum: ${JSON.stringify(
Expand Down

0 comments on commit 2cbeac7

Please sign in to comment.