mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 09:05:45 -06:00
* WIP first attempt to query variable * regex issue repro demo * Refresh variable on time range change if refresh specified * Instantiate variable runner when updating query variable options * Simplify runners getTarget interface * Fix issue with variable ot being updated correctly after other variable changed * Add templateSrv.replace compatibility with query variable * QueryVariable: use datasource variable as source * use proper format * Make sure variables set is correctly updated when query variable errors * Do not destruct scopedVars when using sceneGraph.interpolate in templateSrv * Add support for Legacy variables (metricFindQuery) * Review * Fix lint * Test: Add unit for datasource by variable * test: Add unit for datasource as var * query: delegate interpolation to datasourceSrv * Cleanup Co-authored-by: Ivan Ortega <ivanortegaalba@gmail.com>
59 lines
1.9 KiB
TypeScript
59 lines
1.9 KiB
TypeScript
import { VariableModel, VariableType } from '@grafana/schema';
|
|
|
|
import { FormatVariable } from '../scenes/variables/interpolation/formatRegistry';
|
|
import { VariableValue } from '../scenes/variables/types';
|
|
import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE } from '../variables/constants';
|
|
|
|
export class LegacyVariableWrapper implements FormatVariable {
|
|
state: { name: string; value: VariableValue; text: VariableValue; type: VariableType };
|
|
|
|
constructor(variable: VariableModel, value: VariableValue, text: VariableValue) {
|
|
this.state = { name: variable.name, value, text, type: variable.type };
|
|
}
|
|
|
|
getValue(_fieldPath: string): VariableValue {
|
|
let { value } = this.state;
|
|
|
|
if (value === 'string' || value === 'number' || value === 'boolean') {
|
|
return value;
|
|
}
|
|
|
|
return String(value);
|
|
}
|
|
|
|
getValueText(): string {
|
|
const { value, text } = this.state;
|
|
|
|
if (typeof text === 'string') {
|
|
return value === ALL_VARIABLE_VALUE ? ALL_VARIABLE_TEXT : text;
|
|
}
|
|
|
|
if (Array.isArray(text)) {
|
|
return text.join(' + ');
|
|
}
|
|
|
|
console.log('value', text);
|
|
return String(text);
|
|
}
|
|
}
|
|
|
|
let legacyVariableWrapper: LegacyVariableWrapper | undefined;
|
|
|
|
/**
|
|
* Reuses a single instance to avoid unnecessary memory allocations
|
|
*/
|
|
export function getVariableWrapper(variable: VariableModel, value: VariableValue, text: VariableValue) {
|
|
// TODO: provide more legacy variable properties, i.e. multi, includeAll that are used in custom interpolators,
|
|
// see Prometheus data source for example
|
|
if (!legacyVariableWrapper) {
|
|
legacyVariableWrapper = new LegacyVariableWrapper(variable, value, text);
|
|
} else {
|
|
legacyVariableWrapper.state.name = variable.name;
|
|
legacyVariableWrapper.state.type = variable.type;
|
|
legacyVariableWrapper.state.value = value;
|
|
legacyVariableWrapper.state.text = text;
|
|
}
|
|
|
|
return legacyVariableWrapper;
|
|
}
|