kibana/examples/response_stream/common/api/reducer_stream/reducer.ts
Walter Rafelsberger d50434ed7b
[ML] Explain log rate spikes: Adds API license check. (#135431)
- Adds a check to aiops API endpoints to only allow requests with active platinum license.
- Adds integration tests for basic license where the endpoints should return permission denied.
- Improved error handling:
 - Low level errors (like a non valid argument pushed to a stream) will now be logged to Kibana server's console, because the way HTTP streams work we cannot really emit a useful error to an already running stream to the client. So the stream will just abort but Kibana server will log an error.
 - Higher level errors on the application level (like when we find out an index does not exist to run the analysis) will be pushed to the stream now as an error type action and we can update the UI accordingly. Note this PR only updates the API and corresponding tests to support this, the UI doesn't make use of it yet.
2022-06-30 15:32:31 +02:00

79 lines
2.2 KiB
TypeScript

/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { ReducerStreamApiAction, API_ACTION_NAME } from '.';
export const UI_ACTION_NAME = {
RESET: 'reset',
} as const;
export type UiActionName = typeof UI_ACTION_NAME[keyof typeof UI_ACTION_NAME];
export interface StreamState {
errors: string[];
progress: number;
entities: Record<string, number>;
}
export const initialState: StreamState = {
errors: [],
progress: 0,
entities: {},
};
interface UiActionResetStream {
type: typeof UI_ACTION_NAME.RESET;
}
export function resetStream(): UiActionResetStream {
return { type: UI_ACTION_NAME.RESET };
}
type UiAction = UiActionResetStream;
export type ReducerAction = ReducerStreamApiAction | UiAction;
export function reducerStreamReducer(
state: StreamState,
action: ReducerAction | ReducerAction[]
): StreamState {
if (Array.isArray(action)) {
return action.reduce(reducerStreamReducer, state);
}
switch (action.type) {
case API_ACTION_NAME.UPDATE_PROGRESS:
return {
...state,
progress: action.payload,
};
case API_ACTION_NAME.DELETE_ENTITY:
const deleteFromEntities = { ...state.entities };
delete deleteFromEntities[action.payload];
return {
...state,
entities: deleteFromEntities,
};
case API_ACTION_NAME.ADD_TO_ENTITY:
const addToEntities = { ...state.entities };
if (addToEntities[action.payload.entity] === undefined) {
addToEntities[action.payload.entity] = action.payload.value;
} else {
addToEntities[action.payload.entity] += action.payload.value;
}
return {
...state,
entities: addToEntities,
};
case API_ACTION_NAME.ERROR:
return {
...state,
errors: [...state.errors, action.payload],
};
case UI_ACTION_NAME.RESET:
return initialState;
default:
return state;
}
}