Skip to content

Commit

Permalink
Merge pull request #18 from mullerpeter/feature/variable-query
Browse files Browse the repository at this point in the history
✨ feat: add variable query functionality
  • Loading branch information
mullerpeter authored Mar 23, 2023
2 parents 159dfba + 9b61acb commit 62d5346
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Changelog

## 1.1.2
## 1.1.3

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

---

## 1.1.2

- Use scoped Variables in template variable replacement

### 1.1.0

- Adds proper type reflection to support all databricks data types. (except type `BINARY` which is not supported)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mullerpeter-databricks-datasource",
"private": true,
"version": "1.1.2",
"version": "1.1.3",
"description": "Databricks SQL Connector",
"scripts": {
"build": "grafana-toolkit plugin:build",
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ConfigEditor extends PureComponent<Props, State> {
...options,
jsonData: {
...options.jsonData,
path: event.target.value,
path: event.target.value.replace(/^\//, ''),
},
});
};
Expand Down
62 changes: 49 additions & 13 deletions src/datasource.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,52 @@
import { DataSourceInstanceSettings, ScopedVars } from '@grafana/data';
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
import { MyDataSourceOptions, MyQuery } from './types';
import {DataFrame, DataQueryRequest, DataSourceInstanceSettings, MetricFindValue, ScopedVars} from '@grafana/data';
import {DataSourceWithBackend, getTemplateSrv} from '@grafana/runtime';
import {MyDataSourceOptions, MyQuery} from './types';
import {map, switchMap} from 'rxjs/operators';
import {firstValueFrom} from 'rxjs';

export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptions> {
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
super(instanceSettings);
}
applyTemplateVariables(query: MyQuery, scopedVars: ScopedVars) {
const templateSrv = getTemplateSrv();
return {
...query,
rawSqlQuery: query.rawSqlQuery ? templateSrv.replace(query.rawSqlQuery, scopedVars) : ''
};
}
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
super(instanceSettings);
}

applyTemplateVariables(query: MyQuery, scopedVars: ScopedVars) {
const templateSrv = getTemplateSrv();
return {
...query,
rawSqlQuery: query.rawSqlQuery ? templateSrv.replace(query.rawSqlQuery, scopedVars) : ''
};
}

async metricFindQuery(queryText: string, options?: any): Promise<MetricFindValue[]> {
if (!queryText) {
return Promise.resolve([]);
}

return firstValueFrom(this.query({
targets: [
{
rawSqlQuery: getTemplateSrv().replace(queryText, options.scopedVars),
refId: 'metricFindQuery'
},
],
maxDataPoints: 0,
} as DataQueryRequest<MyQuery>)
.pipe(
switchMap((response) => {
if (response.error) {
console.log('Error: ' + response.error.message);
throw new Error(response.error.message);
}
return response.data;
}),
switchMap((data: DataFrame) => {
return data.fields;
}),
map((field) =>
field.values.toArray().map((value) => {
return {text: value};
})
)
));
}
}

0 comments on commit 62d5346

Please sign in to comment.