Skip to content

Commit

Permalink
fix: 2 queries being fired every time instead of one
Browse files Browse the repository at this point in the history
Fixed #7 and #8
  • Loading branch information
omkarK06 committed May 22, 2023
1 parent d3889b5 commit 411c51f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
51 changes: 48 additions & 3 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Observable } from 'rxjs';
import { getBackendSrv } from '@grafana/runtime';
import { queryLogsVolume } from './features/log/LogsModel';

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

constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
super(instanceSettings);
this.url = instanceSettings.url || '';
this.instanceSettings = instanceSettings;
this.streamFields = [];
this.cachedQuery = {
requestQuery: '',
isFetching: false,
data: null,
promise: null,
};
}

async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
const timestamps = getConsumableTime(options.range);
const promises = options.targets.map((target) => {
if (!this.cachedQuery.data) {
this.cachedQuery.data = new Promise((resolve, reject) => {
this.cachedQuery.promise = {
resolve,
reject,
};
});
}
const reqData = buildQuery(target, timestamps, this.streamFields);
if (JSON.stringify(reqData) === this.cachedQuery.requestQuery) {
return this.cachedQuery.data
?.then((res) => {
if (target?.refId?.includes(REF_ID_STARTER_LOG_VOLUME)) {
return res.graph;
}
return res.logs;
})
.finally(() => {
this.resetQueryCache();
});
}
this.cachedQuery.requestQuery = JSON.stringify(reqData);
this.cachedQuery.isFetching = true;
return this.doRequest(target, reqData)
.then((response) => {
const graphDataFrame = getGraphDataFrame(response, target);
const logsDataFrame = getLogsDataFrame(response, target, this.streamFields);
this.cachedQuery.promise?.resolve({ graph: graphDataFrame, logs: logsDataFrame });
if (target?.refId?.includes(REF_ID_STARTER_LOG_VOLUME)) {
return getGraphDataFrame(response, target);
return graphDataFrame;
}
return getLogsDataFrame(response, target, this.streamFields);
return logsDataFrame;
})
.catch((err) => {
this.cachedQuery.promise?.reject(err);
let error = {
message: '',
detail: '',
Expand All @@ -64,6 +97,9 @@ export class DataSource
error.message = customMessage;
}
throw new Error(error.message + (error.detail ? ` ( ${error.detail} ) ` : ''));
})
.finally(() => {
this.cachedQuery.isFetching = false;
});
});

Expand All @@ -78,6 +114,15 @@ export class DataSource
});
}

resetQueryCache() {
this.cachedQuery = {
requestQuery: '',
isFetching: false,
data: null,
promise: null,
};
}

async testDatasource() {
return getOrganizations({ url: this.url })
.then((res) => {
Expand Down
2 changes: 1 addition & 1 deletion src/features/query/queryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const buildQuery = (queryData: MyQuery, timestamps: TimeRange, streamFiel
sql: 'select * from "[INDEX_NAME]" [WHERE_CLAUSE]',
start_time: timestamps.startTimeInMicro,
end_time: timestamps.endTimeInMirco,
size: 1000,
size: 300,
},
aggs: {
histogram:
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ export interface TimeRange {
startTimeInMicro: number;
endTimeInMirco: number;
}

export interface CachedQuery {
requestQuery: string;
data: Promise<any> | null;
isFetching: boolean;
promise: {
resolve: (value: unknown) => void;
reject: (value: unknown) => void;
} | null;
}

0 comments on commit 411c51f

Please sign in to comment.