2022-06-28 17:32:02 +05:30
|
|
|
/* eslint-disable */
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
// @ts-nocheck
|
|
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
import { getMetricsQueryRange } from 'api/metrics/getQueryRange';
|
2021-09-23 15:43:43 +05:30
|
|
|
import { AxiosError } from 'axios';
|
2022-06-24 15:00:21 +05:30
|
|
|
import { GRAPH_TYPES } from 'container/NewDashboard/ComponentsSlider';
|
2021-09-23 15:43:43 +05:30
|
|
|
import { ITEMS } from 'container/NewDashboard/ComponentsSlider/menuItems';
|
2022-06-24 15:00:21 +05:30
|
|
|
import { WIDGET_QUERY_BUILDER_FORMULA_KEY_NAME } from 'container/NewWidget/LeftContainer/QuerySection/constants';
|
|
|
|
|
import { EQueryTypeToQueryKeyMapping } from 'container/NewWidget/LeftContainer/QuerySection/types';
|
2021-09-23 15:43:43 +05:30
|
|
|
import { timePreferenceType } from 'container/NewWidget/RightContainer/timeItems';
|
2022-06-24 15:00:21 +05:30
|
|
|
import { Time } from 'container/TopNav/DateTimeSelection/config';
|
2021-09-23 15:43:43 +05:30
|
|
|
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';
|
2022-04-05 16:09:57 +05:30
|
|
|
import getStep from 'lib/getStep';
|
2022-06-24 15:00:21 +05:30
|
|
|
import { isEmpty } from 'lodash-es';
|
2021-09-23 15:43:43 +05:30
|
|
|
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';
|
2022-06-24 15:00:21 +05:30
|
|
|
import { ErrorResponse, SuccessResponse } from 'types/api';
|
2021-09-23 15:43:43 +05:30
|
|
|
import { Query } from 'types/api/dashboard/getAll';
|
2022-06-24 15:00:21 +05:30
|
|
|
import { MetricRangePayloadProps } from 'types/api/metrics/getQueryRange';
|
|
|
|
|
import { EDataSource, EPanelType, EQueryType } from 'types/common/dashboard';
|
2021-12-24 12:00:26 +05:30
|
|
|
import { GlobalReducer } from 'types/reducer/globalTime';
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
export async function GetMetricQueryRange({
|
|
|
|
|
query,
|
|
|
|
|
globalSelectedInterval,
|
|
|
|
|
graphType,
|
|
|
|
|
selectedTime,
|
|
|
|
|
}: {
|
|
|
|
|
query: Query;
|
|
|
|
|
graphType: GRAPH_TYPES;
|
|
|
|
|
selectedTime: timePreferenceType;
|
|
|
|
|
globalSelectedInterval: Time;
|
|
|
|
|
}): Promise<SuccessResponse<MetricRangePayloadProps> | ErrorResponse> {
|
|
|
|
|
const { queryType } = query;
|
|
|
|
|
const queryKey: Record<EQueryTypeToQueryKeyMapping, string> =
|
|
|
|
|
EQueryTypeToQueryKeyMapping[EQueryType[query.queryType]];
|
|
|
|
|
const queryData = query[queryKey];
|
|
|
|
|
const legendMap: Record<string, string> = {};
|
2022-06-28 17:32:02 +05:30
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
const QueryPayload = {
|
|
|
|
|
dataSource: EDataSource.METRICS,
|
|
|
|
|
compositeMetricQuery: {
|
|
|
|
|
queryType,
|
|
|
|
|
panelType: EPanelType[graphType],
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
switch (queryType as EQueryType) {
|
|
|
|
|
case EQueryType.QUERY_BUILDER: {
|
|
|
|
|
const builderQueries = {};
|
|
|
|
|
queryData.queryBuilder.map((query) => {
|
|
|
|
|
const generatedQueryPayload = {
|
|
|
|
|
queryName: query.name,
|
|
|
|
|
aggregateOperator: query.aggregateOperator,
|
|
|
|
|
metricName: query.metricName,
|
|
|
|
|
tagFilters: query.tagFilters,
|
|
|
|
|
};
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
if (graphType === 'TIME_SERIES') {
|
|
|
|
|
generatedQueryPayload.groupBy = query.groupBy;
|
|
|
|
|
}
|
2021-12-24 12:00:26 +05:30
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
// Value
|
|
|
|
|
else {
|
|
|
|
|
generatedQueryPayload.reduceTo = query.reduceTo;
|
|
|
|
|
}
|
2021-12-24 12:00:26 +05:30
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
generatedQueryPayload.expression = query.name;
|
|
|
|
|
generatedQueryPayload.disabled = query.disabled;
|
|
|
|
|
builderQueries[query.name] = generatedQueryPayload;
|
|
|
|
|
legendMap[query.name] = query.legend || '';
|
2021-09-23 15:43:43 +05:30
|
|
|
});
|
|
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
queryData[WIDGET_QUERY_BUILDER_FORMULA_KEY_NAME].map((formula) => {
|
|
|
|
|
const generatedFormulaPayload = {};
|
|
|
|
|
generatedFormulaPayload.queryName = formula.name;
|
|
|
|
|
generatedFormulaPayload.expression = formula.expression;
|
|
|
|
|
generatedFormulaPayload.disabled = formula.disabled;
|
|
|
|
|
builderQueries[formula.name] = generatedFormulaPayload;
|
|
|
|
|
});
|
|
|
|
|
QueryPayload.compositeMetricQuery.builderQueries = builderQueries;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case EQueryType.CLICKHOUSE: {
|
|
|
|
|
const chQueries = {};
|
|
|
|
|
queryData.map((query) => {
|
|
|
|
|
if (!query.rawQuery) return;
|
|
|
|
|
chQueries[query.name] = {
|
|
|
|
|
query: query.rawQuery,
|
|
|
|
|
disabled: query.disabled,
|
|
|
|
|
};
|
|
|
|
|
legendMap[query.name] = query.legend;
|
|
|
|
|
});
|
|
|
|
|
QueryPayload.compositeMetricQuery.chQueries = chQueries;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case EQueryType.PROM: {
|
|
|
|
|
const promQueries = {};
|
|
|
|
|
queryData.map((query) => {
|
|
|
|
|
if (!query.query) return;
|
|
|
|
|
promQueries[query.name] = {
|
|
|
|
|
query: query.query,
|
|
|
|
|
disabled: query.disabled,
|
|
|
|
|
};
|
|
|
|
|
legendMap[query.name] = query.legend;
|
2021-09-23 15:43:43 +05:30
|
|
|
});
|
2022-06-24 15:00:21 +05:30
|
|
|
QueryPayload.compositeMetricQuery.promQueries = promQueries;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { globalTime } = store.getState();
|
|
|
|
|
|
|
|
|
|
const minMax = GetMinMax(globalSelectedInterval, [
|
|
|
|
|
globalTime.minTime / 1000000,
|
|
|
|
|
globalTime.maxTime / 1000000,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const getMaxMinTime = GetMaxMinTime({
|
|
|
|
|
graphType: null,
|
|
|
|
|
maxTime: minMax.maxTime,
|
|
|
|
|
minTime: minMax.minTime,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { end, start } = GetStartAndEndTime({
|
|
|
|
|
type: selectedTime,
|
|
|
|
|
maxTime: getMaxMinTime.maxTime,
|
|
|
|
|
minTime: getMaxMinTime.minTime,
|
|
|
|
|
});
|
|
|
|
|
const response = await getMetricsQueryRange({
|
|
|
|
|
start: parseInt(start, 10) * 1e3,
|
|
|
|
|
end: parseInt(end, 10) * 1e3,
|
|
|
|
|
step: getStep({ start, end, inputFormat: 'ms' }),
|
|
|
|
|
...QueryPayload,
|
|
|
|
|
});
|
|
|
|
|
if (response.statusCode >= 400) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`API responded with ${response.statusCode} - ${response.error}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (response.payload?.data?.result) {
|
|
|
|
|
response.payload.data.result = response.payload.data.result.map(
|
|
|
|
|
(queryData) => {
|
|
|
|
|
const newQueryData = queryData;
|
2022-06-29 16:24:49 +05:30
|
|
|
newQueryData.legend = legendMap[queryData.queryName]; // Adds the legend if it is already defined by the user.
|
|
|
|
|
// If metric names is an empty object
|
2022-06-24 15:00:21 +05:30
|
|
|
if (isEmpty(queryData.metric)) {
|
2022-06-29 16:24:49 +05:30
|
|
|
// If metrics list is empty && the user haven't defined a legend then add the legend equal to the name of the query.
|
|
|
|
|
if (!newQueryData.legend) {
|
|
|
|
|
newQueryData.legend = queryData.queryName;
|
|
|
|
|
}
|
|
|
|
|
// If name of the query and the legend if inserted is same then add the same to the metrics object.
|
|
|
|
|
if (queryData.queryName === newQueryData.legend) {
|
|
|
|
|
newQueryData.metric[queryData.queryName] = queryData.queryName;
|
|
|
|
|
}
|
2022-06-24 15:00:21 +05:30
|
|
|
}
|
2022-06-29 16:24:49 +05:30
|
|
|
return newQueryData;
|
2022-06-24 15:00:21 +05:30
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const GetQueryResults = (
|
|
|
|
|
props: GetQueryResultsProps,
|
|
|
|
|
): ((dispatch: Dispatch<AppActions>) => void) => {
|
|
|
|
|
return async (dispatch: Dispatch<AppActions>): Promise<void> => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await GetMetricQueryRange(props);
|
|
|
|
|
|
|
|
|
|
const isError = response.error;
|
|
|
|
|
|
|
|
|
|
if (isError != null) {
|
2021-09-23 15:43:43 +05:30
|
|
|
dispatch({
|
|
|
|
|
type: 'QUERY_ERROR',
|
|
|
|
|
payload: {
|
2022-06-24 15:00:21 +05:30
|
|
|
errorMessage: isError || '',
|
2021-09-23 15:43:43 +05:30
|
|
|
widgetId: props.widgetId,
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-06-24 15:00:21 +05:30
|
|
|
return;
|
2021-09-23 15:43:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'QUERY_SUCCESS',
|
|
|
|
|
payload: {
|
|
|
|
|
widgetId: props.widgetId,
|
2022-06-24 15:00:21 +05:30
|
|
|
data: {
|
|
|
|
|
queryData: response.payload?.data?.result
|
|
|
|
|
? response.payload?.data?.result
|
|
|
|
|
: [],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'QUERY_ERROR',
|
|
|
|
|
payload: {
|
|
|
|
|
errorMessage: '',
|
|
|
|
|
widgetId: props.widgetId,
|
|
|
|
|
errorBoolean: false,
|
2021-09-23 15:43:43 +05:30
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'QUERY_ERROR',
|
|
|
|
|
payload: {
|
|
|
|
|
errorMessage: (error as AxiosError).toString(),
|
|
|
|
|
widgetId: props.widgetId,
|
2022-06-24 15:00:21 +05:30
|
|
|
errorBoolean: true,
|
2021-09-23 15:43:43 +05:30
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export interface GetQueryResultsProps {
|
|
|
|
|
widgetId: string;
|
|
|
|
|
selectedTime: timePreferenceType;
|
2022-06-28 17:32:02 +05:30
|
|
|
query: Query;
|
2021-09-23 15:43:43 +05:30
|
|
|
graphType: ITEMS;
|
2021-12-24 12:00:26 +05:30
|
|
|
globalSelectedInterval: GlobalReducer['selectedTime'];
|
2021-09-23 15:43:43 +05:30
|
|
|
}
|