@@ -28,7 +28,8 @@ export class DataSource
28
28
instanceSettings ?: DataSourceInstanceSettings < MyDataSourceOptions > ;
29
29
url : string ;
30
30
streamFields : any [ ] ;
31
- cachedQuery : CachedQuery ;
31
+ cachedLogsQuery : CachedQuery ;
32
+ cachedHistogramQuery : CachedQuery ;
32
33
timestampColumn : string ;
33
34
histogramQuery : any ;
34
35
@@ -37,7 +38,13 @@ export class DataSource
37
38
this . url = instanceSettings . url || '' ;
38
39
this . instanceSettings = instanceSettings ;
39
40
this . streamFields = [ ] ;
40
- this . cachedQuery = {
41
+ this . cachedLogsQuery = {
42
+ requestQuery : '' ,
43
+ isFetching : false ,
44
+ data : null ,
45
+ promise : null ,
46
+ } ;
47
+ this . cachedHistogramQuery = {
41
48
requestQuery : '' ,
42
49
isFetching : false ,
43
50
data : null ,
@@ -65,14 +72,8 @@ export class DataSource
65
72
// console.log("options", options);
66
73
67
74
const promises = interpolatedTargets . map ( ( target ) => {
68
- if ( ! this . cachedQuery . data ) {
69
- this . cachedQuery . data = new Promise ( ( resolve , reject ) => {
70
- this . cachedQuery . promise = {
71
- resolve,
72
- reject,
73
- } ;
74
- } ) ;
75
- }
75
+ const isHistogramQuery = target ?. refId ?. includes ( REF_ID_STARTER_LOG_VOLUME ) ;
76
+ let currentCache = isHistogramQuery ? this . cachedHistogramQuery : this . cachedLogsQuery ;
76
77
77
78
let reqData = buildQuery ( target , timestamps , this . streamFields , options . app , this . timestampColumn ) ;
78
79
@@ -93,46 +94,58 @@ export class DataSource
93
94
} ) ;
94
95
95
96
console . log ( 'cacheKey' , cacheKey ) ;
96
- console . log ( 'cached request query' , this . cachedQuery ) ;
97
- console . log ( 'Is same' , this . cachedQuery . requestQuery === cacheKey ) ;
98
- if ( cacheKey === this . cachedQuery . requestQuery ) {
99
- return this . cachedQuery . data
97
+ console . log ( 'cached request query' , currentCache ) ;
98
+ console . log ( 'Is same' , currentCache . requestQuery === cacheKey ) ;
99
+ if ( cacheKey === currentCache . requestQuery && currentCache . data ) {
100
+ return currentCache . data
100
101
?. then ( ( res ) => {
101
102
console . log ( 'res in cache' , target , res ) ;
102
103
const mode = target . displayMode || 'auto' ;
103
104
// console.log('mode', mode);
104
105
// console.log('res in cache', res);
105
106
if ( target ?. refId ?. includes ( REF_ID_STARTER_LOG_VOLUME ) ) {
106
- return res . graph ;
107
+ return res ;
107
108
}
108
109
if ( options . app === 'panel-editor' || options . app === 'dashboard' ) {
109
110
if ( mode === 'graph' || mode === 'auto' ) {
110
- return res . graph ;
111
+ return res ;
111
112
}
112
113
}
113
- return res . logs ;
114
- } )
115
- . finally ( ( ) => {
116
- this . resetQueryCache ( ) ;
114
+ return res ;
117
115
} ) ;
118
- }
116
+ } else {
117
+ if ( target ?. refId ?. includes ( REF_ID_STARTER_LOG_VOLUME ) ) {
118
+ this . resetHistogramQueryCache ( ) ;
119
+ } else {
120
+ this . resetLogsQueryCache ( ) ;
121
+ }
122
+
123
+ currentCache = isHistogramQuery ? this . cachedHistogramQuery : this . cachedLogsQuery ;
119
124
120
- // As we don't show histogram for sql mode in explore
121
- if ( options . app === 'explore' && target ?. refId ?. includes ( REF_ID_STARTER_LOG_VOLUME ) && target . sqlMode ) {
122
- return getGraphDataFrame ( [ ] , target , options . app , this . timestampColumn ) ;
125
+ currentCache . data = new Promise ( ( resolve , reject ) => {
126
+ currentCache . promise = {
127
+ resolve,
128
+ reject,
129
+ } ;
130
+ } ) ;
123
131
}
124
132
125
- this . cachedQuery . requestQuery = cacheKey ;
126
- this . cachedQuery . isFetching = true ;
133
+ currentCache . requestQuery = cacheKey ;
134
+ currentCache . isFetching = true ;
127
135
return this . doRequest ( target , reqData )
128
136
. then ( ( response ) => {
129
- // if (options.app === 'panel-editor' || options.app === 'dashboard') {
130
- // const mode = target.displayMode || 'auto';
131
- // const logsDf = getLogsDataFrame(response.hits, target, this.streamFields, this.timestampColumn);
132
- // const graphDf = getGraphDataFrame(response.hits, target, options.app, this.timestampColumn);
133
- // this.cachedQuery.promise?.resolve({ graph: graphDf, logs: logsDf });
134
- // return mode === 'logs' ? logsDf : graphDf;
135
- // }
137
+ if ( options . app === 'panel-editor' || options . app === 'dashboard' ) {
138
+ const mode = target . displayMode || 'auto' ;
139
+ if ( mode === 'graph' ) {
140
+ const graphDf = getGraphDataFrame ( response . hits , target , options . app , this . timestampColumn ) ;
141
+ currentCache . promise ?. resolve ( graphDf ) ;
142
+ return graphDf ;
143
+ } else {
144
+ const logsDf = getLogsDataFrame ( response . hits , target , this . streamFields , this . timestampColumn ) ;
145
+ currentCache . promise ?. resolve ( logsDf ) ;
146
+ return logsDf ;
147
+ }
148
+ }
136
149
137
150
// Handle histogram queries for log volume using partitions
138
151
if ( target ?. refId ?. includes ( REF_ID_STARTER_LOG_VOLUME ) && this . histogramQuery ) {
@@ -145,7 +158,9 @@ export class DataSource
145
158
const partitions = partitionResponse . partitions ;
146
159
147
160
if ( ! partitionResponse . is_histogram_eligible ) {
148
- return null ;
161
+ let dataFrame = getGraphDataFrame ( [ ] , target , options . app , this . timestampColumn ) ;
162
+ currentCache . promise ?. resolve ( dataFrame ) ;
163
+ return dataFrame ;
149
164
}
150
165
151
166
const histogramPromises = partitions . map ( ( partition : any ) => {
@@ -171,7 +186,7 @@ export class DataSource
171
186
} , [ ] ) ;
172
187
173
188
const graphDataFrame = getGraphDataFrame ( combinedHits , target , options . app , 'zo_sql_key' ) ;
174
- this . cachedQuery . promise ?. resolve ( { graph : graphDataFrame , logs : null } ) ;
189
+ currentCache . promise ?. resolve ( graphDataFrame ) ;
175
190
176
191
return graphDataFrame ;
177
192
} ) ;
@@ -184,7 +199,7 @@ export class DataSource
184
199
options . app ,
185
200
this . timestampColumn
186
201
) ;
187
- this . cachedQuery . promise ?. resolve ( { graph : graphDataFrame , logs : null } ) ;
202
+ currentCache . promise ?. resolve ( graphDataFrame ) ;
188
203
return graphDataFrame ;
189
204
} ) ;
190
205
}
@@ -193,21 +208,21 @@ export class DataSource
193
208
console . error ( 'Partition or histogram request failed:' , error ) ;
194
209
// Fallback to empty graph
195
210
const graphDataFrame = getGraphDataFrame ( [ ] , target , options . app , this . timestampColumn ) ;
196
- this . cachedQuery . promise ?. resolve ( { graph : graphDataFrame , logs : null } ) ;
211
+ currentCache . promise ?. resolve ( graphDataFrame ) ;
197
212
return graphDataFrame ;
198
213
} ) ;
199
214
} else if ( target ?. refId ?. includes ( REF_ID_STARTER_LOG_VOLUME ) ) {
200
215
const graphDataFrame = getGraphDataFrame ( [ ] , target , options . app , this . timestampColumn ) ;
201
- this . cachedQuery . promise ?. resolve ( { graph : graphDataFrame , logs : null } ) ;
216
+ currentCache . promise ?. resolve ( graphDataFrame ) ;
202
217
return graphDataFrame ;
203
218
} else {
204
219
const logsDataFrame = getLogsDataFrame ( response . hits , target , this . streamFields , this . timestampColumn ) ;
205
- this . cachedQuery . promise ?. resolve ( { graph : null , logs : logsDataFrame } ) ;
220
+ currentCache . promise ?. resolve ( logsDataFrame ) ;
206
221
return logsDataFrame ;
207
222
}
208
223
} )
209
224
. catch ( ( err ) => {
210
- this . cachedQuery . promise ?. reject ( err ) ;
225
+ currentCache . promise ?. reject ( err ) ;
211
226
let error = {
212
227
message : '' ,
213
228
detail : '' ,
@@ -226,7 +241,7 @@ export class DataSource
226
241
throw new Error ( error . message + ( error . detail ? ` ( ${ error . detail } ) ` : '' ) ) ;
227
242
} )
228
243
. finally ( ( ) => {
229
- this . cachedQuery . isFetching = false ;
244
+ currentCache . isFetching = false ;
230
245
} ) ;
231
246
} ) ;
232
247
@@ -275,8 +290,17 @@ export class DataSource
275
290
} ) ;
276
291
}
277
292
278
- resetQueryCache ( ) {
279
- this . cachedQuery = {
293
+ resetHistogramQueryCache ( ) {
294
+ this . cachedHistogramQuery = {
295
+ requestQuery : '' ,
296
+ isFetching : false ,
297
+ data : null ,
298
+ promise : null ,
299
+ } ;
300
+ }
301
+
302
+ resetLogsQueryCache ( ) {
303
+ this . cachedLogsQuery = {
280
304
requestQuery : '' ,
281
305
isFetching : false ,
282
306
data : null ,
0 commit comments