Skip to content

Commit 411c51f

Browse files
committed
fix: 2 queries being fired every time instead of one
Fixed #7 and #8
1 parent d3889b5 commit 411c51f

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

src/datasource.ts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Observable } from 'rxjs';
1212
import { getBackendSrv } from '@grafana/runtime';
1313
import { queryLogsVolume } from './features/log/LogsModel';
1414

15-
import { MyQuery, MyDataSourceOptions } from './types';
15+
import { MyQuery, MyDataSourceOptions, CachedQuery } from './types';
1616
import { logsErrorMessage, getConsumableTime } from 'utils/zincutils';
1717
import { getOrganizations } from 'services/organizations';
1818
import { cloneDeep } from 'lodash';
@@ -28,26 +28,59 @@ export class DataSource
2828
instanceSettings?: DataSourceInstanceSettings<MyDataSourceOptions>;
2929
url: string;
3030
streamFields: any[];
31+
cachedQuery: CachedQuery;
3132

3233
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
3334
super(instanceSettings);
3435
this.url = instanceSettings.url || '';
3536
this.instanceSettings = instanceSettings;
3637
this.streamFields = [];
38+
this.cachedQuery = {
39+
requestQuery: '',
40+
isFetching: false,
41+
data: null,
42+
promise: null,
43+
};
3744
}
3845

3946
async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
4047
const timestamps = getConsumableTime(options.range);
4148
const promises = options.targets.map((target) => {
49+
if (!this.cachedQuery.data) {
50+
this.cachedQuery.data = new Promise((resolve, reject) => {
51+
this.cachedQuery.promise = {
52+
resolve,
53+
reject,
54+
};
55+
});
56+
}
4257
const reqData = buildQuery(target, timestamps, this.streamFields);
58+
if (JSON.stringify(reqData) === this.cachedQuery.requestQuery) {
59+
return this.cachedQuery.data
60+
?.then((res) => {
61+
if (target?.refId?.includes(REF_ID_STARTER_LOG_VOLUME)) {
62+
return res.graph;
63+
}
64+
return res.logs;
65+
})
66+
.finally(() => {
67+
this.resetQueryCache();
68+
});
69+
}
70+
this.cachedQuery.requestQuery = JSON.stringify(reqData);
71+
this.cachedQuery.isFetching = true;
4372
return this.doRequest(target, reqData)
4473
.then((response) => {
74+
const graphDataFrame = getGraphDataFrame(response, target);
75+
const logsDataFrame = getLogsDataFrame(response, target, this.streamFields);
76+
this.cachedQuery.promise?.resolve({ graph: graphDataFrame, logs: logsDataFrame });
4577
if (target?.refId?.includes(REF_ID_STARTER_LOG_VOLUME)) {
46-
return getGraphDataFrame(response, target);
78+
return graphDataFrame;
4779
}
48-
return getLogsDataFrame(response, target, this.streamFields);
80+
return logsDataFrame;
4981
})
5082
.catch((err) => {
83+
this.cachedQuery.promise?.reject(err);
5184
let error = {
5285
message: '',
5386
detail: '',
@@ -64,6 +97,9 @@ export class DataSource
6497
error.message = customMessage;
6598
}
6699
throw new Error(error.message + (error.detail ? ` ( ${error.detail} ) ` : ''));
100+
})
101+
.finally(() => {
102+
this.cachedQuery.isFetching = false;
67103
});
68104
});
69105

@@ -78,6 +114,15 @@ export class DataSource
78114
});
79115
}
80116

117+
resetQueryCache() {
118+
this.cachedQuery = {
119+
requestQuery: '',
120+
isFetching: false,
121+
data: null,
122+
promise: null,
123+
};
124+
}
125+
81126
async testDatasource() {
82127
return getOrganizations({ url: this.url })
83128
.then((res) => {

src/features/query/queryBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const buildQuery = (queryData: MyQuery, timestamps: TimeRange, streamFiel
1010
sql: 'select * from "[INDEX_NAME]" [WHERE_CLAUSE]',
1111
start_time: timestamps.startTimeInMicro,
1212
end_time: timestamps.endTimeInMirco,
13-
size: 1000,
13+
size: 300,
1414
},
1515
aggs: {
1616
histogram:

src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,13 @@ export interface TimeRange {
3737
startTimeInMicro: number;
3838
endTimeInMirco: number;
3939
}
40+
41+
export interface CachedQuery {
42+
requestQuery: string;
43+
data: Promise<any> | null;
44+
isFetching: boolean;
45+
promise: {
46+
resolve: (value: unknown) => void;
47+
reject: (value: unknown) => void;
48+
} | null;
49+
}

0 commit comments

Comments
 (0)