2023-06-01 20:17:09 +03:00
|
|
|
import {
|
|
|
|
|
initialFormulaBuilderFormValues,
|
2023-06-16 13:38:39 +03:00
|
|
|
initialQueryBuilderFormValuesMap,
|
2023-06-01 20:17:09 +03:00
|
|
|
} from 'constants/queryBuilder';
|
|
|
|
|
import { FORMULA_REGEXP } from 'constants/regExp';
|
2024-07-23 17:20:31 +05:30
|
|
|
import { isUndefined } from 'lodash-es';
|
2023-06-01 20:17:09 +03:00
|
|
|
import {
|
|
|
|
|
BuilderQueryDataResourse,
|
|
|
|
|
IBuilderFormula,
|
|
|
|
|
IBuilderQuery,
|
|
|
|
|
} from 'types/api/queryBuilder/queryBuilderData';
|
|
|
|
|
import { QueryBuilderData } from 'types/common/queryBuilder';
|
|
|
|
|
|
|
|
|
|
export const transformQueryBuilderDataModel = (
|
|
|
|
|
data: BuilderQueryDataResourse,
|
2024-07-23 17:20:31 +05:30
|
|
|
query?: QueryBuilderData,
|
2023-06-01 20:17:09 +03:00
|
|
|
): QueryBuilderData => {
|
|
|
|
|
const queryData: QueryBuilderData['queryData'] = [];
|
|
|
|
|
const queryFormulas: QueryBuilderData['queryFormulas'] = [];
|
|
|
|
|
|
|
|
|
|
Object.entries(data).forEach(([, value]) => {
|
|
|
|
|
if (FORMULA_REGEXP.test(value.queryName)) {
|
|
|
|
|
const formula = value as IBuilderFormula;
|
2024-07-23 17:20:31 +05:30
|
|
|
const baseFormula = query?.queryFormulas?.find(
|
|
|
|
|
(f) => f.queryName === value.queryName,
|
|
|
|
|
);
|
|
|
|
|
if (!isUndefined(baseFormula)) {
|
|
|
|
|
// this is part of the flow where we create alerts from dashboard.
|
|
|
|
|
// we pass the formula as is from the widget query as we do not want anything to update in formula from the format api call
|
|
|
|
|
queryFormulas.push({ ...baseFormula });
|
|
|
|
|
} else {
|
|
|
|
|
queryFormulas.push({ ...initialFormulaBuilderFormValues, ...formula });
|
|
|
|
|
}
|
2023-06-01 20:17:09 +03:00
|
|
|
} else {
|
2024-07-23 17:20:31 +05:30
|
|
|
const queryFromData = value as IBuilderQuery;
|
|
|
|
|
const baseQuery = query?.queryData?.find(
|
|
|
|
|
(q) => q.queryName === queryFromData.queryName,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!isUndefined(baseQuery)) {
|
|
|
|
|
// this is part of the flow where we create alerts from dashboard.
|
|
|
|
|
// we pass the widget query as the base query and accept the filters from the format API response.
|
|
|
|
|
// which fills the variable values inside the same and is used to create alerts
|
|
|
|
|
// do not accept the full object as the stepInterval field is subject to changes
|
|
|
|
|
queryData.push({
|
|
|
|
|
...baseQuery,
|
|
|
|
|
filters: queryFromData.filters,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
queryData.push({
|
|
|
|
|
...initialQueryBuilderFormValuesMap.metrics,
|
|
|
|
|
...queryFromData,
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-06-01 20:17:09 +03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { queryData, queryFormulas };
|
|
|
|
|
};
|