PanelData: move preProcessPanelData to @grafana/data (#63743)

This commit is contained in:
Torkel Ödegaard
2023-03-06 10:59:47 +01:00
committed by GitHub
parent 15fb7e99d9
commit 4bf23b7f5f
6 changed files with 42 additions and 63 deletions

View File

@@ -20,6 +20,8 @@ import {
TIME_SERIES_VALUE_FIELD_NAME,
TIME_SERIES_TIME_FIELD_NAME,
DataQueryResponseData,
PanelData,
LoadingState,
} from '../types/index';
import { ArrayVector } from '../vector/ArrayVector';
import { SortedVector } from '../vector/SortedVector';
@@ -537,3 +539,37 @@ export function getProcessedDataFrames(results?: DataQueryResponseData[]): DataF
return results.map((data) => getProcessedDataFrame(data));
}
/**
* Will process the panel data frames and in case of loading state with no data, will return the last result data but with loading state
* This is to have panels not flicker temporarily with "no data" while loading
*/
export function preProcessPanelData(data: PanelData, lastResult?: PanelData): PanelData {
const { series, annotations } = data;
// for loading states with no data, use last result
if (data.state === LoadingState.Loading && series.length === 0) {
if (!lastResult) {
lastResult = data;
}
return {
...lastResult,
state: LoadingState.Loading,
request: data.request,
};
}
// Make sure the data frames are properly formatted
const STARTTIME = performance.now();
const processedDataFrames = series.map((data) => getProcessedDataFrame(data));
const annotationsProcessed = getProcessedDataFrames(annotations);
const STOPTIME = performance.now();
return {
...data,
series: processedDataFrames,
annotations: annotationsProcessed,
timings: { dataProcessingTime: STOPTIME - STARTTIME },
};
}