Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Move aiops-utils package to x-pack/packages/ml. #134985

Merged
merged 7 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@
/x-pack/test/functional/services/transform/ @elastic/ml-ui
/x-pack/test/functional_basic/apps/transform/ @elastic/ml-ui
/x-pack/packages/ml/ @elastic/ml-ui
/packages/kbn-aiops-utils @elastic/ml-ui
/examples/response_stream/ @elastic/ml-ui

# Maps
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
"@hapi/inert": "^6.0.4",
"@hapi/wreck": "^17.1.0",
"@kbn/ace": "link:bazel-bin/packages/kbn-ace",
"@kbn/aiops-utils": "link:bazel-bin/packages/kbn-aiops-utils",
"@kbn/aiops-utils": "link:bazel-bin/x-pack/packages/ml/aiops_utils",
"@kbn/alerts": "link:bazel-bin/packages/kbn-alerts",
"@kbn/ambient-storybook-types": "link:bazel-bin/packages/kbn-ambient-storybook-types",
"@kbn/ambient-ui-types": "link:bazel-bin/packages/kbn-ambient-ui-types",
Expand Down Expand Up @@ -661,7 +661,7 @@
"@types/json-stable-stringify": "^1.0.32",
"@types/json5": "^0.0.30",
"@types/kbn__ace": "link:bazel-bin/packages/kbn-ace/npm_module_types",
"@types/kbn__aiops-utils": "link:bazel-bin/packages/kbn-aiops-utils/npm_module_types",
"@types/kbn__aiops-utils": "link:bazel-bin/x-pack/packages/ml/aiops_utils/npm_module_types",
"@types/kbn__alerts": "link:bazel-bin/packages/kbn-alerts/npm_module_types",
"@types/kbn__analytics": "link:bazel-bin/packages/kbn-analytics/npm_module_types",
"@types/kbn__analytics-client": "link:bazel-bin/packages/analytics/client/npm_module_types",
Expand Down
4 changes: 2 additions & 2 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ filegroup(
"//packages/elastic-apm-synthtrace:build",
"//packages/elastic-safer-lodash-set:build",
"//packages/kbn-ace:build",
"//packages/kbn-aiops-utils:build",
"//packages/kbn-alerts:build",
"//packages/kbn-ambient-storybook-types:build",
"//packages/kbn-ambient-ui-types:build",
Expand Down Expand Up @@ -160,6 +159,7 @@ filegroup(
"//packages/shared-ux/page/kibana_no_data:build",
"//packages/shared-ux/prompt/no_data_views:build",
"//x-pack/packages/ml/agg_utils:build",
"//x-pack/packages/ml/aiops_utils:build",
"//x-pack/packages/ml/is_populated_object:build",
"//x-pack/packages/ml/string_hash:build",
],
Expand Down Expand Up @@ -209,7 +209,6 @@ filegroup(
"//packages/elastic-apm-synthtrace:build_types",
"//packages/elastic-safer-lodash-set:build_types",
"//packages/kbn-ace:build_types",
"//packages/kbn-aiops-utils:build_types",
"//packages/kbn-alerts:build_types",
"//packages/kbn-analytics:build_types",
"//packages/kbn-apm-config-loader:build_types",
Expand Down Expand Up @@ -305,6 +304,7 @@ filegroup(
"//packages/shared-ux/page/kibana_no_data:build_types",
"//packages/shared-ux/prompt/no_data_views:build_types",
"//x-pack/packages/ml/agg_utils:build_types",
"//x-pack/packages/ml/aiops_utils:build_types",
"//x-pack/packages/ml/is_populated_object:build_types",
"//x-pack/packages/ml/string_hash:build_types",
],
Expand Down
3 changes: 0 additions & 3 deletions packages/kbn-aiops-utils/README.md

This file was deleted.

13 changes: 0 additions & 13 deletions packages/kbn-aiops-utils/jest.config.js

This file was deleted.

8 changes: 0 additions & 8 deletions packages/kbn-aiops-utils/package.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("@npm//@bazel/typescript:index.bzl", "ts_config")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm", "pkg_npm_types", "ts_project")

PKG_DIRNAME = "kbn-aiops-utils"
PKG_DIRNAME = "aiops_utils"
PKG_REQUIRE_NAME = "@kbn/aiops-utils"

SOURCE_FILES = glob(
Expand Down
134 changes: 134 additions & 0 deletions x-pack/packages/ml/aiops_utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# @kbn/aiops-utils

The `aiops-utils` package contains static utilities maintained by the ML team for AIOps related efforts.

<!-- INSERT GENERATED DOCS START -->

### `acceptCompression` (function)

Returns whether request headers accept a response using gzip compression.

**Parameters:**

- headers (`Headers`) - Request headers.

**returns:** boolean

### `streamFactory` (function)

Overload to set up a string based response stream with support
for gzip compression depending on provided request headers.

**Parameters:**

- headers (`Headers`) - Request headers.

**returns:** StreamFactoryReturnType<T>

### `streamFactory` (function)

Sets up a response stream with support for gzip compression depending on provided
request headers. Any non-string data pushed to the stream will be stream as NDJSON.

**Parameters:**

- headers (`Headers`) - Request headers.

**returns:** StreamFactoryReturnType<T>

### `fetchStream` (function)

Uses `fetch` and `getReader` to receive an API call as a stream with multiple chunks
as soon as they are available. `fetchStream` is implemented as a generator that will
yield/emit chunks and can be consumed for example like this:

```js
for await (const [error, chunk] of fetchStream(...) {
...
}
```

**Parameters:**

- endpoint (`${BasePath}${I["endpoint"]}`) - — The API endpoint including the Kibana basepath.
- abortCtrl (`MutableRefObject<AbortController>`) - — Abort controller for cancelling the request.
- body (`I["body"]`) - — The request body. For now all requests are POST.
- ndjson (`boolean`) - — Boolean flag to receive the stream as a raw string or NDJSON.
- bufferBounce (`number`) - — A buffer timeout which defaults to 100ms. This collects stream
chunks for the time of the timeout and only then yields/emits them.
This is useful so we are more in control of passing on data to
consuming React components and we won't hammer the DOM with
updates on every received chunk.

**returns:** AsyncGenerator<[string, ReducerAction<I["reducer"]> | ReducerAction<I["reducer"]>[]], any, unknown>

### `StringReducer` (type)

### `stringReducer` (function)

The `stringReducer` is provided to handle plain string based streams with `streamFactory()`.

**Parameters:**

- state (`string`) - The current state, being the string fetched so far.
- payload (`StringReducerPayload`) - — The state update can be a plain string, an array of strings or `undefined`.
- An array of strings will be joined without a delimiter and added to the current string.
In combination with `useFetchStream`'s buffering this allows to do bulk updates
within the reducer without triggering a React/DOM update on every stream chunk.
- `undefined` can be used to reset the state to an empty string, for example, when a
UI has the option to trigger a refetch of a stream.

**returns:** string

### `UseFetchStreamCustomReducerParams` (interface)

Custom hook type definition of the base params for an NDJSON stream with custom reducer.

**Members:**

- endpoint (`string`)
- body (`object`)
- reducer (`Reducer<any, any>`)

### `UseFetchStreamParamsDefault` (interface)

Custom hook type definition of the base params for a string base stream without a custom reducer.

**Members:**

- endpoint (`string`)
- body (`object`)
- reducer (`StringReducer`)

### `useFetchStream` (function)

**Parameters:**

- endpoint (`${BasePath}${I["endpoint"]}`)
- body (`I["body"]`)

**returns:** UseFetchStreamReturnType<string, ReducerAction<I["reducer"]>>

### `useFetchStream` (function)

**Parameters:**

- endpoint (`${BasePath}${I["endpoint"]}`)
- body (`I["body"]`)
- options (`{ reducer: I["reducer"]; initialState: ReducerState<I["reducer"]>; }`)

**returns:** UseFetchStreamReturnType<string, ReducerAction<I["reducer"]>>

### `useFetchStream` (function)

Custom hook to receive streaming data.

**Parameters:**

- endpoint (`${BasePath}${I["endpoint"]}`) - API endpoint including Kibana base path.
- body (`I["body"]`) - API request body.
- options (`{ reducer: I["reducer"]; initialState: ReducerState<I["reducer"]>; }`) - Optional custom reducer and initial state.

**returns:** UseFetchStreamReturnType<string, ReducerAction<I["reducer"]>>

<!-- INSERT GENERATED DOCS END -->
12 changes: 12 additions & 0 deletions x-pack/packages/ml/aiops_utils/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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.
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../../../..',
roots: ['<rootDir>/x-pack/packages/ml/aiops_utils'],
};
14 changes: 14 additions & 0 deletions x-pack/packages/ml/aiops_utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@kbn/aiops-utils",
"private": true,
"version": "1.0.0",
"main": "./target_node/index.js",
"browser": "./target_web/index.js",
"license": "SSPL-1.0 OR Elastic License 2.0",
"devDependencies": {
"ts-readme": "^1.1.3"
},
"scripts": {
"generate-docs": "ts-readme src/index.ts"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*
* 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.
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export { streamFactory } from './lib/stream_factory';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*
* 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.
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { acceptCompression } from './accept_compression';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
/*
* 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.
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

// TODO: Replace these with kbn packaged versions once we have those available to us.
// At the moment imports from runtime plugins into packages are not supported.
// import type { Headers } from '@kbn/core/server';
type Headers = Record<string, string | string[] | undefined>;

function containsGzip(s: string) {
return s
.split(',')
.map((d) => d.trim())
.includes('gzip');
}

/**
* Returns whether request headers accept a response using gzip compression.
*
Expand All @@ -24,13 +30,6 @@ export function acceptCompression(headers: Headers) {
if (key.toLocaleLowerCase() === 'accept-encoding') {
const acceptEncoding = headers[key];

function containsGzip(s: string) {
return s
.split(',')
.map((d) => d.trim())
.includes('gzip');
}

if (typeof acceptEncoding === 'string') {
compressed = containsGzip(acceptEncoding);
} else if (Array.isArray(acceptEncoding)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*
* 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.
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { ReducerAction } from 'react';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/*
* 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.
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import zlib from 'zlib';
import * as zlib from 'zlib';

import { streamFactory } from './stream_factory';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*
* 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.
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { Stream } from 'stream';
import zlib from 'zlib';
import * as zlib from 'zlib';

// TODO: Replace these with kbn packaged versions once we have those available to us.
// At the moment imports from runtime plugins into packages are not supported.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*
* 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.
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { Reducer, ReducerAction, ReducerState } from 'react';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*
* 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.
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.bazel.json",
"extends": "../../../../tsconfig.bazel.json",
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
Expand Down
Loading