[ML] Explain log rate spikes: Fix state reset on refetch. (#136177)

Adds a fix to reset the state when a user restarts the analysis.
This commit is contained in:
Walter Rafelsberger 2022-07-13 10:48:18 +02:00 committed by GitHub
parent 560fc63082
commit 43b16a9ef7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 4 deletions

View file

@ -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;

View file

@ -8,6 +8,7 @@
export {
addChangePointsAction,
errorAction,
resetAction,
updateLoadingStateAction,
API_ACTION_NAME,
} from './actions';

View file

@ -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);
});
});

View file

@ -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:

View file

@ -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,

View file

@ -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: [