From 3bd59ccd63d5cab432ea53e3c4942415234d27e0 Mon Sep 17 00:00:00 2001 From: Andy McHugh Date: Wed, 21 Feb 2024 21:07:17 +0000 Subject: [PATCH] correct toDataFrame output interpretation After putting timeseries data through toDataFrame time comes out capitalized and the value name is on the frame rather than the field. This change improves that data interpretation safety and correctness. --- src/components/FlowPanel.tsx | 1 + src/components/TimeSeries.tsx | 34 ++++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/components/FlowPanel.tsx b/src/components/FlowPanel.tsx index 5513abd..893c7f5 100644 --- a/src/components/FlowPanel.tsx +++ b/src/components/FlowPanel.tsx @@ -130,6 +130,7 @@ export const FlowPanel: React.FC = ({ options, data, width, height, timeZ const templateSrv = getTemplateSrv(); const timeMin = Number(templateSrv.replace("${__from}")); const timeMax = Number(templateSrv.replace("${__to}")); + const dataFrames = data.series ? data.series.map((item) => toDataFrame(item)) : []; let tsData = seriesTransform(dataFrames, timeMin, timeMax); diff --git a/src/components/TimeSeries.tsx b/src/components/TimeSeries.tsx index 2b220cb..fa80cb1 100644 --- a/src/components/TimeSeries.tsx +++ b/src/components/TimeSeries.tsx @@ -57,24 +57,30 @@ export function seriesTransform(series: any[], timeMin: number, timeMax: number) let tsNamed: Record = {}; frame.fields.forEach(function(ts: any) { - if (ts.name === 'time') { - // The index is stored alongside the ts because it has potential to be shared - // and if so, only has to be calculated once. - tsTime = {valuesIndex: null, values: ts.values}; - if (tsTime.values.length > 0) { - const maxInd = tsTime.values.length - 1; - timeMin = Math.min(timeMin ?? tsTime.values[0], tsTime.values[0]); - timeMax = Math.max(timeMax ?? tsTime.values[maxInd], tsTime.values[maxInd]); + if (typeof ts.name === 'string') { + const nameLwr = ts.name.toLowerCase(); + if (nameLwr === 'time') { + // The index is stored alongside the ts because it has potential to be shared + // and if so, only has to be calculated once. + tsTime = {valuesIndex: null, values: ts.values}; + if (tsTime.values.length > 0) { + const maxInd = tsTime.values.length - 1; + timeMin = Math.min(timeMin ?? tsTime.values[0], tsTime.values[0]); + timeMax = Math.max(timeMax ?? tsTime.values[maxInd], tsTime.values[maxInd]); + } + } + else { + const name = nameLwr === 'value' ? frame.name : ts.name; + tsNamed[name] = {values: ts.values, time: null}; } - } - else { - tsNamed[ts.name] = {values: ts.values, time: null}; } }); // Embed a time shallow copy against each ts in the frame and export to holder - for (const [name, ts] of Object.entries(tsNamed)) { - ts.time = tsTime; - timeSeries.set(name, ts); + if (tsTime) { + for (const [name, ts] of Object.entries(tsNamed)) { + ts.time = tsTime; + timeSeries.set(name, ts); + } } } });