Skip to content

Commit 81a23a2

Browse files
authored
Now it's possible use variables in query and the field name (#6)
1 parent 41d709d commit 81a23a2

File tree

2 files changed

+10
-32
lines changed

2 files changed

+10
-32
lines changed

src/DataSource.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
MetricFindValue,
1212
FieldType,
1313
} from '@grafana/data';
14+
import { getTemplateSrv } from '@grafana/runtime';
1415

1516
import API from './api';
1617
import { JsonApiQuery, JsonApiVariableQuery, JsonApiDataSourceOptions } from './types';
@@ -30,17 +31,20 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
3031
const fields = query.fields
3132
.filter(field => field.jsonPath)
3233
.map(field => {
33-
const values = JSONPath({ path: field.jsonPath, json: response });
34-
const [type, newvals] = detectFieldType(values);
34+
const jsonPathTreated = getTemplateSrv().replace(field.jsonPath, request.scopedVars);
35+
const nameTreated = getTemplateSrv().replace(field.name, request.scopedVars);
36+
37+
const values = JSONPath({ path: jsonPathTreated, json: response });
3538

3639
// Get the path for automatic setting of the field name.
3740
//
3841
// Casted to any due to typing issues with JSONPath-Plus
39-
const paths = (JSONPath as any).toPathArray(field.jsonPath);
40-
const propertyName = paths[paths.length - 1];
42+
const paths = (JSONPath as any).toPathArray(jsonPathTreated);
43+
44+
const [type, newvals] = detectFieldType(values);
4145

4246
return {
43-
name: field.name || propertyName,
47+
name: nameTreated || paths[paths.length - 1],
4448
type: type,
4549
values: newvals,
4650
};

yarn.lock

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4883,7 +4883,7 @@ escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
48834883
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
48844884
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=
48854885

4886-
escodegen@^1.8.1, escodegen@^1.9.1:
4886+
escodegen@^1.9.1:
48874887
version "1.14.3"
48884888
resolved "https://registry.yarnpkg.com/escodegen/-/escodegen-1.14.3.tgz#4e7b81fba61581dc97582ed78cab7f0e8d63f503"
48894889
integrity sha512-qFcX0XJkdg+PB3xjZZG/wKSuT1PnQWx57+TVSjIMmILd2yC/6ByYElPwJnslDsuWuSAp4AwJGumarAAmJch5Kw==
@@ -5002,11 +5002,6 @@ espree@^6.1.2:
50025002
acorn-jsx "^5.2.0"
50035003
eslint-visitor-keys "^1.1.0"
50045004

5005-
esprima@1.2.2:
5006-
version "1.2.2"
5007-
resolved "https://registry.yarnpkg.com/esprima/-/esprima-1.2.2.tgz#76a0fd66fcfe154fd292667dc264019750b1657b"
5008-
integrity sha1-dqD9Zvz+FU/SkmZ9wmQBl1CxZXs=
5009-
50105005
esprima@^4.0.0, esprima@^4.0.1:
50115006
version "4.0.1"
50125007
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
@@ -7257,15 +7252,6 @@ jsonpath-plus@^4.0.0:
72577252
resolved "https://registry.yarnpkg.com/jsonpath-plus/-/jsonpath-plus-4.0.0.tgz#954b69faa3d8b07f30ae2f9e601176a4b0d2806e"
72587253
integrity sha512-e0Jtg4KAzDJKKwzbLaUtinCn0RZseWBVRTRGihSpvFlM3wTR7ExSp+PTdeTsDrLNJUe7L7JYJe8mblHX5SCT6A==
72597254

7260-
jsonpath@^1.0.2:
7261-
version "1.0.2"
7262-
resolved "https://registry.yarnpkg.com/jsonpath/-/jsonpath-1.0.2.tgz#e6aae681d03e9a77b4651d5d96eac5fc63b1fd13"
7263-
integrity sha512-rmzlgFZiQPc6q4HDyK8s9Qb4oxBnI5sF61y/Co5PV0lc3q2bIuRsNdueVbhoSHdKM4fxeimphOAtfz47yjCfeA==
7264-
dependencies:
7265-
esprima "1.2.2"
7266-
static-eval "2.0.2"
7267-
underscore "1.7.0"
7268-
72697255
jsprim@^1.2.2:
72707256
version "1.4.1"
72717257
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
@@ -10990,13 +10976,6 @@ stack-utils@^1.0.1:
1099010976
resolved "https://registry.yarnpkg.com/stack-utils/-/stack-utils-1.0.2.tgz#33eba3897788558bebfc2db059dc158ec36cebb8"
1099110977
integrity sha512-MTX+MeG5U994cazkjd/9KNAapsHnibjMLnfXodlkXw76JEea0UiNzrqidzo1emMwk7w5Qhc9jd4Bn9TBb1MFwA==
1099210978

10993-
static-eval@2.0.2:
10994-
version "2.0.2"
10995-
resolved "https://registry.yarnpkg.com/static-eval/-/static-eval-2.0.2.tgz#2d1759306b1befa688938454c546b7871f806a42"
10996-
integrity sha512-N/D219Hcr2bPjLxPiV+TQE++Tsmrady7TqAJugLy7Xk1EumfDWS/f5dtBbkRCGE7wKKXuYockQoj8Rm2/pVKyg==
10997-
dependencies:
10998-
escodegen "^1.8.1"
10999-
1100010979
static-extend@^0.1.1:
1100110980
version "0.1.2"
1100210981
resolved "https://registry.yarnpkg.com/static-extend/-/static-extend-0.1.2.tgz#60809c39cbff55337226fd5e0b520f341f1fb5c6"
@@ -11713,11 +11692,6 @@ uglify-to-browserify@~1.0.0:
1171311692
resolved "https://registry.yarnpkg.com/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz#6e0924d6bda6b5afe349e39a6d632850a0f882b7"
1171411693
integrity sha1-bgkk1r2mta/jSeOabWMoUKD4grc=
1171511694

11716-
underscore@1.7.0:
11717-
version "1.7.0"
11718-
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.7.0.tgz#6bbaf0877500d36be34ecaa584e0db9fef035209"
11719-
integrity sha1-a7rwh3UA02vjTsqlhODbn+8DUgk=
11720-
1172111695
unicode-canonical-property-names-ecmascript@^1.0.4:
1172211696
version "1.0.4"
1172311697
resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz#2619800c4c825800efdd8343af7dd9933cbe2818"

0 commit comments

Comments
 (0)