Skip to content

Commit

Permalink
[ML] Explain log rate spikes: Fix state reset on refetch. (#136177)
Browse files Browse the repository at this point in the history
Adds a fix to reset the state when a user restarts the analysis.
  • Loading branch information
walterra authored Jul 13, 2022
1 parent 560fc63 commit 43b16a9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
10 changes: 10 additions & 0 deletions x-pack/plugins/aiops/common/api/explain_log_rate_spikes/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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: {
Expand All @@ -61,4 +70,5 @@ export function updateLoadingStateAction(
export type AiopsExplainLogRateSpikesApiAction =
| ApiActionAddChangePoints
| ApiActionError
| ApiActionReset
| ApiActionUpdateLoadingState;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
export {
addChangePointsAction,
errorAction,
resetAction,
updateLoadingStateAction,
API_ACTION_NAME,
} from './actions';
Expand Down
51 changes: 51 additions & 0 deletions x-pack/plugins/aiops/common/api/stream_reducer.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 2 additions & 0 deletions x-pack/plugins/aiops/common/api/stream_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/aiops/server/routes/explain_log_rate_spikes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
addChangePointsAction,
aiopsExplainLogRateSpikesSchema,
errorAction,
resetAction,
updateLoadingStateAction,
AiopsExplainLogRateSpikesApiAction,
} from '../../common/api/explain_log_rate_spikes';
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down

0 comments on commit 43b16a9

Please sign in to comment.