Skip to content

Commit

Permalink
Fix metric label queries not replacing variables
Browse files Browse the repository at this point in the history
  • Loading branch information
meln5674 committed Nov 25, 2022
1 parent 164dca5 commit ef4a199
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
4 changes: 2 additions & 2 deletions integration-test/dashboards/retweets.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{
"datasource": {
"type": "meln5674-mongodb-community",
"uid": "GLlI6wg4z"
"uid": "${datasource}"
},
"fieldConfig": {
"defaults": {
Expand Down Expand Up @@ -109,7 +109,7 @@
"database": "twitter",
"datasource": {
"type": "meln5674-mongodb-community",
"uid": "GLlI6wg4z"
"uid": "${datasource}"
},
"labelFields": [
"user_url"
Expand Down
2 changes: 1 addition & 1 deletion integration-test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ helm upgrade --install --wait grafana bitnami/grafana "${GRAFANA_ARGS[@]}"
if [ -n "${INTEGRATION_TEST_DEV_MODE}" ]; then
kubectl rollout restart deploy/grafana
kubectl rollout status deploy/grafana
sleep 2
sleep 5
echo 'Forwarding ports. Press Ctrl+C to exit and re-run this script to make changes'
kubectl port-forward deploy/grafana 3000:3000
else
Expand Down
46 changes: 18 additions & 28 deletions src/datasource.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { lastValueFrom, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import {
DataSourceInstanceSettings,
ScopedVars,
DataQuery,
DataQueryRequest,
DataQueryResponse,
MetricFindValue
MetricFindValue,
ScopedVars
} from '@grafana/data';
import {
DataSourceWithBackend,
BackendDataSourceResponse,
FetchResponse,
getTemplateSrv,
getBackendSrv,
toDataQueryResponse
frameToMetricFindValue
} from '@grafana/runtime';
import { MongoDBDataSourceOptions, MongoDBQuery, MongoDBQueryType, MongoDBVariableQuery } from './types';

Expand All @@ -31,14 +26,16 @@ export class DataSource extends DataSourceWithBackend<MongoDBQuery, MongoDBDataS
};
}


query(request: DataQueryRequest<MongoDBQuery>): Observable<DataQueryResponse> {
const templateSrv = getTemplateSrv();
templateSrv.updateTimeRange(request.range);
return super.query(request);
}

async metricFindQuery(query: MongoDBVariableQuery, options?: any): Promise<MetricFindValue[]> {
const request: Partial<MongoDBQuery> = {
const target: Partial<MongoDBQuery> = {
refId: 'metricFindQuery',
database: query.database,
collection: query.collection,
queryType: MongoDBQueryType.Table,
Expand All @@ -51,27 +48,20 @@ export class DataSource extends DataSourceWithBackend<MongoDBQuery, MongoDBDataS
autoTimeBound: false,
autoTimeSort: false
}
const refId = request.refId || 'variable-query';
const queries: DataQuery[] = [{ ...request, datasource: this.getRef(), refId }];

const frame = await lastValueFrom(getBackendSrv().fetch<BackendDataSourceResponse>({
url: '/api/ds/query',
method: 'POST',
data: {
queries,
},
requestId: refId,
})
.pipe(
map((res: FetchResponse<BackendDataSourceResponse>) => {
const rsp = toDataQueryResponse(res, queries);
return rsp.data[0];
})
));
let dataQuery = {
...options,
targets: [target]
}
let dataQueryRequest = dataQuery as DataQueryRequest<MongoDBQuery>

return frame.fields[0].values.buffer.map((value: any) => {
const metricValue: MetricFindValue = { text: value.toString() };
return metricValue
return lastValueFrom(
this.query(dataQueryRequest)
).then((rsp) => {
if (rsp.data?.length) {
return frameToMetricFindValue(rsp.data[0]);
}
return [];
});
}
}

0 comments on commit ef4a199

Please sign in to comment.