Skip to content

Commit 62d5346

Browse files
authored
Merge pull request #18 from mullerpeter/feature/variable-query
✨ feat: add variable query functionality
2 parents 159dfba + 9b61acb commit 62d5346

File tree

4 files changed

+58
-17
lines changed

4 files changed

+58
-17
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# Changelog
22

3-
## 1.1.2
3+
## 1.1.3
44

5-
- Use scoped Variables in template variable replacement
5+
- Add variable query support
6+
- Bugfix: Remove leading slash from SQL path in configuration
67

78
---
89

10+
## 1.1.2
11+
12+
- Use scoped Variables in template variable replacement
13+
914
### 1.1.0
1015

1116
- Adds proper type reflection to support all databricks data types. (except type `BINARY` which is not supported)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mullerpeter-databricks-datasource",
33
"private": true,
4-
"version": "1.1.2",
4+
"version": "1.1.3",
55
"description": "Databricks SQL Connector",
66
"scripts": {
77
"build": "grafana-toolkit plugin:build",

src/ConfigEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class ConfigEditor extends PureComponent<Props, State> {
4040
...options,
4141
jsonData: {
4242
...options.jsonData,
43-
path: event.target.value,
43+
path: event.target.value.replace(/^\//, ''),
4444
},
4545
});
4646
};

src/datasource.ts

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
1-
import { DataSourceInstanceSettings, ScopedVars } from '@grafana/data';
2-
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
3-
import { MyDataSourceOptions, MyQuery } from './types';
1+
import {DataFrame, DataQueryRequest, DataSourceInstanceSettings, MetricFindValue, ScopedVars} from '@grafana/data';
2+
import {DataSourceWithBackend, getTemplateSrv} from '@grafana/runtime';
3+
import {MyDataSourceOptions, MyQuery} from './types';
4+
import {map, switchMap} from 'rxjs/operators';
5+
import {firstValueFrom} from 'rxjs';
46

57
export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptions> {
6-
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
7-
super(instanceSettings);
8-
}
9-
applyTemplateVariables(query: MyQuery, scopedVars: ScopedVars) {
10-
const templateSrv = getTemplateSrv();
11-
return {
12-
...query,
13-
rawSqlQuery: query.rawSqlQuery ? templateSrv.replace(query.rawSqlQuery, scopedVars) : ''
14-
};
15-
}
8+
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
9+
super(instanceSettings);
10+
}
11+
12+
applyTemplateVariables(query: MyQuery, scopedVars: ScopedVars) {
13+
const templateSrv = getTemplateSrv();
14+
return {
15+
...query,
16+
rawSqlQuery: query.rawSqlQuery ? templateSrv.replace(query.rawSqlQuery, scopedVars) : ''
17+
};
18+
}
19+
20+
async metricFindQuery(queryText: string, options?: any): Promise<MetricFindValue[]> {
21+
if (!queryText) {
22+
return Promise.resolve([]);
23+
}
24+
25+
return firstValueFrom(this.query({
26+
targets: [
27+
{
28+
rawSqlQuery: getTemplateSrv().replace(queryText, options.scopedVars),
29+
refId: 'metricFindQuery'
30+
},
31+
],
32+
maxDataPoints: 0,
33+
} as DataQueryRequest<MyQuery>)
34+
.pipe(
35+
switchMap((response) => {
36+
if (response.error) {
37+
console.log('Error: ' + response.error.message);
38+
throw new Error(response.error.message);
39+
}
40+
return response.data;
41+
}),
42+
switchMap((data: DataFrame) => {
43+
return data.fields;
44+
}),
45+
map((field) =>
46+
field.values.toArray().map((value) => {
47+
return {text: value};
48+
})
49+
)
50+
));
51+
}
1652
}

0 commit comments

Comments
 (0)