Skip to content

Commit 63c6c19

Browse files
ggranberryGeorge Granberryretzkek
authored
added support for basic variables queries (#38)
Co-authored-by: George Granberry <george.granberry@disneystreaming.com> Co-authored-by: Kevin Retzke <kretzke@fnal.gov>
1 parent 15bc0b6 commit 63c6c19

7 files changed

Lines changed: 283 additions & 6 deletions

File tree

dist/module.js

Lines changed: 151 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/module.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
{ "name": "GitHub Security Advisories", "path": "img/github_security_advisories.png"}
2626
],
2727
"version": "1.1.4",
28-
"updated": "2020-11-04"
28+
"updated": "2021-01-14"
2929
},
3030

3131
"dependencies": {

src/DataSource.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,28 @@ import {
66
DataQueryRequest,
77
DataQueryResponse,
88
DataSourceApi,
9+
MetricFindValue,
910
DataSourceInstanceSettings,
1011
ScopedVars,
1112
TimeRange,
1213
} from '@grafana/data';
1314

14-
import { MyQuery, MyDataSourceOptions, defaultQuery } from './types';
15+
import {
16+
MyQuery,
17+
MyDataSourceOptions,
18+
defaultQuery,
19+
MyVariableQuery,
20+
MultiValueVariable,
21+
TextValuePair,
22+
} from './types';
1523
import { dateTime, MutableDataFrame, FieldType, DataFrame } from '@grafana/data';
1624
import { getTemplateSrv } from '@grafana/runtime';
1725
import _ from 'lodash';
26+
import { isEqual } from 'lodash';
1827
import { flatten, isRFC3339_ISO6801 } from './util';
1928

29+
const supportedVariableTypes = ['constant', 'custom', 'query', 'textbox'];
30+
2031
export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
2132
basicAuth: string | undefined;
2233
withCredentials: boolean | undefined;
@@ -290,4 +301,54 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
290301
}
291302
);
292303
}
304+
305+
async metricFindQuery(query: MyVariableQuery, options?: any) {
306+
const metricFindValues: MetricFindValue[] = [];
307+
308+
query = defaults(query, defaultQuery);
309+
310+
let payload = query.queryText;
311+
payload = getTemplateSrv().replace(payload, { ...this.getVariables });
312+
313+
const response = await this.postQuery(query, payload);
314+
315+
const docs: any[] = DataSource.getDocs(response.results.data, query.dataPath);
316+
317+
for (const doc of docs) {
318+
for (const fieldName in doc) {
319+
metricFindValues.push({ text: doc[fieldName] });
320+
}
321+
}
322+
323+
return metricFindValues;
324+
}
325+
326+
getVariables() {
327+
const variables: { [id: string]: TextValuePair } = {};
328+
Object.values(getTemplateSrv().getVariables()).forEach(variable => {
329+
if (!supportedVariableTypes.includes(variable.type)) {
330+
console.warn(`Variable of type "${variable.type}" is not supported`);
331+
332+
return;
333+
}
334+
335+
const supportedVariable = variable as MultiValueVariable;
336+
337+
let variableValue = supportedVariable.current.value;
338+
if (variableValue === '$__all' || isEqual(variableValue, ['$__all'])) {
339+
if (supportedVariable.allValue === null || supportedVariable.allValue === '') {
340+
variableValue = supportedVariable.options.slice(1).map(textValuePair => textValuePair.value);
341+
} else {
342+
variableValue = supportedVariable.allValue;
343+
}
344+
}
345+
346+
variables[supportedVariable.id] = {
347+
text: supportedVariable.current.text,
348+
value: variableValue,
349+
};
350+
});
351+
352+
return variables;
353+
}
293354
}

src/VariableQueryEditor.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { QueryField } from '@grafana/ui';
2+
import React, { useState } from 'react';
3+
import { MyQuery } from './types';
4+
5+
interface VariableQueryProps {
6+
query: MyQuery;
7+
onChange: (query: MyQuery, definition: string) => void;
8+
}
9+
10+
export const VariableQueryEditor: React.FC<VariableQueryProps> = ({ onChange, query }) => {
11+
const [state, setState] = useState(query);
12+
13+
const saveQuery = () => {
14+
onChange(state, `${state.queryText} (${state.dataPath})`);
15+
};
16+
17+
const onChangeQuery = (value: string, override?: boolean) =>
18+
setState({
19+
...state,
20+
queryText: value,
21+
});
22+
23+
const handleChange = (event: React.FormEvent<HTMLInputElement>) =>
24+
setState({
25+
...state,
26+
[event.currentTarget.name]: event.currentTarget.value,
27+
});
28+
29+
return (
30+
<>
31+
<div className="gf-form">
32+
<span className="gf-form-label width-10">Data Path</span>
33+
<input
34+
name="dataPath"
35+
className="gf-form-input"
36+
onBlur={saveQuery}
37+
onChange={handleChange}
38+
value={state.dataPath}
39+
/>
40+
</div>
41+
<div className="gf-form">
42+
<span className="gf-form-label width-10">Query</span>
43+
<QueryField query={state.queryText || ''} onBlur={saveQuery} onChange={onChangeQuery} portalOrigin="graphQL" />
44+
</div>
45+
</>
46+
);
47+
};

src/module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { ConfigEditor } from './ConfigEditor';
44
import { QueryEditor } from './QueryEditor';
55
import { MyQuery, MyDataSourceOptions } from './types';
66
import { GraphQLAnnotationsQueryCtrl } from './GraphQLAnnotationsQueryCtrl';
7+
import { VariableQueryEditor } from './VariableQueryEditor';
78

89
export const plugin = new DataSourcePlugin<DataSource, MyQuery, MyDataSourceOptions>(DataSource)
910
.setConfigEditor(ConfigEditor)
1011
.setAnnotationQueryCtrl(GraphQLAnnotationsQueryCtrl)
11-
.setQueryEditor(QueryEditor);
12+
.setQueryEditor(QueryEditor)
13+
.setVariableQueryEditor(VariableQueryEditor);

src/types.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DataQuery, DataSourceJsonData } from '@grafana/data';
1+
import { DataQuery, DataSourceJsonData, VariableModel } from '@grafana/data';
22

33
export interface MyQuery extends DataQuery {
44
queryText: string;
@@ -33,3 +33,20 @@ export const defaultQuery: Partial<MyQuery> = {
3333
export interface MyDataSourceOptions extends DataSourceJsonData {
3434
apiKey?: string;
3535
}
36+
37+
export interface MyVariableQuery extends DataQuery {
38+
dataPath: string;
39+
queryText: string;
40+
}
41+
42+
export interface TextValuePair {
43+
text: string;
44+
value: any;
45+
}
46+
47+
export interface MultiValueVariable extends VariableModel {
48+
allValue: string | null;
49+
id: string;
50+
current: TextValuePair;
51+
options: TextValuePair[];
52+
}

0 commit comments

Comments
 (0)