5d58d0aabb
* Don't suggest template strings using fields when there are mutliple dataframes
* Change to use label instead of state
(cherry picked from commit f48a52e590)
Co-authored-by: Oscar Kilhed <oscar.kilhed@grafana.com>
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
import toNumber from 'lodash/toNumber';
|
|
import { DataFrame, DisplayValue, GrafanaTheme, TimeZone } from '../types';
|
|
import { getDisplayProcessor } from './displayProcessor';
|
|
import { formattedValueToString } from '../valueFormats';
|
|
|
|
/**
|
|
*
|
|
* @param frame
|
|
* @param rowIndex
|
|
* @param options
|
|
* @internal
|
|
*/
|
|
export function getFieldDisplayValuesProxy(
|
|
frame: DataFrame,
|
|
rowIndex: number,
|
|
options: {
|
|
theme: GrafanaTheme;
|
|
timeZone?: TimeZone;
|
|
}
|
|
): Record<string, DisplayValue> {
|
|
return new Proxy({} as Record<string, DisplayValue>, {
|
|
get: (obj: any, key: string) => {
|
|
// 1. Match the name
|
|
let field = frame.fields.find((f) => key === f.name);
|
|
if (!field) {
|
|
// 2. Match the array index
|
|
const k = toNumber(key);
|
|
field = frame.fields[k];
|
|
}
|
|
if (!field) {
|
|
// 3. Match the config displayName
|
|
field = frame.fields.find((f) => key === f.config.displayName);
|
|
}
|
|
if (!field) {
|
|
// 4. Match the name label
|
|
field = frame.fields.find((f) => {
|
|
if (f.labels) {
|
|
return key === f.labels.name;
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
if (!field) {
|
|
return undefined;
|
|
}
|
|
if (!field.display) {
|
|
// Lazy load the display processor
|
|
field.display = getDisplayProcessor({
|
|
field,
|
|
theme: options.theme,
|
|
timeZone: options.timeZone,
|
|
});
|
|
}
|
|
const raw = field.values.get(rowIndex);
|
|
const disp = field.display(raw);
|
|
disp.toString = () => formattedValueToString(disp);
|
|
return disp;
|
|
},
|
|
});
|
|
}
|