[8.6] [Guided onboarding] Fix API requests spam when request fails (#145728) (#146456)

# Backport

This will backport the following commits from `main` to `8.6`:
- [[Guided onboarding] Fix API requests spam when request fails
(#145728)](https://github.com/elastic/kibana/pull/145728)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Yulia
Čech","email":"6585477+yuliacech@users.noreply.github.com"},"sourceCommit":{"committedDate":"2022-11-28T18:03:03Z","message":"[Guided
onboarding] Fix API requests spam when request fails
(#145728)","sha":"91ae3df37d549d57e69f63a28ab8b74fa944b44a","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","auto-backport","backport:prev-minor","Team:Journey/Onboarding","v8.6.0"],"number":145728,"url":"https://github.com/elastic/kibana/pull/145728","mergeCommit":{"message":"[Guided
onboarding] Fix API requests spam when request fails
(#145728)","sha":"91ae3df37d549d57e69f63a28ab8b74fa944b44a"}},"sourceBranch":"main","suggestedTargetBranches":["8.6"],"targetPullRequestStates":[{"branch":"8.6","label":"v8.6.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Yulia Čech <6585477+yuliacech@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2022-11-28 14:13:26 -05:00 committed by GitHub
parent 8bb5ea2b5e
commit d8a98e2d05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 12 deletions

View file

@ -15,8 +15,15 @@ import { GuideState } from '@kbn/guided-onboarding';
* complete: at least one guide has been completed
* quit: the user quit a guide before completion
* skipped: the user skipped on the landing page
* error: unable to retrieve the plugin state from saved objects
*/
export type PluginStatus = 'not_started' | 'in_progress' | 'complete' | 'quit' | 'skipped';
export type PluginStatus =
| 'not_started'
| 'in_progress'
| 'complete'
| 'quit'
| 'skipped'
| 'error';
export interface PluginState {
status: PluginStatus;

View file

@ -70,13 +70,13 @@ describe('GuidedOnboarding ApiService', () => {
expect(httpClient.get).toHaveBeenCalledTimes(1);
});
it(`re-sends the request if the previous one failed`, async () => {
it(`doesn't send multiple requests if the request failed`, async () => {
httpClient.get.mockRejectedValueOnce(new Error('request failed'));
subscription = apiService.fetchPluginState$().subscribe();
// wait until the request fails
await new Promise((resolve) => process.nextTick(resolve));
anotherSubscription = apiService.fetchPluginState$().subscribe();
expect(httpClient.get).toHaveBeenCalledTimes(2);
expect(httpClient.get).toHaveBeenCalledTimes(1);
});
it(`re-sends the request if the subscription was unsubscribed before the request completed`, async () => {

View file

@ -55,7 +55,13 @@ export class ApiService implements GuidedOnboardingApi {
})
.catch((error) => {
this.isPluginStateLoading = false;
observer.error(error);
// if the request fails, we initialize the state with error
observer.next({ status: 'error', isActivePeriod: false });
this.pluginState$.next({
status: 'error',
isActivePeriod: false,
});
observer.complete();
});
return () => {
this.isPluginStateLoading = false;

View file

@ -40,14 +40,7 @@ export const getPluginState = async (savedObjectsClient: SavedObjectsClient) =>
return pluginState;
} else {
// create a SO to keep track of the correct creation date
try {
await updatePluginStatus(savedObjectsClient, 'not_started');
// @yulia, we need to add a user permissions
// check here instead of swallowing this error
// see issue: https://github.com/elastic/kibana/issues/145434
// eslint-disable-next-line no-empty
} catch (e) {}
await updatePluginStatus(savedObjectsClient, 'not_started');
return {
status: 'not_started',
isActivePeriod: true,