Skip to content

Commit 05a5ace

Browse files
authored
Support key/value variables (#48)
* upgrade deps @grafana 7.4.3 * support key/value variable queries * update README
1 parent f3de5c3 commit 05a5ace

8 files changed

Lines changed: 1386 additions & 1818 deletions

File tree

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ annotations, and dashboard variables.
2929
![DeutscheBahn Arrivals Table](https://user-images.githubusercontent.com/1627510/90258294-f1bf2b00-de0d-11ea-8768-34b4ef37c125.png)
3030
![DeutscheBahn Arrivals Annotations](https://user-images.githubusercontent.com/1627510/90258316-f8e63900-de0d-11ea-91eb-d40532d5b768.png)
3131
![GitHub Security Advisories](https://user-images.githubusercontent.com/1627510/90258319-fbe12980-de0d-11ea-8ea2-c97bbc398aa4.png)
32+
![DeutscheBahn Station Variable](https://user-images.githubusercontent.com/1627510/110505565-e1c9aa00-80c3-11eb-85bb-10e5471fb151.png)
3233

3334
# Examples
3435

@@ -83,7 +84,7 @@ In the above example, "Group by" and "Alias by" are defined. "Group by" allows
8384
you to split up an array of data into multiple data points. "Alias by" is used
8485
as the name of the data point. You can make alias use text from the query or
8586
even the field name by using `$field_<your.field.name>` for the value of the
86-
field, or `$fieldName` for the name of the field. For instance, if `$fieldName`
87+
field,
8788
was used, it would be replaced by "batteryVoltage" because that's the name of
8889
the field. If `$field_identityInfo.displayName` was used, it would be replaced
8990
with the value of displayName. Using `$fieldName` can be useful if you're
@@ -126,3 +127,24 @@ The above example has two tags: "tag1" and "tag2". If a `TimeEnd` field is
126127
present, the annotation will be shown over a period of time. You can also
127128
separate the data path with commas to provide multiple data paths as shown with
128129
both server1 and server2.
130+
131+
## Dashboard Variable Queries
132+
133+
Dashboard variables can be populated by a GraphQL query that returns an array of
134+
objects. If the objects contain both `__text` and `__value` fields then they
135+
will be used (the `__text` field will be displayed, the `__value` field will be
136+
used in substitutions). Otherwise the values of all fields will be appended to
137+
the variable value list.
138+
139+
```graphql
140+
query {
141+
search(searchTerm: "$query") {
142+
stations {
143+
__value: primaryEvaId
144+
__text: name
145+
}
146+
}
147+
}
148+
```
149+
150+
- Data path: `search.stations`

dist/CHANGELOG.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
1-
# v1.2.0
1+
# Changelog
2+
3+
## v1.2.0
24

35
- **improvement** Add support for dashboard variable queries (@ggranberry #38)
46
- **bug fix** Properly scope variables, fixes repeated panel queries (@retzkek #41)
57

6-
# v1.1.4
8+
## v1.1.4
79

810
- **improvement** Use templateSrv to interpolate timeFrom and timeTo variables. (@carvid #31)
911
- **bug fix** Fix error in isRFC3339_ISO6801 when field is non-string (@ricochet1k #32, @retzkek #33)
1012

11-
# v1.1.3
13+
## v1.1.3
1214

1315
- use `QueryField` component for a nicer query editing experience (@michaelneale #24)
1416
- packaging, documentation, and testing improvements (@michaelneale #27, @retzkek #29)
1517
- **DEPRECATED**: no more Grafana 6 releases
1618

17-
# v1.1.2
19+
## v1.1.2
1820

1921
- Fix aliases in Grafana 7
2022

21-
# v1.1.1
23+
## v1.1.1
2224

2325
- Fixes error when null field in response
2426

25-
# v1.1.0
27+
## v1.1.0
2628

2729
- **BREAKING**: top-level `data` should no longer be included in data paths
2830
- support multiple data paths, comma-separated
2931

30-
# v1.0.0
32+
## v1.0.0
3133

3234
Initial release, basic support for tabular and timeseries data and annotations.

dist/module.js

Lines changed: 9 additions & 2 deletions
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.2.0",
28-
"updated": "2021-01-14"
28+
"updated": "2021-03-09"
2929
},
3030

3131
"dependencies": {

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
"author": "Kevin Retzke",
1212
"license": "Apache-2.0",
1313
"devDependencies": {
14-
"@grafana/data": "7.3.7",
15-
"@grafana/runtime": "7.3.7",
16-
"@grafana/toolkit": "7.3.7",
17-
"@grafana/ui": "7.3.7",
14+
"@grafana/data": "7.4.3",
15+
"@grafana/runtime": "7.4.3",
16+
"@grafana/toolkit": "7.4.3",
17+
"@grafana/ui": "7.4.3",
1818
"moment": "*"
1919
},
2020
"dependencies": {

src/DataSource.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
139139

140140
async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
141141
return Promise.all(
142-
options.targets.map(target => {
142+
options.targets.map((target) => {
143143
return this.createQuery(defaults(target, defaultQuery), options.range, options.scopedVars);
144144
})
145145
).then((results: any) => {
@@ -312,8 +312,12 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
312312
const docs: any[] = DataSource.getDocs(response.results.data, query.dataPath);
313313

314314
for (const doc of docs) {
315-
for (const fieldName in doc) {
316-
metricFindValues.push({ text: doc[fieldName] });
315+
if ('__text' in doc && '__value' in doc) {
316+
metricFindValues.push({ text: doc['__text'], value: doc['__value'] });
317+
} else {
318+
for (const fieldName in doc) {
319+
metricFindValues.push({ text: doc[fieldName] });
320+
}
317321
}
318322
}
319323

@@ -322,7 +326,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
322326

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

@@ -334,7 +338,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
334338
let variableValue = supportedVariable.current.value;
335339
if (variableValue === '$__all' || isEqual(variableValue, ['$__all'])) {
336340
if (supportedVariable.allValue === null || supportedVariable.allValue === '') {
337-
variableValue = supportedVariable.options.slice(1).map(textValuePair => textValuePair.value);
341+
variableValue = supportedVariable.options.slice(1).map((textValuePair) => textValuePair.value);
338342
} else {
339343
variableValue = supportedVariable.allValue;
340344
}

0 commit comments

Comments
 (0)