Skip to content

Commit

Permalink
Merge pull request #164 from concord-consortium/187816321-add-units-t…
Browse files Browse the repository at this point in the history
…o-csv-download

feat: Add timeseries units to csv output [PT-187816321]
  • Loading branch information
dougmartin authored Jul 11, 2024
2 parents 9c023fc + e9e7186 commit e8d863d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/lara-app/utils/download-csv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
]
};

Expand Down Expand Up @@ -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",
Expand Down
11 changes: 6 additions & 5 deletions src/lara-app/utils/download-csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ITimeSeriesMetadata, TimeSeriesDataKey, TimeSeriesMetadataKey } from ".

type TimeValues = Record<string, string|undefined>;
type OtherValues = Record<string, any>;
type TimeSeriesRow = {timeValues: TimeValues, otherValues: OtherValues};
type TimeSeriesRow = {timeValues: TimeValues, units: string, otherValues: OtherValues};

export const getTimeKey = (time: number) => time.toFixed(3).replace(/\.?0+$/, "");

Expand Down Expand Up @@ -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;
Expand All @@ -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));

Expand All @@ -72,9 +72,10 @@ export const getRows = (experiment: IExperiment, data: IExperimentData) => {
const timeSeriesKey = titleMap[TimeSeriesDataKey] ?? "Time Series Value";
sortedTimeKeys.forEach(timeKey => {
const row: Record<string, any> = {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 => {
Expand Down

0 comments on commit e8d863d

Please sign in to comment.