Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Typescript timestream parser
/**
* This file is based on the example from here: https://github.com/awslabs/amazon-timestream-tools/blob/master/sample_apps/js/query-example.js
*/
import TimestreamQuery = require('aws-sdk//clients/timestreamquery')
import { QueryResponse } from 'aws-sdk/clients/timestreamquery';
type Datum = TimestreamQuery.Datum;
type DatumList = TimestreamQuery.DatumList;
type ColumnInfo = TimestreamQuery.ColumnInfo;
interface TimestreamRow {
[key: string]: string;
}
const parseQueryResult = (queryResult: QueryResponse):Array<TimestreamRow> => {
const columnInfo = queryResult.ColumnInfo;
const rows = queryResult.Rows;
const data:Array<TimestreamRow> = [];
rows.forEach(function (row) {
data.push(parseRow(columnInfo, row));
});
return data;
}
const parseRow = (columnInfo:TimestreamQuery.ColumnInfoList, row:TimestreamQuery.Row|undefined) => {
let rowObject = {};
if (row !== undefined ) {
const data = row.Data;
// Join all columns in the row into one object.
for ( let i = 0; i < data.length; i++ ) {
const datum = parseDatum(columnInfo[i], data[i]);
rowObject = {...rowObject, ...datum};
}
}
return rowObject;
}
function parseDatum(info:TimestreamQuery.ColumnInfo, datum:Datum):Record<string, unknown> {
const columnName = parseColumnName(info);
if ('NullValue' in datum && datum.NullValue === true) {
return {[columnName]: null}
}
const columnType = info.Type;
// If the column is of TimeSeries Type
if (columnType.TimeSeriesMeasureValueColumnInfo !== undefined) {
return parseTimeSeries(columnName, datum);
}
// If the column is of Array Type
if (columnType.ArrayColumnInfo !== undefined) {
const arrayValues = datum.ArrayValue;
if (info.Type.ArrayColumnInfo && arrayValues) {
return {
[columnName]: parseArray(info.Type.ArrayColumnInfo, arrayValues)
}
}
}
// If the column is of Row Type
if (columnType.RowColumnInfo !== undefined) {
const rowColumnInfo = columnType.RowColumnInfo;
const rowValues = datum.RowValue;
return parseRow(rowColumnInfo, rowValues);
}
// Then we must have a Scalar
return parseScalarType(columnName, datum);
}
function parseTimeSeries(columnName:string, datum:Datum) {
const returnVal = {
[columnName]: ''
}
if (datum.TimeSeriesValue) {
returnVal[columnName] = datum.TimeSeriesValue[0].Value.ScalarValue ?? ''
}
return returnVal;
}
function parseScalarType(columnName:string, datum:Datum) {
const value = datum.ScalarValue;
return {
[columnName]: value
};
}
function parseColumnName(info:ColumnInfo) {
return info.Name == null ? "" : `${info.Name}`;
}
function parseArray(arrayColumnInfo:TimestreamQuery.ColumnInfo, arrayValues:DatumList) {
const arrayOutput:Record<string, unknown>[] = [];
arrayValues.forEach(function (datum) {
arrayOutput.push(parseDatum(arrayColumnInfo, datum));
});
return arrayOutput;
}
export { TimestreamRow, parseQueryResult }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment