From 43b16a9ef746efde2d21d3f1d4a09f61e16e2cf8 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 13 Jul 2022 10:48:18 +0200 Subject: [PATCH] [ML] Explain log rate spikes: Fix state reset on refetch. (#136177) Adds a fix to reset the state when a user restarts the analysis. --- .../api/explain_log_rate_spikes/actions.ts | 10 ++++ .../api/explain_log_rate_spikes/index.ts | 1 + .../aiops/common/api/stream_reducer.test.ts | 51 +++++++++++++++++++ .../aiops/common/api/stream_reducer.ts | 2 + .../server/routes/explain_log_rate_spikes.ts | 2 + .../apis/aiops/explain_log_rate_spikes.ts | 8 +-- 6 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 x-pack/plugins/aiops/common/api/stream_reducer.test.ts diff --git a/x-pack/plugins/aiops/common/api/explain_log_rate_spikes/actions.ts b/x-pack/plugins/aiops/common/api/explain_log_rate_spikes/actions.ts index cd6eb734da503..be380d966e250 100644 --- a/x-pack/plugins/aiops/common/api/explain_log_rate_spikes/actions.ts +++ b/x-pack/plugins/aiops/common/api/explain_log_rate_spikes/actions.ts @@ -10,6 +10,7 @@ import type { ChangePoint } from '../../types'; export const API_ACTION_NAME = { ADD_CHANGE_POINTS: 'add_change_points', ERROR: 'error', + RESET: 'reset', UPDATE_LOADING_STATE: 'update_loading_state', } as const; export type ApiActionName = typeof API_ACTION_NAME[keyof typeof API_ACTION_NAME]; @@ -40,6 +41,14 @@ export function errorAction(payload: ApiActionError['payload']): ApiActionError }; } +interface ApiActionReset { + type: typeof API_ACTION_NAME.RESET; +} + +export function resetAction(): ApiActionReset { + return { type: API_ACTION_NAME.RESET }; +} + interface ApiActionUpdateLoadingState { type: typeof API_ACTION_NAME.UPDATE_LOADING_STATE; payload: { @@ -61,4 +70,5 @@ export function updateLoadingStateAction( export type AiopsExplainLogRateSpikesApiAction = | ApiActionAddChangePoints | ApiActionError + | ApiActionReset | ApiActionUpdateLoadingState; diff --git a/x-pack/plugins/aiops/common/api/explain_log_rate_spikes/index.ts b/x-pack/plugins/aiops/common/api/explain_log_rate_spikes/index.ts index eb37d6e489be7..fcf5ce818f4d3 100644 --- a/x-pack/plugins/aiops/common/api/explain_log_rate_spikes/index.ts +++ b/x-pack/plugins/aiops/common/api/explain_log_rate_spikes/index.ts @@ -8,6 +8,7 @@ export { addChangePointsAction, errorAction, + resetAction, updateLoadingStateAction, API_ACTION_NAME, } from './actions'; diff --git a/x-pack/plugins/aiops/common/api/stream_reducer.test.ts b/x-pack/plugins/aiops/common/api/stream_reducer.test.ts new file mode 100644 index 0000000000000..43735f586b88b --- /dev/null +++ b/x-pack/plugins/aiops/common/api/stream_reducer.test.ts @@ -0,0 +1,51 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { + addChangePointsAction, + resetAction, + updateLoadingStateAction, +} from './explain_log_rate_spikes'; +import { initialState, streamReducer } from './stream_reducer'; + +describe('streamReducer', () => { + it('updates loading state', () => { + const state = streamReducer( + initialState, + updateLoadingStateAction({ ccsWarning: true, loaded: 50, loadingState: 'Loaded 50%' }) + ); + + expect(state).toEqual({ + ccsWarning: true, + loaded: 50, + loadingState: 'Loaded 50%', + changePoints: [], + }); + }); + + it('adds change point, then resets state again', () => { + const state1 = streamReducer( + initialState, + addChangePointsAction([ + { + fieldName: 'the-field-name', + fieldValue: 'the-field-value', + doc_count: 10, + bg_count: 100, + score: 0.1, + pValue: 0.01, + }, + ]) + ); + + expect(state1.changePoints).toHaveLength(1); + + const state2 = streamReducer(state1, resetAction()); + + expect(state2.changePoints).toHaveLength(0); + }); +}); diff --git a/x-pack/plugins/aiops/common/api/stream_reducer.ts b/x-pack/plugins/aiops/common/api/stream_reducer.ts index fe58dc9552e3f..97c767a43ec47 100644 --- a/x-pack/plugins/aiops/common/api/stream_reducer.ts +++ b/x-pack/plugins/aiops/common/api/stream_reducer.ts @@ -34,6 +34,8 @@ export function streamReducer( switch (action.type) { case API_ACTION_NAME.ADD_CHANGE_POINTS: return { ...state, changePoints: [...state.changePoints, ...action.payload] }; + case API_ACTION_NAME.RESET: + return initialState; case API_ACTION_NAME.UPDATE_LOADING_STATE: return { ...state, ...action.payload }; default: diff --git a/x-pack/plugins/aiops/server/routes/explain_log_rate_spikes.ts b/x-pack/plugins/aiops/server/routes/explain_log_rate_spikes.ts index 23b1d7a8af0dc..ce70c8eb386af 100644 --- a/x-pack/plugins/aiops/server/routes/explain_log_rate_spikes.ts +++ b/x-pack/plugins/aiops/server/routes/explain_log_rate_spikes.ts @@ -16,6 +16,7 @@ import { addChangePointsAction, aiopsExplainLogRateSpikesSchema, errorAction, + resetAction, updateLoadingStateAction, AiopsExplainLogRateSpikesApiAction, } from '../../common/api/explain_log_rate_spikes'; @@ -70,6 +71,7 @@ export const defineExplainLogRateSpikesRoute = ( // Async IIFE to run the analysis while not blocking returning `responseWithHeaders`. (async () => { + push(resetAction()); push( updateLoadingStateAction({ ccsWarning: false, diff --git a/x-pack/test/api_integration/apis/aiops/explain_log_rate_spikes.ts b/x-pack/test/api_integration/apis/aiops/explain_log_rate_spikes.ts index be65531da0b02..82a6ca42fce83 100644 --- a/x-pack/test/api_integration/apis/aiops/explain_log_rate_spikes.ts +++ b/x-pack/test/api_integration/apis/aiops/explain_log_rate_spikes.ts @@ -34,10 +34,10 @@ export default ({ getService }: FtrProviderContext) => { }; const expected = { - chunksLength: 7, - actionsLength: 6, - noIndexChunksLength: 3, - noIndexActionsLength: 2, + chunksLength: 8, + actionsLength: 7, + noIndexChunksLength: 4, + noIndexActionsLength: 3, actionFilter: 'add_change_points', errorFilter: 'error', changePoints: [