grafana/public/app/features/templating/templateProxies.ts
Torkel Ödegaard 507c6e7d97
Templating: Replace __data , __field and __cell_n with macros (#65324)
* Templating: __data __field and __series macros

* filter out datacontext from json serialization

* Fix condition

* Update

* Added test cases for formatting data, and field macros
2023-04-05 11:10:33 +02:00

38 lines
1.1 KiB
TypeScript

import { Field, DataFrame, getFieldDisplayName, formatLabels } from '@grafana/data';
/**
* 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(
{}, // This object shows up in test snapshots
{
get: (obj, key) => {
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,
__values: Object.values(field.labels).sort().join(', '),
toString: () => {
return formatLabels(field.labels!, '', true);
},
};
}
return undefined;
},
}
);
}