2023-04-05 11:10:33 +02:00
|
|
|
import { Field, DataFrame, getFieldDisplayName, formatLabels } from '@grafana/data';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2020-08-04 21:22:14 -07:00
|
|
|
/**
|
|
|
|
|
* This object is created often, and only used when tmplates exist. Using a proxy lets us delay
|
|
|
|
|
* calculations of the more complex structures (label names) until they are actually used
|
|
|
|
|
*/
|
|
|
|
|
export function getTemplateProxyForField(field: Field, frame?: DataFrame, frames?: DataFrame[]): any {
|
|
|
|
|
return new Proxy(
|
2022-12-16 09:59:00 +00:00
|
|
|
{}, // This object shows up in test snapshots
|
2020-08-04 21:22:14 -07:00
|
|
|
{
|
2022-12-16 09:59:00 +00:00
|
|
|
get: (obj, key) => {
|
2020-08-04 21:22:14 -07:00
|
|
|
if (key === 'name') {
|
|
|
|
|
return field.name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (key === 'displayName') {
|
|
|
|
|
return getFieldDisplayName(field, frame, frames);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (key === 'labels' || key === 'formattedLabels') {
|
|
|
|
|
// formattedLabels deprecated
|
|
|
|
|
if (!field.labels) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...field.labels,
|
2021-01-20 07:59:48 +01:00
|
|
|
__values: Object.values(field.labels).sort().join(', '),
|
2020-08-04 21:22:14 -07:00
|
|
|
toString: () => {
|
|
|
|
|
return formatLabels(field.labels!, '', true);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-12-16 09:59:00 +00:00
|
|
|
return undefined;
|
2020-08-04 21:22:14 -07:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|