Skip to content

Commit

Permalink
fix: fix the issue of saved view overriding query for logs and traces…
Browse files Browse the repository at this point in the history
… for builder type query
  • Loading branch information
ahmadshaheer committed Dec 11, 2024
1 parent b25df66 commit 29f41c9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
14 changes: 13 additions & 1 deletion frontend/src/container/ExplorerOptions/ExplorerOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ function ExplorerOptions({
panelType,
isStagedQueryUpdated,
redirectWithQueryBuilderData,
isDefaultQuery,
} = useQueryBuilder();

const handleSaveViewModalToggle = (): void => {
Expand Down Expand Up @@ -480,6 +481,11 @@ function ExplorerOptions({
] = useState(false);

useEffect(() => {
// If the query is not the default query, don't set the recently used saved view
if (!isDefaultQuery({ currentQuery, sourcePage: sourcepage })) {
return;
}

const parsedPreservedView = JSON.parse(
localStorage.getItem(PRESERVED_VIEW_LOCAL_STORAGE_KEY) || '{}',
);
Expand All @@ -501,12 +507,18 @@ function ExplorerOptions({
setIsRecentlyUsedSavedViewSelected(false);
}

return (): void => clearTimeout(timeoutId);
// eslint-disable-next-line consistent-return
return (): void => {
clearTimeout(timeoutId);
};
}, [
PRESERVED_VIEW_LOCAL_STORAGE_KEY,
PRESERVED_VIEW_TYPE,
currentQuery,
isDefaultQuery,
isRecentlyUsedSavedViewSelected,
onMenuItemSelectHandler,
sourcepage,
viewKey,
viewName,
viewsData?.data?.data,
Expand Down
64 changes: 64 additions & 0 deletions frontend/src/providers/QueryBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { ViewProps } from 'types/api/saveViews/types';
import { EQueryType } from 'types/common/dashboard';
import {
DataSource,
IsDefaultQueryProps,
QueryBuilderContextType,
QueryBuilderData,
} from 'types/common/queryBuilder';
Expand Down Expand Up @@ -87,6 +88,7 @@ export const QueryBuilderContext = createContext<QueryBuilderContextType>({
initQueryBuilderData: () => {},
handleOnUnitsChange: () => {},
isStagedQueryUpdated: () => false,
isDefaultQuery: () => false,
});

export function QueryBuilderProvider({
Expand Down Expand Up @@ -249,6 +251,66 @@ export function QueryBuilderProvider({
[getElementWithActualOperator],
);

const isDefaultQuery = useCallback(
({ currentQuery, sourcePage }: IsDefaultQueryProps): boolean => {
// Get default query with updated operators
const defaultQuery = updateAllQueriesOperators(
initialQueriesMap[sourcePage],
PANEL_TYPES.LIST,
sourcePage,
);

// Early return if query types don't match
if (currentQuery.queryType !== defaultQuery.queryType) {
return false;
}

// Only compare builder queries
if (currentQuery.queryType !== EQueryType.QUERY_BUILDER) {
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;
},
[updateAllQueriesOperators],
);
const updateQueriesData = useCallback(
<T extends keyof QueryBuilderData>(
query: Query,
Expand Down Expand Up @@ -883,6 +945,7 @@ export function QueryBuilderProvider({
handleRunQuery,
resetQuery,
updateAllQueriesOperators,
isDefaultQuery,
updateQueriesData,
initQueryBuilderData,
handleOnUnitsChange,
Expand All @@ -909,6 +972,7 @@ export function QueryBuilderProvider({
redirectWithQueryBuilderData,
handleRunQuery,
updateAllQueriesOperators,
isDefaultQuery,
updateQueriesData,
initQueryBuilderData,
handleOnUnitsChange,
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/types/common/queryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,15 @@ export type QueryBuilderContextType = {
viewData: ViewProps[] | undefined,
viewKey: string,
) => boolean;
isDefaultQuery: (props: IsDefaultQueryProps) => boolean;
};

export type QueryAdditionalFilter = {
field: keyof IBuilderQuery;
text: string;
};

export type IsDefaultQueryProps = {
currentQuery: Query;
sourcePage: DataSource;
};

0 comments on commit 29f41c9

Please sign in to comment.