Skip to content

Commit

Permalink
feat(headless): executeSearch action for insight refactored to be com…
Browse files Browse the repository at this point in the history
…patible with the new event protocol (#3678)

## [SFINT-5418](https://coveord.atlassian.net/browse/SFINT-5418)

In this PR:

- I adapted the `ExecuteSearch` action for the insight use case to be
compatible with the new event protocol, instead of accepting an insight
analytics action as a parameter now it accepts a transitive action:
  ```
  interface TransitiveInsightSearchAction {
    legacy: LegacyInsightAction;
    next?: InsightSearchAction;
  }
  ```
when the analytics mode will be set to `legacy`, the `ExecuteSearch`
action will keep working exactly how it was before by executing the
search and logging a search event.
However, when the analytics mode will be set to `next`, we will no
longer send search event.

- I refactored the logic executed inside the `ExecuteSearch` insight
action to make it follow the pattern of the `ExecuteSearch` search
action.
This new pattern implements the class
`AsyncInsightSearchThunkProcessor`, this class will take care of doing
the search query and will take care of properly processing its response.
This new logic allows us to better structure and organize the code and
it makes it easier to test the logic executed by the `ExecuteSearch`
insight action

[SFINT-5418]:
https://coveord.atlassian.net/browse/SFINT-5418?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

---------

Co-authored-by: Sylvie Allain <58052881+sallainCoveo@users.noreply.github.com>
  • Loading branch information
mmitiche and sallainCoveo authored Mar 11, 2024
1 parent a3f5b9c commit b18d0ce
Show file tree
Hide file tree
Showing 26 changed files with 1,105 additions and 296 deletions.
2 changes: 1 addition & 1 deletion packages/headless/src/app/insight-engine/insight-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function buildInsightEngine(
return;
}

engine.dispatch(executeSearch(analyticsEvent));
engine.dispatch(executeSearch({legacy: analyticsEvent}));
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function buildBreadcrumbManager(
dispatch(
updateFreezeCurrentValues({facetId, freezeCurrentValues: false})
);
dispatch(executeSearch(analyticsAction));
dispatch(executeSearch({legacy: analyticsAction}));
},
executeToggleExclude: ({facetId, selection}) => {
const analyticsAction = logFacetBreadcrumb({
Expand All @@ -118,7 +118,7 @@ export function buildBreadcrumbManager(
dispatch(
updateFreezeCurrentValues({facetId, freezeCurrentValues: false})
);
dispatch(executeSearch(analyticsAction));
dispatch(executeSearch({legacy: analyticsAction}));
},
facetValuesSelector: facetResponseActiveValuesSelector,
};
Expand All @@ -133,11 +133,11 @@ export function buildBreadcrumbManager(
facetSet: getState().numericFacetSet,
executeToggleSelect: (payload) => {
dispatch(toggleSelectNumericFacetValue(payload));
dispatch(executeSearch(logNumericFacetBreadcrumb(payload)));
dispatch(executeSearch({legacy: logNumericFacetBreadcrumb(payload)}));
},
executeToggleExclude: (payload) => {
dispatch(toggleExcludeNumericFacetValue(payload));
dispatch(executeSearch(logNumericFacetBreadcrumb(payload)));
dispatch(executeSearch({legacy: logNumericFacetBreadcrumb(payload)}));
},
facetValuesSelector: numericFacetActiveValuesSelector,
};
Expand All @@ -152,11 +152,11 @@ export function buildBreadcrumbManager(
facetSet: getState().dateFacetSet,
executeToggleSelect: (payload) => {
dispatch(toggleSelectDateFacetValue(payload));
dispatch(executeSearch(logDateFacetBreadcrumb(payload)));
dispatch(executeSearch({legacy: logDateFacetBreadcrumb(payload)}));
},
executeToggleExclude: (payload) => {
dispatch(toggleExcludeDateFacetValue(payload));
dispatch(executeSearch(logDateFacetBreadcrumb(payload)));
dispatch(executeSearch({legacy: logDateFacetBreadcrumb(payload)}));
},
facetValuesSelector: dateFacetActiveValuesSelector,
};
Expand All @@ -176,14 +176,14 @@ export function buildBreadcrumbManager(
deselect: () => {
dispatch(deselectAllCategoryFacetValues(facetId));
dispatch(
executeSearch(
logCategoryFacetBreadcrumb({
executeSearch({
legacy: logCategoryFacetBreadcrumb({
categoryFacetPath: path.map(
(categoryFacetValue) => categoryFacetValue.value
),
categoryFacetId: facetId,
})
)
}),
})
);
},
};
Expand Down Expand Up @@ -229,7 +229,7 @@ export function buildBreadcrumbManager(
} else if (value.state === 'excluded') {
dispatch(toggleExcludeStaticFilterValue({id, value}));
}
dispatch(executeSearch(analytics));
dispatch(executeSearch({legacy: analytics}));
},
};
};
Expand Down Expand Up @@ -260,7 +260,7 @@ export function buildBreadcrumbManager(

deselectAll: () => {
controller.deselectAll();
dispatch(executeSearch(logClearBreadcrumbs()));
dispatch(executeSearch({legacy: logClearBreadcrumbs()}));
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function buildDidYouMean(engine: InsightEngine): DidYouMean {

applyCorrection() {
controller.applyCorrection();
dispatch(executeSearch(logDidYouMeanClick()));
dispatch(executeSearch({legacy: logDidYouMeanClick()}));
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,23 @@ export function buildCategoryFacet(
getFacetId(),
selection
);
dispatch(executeSearch(analyticsAction));
dispatch(executeSearch({legacy: analyticsAction}));
},

deselectAll() {
coreController.deselectAll();
dispatch(executeSearch(logFacetClearAll(getFacetId())));
dispatch(executeSearch({legacy: logFacetClearAll(getFacetId())}));
},

sortBy(criterion: CategoryFacetSortCriterion) {
coreController.sortBy(criterion);
dispatch(
executeSearch(
logFacetUpdateSort({facetId: getFacetId(), sortCriterion: criterion})
)
executeSearch({
legacy: logFacetUpdateSort({
facetId: getFacetId(),
sortCriterion: criterion,
}),
})
);
},

Expand All @@ -115,12 +118,12 @@ export function buildCategoryFacet(

showMoreValues() {
coreController.showMoreValues();
dispatch(fetchFacetValues(logFacetShowMore(getFacetId())));
dispatch(fetchFacetValues({legacy: logFacetShowMore(getFacetId())}));
},

showLessValues() {
coreController.showLessValues();
dispatch(fetchFacetValues(logFacetShowLess(getFacetId())));
dispatch(fetchFacetValues({legacy: logFacetShowLess(getFacetId())}));
},

get state() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,26 @@ export function buildFacet(engine: InsightEngine, props: FacetProps): Facet {
toggleSelect(selection) {
coreController.toggleSelect(selection);
dispatch(
executeSearch(
getInsightAnalyticsActionForToggleFacetSelect(getFacetId(), selection)
)
executeSearch({
legacy: getInsightAnalyticsActionForToggleFacetSelect(
getFacetId(),
selection
),
})
);
},

deselectAll() {
coreController.deselectAll();
dispatch(executeSearch(logFacetClearAll(getFacetId())));
dispatch(executeSearch({legacy: logFacetClearAll(getFacetId())}));
},

sortBy(sortCriterion: FacetSortCriterion) {
coreController.sortBy(sortCriterion);
dispatch(
executeSearch(
logFacetUpdateSort({facetId: getFacetId(), sortCriterion})
)
executeSearch({
legacy: logFacetUpdateSort({facetId: getFacetId(), sortCriterion}),
})
);
},

Expand All @@ -153,12 +156,12 @@ export function buildFacet(engine: InsightEngine, props: FacetProps): Facet {

showMoreValues() {
coreController.showMoreValues();
dispatch(fetchFacetValues(logFacetShowMore(getFacetId())));
dispatch(fetchFacetValues({legacy: logFacetShowMore(getFacetId())}));
},

showLessValues() {
coreController.showLessValues();
dispatch(fetchFacetValues(logFacetShowLess(getFacetId())));
dispatch(fetchFacetValues({legacy: logFacetShowLess(getFacetId())}));
},

get state() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,27 @@ export function buildDateFacet(

deselectAll() {
coreController.deselectAll();
dispatch(executeSearch(logFacetClearAll(getFacetId())));
dispatch(executeSearch({legacy: logFacetClearAll(getFacetId())}));
},

sortBy(sortCriterion: RangeFacetSortCriterion) {
coreController.sortBy(sortCriterion);
dispatch(
executeSearch(
logFacetUpdateSort({facetId: getFacetId(), sortCriterion})
)
executeSearch({
legacy: logFacetUpdateSort({facetId: getFacetId(), sortCriterion}),
})
);
},

toggleSelect: (selection: DateFacetValue) => {
coreController.toggleSelect(selection);
dispatch(
executeSearch(
getInsightAnalyticsActionForToggleRangeFacetSelect(
executeSearch({
legacy: getInsightAnalyticsActionForToggleRangeFacetSelect(
getFacetId(),
selection
)
)
),
})
);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ export function buildDateFilter(
...coreController,
clear: () => {
coreController.clear();
dispatch(executeSearch(logFacetClearAll(getFacetId())));
dispatch(executeSearch({legacy: logFacetClearAll(getFacetId())}));
},
setRange: (range) => {
const success = coreController.setRange(range);
if (success) {
dispatch(
executeSearch(
logFacetSelect({
executeSearch({
legacy: logFacetSelect({
facetId: getFacetId(),
facetValue: `${range.start}..${range.end}`,
})
)
}),
})
);
}
return success;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,27 @@ export function buildNumericFacet(

deselectAll() {
coreController.deselectAll();
dispatch(executeSearch(logFacetClearAll(getFacetId())));
dispatch(executeSearch({legacy: logFacetClearAll(getFacetId())}));
},

sortBy(sortCriterion: RangeFacetSortCriterion) {
coreController.sortBy(sortCriterion);
dispatch(
executeSearch(
logFacetUpdateSort({facetId: getFacetId(), sortCriterion})
)
executeSearch({
legacy: logFacetUpdateSort({facetId: getFacetId(), sortCriterion}),
})
);
},

toggleSelect: (selection: NumericFacetValue) => {
coreController.toggleSelect(selection);
dispatch(
executeSearch(
getInsightAnalyticsActionForToggleRangeFacetSelect(
executeSearch({
legacy: getInsightAnalyticsActionForToggleRangeFacetSelect(
getFacetId(),
selection
)
)
),
})
);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ export function buildNumericFilter(
...coreController,
clear: () => {
coreController.clear();
dispatch(executeSearch(logFacetClearAll(getFacetId())));
dispatch(executeSearch({legacy: logFacetClearAll(getFacetId())}));
},
setRange: (range) => {
const success = coreController.setRange(range);
if (success) {
dispatch(
executeSearch(
logFacetSelect({
executeSearch({
legacy: logFacetSelect({
facetId: getFacetId(),
facetValue: `${range.start}..${range.end}`,
})
)
}),
})
);
}
return success;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,22 @@ export function buildGeneratedAnswer(

retry() {
dispatch(
executeSearch(
generatedAnswerInsightAnalyticsClient.logRetryGeneratedAnswer()
)
executeSearch({
legacy:
generatedAnswerInsightAnalyticsClient.logRetryGeneratedAnswer(),
})
);
},

rephrase(responseFormat: GeneratedResponseFormat) {
controller.rephrase(responseFormat);
dispatch(
executeSearch(
generatedAnswerInsightAnalyticsClient.logRephraseGeneratedAnswer(
responseFormat
)
)
executeSearch({
legacy:
generatedAnswerInsightAnalyticsClient.logRephraseGeneratedAnswer(
responseFormat
),
})
);
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ export function buildPager(

selectPage(page: number) {
pager.selectPage(page);
dispatch(fetchPage(logPageNumber()));
dispatch(fetchPage({legacy: logPageNumber()}));
},

nextPage() {
pager.nextPage();
dispatch(fetchPage(logPageNext()));
dispatch(fetchPage({legacy: logPageNext()}));
},

previousPage() {
pager.previousPage();
dispatch(fetchPage(logPagePrevious()));
dispatch(fetchPage({legacy: logPagePrevious()}));
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function buildResultsPerPage(

set(num: number) {
coreController.set(num);
dispatch(executeSearch(logPagerResize()));
dispatch(executeSearch({legacy: logPagerResize()}));
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function buildSearchBox(
...props,
executeSearchActionCreator: executeSearch,
fetchQuerySuggestionsActionCreator: fetchQuerySuggestions,
isNextAnalyticsReady: false,
isNextAnalyticsReady: true,
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export function buildSearchParameterManager(
return;
}
controller.synchronize(parameters);
dispatch(executeSearch(logParametersChange(oldParams, newParams)));
dispatch(
executeSearch({legacy: logParametersChange(oldParams, newParams)})
);
},

get state() {
Expand Down
Loading

0 comments on commit b18d0ce

Please sign in to comment.