Skip to content

Commit

Permalink
correct toDataFrame output interpretation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andymchugh committed Feb 21, 2024
1 parent 0046e55 commit 3bd59cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/components/FlowPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const FlowPanel: React.FC<Props> = ({ 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);

Expand Down
34 changes: 20 additions & 14 deletions src/components/TimeSeries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,30 @@ export function seriesTransform(series: any[], timeMin: number, timeMax: number)
let tsNamed: Record<string, any> = {};

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<any>(tsNamed)) {
ts.time = tsTime;
timeSeries.set(name, ts);
if (tsTime) {
for (const [name, ts] of Object.entries<any>(tsNamed)) {
ts.time = tsTime;
timeSeries.set(name, ts);
}
}
}
});
Expand Down

0 comments on commit 3bd59cc

Please sign in to comment.