Skip to content

Commit

Permalink
Refactor discover totalHits code
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Oct 14, 2024
1 parent 2b7c72c commit 3f08047
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const useDiscoverHistogram = ({
}: UseDiscoverHistogramProps) => {
const services = useDiscoverServices();
const savedSearchData$ = stateContainer.dataState.data$;
const savedSearchHits$ = savedSearchData$.totalHits$;
const savedSearchState = useSavedSearch();
const isEsqlMode = useIsEsqlMode();

Expand Down Expand Up @@ -153,10 +154,7 @@ export const useDiscoverHistogram = ({
* Total hits
*/

const setTotalHitsError = useMemo(
() => sendErrorTo(savedSearchData$.totalHits$),
[savedSearchData$.totalHits$]
);
const setTotalHitsError = useMemo(() => sendErrorTo(savedSearchHits$), [savedSearchHits$]);

useEffect(() => {
const subscription = createTotalHitsObservable(unifiedHistogram?.state$)?.subscribe(
Expand All @@ -172,7 +170,7 @@ export const useDiscoverHistogram = ({
return;
}

const { result: totalHitsResult } = savedSearchData$.totalHits$.getValue();
const { result: totalHitsResult } = savedSearchHits$.getValue();

if (
(status === UnifiedHistogramFetchStatus.loading ||
Expand All @@ -184,11 +182,34 @@ export const useDiscoverHistogram = ({
return;
}

// Sync the totalHits$ observable with the unified histogram state
savedSearchData$.totalHits$.next({
fetchStatus: status.toString() as FetchStatus,
result,
});
const fetchStatus = status.toString() as FetchStatus;
if (
fetchStatus === FetchStatus.COMPLETE &&
savedSearchHits$.getValue().fetchStatus === FetchStatus.COMPLETE &&
result !== savedSearchHits$.getValue().result
) {
// this is a workaround to make sure the new total hits value is displayed
// a different value without a loading state in between would lead to be ignored by useDataState
addLog(
'[UnifiedHistogram] send loading to totalHits$ to make sure the new value is displayed',
{ status, result }
);
savedSearchHits$.next({
fetchStatus: FetchStatus.LOADING,
});
}

const isSavedSearchLoading =
savedSearchHits$.getValue().fetchStatus === FetchStatus.LOADING;
const isUnifiedHistogramLoading = status === UnifiedHistogramFetchStatus.loading;

// Sync the totalHits$ observable with the unified histogram state unless both are set to loading
if (isSavedSearchLoading !== isUnifiedHistogramLoading) {
savedSearchHits$.next({
fetchStatus,
result,
});
}

if (status !== UnifiedHistogramFetchStatus.complete || typeof result !== 'number') {
return;
Expand All @@ -205,7 +226,7 @@ export const useDiscoverHistogram = ({
}, [
isEsqlMode,
savedSearchData$.main$,
savedSearchData$.totalHits$,
savedSearchHits$,
setTotalHitsError,
stateContainer.appState,
unifiedHistogram?.state$,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export function fetchAll(
sendLoadingMsg(dataSubjects.totalHits$, {
result: dataSubjects.totalHits$.getValue().result,
});
} else {
sendLoadingMsg(dataSubjects.totalHits$);
}

// Start fetching all required requests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ export function getDataStateContainer({
documents$: new BehaviorSubject<DataDocumentsMsg>(initialState),
totalHits$: new BehaviorSubject<DataTotalHitsMsg>(initialState),
};
// This is debugging code, helping you to understand which messages are sent to the data observables
// Adding a debugger in the functions can be helpful to understand what triggers a message
// dataSubjects.main$.subscribe((msg) => addLog('dataSubjects.main$', msg));
// dataSubjects.documents$.subscribe((msg) => addLog('dataSubjects.documents$', msg));
// dataSubjects.totalHits$.subscribe((msg) => addLog('dataSubjects.totalHits$', msg););
// Add window.ELASTIC_DISCOVER_LOGGER = 'debug' to see messages in console

let autoRefreshDone: AutoRefreshDoneFn | undefined | null = null;
/**
Expand Down

0 comments on commit 3f08047

Please sign in to comment.