mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
[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:
parent
560fc63082
commit
43b16a9ef7
6 changed files with 70 additions and 4 deletions
|
@ -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;
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
export {
|
||||
addChangePointsAction,
|
||||
errorAction,
|
||||
resetAction,
|
||||
updateLoadingStateAction,
|
||||
API_ACTION_NAME,
|
||||
} from './actions';
|
||||
|
|
51
x-pack/plugins/aiops/common/api/stream_reducer.test.ts
Normal file
51
x-pack/plugins/aiops/common/api/stream_reducer.test.ts
Normal 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);
|
||||
});
|
||||
});
|
|
@ -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:
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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: [
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue