@@ -35,40 +35,7 @@ export class JsonDataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourc
35
35
* name it as you like.
36
36
*/
37
37
async metadataRequest ( query : JsonApiQuery , range ?: TimeRange ) {
38
- const scopedVars = { } ;
39
- const templateSrv = getTemplateSrv ( ) ;
40
-
41
- const replaceMacros = ( str : string ) => {
42
- return range
43
- ? str
44
- . replace ( / \$ _ _ u n i x E p o c h F r o m \( \) / g, range . from . unix ( ) . toString ( ) )
45
- . replace ( / \$ _ _ u n i x E p o c h T o \( \) / g, range . to . unix ( ) . toString ( ) )
46
- : str ;
47
- } ;
48
-
49
- const urlPathTreated = templateSrv . replace ( query . urlPath , scopedVars ) ;
50
- const bodyTreated = templateSrv . replace ( query . body , scopedVars ) ;
51
-
52
- const paramsTreated : Array < Pair < string , string > > = ( query . params ?? [ ] ) . map ( ( [ key , value ] ) => {
53
- const keyTreated = replaceMacros ( templateSrv . replace ( key , scopedVars ) ) ;
54
- const valueTreated = replaceMacros ( templateSrv . replace ( value , scopedVars ) ) ;
55
- return [ keyTreated , valueTreated ] ;
56
- } ) ;
57
-
58
- const headersTreated : Array < Pair < string , string > > = ( query . headers ?? [ ] ) . map ( ( [ key , value ] ) => {
59
- const keyTreated = templateSrv . replace ( key , scopedVars ) ;
60
- const valueTreated = templateSrv . replace ( value , scopedVars ) ;
61
- return [ keyTreated , valueTreated ] ;
62
- } ) ;
63
-
64
- return await this . api . cachedGet (
65
- query . cacheDurationSeconds ,
66
- query . method ,
67
- urlPathTreated ,
68
- paramsTreated ,
69
- headersTreated ,
70
- bodyTreated
71
- ) ;
38
+ return this . requestJson ( query , replace ( { } , range ) ) ;
72
39
}
73
40
74
41
async query ( request : DataQueryRequest < JsonApiQuery > ) : Promise < DataQueryResponse > {
@@ -135,62 +102,30 @@ export class JsonDataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourc
135
102
}
136
103
137
104
async doRequest ( query : JsonApiQuery , range ?: TimeRange , scopedVars ?: ScopedVars ) {
138
- const templateSrv = getTemplateSrv ( ) ;
139
-
140
- const replaceMacros = ( str : string ) => {
141
- return range
142
- ? str
143
- . replace ( / \$ _ _ u n i x E p o c h F r o m \( \) / g, range . from . unix ( ) . toString ( ) )
144
- . replace ( / \$ _ _ u n i x E p o c h T o \( \) / g, range . to . unix ( ) . toString ( ) )
145
- : str ;
146
- } ;
147
-
148
- const urlPathTreated = templateSrv . replace ( query . urlPath , scopedVars ) ;
149
- const bodyTreated = templateSrv . replace ( query . body , scopedVars ) ;
105
+ const replaceWithVars = replace ( scopedVars , range ) ;
150
106
151
- const paramsTreated : Array < Pair < string , string > > = ( query . params ?? [ ] ) . map ( ( [ key , value ] ) => {
152
- const keyTreated = replaceMacros ( templateSrv . replace ( key , scopedVars ) ) ;
153
- const valueTreated = replaceMacros ( templateSrv . replace ( value , scopedVars ) ) ;
154
- return [ keyTreated , valueTreated ] ;
155
- } ) ;
156
-
157
- const headersTreated : Array < Pair < string , string > > = ( query . headers ?? [ ] ) . map ( ( [ key , value ] ) => {
158
- const keyTreated = templateSrv . replace ( key , scopedVars ) ;
159
- const valueTreated = templateSrv . replace ( value , scopedVars ) ;
160
- return [ keyTreated , valueTreated ] ;
161
- } ) ;
107
+ const json = await this . requestJson ( query , replaceWithVars ) ;
162
108
163
- const response = await this . api . cachedGet (
164
- query . cacheDurationSeconds ,
165
- query . method ,
166
- urlPathTreated ,
167
- paramsTreated ,
168
- headersTreated ,
169
- bodyTreated
170
- ) ;
171
-
172
- if ( ! response ) {
109
+ if ( ! json ) {
173
110
throw new Error ( 'Query returned empty data' ) ;
174
111
}
175
112
176
113
const fields = query . fields
177
114
. filter ( ( field ) => field . jsonPath )
178
115
. map ( ( field ) => {
179
- const jsonPathTreated = replaceMacros ( templateSrv . replace ( field . jsonPath , scopedVars ) ) ;
180
- const nameTreated = templateSrv . replace ( field . name , scopedVars ) ;
181
-
182
- const values = JSONPath ( { path : jsonPathTreated , json : response } ) ;
116
+ const path = replaceWithVars ( field . jsonPath ) ;
117
+ const values = JSONPath ( { path, json } ) ;
183
118
184
119
// Get the path for automatic setting of the field name.
185
120
//
186
121
// Casted to any due to typing issues with JSONPath-Plus
187
- const paths = ( JSONPath as any ) . toPathArray ( jsonPathTreated ) ;
122
+ const paths = ( JSONPath as any ) . toPathArray ( path ) ;
188
123
189
124
const propertyType = field . type ? field . type : detectFieldType ( values ) ;
190
125
const typedValues = parseValues ( values , propertyType ) ;
191
126
192
127
return {
193
- name : nameTreated || paths [ paths . length - 1 ] ,
128
+ name : paths [ paths . length - 1 ] ,
194
129
type : propertyType ,
195
130
values : typedValues ,
196
131
} ;
@@ -210,8 +145,36 @@ export class JsonDataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourc
210
145
fields : fields ,
211
146
} ) ;
212
147
}
148
+
149
+ async requestJson ( query : JsonApiQuery , interpolate : ( text : string ) => string ) {
150
+ const interpolateKeyValue = ( [ key , value ] : Pair < string , string > ) : Pair < string , string > => {
151
+ return [ interpolate ( key ) , interpolate ( value ) ] ;
152
+ } ;
153
+
154
+ return await this . api . cachedGet (
155
+ query . cacheDurationSeconds ,
156
+ query . method ,
157
+ interpolate ( query . urlPath ) ,
158
+ ( query . params ?? [ ] ) . map ( interpolateKeyValue ) ,
159
+ ( query . headers ?? [ ] ) . map ( interpolateKeyValue ) ,
160
+ interpolate ( query . body )
161
+ ) ;
162
+ }
213
163
}
214
164
165
+ const replace = ( scopedVars ?: any , range ?: TimeRange ) => ( str : string ) : string => {
166
+ return replaceMacros ( getTemplateSrv ( ) . replace ( str , scopedVars ) , range ) ;
167
+ } ;
168
+
169
+ // replaceMacros substitutes all available macros with their current value.
170
+ const replaceMacros = ( str : string , range ?: TimeRange ) => {
171
+ return range
172
+ ? str
173
+ . replace ( / \$ _ _ u n i x E p o c h F r o m \( \) / g, range . from . unix ( ) . toString ( ) )
174
+ . replace ( / \$ _ _ u n i x E p o c h T o \( \) / g, range . to . unix ( ) . toString ( ) )
175
+ : str ;
176
+ } ;
177
+
215
178
/**
216
179
* Detects the field type from an array of values.
217
180
*/
0 commit comments