Skip to content

Commit

Permalink
refactor(grafana): use native variable types (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod authored Feb 19, 2023
1 parent 727a740 commit ff6e193
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
44 changes: 19 additions & 25 deletions src/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
ScopedVars,
SelectableValue,
toDataFrame,
VariableOption,
VariableWithMultiSupport,
} from '@grafana/data';
import {
BackendDataSourceResponse,
Expand All @@ -14,7 +16,7 @@ import {
getTemplateSrv,
BackendSrvRequest,
} from '@grafana/runtime';
import { isArray, isEqual, isObject } from 'lodash';
import { isArray, isObject } from 'lodash';
import { lastValueFrom, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { ResponseParser } from './response_parser';
Expand All @@ -25,14 +27,12 @@ import {
MetricFindTagKeys,
MetricFindTagValues,
MetricPayloadConfig,
MultiValueVariable,
QueryEditorMode,
QueryRequest,
TextValuePair,
VariableQuery,
} from './types';

const supportedVariableTypes = ['adhoc', 'constant', 'custom', 'interval', 'query', 'textbox'];
import { match, P } from 'ts-pattern';
import { valueFromVariableWithMultiSupport } from './variable/valueFromVariableWithMultiSupport';

export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
url: string;
Expand Down Expand Up @@ -344,36 +344,30 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
}

getVariables() {
const variables: { [id: string]: TextValuePair } = {};
Object.values(getTemplateSrv().getVariables()).forEach((variable) => {
if (!supportedVariableTypes.includes(variable.type)) {
console.warn(`Variable of type "${variable.type}" is not supported`);

return;
}
const variableOptions: Record<VariableWithMultiSupport['id'], VariableOption> = {};

Object.values(getTemplateSrv().getVariables()).forEach((variable) => {
if (variable.type === 'adhoc') {
// These belong to request.adhocFilters
return;
}

const supportedVariable = variable as MultiValueVariable;

let variableValue = supportedVariable.current.value;
if (variableValue === '$__all' || isEqual(variableValue, ['$__all'])) {
if (supportedVariable.allValue === null || supportedVariable.allValue === '') {
variableValue = supportedVariable.options.slice(1).map((textValuePair) => textValuePair.value);
} else {
variableValue = supportedVariable.allValue;
}
if (variable.type === 'system') {
return;
}

variables[supportedVariable.id] = {
text: supportedVariable.current.text,
value: variableValue,
const value = match(variable)
.with({ type: P.union('custom', 'query') }, (v) => valueFromVariableWithMultiSupport(v))
.with({ type: P.union('constant', 'datasource', 'interval', 'textbox') }, (v) => v.current.value)
.exhaustive();

variableOptions[variable.id] = {
selected: false,
text: variable.current.text,
value: value,
};
});

return variables;
return variableOptions;
}
}
21 changes: 1 addition & 20 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
DataQuery,
DataQueryRequest,
DataSourceJsonData,
MetricFindValue,
SelectableValue,
VariableModel,
} from '@grafana/data';
import { DataQuery, DataQueryRequest, DataSourceJsonData, MetricFindValue, SelectableValue } from '@grafana/data';
import { TemplateSrv as GrafanaTemplateSrv } from '@grafana/runtime';

declare module '@grafana/runtime' {
Expand Down Expand Up @@ -45,18 +38,6 @@ export interface MetricFindTagValues extends MetricFindValue {
text: string;
}

export interface TextValuePair {
text: string;
value: any;
}

export interface MultiValueVariable extends VariableModel {
allValue: string | null;
id: string;
current: TextValuePair;
options: TextValuePair[];
}

export interface MetricPayloadConfig {
width?: number;
placeholder?: string;
Expand Down
23 changes: 23 additions & 0 deletions src/variable/valueFromVariableWithMultiSupport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { VariableWithMultiSupport } from '@grafana/data';
import { isEqual } from 'lodash';

export const valueFromVariableWithMultiSupport = (variable: VariableWithMultiSupport) => {
let variableValue = variable.current.value;
if (variableValue === '$__all' || isEqual(variableValue, ['$__all'])) {
if (variable.allValue === null || variable.allValue === '' || variable.allValue === undefined) {
variableValue = variable.options.slice(1).map((variableOption) => {
if (typeof variableOption.value !== 'string') {
const error = new Error('Variable option value is not a string');
console.error(error.message, variableOption, variable);

throw error;
}
return variableOption.value;
});
} else {
variableValue = variable.allValue;
}
}

return variableValue;
};

0 comments on commit ff6e193

Please sign in to comment.