2021-09-23 15:43:43 +05:30
|
|
|
import getQueryResult from 'api/widgets/getQuery';
|
|
|
|
|
import { AxiosError } from 'axios';
|
|
|
|
|
import { ITEMS } from 'container/NewDashboard/ComponentsSlider/menuItems';
|
|
|
|
|
import { timePreferenceType } from 'container/NewWidget/RightContainer/timeItems';
|
|
|
|
|
import GetMaxMinTime from 'lib/getMaxMinTime';
|
2021-12-24 12:00:26 +05:30
|
|
|
import GetMinMax from 'lib/getMinMax';
|
2021-09-23 15:43:43 +05:30
|
|
|
import GetStartAndEndTime from 'lib/getStartAndEndTime';
|
|
|
|
|
import { Dispatch } from 'redux';
|
2022-03-14 20:12:42 +05:30
|
|
|
import store from 'store';
|
2021-09-23 15:43:43 +05:30
|
|
|
import AppActions from 'types/actions';
|
|
|
|
|
import { Query } from 'types/api/dashboard/getAll';
|
2021-12-24 12:00:26 +05:30
|
|
|
import { GlobalReducer } from 'types/reducer/globalTime';
|
2021-09-23 15:43:43 +05:30
|
|
|
|
|
|
|
|
export const GetQueryResults = (
|
|
|
|
|
props: GetQueryResultsProps,
|
|
|
|
|
): ((dispatch: Dispatch<AppActions>) => void) => {
|
|
|
|
|
return async (dispatch: Dispatch<AppActions>): Promise<void> => {
|
|
|
|
|
try {
|
|
|
|
|
const queryData = props.query;
|
|
|
|
|
|
2021-12-24 12:00:26 +05:30
|
|
|
const { globalTime } = store.getState();
|
|
|
|
|
|
|
|
|
|
const minMax = GetMinMax(props.globalSelectedInterval, [
|
|
|
|
|
globalTime.minTime / 1000000,
|
|
|
|
|
globalTime.maxTime / 1000000,
|
|
|
|
|
]);
|
|
|
|
|
|
2021-09-23 15:43:43 +05:30
|
|
|
const getMaxMinTime = GetMaxMinTime({
|
|
|
|
|
graphType: props.graphType,
|
2021-12-24 12:00:26 +05:30
|
|
|
maxTime: minMax.maxTime,
|
|
|
|
|
minTime: minMax.minTime,
|
2021-09-23 15:43:43 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { end, start } = GetStartAndEndTime({
|
|
|
|
|
type: props.selectedTime,
|
|
|
|
|
maxTime: getMaxMinTime.maxTime,
|
|
|
|
|
minTime: getMaxMinTime.minTime,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = await Promise.all(
|
|
|
|
|
queryData
|
|
|
|
|
.filter((e) => e.query)
|
|
|
|
|
.map(async (query) => {
|
|
|
|
|
const result = await getQueryResult({
|
|
|
|
|
end,
|
|
|
|
|
query: encodeURIComponent(query.query),
|
2022-03-22 12:10:31 +05:30
|
|
|
start,
|
2021-10-20 09:24:55 +05:30
|
|
|
step: '60',
|
2021-09-23 15:43:43 +05:30
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
query: query.query,
|
|
|
|
|
queryData: result,
|
|
|
|
|
legend: query.legend,
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const isError = response.find(
|
|
|
|
|
({ queryData }) => queryData.statusCode !== 200,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// want to make sure query is not empty
|
|
|
|
|
const isEmptyQuery =
|
|
|
|
|
queryData.map((e) => e.query).filter((e) => e).length ===
|
|
|
|
|
queryData.map((e) => e.query).length;
|
|
|
|
|
|
|
|
|
|
if (isError !== undefined && isEmptyQuery) {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'QUERY_ERROR',
|
|
|
|
|
payload: {
|
|
|
|
|
errorMessage: isError.queryData.error || '',
|
|
|
|
|
widgetId: props.widgetId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data = response.map((e) => ({
|
|
|
|
|
query: e.query,
|
|
|
|
|
legend: e.legend || '',
|
|
|
|
|
queryData: e.queryData.payload?.result || [],
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'QUERY_SUCCESS',
|
|
|
|
|
payload: {
|
|
|
|
|
widgetId: props.widgetId,
|
|
|
|
|
data,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'QUERY_ERROR',
|
|
|
|
|
payload: {
|
|
|
|
|
errorMessage: (error as AxiosError).toString(),
|
|
|
|
|
widgetId: props.widgetId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export interface GetQueryResultsProps {
|
|
|
|
|
widgetId: string;
|
|
|
|
|
selectedTime: timePreferenceType;
|
|
|
|
|
query: Query[];
|
|
|
|
|
graphType: ITEMS;
|
2021-12-24 12:00:26 +05:30
|
|
|
globalSelectedInterval: GlobalReducer['selectedTime'];
|
2021-09-23 15:43:43 +05:30
|
|
|
}
|