Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support key/value variables #48

Merged
merged 3 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ annotations, and dashboard variables.
![DeutscheBahn Arrivals Table](https://user-images.githubusercontent.com/1627510/90258294-f1bf2b00-de0d-11ea-8768-34b4ef37c125.png)
![DeutscheBahn Arrivals Annotations](https://user-images.githubusercontent.com/1627510/90258316-f8e63900-de0d-11ea-91eb-d40532d5b768.png)
![GitHub Security Advisories](https://user-images.githubusercontent.com/1627510/90258319-fbe12980-de0d-11ea-8ea2-c97bbc398aa4.png)
![DeutscheBahn Station Variable](https://user-images.githubusercontent.com/1627510/110505565-e1c9aa00-80c3-11eb-85bb-10e5471fb151.png)

# Examples

Expand Down Expand Up @@ -83,7 +84,7 @@ In the above example, "Group by" and "Alias by" are defined. "Group by" allows
you to split up an array of data into multiple data points. "Alias by" is used
as the name of the data point. You can make alias use text from the query or
even the field name by using `$field_<your.field.name>` for the value of the
field, or `$fieldName` for the name of the field. For instance, if `$fieldName`
field,
was used, it would be replaced by "batteryVoltage" because that's the name of
the field. If `$field_identityInfo.displayName` was used, it would be replaced
with the value of displayName. Using `$fieldName` can be useful if you're
Expand Down Expand Up @@ -126,3 +127,24 @@ The above example has two tags: "tag1" and "tag2". If a `TimeEnd` field is
present, the annotation will be shown over a period of time. You can also
separate the data path with commas to provide multiple data paths as shown with
both server1 and server2.

## Dashboard Variable Queries

Dashboard variables can be populated by a GraphQL query that returns an array of
objects. If the objects contain both `__text` and `__value` fields then they
will be used (the `__text` field will be displayed, the `__value` field will be
used in substitutions). Otherwise the values of all fields will be appended to
the variable value list.

```graphql
query {
search(searchTerm: "$query") {
stations {
__value: primaryEvaId
__text: name
}
}
}
```

- Data path: `search.stations`
16 changes: 9 additions & 7 deletions dist/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
# v1.2.0
# Changelog

## v1.2.0

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

# v1.1.4
## v1.1.4

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

# v1.1.3
## v1.1.3

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

# v1.1.2
## v1.1.2

- Fix aliases in Grafana 7

# v1.1.1
## v1.1.1

- Fixes error when null field in response

# v1.1.0
## v1.1.0

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

# v1.0.0
## v1.0.0

Initial release, basic support for tabular and timeseries data and annotations.
11 changes: 9 additions & 2 deletions dist/module.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/module.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{ "name": "GitHub Security Advisories", "path": "img/github_security_advisories.png"}
],
"version": "1.2.0",
"updated": "2021-01-14"
"updated": "2021-03-09"
},

"dependencies": {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"author": "Kevin Retzke",
"license": "Apache-2.0",
"devDependencies": {
"@grafana/data": "7.3.7",
"@grafana/runtime": "7.3.7",
"@grafana/toolkit": "7.3.7",
"@grafana/ui": "7.3.7",
"@grafana/data": "7.4.3",
"@grafana/runtime": "7.4.3",
"@grafana/toolkit": "7.4.3",
"@grafana/ui": "7.4.3",
"moment": "*"
},
"dependencies": {
Expand Down
14 changes: 9 additions & 5 deletions src/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {

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

for (const doc of docs) {
for (const fieldName in doc) {
metricFindValues.push({ text: doc[fieldName] });
if ('__text' in doc && '__value' in doc) {
metricFindValues.push({ text: doc['__text'], value: doc['__value'] });
} else {
for (const fieldName in doc) {
metricFindValues.push({ text: doc[fieldName] });
}
}
}

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

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

Expand All @@ -334,7 +338,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
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);
variableValue = supportedVariable.options.slice(1).map((textValuePair) => textValuePair.value);
} else {
variableValue = supportedVariable.allValue;
}
Expand Down
Loading