Skip to content

Commit a4c6b14

Browse files
MrLightSebastian Balzdevnied
authored
Add support for custom “display names” in Template Variable (#97)
* Add text and value Field for Template Variable --------- Co-authored-by: Sebastian Balz <[email protected]> Co-authored-by: Julien Millau <[email protected]> Co-authored-by: Julien Millau <[email protected]>
1 parent 226c0ec commit a4c6b14

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.9.0
4+
5+
### ⭐ Added
6+
- Template Variable: custom “display names” support with `__text` & `__value`
7+
38
## 1.8.1
49

510
### 🐞 Bug Fixes

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ SELECT column FROM table WHERE column in ${variable:regex}
145145
SELECT column FROM table WHERE column in (test1|test2)
146146
```
147147

148+
Add a Template Variable:<br/>
149+
You can use a SQL Query to define a [Template Variable](https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#add-a-query-variable)<br/>
150+
```sql
151+
-- Add __text and __value columns to your query to support custom "display names"
152+
SELECT value_column as __value, text_column as __text FROM table
153+
```
154+
148155
##### Layout of a query
149156

150157
*Simple query*

src/datasource.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DataQueryRequest, DataFrame, MetricFindValue, DataSourceInstanceSetting
33
import { SnowflakeQuery, SnowflakeOptions } from './types';
44
import { switchMap, map } from 'rxjs/operators';
55
import { firstValueFrom } from 'rxjs';
6+
import { uniqBy } from 'lodash';
67

78
export class DataSource extends DataSourceWithBackend<SnowflakeQuery, SnowflakeOptions> {
89
constructor(instanceSettings: DataSourceInstanceSettings<SnowflakeOptions>) {
@@ -41,14 +42,25 @@ export class DataSource extends DataSourceWithBackend<SnowflakeQuery, SnowflakeO
4142
}
4243
return response.data;
4344
}),
44-
switchMap((data: DataFrame) => {
45-
return data.fields;
46-
}),
47-
map((field) =>
48-
field.values.toArray().map((value) => {
49-
return { text: value };
50-
})
51-
)
45+
map((data: DataFrame) => {
46+
const values: MetricFindValue[] = [];
47+
const textField = data.fields.find((f) => f.name.toLowerCase() === '__text');
48+
const valueField = data.fields.find((f) => f.name.toLowerCase() === '__value');
49+
50+
if (textField && valueField) {
51+
for (let i = 0; i < textField.values.length; i++) {
52+
values.push({ text: '' + textField.values[i], value: '' + valueField.values[i] });
53+
}
54+
} else {
55+
for (const field of data.fields) {
56+
for (const value of field.values) {
57+
values.push({ text: value });
58+
}
59+
}
60+
}
61+
62+
return uniqBy(values, 'text');
63+
})
5264
));
5365
}
5466
}

0 commit comments

Comments
 (0)