mirror of
https://github.com/grafana/grafana.git
synced 2025-02-15 10:03:33 -06:00
Add new default instant query UI option for prometheus users in Explore. Co-authored-by: Beto Muniz <contato@betomuniz.com> Co-authored-by: Giordano Ricci <me@giordanoricci.com>
61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
import { DataFrame, formattedValueToString } from '@grafana/data/src';
|
|
|
|
import { instantQueryRawVirtualizedListData } from '../PrometheusListView/RawListContainer';
|
|
|
|
type instantQueryMetricList = { [index: string]: { [index: string]: instantQueryRawVirtualizedListData } };
|
|
|
|
export const RawPrometheusListItemEmptyValue = ' ';
|
|
|
|
/**
|
|
* transform dataFrame to instantQueryRawVirtualizedListData
|
|
* @param dataFrame
|
|
*/
|
|
export const getRawPrometheusListItemsFromDataFrame = (dataFrame: DataFrame): instantQueryRawVirtualizedListData[] => {
|
|
const metricList: instantQueryMetricList = {};
|
|
const outputList: instantQueryRawVirtualizedListData[] = [];
|
|
|
|
// Filter out time
|
|
const newFields = dataFrame.fields.filter((field) => !['Time'].includes(field.name));
|
|
|
|
// Get name from each series
|
|
let metricNames: string[] = newFields.find((field) => field.name === '__name__')?.values.toArray() ?? [];
|
|
if (!metricNames.length && newFields.length && newFields[0].values.length) {
|
|
// These results do not have series labels
|
|
// Matching the native prometheus UI which appears to only show the permutations of the first field in the query result.
|
|
metricNames = Array(newFields[0].values.length).fill('');
|
|
}
|
|
|
|
// Get everything that isn't the name from each series
|
|
const metricLabels = dataFrame.fields.filter((field) => !['__name__'].includes(field.name));
|
|
|
|
metricNames.forEach(function (metric: string, i: number) {
|
|
metricList[metric] = {};
|
|
const formattedMetric: instantQueryRawVirtualizedListData = metricList[metric][i] ?? {};
|
|
|
|
for (const field of metricLabels) {
|
|
const label = field.name;
|
|
|
|
if (label !== 'Time') {
|
|
// Initialize the objects
|
|
if (typeof field?.display === 'function') {
|
|
const stringValue = formattedValueToString(field?.display(field.values.get(i)));
|
|
if (stringValue) {
|
|
formattedMetric[label] = stringValue;
|
|
} else if (label.includes('Value #')) {
|
|
formattedMetric[label] = RawPrometheusListItemEmptyValue;
|
|
}
|
|
} else {
|
|
console.warn('Field display method is missing!');
|
|
}
|
|
}
|
|
}
|
|
|
|
outputList.push({
|
|
...formattedMetric,
|
|
__name__: metric,
|
|
});
|
|
});
|
|
|
|
return outputList;
|
|
};
|