From e9e718648bb7c60c056692c9c3e211a1120ae009 Mon Sep 17 00:00:00 2001 From: Doug Martin Date: Thu, 11 Jul 2024 08:51:56 -0400 Subject: [PATCH] feat: Add timeseries units to csv output [PT-187816321] This adds the sensor units, if present, to the csv header. --- src/lara-app/utils/download-csv.test.ts | 12 ++++++------ src/lara-app/utils/download-csv.ts | 11 ++++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/lara-app/utils/download-csv.test.ts b/src/lara-app/utils/download-csv.test.ts index 054214c..0a026c3 100644 --- a/src/lara-app/utils/download-csv.test.ts +++ b/src/lara-app/utils/download-csv.test.ts @@ -83,8 +83,8 @@ const timeSeriesExperiment: IExperiment = { const timeSeriesData: IExperimentData = { timestamp: 987654321, experimentData: [ - {[TimeSeriesDataKey]: [1, 2], [TimeSeriesMetadataKey]: { measurementPeriod: 1000}, label: "First Label", extraData: "one"}, - {[TimeSeriesDataKey]: [3, 4], [TimeSeriesMetadataKey]: { measurementPeriod: 2000}, label: "Second Label", extraData: "two"} + {[TimeSeriesDataKey]: [1, 2], [TimeSeriesMetadataKey]: { measurementPeriod: 1000, units: "N"}, label: "First Label", extraData: "one"}, + {[TimeSeriesDataKey]: [3, 4], [TimeSeriesMetadataKey]: { measurementPeriod: 2000, /* no units here intentionally */}, label: "Second Label", extraData: "two"} ] }; @@ -130,21 +130,21 @@ describe("download csv functions", () => { expect(getRows(timeSeriesExperiment, timeSeriesData)).toStrictEqual([ { "Time": "0", - "First Label": "1", - "Second Label": "3", + "First Label (N)": "1", + "Second Label": "3", // no units for second timeseries so no unit should show "Row 1 Extra Data": "one", "Row 2 Extra Data": "two", }, { "Time": "1", - "First Label": "2", + "First Label (N)": "2", "Second Label": "", "Row 1 Extra Data": "one", "Row 2 Extra Data": "two", }, { "Time": "2", - "First Label": "", + "First Label (N)": "", "Second Label": "4", "Row 1 Extra Data": "one", "Row 2 Extra Data": "two", diff --git a/src/lara-app/utils/download-csv.ts b/src/lara-app/utils/download-csv.ts index a33cccb..a0aa403 100644 --- a/src/lara-app/utils/download-csv.ts +++ b/src/lara-app/utils/download-csv.ts @@ -7,7 +7,7 @@ import { ITimeSeriesMetadata, TimeSeriesDataKey, TimeSeriesMetadataKey } from ". type TimeValues = Record; type OtherValues = Record; -type TimeSeriesRow = {timeValues: TimeValues, otherValues: OtherValues}; +type TimeSeriesRow = {timeValues: TimeValues, units: string, otherValues: OtherValues}; export const getTimeKey = (time: number) => time.toFixed(3).replace(/\.?0+$/, ""); @@ -54,7 +54,7 @@ export const getRows = (experiment: IExperiment, data: IExperimentData) => { }); const timeSeries = (rawRow[TimeSeriesDataKey] ?? []) as number[]; - const {measurementPeriod} = (rawRow[TimeSeriesMetadataKey] ?? {measurementPeriod: 0}) as ITimeSeriesMetadata; + const {measurementPeriod, units} = (rawRow[TimeSeriesMetadataKey] ?? {measurementPeriod: 0, units: ""}) as ITimeSeriesMetadata; const timeDelta = measurementPeriod / 1000; timeSeries.forEach((value, index) => { const time = index * timeDelta; @@ -63,7 +63,7 @@ export const getRows = (experiment: IExperiment, data: IExperimentData) => { timeKeys.add(timeKey); }); - timeSeriesRows.push({timeValues, otherValues}); + timeSeriesRows.push({timeValues, units, otherValues}); }, []); const sortedTimeKeys = sortNaturally(Array.from(timeKeys)); @@ -72,9 +72,10 @@ export const getRows = (experiment: IExperiment, data: IExperimentData) => { const timeSeriesKey = titleMap[TimeSeriesDataKey] ?? "Time Series Value"; sortedTimeKeys.forEach(timeKey => { const row: Record = {Time: timeKey}; - timeSeriesRows.forEach(({timeValues}, rowIndex) => { + timeSeriesRows.forEach(({timeValues, units}, rowIndex) => { const rowKey = ((timeSeriesLabelKey && rawRows[rowIndex][timeSeriesLabelKey]) as string|undefined) || `Row ${rowIndex + 1} ${timeSeriesKey}`; - row[rowKey] = timeValues[timeKey] ?? ""; + const unitsSuffix = units ? ` (${units})` : ""; + row[`${rowKey}${unitsSuffix}`] = timeValues[timeKey] ?? ""; }); timeSeriesRows.forEach(({otherValues}, rowIndex) => { nonTimeSeriesTitles.forEach(key => {