Skip to content

Commit

Permalink
[ML] AIOps: Remove v1 of log rate analysis API. (#181803)
Browse files Browse the repository at this point in the history
## Summary

Follow up to #170274. Part of #181111 and #181603.

We had v1 and v2 of the log rate analysis API for a while now (Nov 23).
Originally we did this to practice API versioning for serverless. Enough
time has passed so we can remove v1 which this PR does.

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
  • Loading branch information
walterra committed Apr 26, 2024
1 parent 0de7467 commit a79d201
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 359 deletions.
115 changes: 29 additions & 86 deletions x-pack/packages/ml/aiops_log_rate_analysis/api/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import type {
SignificantItemGroupHistogram,
} from '@kbn/ml-agg-utils';

import type { AiopsLogRateAnalysisApiVersion as ApiVersion } from './schema';

export const API_ACTION_NAME = {
/** @since API v2 */
ADD_SIGNIFICANT_ITEMS: 'add_significant_items',
Expand All @@ -23,13 +21,6 @@ export const API_ACTION_NAME = {
ADD_SIGNIFICANT_ITEMS_GROUP: 'add_significant_items_group',
/** @since API v2 */
ADD_SIGNIFICANT_ITEMS_GROUP_HISTOGRAM: 'add_significant_items_group_histogram',
/** @deprecated since API v2 */
ADD_SIGNIFICANT_TERMS: 'add_significant_terms',
/** @deprecated since API v2 */
ADD_SIGNIFICANT_TERMS_HISTOGRAM: 'add_significant_terms_histogram',
/** @deprecated since API v2 */
ADD_SIGNIFICANT_TERMS_GROUP: 'add_significant_terms_group',
/** @deprecated since API v2 */
ADD_SIGNIFICANT_TERMS_GROUP_HISTOGRAM: 'add_significant_terms_group_histogram',
ADD_ERROR: 'add_error',
PING: 'ping',
Expand All @@ -41,108 +32,60 @@ export const API_ACTION_NAME = {
} as const;
export type ApiActionName = typeof API_ACTION_NAME[keyof typeof API_ACTION_NAME];

interface ApiActionAddSignificantItems<T extends ApiVersion> {
type: T extends '1'
? typeof API_ACTION_NAME.ADD_SIGNIFICANT_TERMS
: T extends '2'
? typeof API_ACTION_NAME.ADD_SIGNIFICANT_ITEMS
: never;
interface ApiActionAddSignificantItems {
type: typeof API_ACTION_NAME.ADD_SIGNIFICANT_ITEMS;
payload: SignificantItem[];
}

export function addSignificantItemsAction<T extends ApiVersion>(
payload: ApiActionAddSignificantItems<T>['payload'],
version: T
): ApiActionAddSignificantItems<T> {
if (version === '1') {
return {
type: API_ACTION_NAME.ADD_SIGNIFICANT_TERMS,
payload,
} as ApiActionAddSignificantItems<T>;
}

export function addSignificantItemsAction(
payload: ApiActionAddSignificantItems['payload']
): ApiActionAddSignificantItems {
return {
type: API_ACTION_NAME.ADD_SIGNIFICANT_ITEMS,
payload,
} as ApiActionAddSignificantItems<T>;
};
}

interface ApiActionAddSignificantItemsHistogram<T extends ApiVersion> {
type: T extends '1'
? typeof API_ACTION_NAME.ADD_SIGNIFICANT_TERMS_HISTOGRAM
: T extends '2'
? typeof API_ACTION_NAME.ADD_SIGNIFICANT_ITEMS_HISTOGRAM
: never;
interface ApiActionAddSignificantItemsHistogram {
type: typeof API_ACTION_NAME.ADD_SIGNIFICANT_ITEMS_HISTOGRAM;
payload: SignificantItemHistogram[];
}

export function addSignificantItemsHistogramAction<T extends ApiVersion>(
payload: ApiActionAddSignificantItemsHistogram<T>['payload'],
version: T
): ApiActionAddSignificantItemsHistogram<T> {
if (version === '1') {
return {
type: API_ACTION_NAME.ADD_SIGNIFICANT_TERMS_HISTOGRAM,
payload,
} as ApiActionAddSignificantItemsHistogram<T>;
}

export function addSignificantItemsHistogramAction(
payload: ApiActionAddSignificantItemsHistogram['payload']
): ApiActionAddSignificantItemsHistogram {
return {
type: API_ACTION_NAME.ADD_SIGNIFICANT_ITEMS_HISTOGRAM,
payload,
} as ApiActionAddSignificantItemsHistogram<T>;
};
}

interface ApiActionAddSignificantItemsGroup<T extends ApiVersion> {
type: T extends '1'
? typeof API_ACTION_NAME.ADD_SIGNIFICANT_TERMS_GROUP
: T extends '2'
? typeof API_ACTION_NAME.ADD_SIGNIFICANT_ITEMS_GROUP
: never;
interface ApiActionAddSignificantItemsGroup {
type: typeof API_ACTION_NAME.ADD_SIGNIFICANT_ITEMS_GROUP;
payload: SignificantItemGroup[];
}

export function addSignificantItemsGroupAction<T extends ApiVersion>(
payload: ApiActionAddSignificantItemsGroup<T>['payload'],
version: T
): ApiActionAddSignificantItemsGroup<T> {
if (version === '1') {
return {
type: API_ACTION_NAME.ADD_SIGNIFICANT_TERMS_GROUP,
payload,
} as ApiActionAddSignificantItemsGroup<T>;
}

export function addSignificantItemsGroupAction(
payload: ApiActionAddSignificantItemsGroup['payload']
): ApiActionAddSignificantItemsGroup {
return {
type: API_ACTION_NAME.ADD_SIGNIFICANT_ITEMS_GROUP,
payload,
} as ApiActionAddSignificantItemsGroup<T>;
};
}

interface ApiActionAddSignificantItemsGroupHistogram<T extends ApiVersion> {
type: T extends '1'
? typeof API_ACTION_NAME.ADD_SIGNIFICANT_TERMS_GROUP_HISTOGRAM
: T extends '2'
? typeof API_ACTION_NAME.ADD_SIGNIFICANT_ITEMS_GROUP_HISTOGRAM
: never;
interface ApiActionAddSignificantItemsGroupHistogram {
type: typeof API_ACTION_NAME.ADD_SIGNIFICANT_ITEMS_GROUP_HISTOGRAM;
payload: SignificantItemGroupHistogram[];
}

export function addSignificantItemsGroupHistogramAction<T extends ApiVersion>(
payload: ApiActionAddSignificantItemsGroupHistogram<T>['payload'],
version: T
): ApiActionAddSignificantItemsGroupHistogram<T> {
if (version === '1') {
return {
type: API_ACTION_NAME.ADD_SIGNIFICANT_TERMS_GROUP_HISTOGRAM,
payload,
} as ApiActionAddSignificantItemsGroupHistogram<T>;
}

export function addSignificantItemsGroupHistogramAction(
payload: ApiActionAddSignificantItemsGroupHistogram['payload']
): ApiActionAddSignificantItemsGroupHistogram {
return {
type: API_ACTION_NAME.ADD_SIGNIFICANT_ITEMS_GROUP_HISTOGRAM,
payload,
} as ApiActionAddSignificantItemsGroupHistogram<T>;
};
}

interface ApiActionAddError {
Expand Down Expand Up @@ -225,11 +168,11 @@ export function setZeroDocsFallback(
};
}

export type AiopsLogRateAnalysisApiAction<T extends ApiVersion> =
| ApiActionAddSignificantItems<T>
| ApiActionAddSignificantItemsGroup<T>
| ApiActionAddSignificantItemsHistogram<T>
| ApiActionAddSignificantItemsGroupHistogram<T>
export type AiopsLogRateAnalysisApiAction =
| ApiActionAddSignificantItems
| ApiActionAddSignificantItemsGroup
| ApiActionAddSignificantItemsHistogram
| ApiActionAddSignificantItemsGroupHistogram
| ApiActionAddError
| ApiActionPing
| ApiActionResetAll
Expand Down
9 changes: 2 additions & 7 deletions x-pack/packages/ml/aiops_log_rate_analysis/api/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
* 2.0.
*/

import type { AiopsLogRateAnalysisSchemaV1 } from './schema_v1';
import type { AiopsLogRateAnalysisSchemaV2 } from './schema_v2';

export type AiopsLogRateAnalysisApiVersion = '1' | '2';
export type AiopsLogRateAnalysisApiVersion = '2';

const LATEST_API_VERSION: AiopsLogRateAnalysisApiVersion = '2';

export type AiopsLogRateAnalysisSchema<
T extends AiopsLogRateAnalysisApiVersion = typeof LATEST_API_VERSION
> = T extends '1'
? AiopsLogRateAnalysisSchemaV1
: T extends '2'
? AiopsLogRateAnalysisSchemaV2
: never;
> = T extends '2' ? AiopsLogRateAnalysisSchemaV2 : never;
42 changes: 0 additions & 42 deletions x-pack/packages/ml/aiops_log_rate_analysis/api/schema_v1.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,21 @@ describe('streamReducer', () => {
it('adds significant item, then resets all state again', () => {
const state1 = streamReducer(
initialState,
addSignificantItemsAction(
[
{
key: 'the-field-name:the-field-value',
type: 'keyword',
fieldName: 'the-field-name',
fieldValue: 'the-field-value',
doc_count: 10,
bg_count: 100,
total_doc_count: 1000,
total_bg_count: 10000,
score: 0.1,
pValue: 0.01,
normalizedScore: 0.123,
},
],
'2'
)
addSignificantItemsAction([
{
key: 'the-field-name:the-field-value',
type: 'keyword',
fieldName: 'the-field-name',
fieldValue: 'the-field-value',
doc_count: 10,
bg_count: 100,
total_doc_count: 1000,
total_bg_count: 10000,
score: 0.1,
pValue: 0.01,
normalizedScore: 0.123,
},
])
);

expect(state1.significantItems).toHaveLength(1);
Expand All @@ -66,14 +63,14 @@ describe('streamReducer', () => {
});

it('adds significant items and groups, then resets groups only', () => {
const state1 = streamReducer(initialState, addSignificantItemsAction(significantTerms, '2'));
const state1 = streamReducer(initialState, addSignificantItemsAction(significantTerms));

expect(state1.significantItems).toHaveLength(4);
expect(state1.significantItemsGroups).toHaveLength(0);

const state2 = streamReducer(
state1,
addSignificantItemsGroupAction(finalSignificantItemGroups, '2')
addSignificantItemsGroupAction(finalSignificantItemGroups)
);

expect(state2.significantItems).toHaveLength(4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { SignificantItem, SignificantItemGroup } from '@kbn/ml-agg-utils';
import type { AiopsLogRateAnalysisApiAction } from './actions';
import { API_ACTION_NAME } from './actions';

interface StreamState {
export interface StreamState {
ccsWarning: boolean;
significantItems: SignificantItem[];
significantItemsGroups: SignificantItemGroup[];
Expand All @@ -34,7 +34,7 @@ export const initialState: StreamState = {

export function streamReducer(
state: StreamState,
action: AiopsLogRateAnalysisApiAction<'2'> | Array<AiopsLogRateAnalysisApiAction<'2'>>
action: AiopsLogRateAnalysisApiAction | AiopsLogRateAnalysisApiAction[]
): StreamState {
if (Array.isArray(action)) {
return action.reduce(streamReducer, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export const groupingHandlerFactory =
logDebugMessage,
logger,
stateHandler,
version,
}: ResponseStreamFetchOptions<T>) =>
async (
significantCategories: SignificantItem[],
Expand Down Expand Up @@ -134,7 +133,7 @@ export const groupingHandlerFactory =
const maxItems = Math.max(...significantItemGroups.map((g) => g.group.length));

if (maxItems > 1) {
responseStream.push(addSignificantItemsGroupAction(significantItemGroups, version));
responseStream.push(addSignificantItemsGroupAction(significantItemGroups));
}

stateHandler.loaded(PROGRESS_STEP_GROUPING, false);
Expand Down Expand Up @@ -203,15 +202,6 @@ export const groupingHandlerFactory =
doc_count: 0,
};

if (version === '1') {
return {
key: o.key,
key_as_string: o.key_as_string ?? '',
doc_count_significant_term: current.doc_count,
doc_count_overall: Math.max(0, o.doc_count - current.doc_count),
};
}

return {
key: o.key,
key_as_string: o.key_as_string ?? '',
Expand All @@ -221,15 +211,12 @@ export const groupingHandlerFactory =
}) ?? [];

responseStream.push(
addSignificantItemsGroupHistogramAction(
[
{
id: cpg.id,
histogram,
},
],
version
)
addSignificantItemsGroupHistogramAction([
{
id: cpg.id,
histogram,
},
])
);
}
}, MAX_CONCURRENT_QUERIES);
Expand Down
Loading

0 comments on commit a79d201

Please sign in to comment.