Skip to content

Commit fa11c75

Browse files
committed
fix: fixed logs and volume caching
1 parent 39c0a60 commit fa11c75

File tree

1 file changed

+67
-43
lines changed

1 file changed

+67
-43
lines changed

src/datasource.ts

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export class DataSource
2828
instanceSettings?: DataSourceInstanceSettings<MyDataSourceOptions>;
2929
url: string;
3030
streamFields: any[];
31-
cachedQuery: CachedQuery;
31+
cachedLogsQuery: CachedQuery;
32+
cachedHistogramQuery: CachedQuery;
3233
timestampColumn: string;
3334
histogramQuery: any;
3435

@@ -37,7 +38,13 @@ export class DataSource
3738
this.url = instanceSettings.url || '';
3839
this.instanceSettings = instanceSettings;
3940
this.streamFields = [];
40-
this.cachedQuery = {
41+
this.cachedLogsQuery = {
42+
requestQuery: '',
43+
isFetching: false,
44+
data: null,
45+
promise: null,
46+
};
47+
this.cachedHistogramQuery = {
4148
requestQuery: '',
4249
isFetching: false,
4350
data: null,
@@ -65,14 +72,8 @@ export class DataSource
6572
// console.log("options", options);
6673

6774
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;
7677

7778
let reqData = buildQuery(target, timestamps, this.streamFields, options.app, this.timestampColumn);
7879

@@ -93,46 +94,58 @@ export class DataSource
9394
});
9495

9596
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
100101
?.then((res) => {
101102
console.log('res in cache', target, res);
102103
const mode = target.displayMode || 'auto';
103104
// console.log('mode', mode);
104105
// console.log('res in cache', res);
105106
if (target?.refId?.includes(REF_ID_STARTER_LOG_VOLUME)) {
106-
return res.graph;
107+
return res;
107108
}
108109
if (options.app === 'panel-editor' || options.app === 'dashboard') {
109110
if (mode === 'graph' || mode === 'auto') {
110-
return res.graph;
111+
return res;
111112
}
112113
}
113-
return res.logs;
114-
})
115-
.finally(() => {
116-
this.resetQueryCache();
114+
return res;
117115
});
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;
119124

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+
});
123131
}
124132

125-
this.cachedQuery.requestQuery = cacheKey;
126-
this.cachedQuery.isFetching = true;
133+
currentCache.requestQuery = cacheKey;
134+
currentCache.isFetching = true;
127135
return this.doRequest(target, reqData)
128136
.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+
}
136149

137150
// Handle histogram queries for log volume using partitions
138151
if (target?.refId?.includes(REF_ID_STARTER_LOG_VOLUME) && this.histogramQuery) {
@@ -145,7 +158,9 @@ export class DataSource
145158
const partitions = partitionResponse.partitions;
146159

147160
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;
149164
}
150165

151166
const histogramPromises = partitions.map((partition: any) => {
@@ -171,7 +186,7 @@ export class DataSource
171186
}, []);
172187

173188
const graphDataFrame = getGraphDataFrame(combinedHits, target, options.app, 'zo_sql_key');
174-
this.cachedQuery.promise?.resolve({ graph: graphDataFrame, logs: null });
189+
currentCache.promise?.resolve(graphDataFrame);
175190

176191
return graphDataFrame;
177192
});
@@ -184,7 +199,7 @@ export class DataSource
184199
options.app,
185200
this.timestampColumn
186201
);
187-
this.cachedQuery.promise?.resolve({ graph: graphDataFrame, logs: null });
202+
currentCache.promise?.resolve(graphDataFrame);
188203
return graphDataFrame;
189204
});
190205
}
@@ -193,21 +208,21 @@ export class DataSource
193208
console.error('Partition or histogram request failed:', error);
194209
// Fallback to empty graph
195210
const graphDataFrame = getGraphDataFrame([], target, options.app, this.timestampColumn);
196-
this.cachedQuery.promise?.resolve({ graph: graphDataFrame, logs: null });
211+
currentCache.promise?.resolve(graphDataFrame);
197212
return graphDataFrame;
198213
});
199214
} else if (target?.refId?.includes(REF_ID_STARTER_LOG_VOLUME)) {
200215
const graphDataFrame = getGraphDataFrame([], target, options.app, this.timestampColumn);
201-
this.cachedQuery.promise?.resolve({ graph: graphDataFrame, logs: null });
216+
currentCache.promise?.resolve(graphDataFrame);
202217
return graphDataFrame;
203218
} else {
204219
const logsDataFrame = getLogsDataFrame(response.hits, target, this.streamFields, this.timestampColumn);
205-
this.cachedQuery.promise?.resolve({ graph: null, logs: logsDataFrame });
220+
currentCache.promise?.resolve(logsDataFrame);
206221
return logsDataFrame;
207222
}
208223
})
209224
.catch((err) => {
210-
this.cachedQuery.promise?.reject(err);
225+
currentCache.promise?.reject(err);
211226
let error = {
212227
message: '',
213228
detail: '',
@@ -226,7 +241,7 @@ export class DataSource
226241
throw new Error(error.message + (error.detail ? ` ( ${error.detail} ) ` : ''));
227242
})
228243
.finally(() => {
229-
this.cachedQuery.isFetching = false;
244+
currentCache.isFetching = false;
230245
});
231246
});
232247

@@ -275,8 +290,17 @@ export class DataSource
275290
});
276291
}
277292

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 = {
280304
requestQuery: '',
281305
isFetching: false,
282306
data: null,

0 commit comments

Comments
 (0)