Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Traces Explorer): prevent duplicate API calls to query_range in traces explorer #6677

64 changes: 45 additions & 19 deletions frontend/src/container/TracesExplorer/ListView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { REACT_QUERY_KEY } from 'constants/reactQueryKeys';
import EmptyLogsSearch from 'container/EmptyLogsSearch/EmptyLogsSearch';
import NoLogs from 'container/NoLogs/NoLogs';
import { useOptionsMenu } from 'container/OptionsMenu';
import { CustomTimeType } from 'container/TopNav/DateTimeSelectionV2/config';
import TraceExplorerControls from 'container/TracesExplorer/Controls';
import { useGetQueryRange } from 'hooks/queryBuilder/useGetQueryRange';
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
import { Pagination } from 'hooks/queryPagination';
import { getDefaultPaginationConfig } from 'hooks/queryPagination/utils';
import useDragColumns from 'hooks/useDragColumns';
import { getDraggedColumns } from 'hooks/useDragColumns/utils';
import useUrlQueryData from 'hooks/useUrlQueryData';
Expand All @@ -32,12 +34,19 @@ interface ListViewProps {
}

function ListView({ isFilterApplied }: ListViewProps): JSX.Element {
const { stagedQuery, panelType } = useQueryBuilder();
const {
stagedQuery,
panelType: panelTypeFromQueryBuilder,
} = useQueryBuilder();

const { selectedTime: globalSelectedTime, maxTime, minTime } = useSelector<
AppState,
GlobalReducer
>((state) => state.globalTime);
const panelType = panelTypeFromQueryBuilder || PANEL_TYPES.LIST;

const {
selectedTime: globalSelectedTime,
maxTime,
minTime,
loading: timeRangeUpdateLoading,
} = useSelector<AppState, GlobalReducer>((state) => state.globalTime);

const { options, config } = useOptionsMenu({
storageKey: LOCALSTORAGE.TRACES_LIST_OPTIONS,
Expand All @@ -55,34 +64,51 @@ function ListView({ isFilterApplied }: ListViewProps): JSX.Element {
const { queryData: paginationQueryData } = useUrlQueryData<Pagination>(
QueryParams.pagination,
);
const paginationConfig =
paginationQueryData ?? getDefaultPaginationConfig(PER_PAGE_OPTIONS);

const queryKey = useMemo(
() => [
REACT_QUERY_KEY.GET_QUERY_RANGE,
globalSelectedTime,
maxTime,
minTime,
stagedQuery,
panelType,
paginationConfig,
options?.selectColumns,
],
[
stagedQuery,
panelType,
globalSelectedTime,
paginationConfig,
options?.selectColumns,
maxTime,
minTime,
],
);

const { data, isFetching, isLoading, isError } = useGetQueryRange(
{
query: stagedQuery || initialQueriesMap.traces,
graphType: panelType || PANEL_TYPES.LIST,
selectedTime: 'GLOBAL_TIME',
globalSelectedInterval: globalSelectedTime,
graphType: panelType,
selectedTime: 'GLOBAL_TIME' as const,
globalSelectedInterval: globalSelectedTime as CustomTimeType,
params: {
dataSource: 'traces',
},
tableParams: {
pagination: paginationQueryData,
pagination: paginationConfig,
selectColumns: options?.selectColumns,
},
},
DEFAULT_ENTITY_VERSION,
{
queryKey: [
REACT_QUERY_KEY.GET_QUERY_RANGE,
globalSelectedTime,
maxTime,
minTime,
stagedQuery,
panelType,
paginationQueryData,
options?.selectColumns,
],
queryKey,
enabled:
// don't make api call while the time range state in redux is loading
!timeRangeUpdateLoading &&
!!stagedQuery &&
panelType === PANEL_TYPES.LIST &&
!!options?.selectColumns?.length,
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/pages/TracesExplorer/Filter/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ export function Filter(props: FilterProps): JSX.Element {
selectedFilters,
});
}

if (isEqual(currentQuery, preparedQuery)) {
return;
}
redirectWithQueryBuilderData(preparedQuery);
},
[currentQuery, redirectWithQueryBuilderData, selectedFilters],
Expand Down
18 changes: 15 additions & 3 deletions frontend/src/providers/QueryBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,17 @@ export function QueryBuilderProvider({
const urlQuery = useUrlQuery();
const history = useHistory();
const location = useLocation();
const currentPathnameRef = useRef<string | null>(null);
/**
// set location.pathname as the initial value for currentPathnameRef, absense of this causes `stagedQuery && location.pathname !== currentPathnameRef.current` to be truthy (since currentPathnameRef.current is null), and therefore causes duplicate triggering of initQueryBuilderData(compositeQueryParam); in the useEffect of line ~784
ahmadshaheer marked this conversation as resolved.
Show resolved Hide resolved

if (!isValid) {
redirectWithQueryBuilderData(validData);
} else {
initQueryBuilderData(compositeQueryParam);
}
*
*/
const currentPathnameRef = useRef<string | null>(location.pathname);

const { maxTime, minTime } = useSelector<AppState, GlobalReducer>(
(state) => state.globalTime,
Expand Down Expand Up @@ -233,6 +243,7 @@ export function QueryBuilderProvider({
timeUpdated ? merge(currentQuery, newQueryState) : newQueryState,
);
setQueryType(type);
// debugger;
ahmadshaheer marked this conversation as resolved.
Show resolved Hide resolved
},
[prepareQueryBuilderData, currentQuery],
);
Expand Down Expand Up @@ -792,6 +803,7 @@ export function QueryBuilderProvider({
initialQueriesMap.metrics,
);

// debugger;
ahmadshaheer marked this conversation as resolved.
Show resolved Hide resolved
if (!isValid) {
redirectWithQueryBuilderData(validData);
} else {
Expand All @@ -814,14 +826,14 @@ export function QueryBuilderProvider({
};

useEffect(() => {
if (stagedQuery && location.pathname !== currentPathnameRef.current) {
if (location.pathname !== currentPathnameRef.current) {
currentPathnameRef.current = location.pathname;

setStagedQuery(null);
// reset the last used query to 0 when navigating away from the page
setLastUsedQuery(0);
}
}, [location, stagedQuery, currentQuery]);
}, [location.pathname]);

const handleOnUnitsChange = useCallback(
(unit: string) => {
Expand Down
Loading