From ef4a19920581c1287b15af09793879bb7cabd19f Mon Sep 17 00:00:00 2001 From: Andrew Melnick Date: Fri, 25 Nov 2022 14:28:03 -0700 Subject: [PATCH] Fix metric label queries not replacing variables --- integration-test/dashboards/retweets.json | 4 +- integration-test/run.sh | 2 +- src/datasource.ts | 46 +++++++++-------------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/integration-test/dashboards/retweets.json b/integration-test/dashboards/retweets.json index 223ee5a..9ee1489 100644 --- a/integration-test/dashboards/retweets.json +++ b/integration-test/dashboards/retweets.json @@ -30,7 +30,7 @@ { "datasource": { "type": "meln5674-mongodb-community", - "uid": "GLlI6wg4z" + "uid": "${datasource}" }, "fieldConfig": { "defaults": { @@ -109,7 +109,7 @@ "database": "twitter", "datasource": { "type": "meln5674-mongodb-community", - "uid": "GLlI6wg4z" + "uid": "${datasource}" }, "labelFields": [ "user_url" diff --git a/integration-test/run.sh b/integration-test/run.sh index 429e7c2..4d49477 100755 --- a/integration-test/run.sh +++ b/integration-test/run.sh @@ -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 diff --git a/src/datasource.ts b/src/datasource.ts index f2f6f1c..586ebd1 100644 --- a/src/datasource.ts +++ b/src/datasource.ts @@ -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'; @@ -31,6 +26,7 @@ export class DataSource extends DataSourceWithBackend): Observable { const templateSrv = getTemplateSrv(); templateSrv.updateTimeRange(request.range); @@ -38,7 +34,8 @@ export class DataSource extends DataSourceWithBackend { - const request: Partial = { + const target: Partial = { + refId: 'metricFindQuery', database: query.database, collection: query.collection, queryType: MongoDBQueryType.Table, @@ -51,27 +48,20 @@ export class DataSource extends DataSourceWithBackend({ - url: '/api/ds/query', - method: 'POST', - data: { - queries, - }, - requestId: refId, - }) - .pipe( - map((res: FetchResponse) => { - const rsp = toDataQueryResponse(res, queries); - return rsp.data[0]; - }) - )); + let dataQuery = { + ...options, + targets: [target] + } + let dataQueryRequest = dataQuery as DataQueryRequest - 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 []; }); } }