diff --git a/packages/grafana-ui/src/utils/singlestat.ts b/packages/grafana-ui/src/utils/singlestat.ts new file mode 100644 index 00000000000..336190c0a94 --- /dev/null +++ b/packages/grafana-ui/src/utils/singlestat.ts @@ -0,0 +1,30 @@ +import { PanelData, NullValueMode, SingleStatValueInfo } from '../types'; +import { processTimeSeries } from './processTimeSeries'; + +export interface SingleStatProcessingOptions { + panelData: PanelData; + stat: string; +} + +export function processSingleStatPanelData(options: SingleStatProcessingOptions): SingleStatValueInfo[] { + const { panelData, stat } = options; + + if (panelData.timeSeries) { + const timeSeries = processTimeSeries({ + timeSeries: panelData.timeSeries, + nullValueMode: NullValueMode.Null, + }); + + return timeSeries.map((series, index) => { + const value = stat !== 'name' ? series.stats[stat] : series.label; + + return { + value: value, + }; + }); + } else if (panelData.tableData) { + throw { message: 'Panel data not supported' }; + } + + return []; +}