Skip to content

Commit

Permalink
chore: refactored isDefaultQuery to use a function to extract relevan…
Browse files Browse the repository at this point in the history
…t keys and use lodash's isEqual
  • Loading branch information
ahmadshaheer committed Dec 11, 2024
1 parent 29f41c9 commit be9c851
Showing 1 changed file with 53 additions and 39 deletions.
92 changes: 53 additions & 39 deletions frontend/src/providers/QueryBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { createIdFromObjectFields } from 'lib/createIdFromObjectFields';
import { createNewBuilderItemName } from 'lib/newQueryBuilder/createNewBuilderItemName';
import { getOperatorsBySourceAndPanelType } from 'lib/newQueryBuilder/getOperatorsBySourceAndPanelType';
import { replaceIncorrectObjectFields } from 'lib/replaceIncorrectObjectFields';
import { cloneDeep, get, merge, set } from 'lodash-es';
import { cloneDeep, get, isEqual, merge, set } from 'lodash-es';
import {
createContext,
PropsWithChildren,
Expand Down Expand Up @@ -270,44 +270,58 @@ export function QueryBuilderProvider({
return false;
}

const defaultBuilderData = defaultQuery.builder
.queryData[0] as IBuilderQuery;

const currentBuilderData = currentQuery.builder
.queryData[0] as IBuilderQuery;

// Check if filters have same operator and length
const isFiltersEqual =
currentBuilderData.filters.op === defaultBuilderData.filters.op &&
currentBuilderData.filters.items.length ===
defaultBuilderData.filters.items.length;

// Compare builder data fields
const fieldsMatch =
currentBuilderData.dataSource === defaultBuilderData.dataSource &&
currentBuilderData.queryName === defaultBuilderData.queryName &&
currentBuilderData.aggregateOperator ===
defaultBuilderData.aggregateOperator &&
currentBuilderData.timeAggregation === defaultBuilderData.timeAggregation &&
currentBuilderData.spaceAggregation ===
defaultBuilderData.spaceAggregation &&
currentBuilderData.expression === defaultBuilderData.expression &&
currentBuilderData.disabled === defaultBuilderData.disabled &&
currentBuilderData.stepInterval === defaultBuilderData.stepInterval &&
currentBuilderData.reduceTo === defaultBuilderData.reduceTo &&
currentBuilderData.legend === defaultBuilderData.legend;

// Compare array fields
const arrayFieldsMatch =
isFiltersEqual &&
JSON.stringify(currentBuilderData.functions) ===
JSON.stringify(defaultBuilderData.functions) &&
JSON.stringify(currentBuilderData.having) ===
JSON.stringify(defaultBuilderData.having) &&
JSON.stringify(currentBuilderData.groupBy) ===
JSON.stringify(defaultBuilderData.groupBy);

return fieldsMatch && arrayFieldsMatch;
const extractRelevantKeys = (queryData: IBuilderQuery): IBuilderQuery => {
const {
dataSource,
queryName,
aggregateOperator,
aggregateAttribute,
timeAggregation,
spaceAggregation,
functions,
filters,
expression,
disabled,
stepInterval,
having,
groupBy,
legend,
} = queryData;

return {
dataSource,
queryName,
aggregateOperator,
// remove id from aggregateAttribute
aggregateAttribute: {
...aggregateAttribute,
id: '',
},
timeAggregation,
spaceAggregation,
functions,
filters,
expression,
disabled,
stepInterval,
having,
groupBy,
legend,
// set to default values
orderBy: [],
limit: null,
reduceTo: 'avg',
};
};

const currentBuilderData = extractRelevantKeys(
currentQuery.builder.queryData[0],
);
const defaultBuilderData = extractRelevantKeys(
defaultQuery.builder.queryData[0],
);

return isEqual(currentBuilderData, defaultBuilderData);
},
[updateAllQueriesOperators],
);
Expand Down

0 comments on commit be9c851

Please sign in to comment.