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] @kbn/ml-agg-utils, @kbn/ml-is-populated-object, @kbn/ml-string-hash packages. #132963

Merged
merged 12 commits into from
Jun 23, 2022
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
/x-pack/test/functional/apps/transform/ @elastic/ml-ui
/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

Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@
"@kbn/logging": "link:bazel-bin/packages/kbn-logging",
"@kbn/logging-mocks": "link:bazel-bin/packages/kbn-logging-mocks",
"@kbn/mapbox-gl": "link:bazel-bin/packages/kbn-mapbox-gl",
"@kbn/ml-agg-utils": "link:bazel-bin/x-pack/packages/ml/agg_utils",
"@kbn/ml-is-populated-object": "link:bazel-bin/x-pack/packages/ml/is_populated_object",
"@kbn/ml-string-hash": "link:bazel-bin/x-pack/packages/ml/string_hash",
"@kbn/monaco": "link:bazel-bin/packages/kbn-monaco",
"@kbn/plugin-discovery": "link:bazel-bin/packages/kbn-plugin-discovery",
"@kbn/react-field": "link:bazel-bin/packages/kbn-react-field",
Expand Down Expand Up @@ -722,6 +725,9 @@
"@types/kbn__logging": "link:bazel-bin/packages/kbn-logging/npm_module_types",
"@types/kbn__logging-mocks": "link:bazel-bin/packages/kbn-logging-mocks/npm_module_types",
"@types/kbn__mapbox-gl": "link:bazel-bin/packages/kbn-mapbox-gl/npm_module_types",
"@types/kbn__ml-agg-utils": "link:bazel-bin/x-pack/packages/ml/agg_utils/npm_module_types",
"@types/kbn__ml-is-populated-object": "link:bazel-bin/x-pack/packages/ml/is_populated_object/npm_module_types",
"@types/kbn__ml-string-hash": "link:bazel-bin/x-pack/packages/ml/string_hash/npm_module_types",
"@types/kbn__monaco": "link:bazel-bin/packages/kbn-monaco/npm_module_types",
"@types/kbn__optimizer": "link:bazel-bin/packages/kbn-optimizer/npm_module_types",
"@types/kbn__optimizer-webpack-helpers": "link:bazel-bin/packages/kbn-optimizer-webpack-helpers/npm_module_types",
Expand Down
6 changes: 6 additions & 0 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ filegroup(
"//packages/shared-ux/page/analytics_no_data:build",
"//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/is_populated_object:build",
"//x-pack/packages/ml/string_hash:build",
],
)

Expand Down Expand Up @@ -285,6 +288,9 @@ filegroup(
"//packages/shared-ux/page/analytics_no_data:build_types",
"//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/is_populated_object:build_types",
"//x-pack/packages/ml/string_hash:build_types",
],
)

Expand Down
1 change: 1 addition & 0 deletions packages/kbn-bazel-packages/src/bazel_package_dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const BAZEL_PACKAGE_DIRS = [
'packages/analytics/shippers',
'packages/analytics/shippers/elastic_v3',
'packages/core/*',
'x-pack/packages/ml',
];

/**
Expand Down
125 changes: 125 additions & 0 deletions x-pack/packages/ml/agg_utils/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
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 = "agg_utils"
PKG_REQUIRE_NAME = "@kbn/ml-agg-utils"

SOURCE_FILES = glob(
[
"src/**/*.ts",
],
exclude = [
"**/*.test.*",
],
)

SRCS = SOURCE_FILES

filegroup(
name = "srcs",
srcs = SRCS,
)

NPM_MODULE_EXTRA_FILES = [
"package.json",
]

# In this array place runtime dependencies, including other packages and NPM packages
# which must be available for this code to run.
#
# To reference other packages use:
# "//repo/relative/path/to/package"
# eg. "//packages/kbn-utils"
#
# To reference a NPM package use:
# "@npm//name-of-package"
# eg. "@npm//lodash"
RUNTIME_DEPS = [
"@npm//@elastic/elasticsearch",
"@npm//lodash",
"//packages/kbn-field-types",
"//x-pack/packages/ml/is_populated_object",
"//x-pack/packages/ml/string_hash",
]

# In this array place dependencies necessary to build the types, which will include the
# :npm_module_types target of other packages and packages from NPM, including @types/*
# packages.
#
# To reference the types for another package use:
# "//repo/relative/path/to/package:npm_module_types"
# eg. "//packages/kbn-utils:npm_module_types"
#
# References to NPM packages work the same as RUNTIME_DEPS
TYPES_DEPS = [
"@npm//@types/node",
"@npm//@types/jest",
"@npm//@types/lodash",
"@npm//@elastic/elasticsearch",
"@npm//tslib",
"//packages/kbn-field-types:npm_module_types",
"//x-pack/packages/ml/is_populated_object:npm_module_types",
"//x-pack/packages/ml/string_hash:npm_module_types",
]

jsts_transpiler(
name = "target_node",
srcs = SRCS,
build_pkg_name = package_name(),
)

ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
"//:tsconfig.base.json",
"//:tsconfig.bazel.json",
],
)

ts_project(
name = "tsc_types",
args = ['--pretty'],
srcs = SRCS,
deps = TYPES_DEPS,
declaration = True,
emit_declaration_only = True,
out_dir = "target_types",
root_dir = "src",
tsconfig = ":tsconfig",
)

js_library(
name = PKG_DIRNAME,
srcs = NPM_MODULE_EXTRA_FILES,
deps = RUNTIME_DEPS + [":target_node"],
package_name = PKG_REQUIRE_NAME,
visibility = ["//visibility:public"],
)

pkg_npm(
name = "npm_module",
deps = [":" + PKG_DIRNAME],
)

filegroup(
name = "build",
srcs = [":npm_module"],
visibility = ["//visibility:public"],
)

pkg_npm_types(
name = "npm_module_types",
srcs = SRCS,
deps = [":tsc_types"],
package_name = PKG_REQUIRE_NAME,
tsconfig = ":tsconfig",
visibility = ["//visibility:public"],
)

filegroup(
name = "build_types",
srcs = [":npm_module_types"],
visibility = ["//visibility:public"],
)
32 changes: 32 additions & 0 deletions x-pack/packages/ml/agg_utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# @kbn/ml-agg-utils

This package includes utility functions provided by the ML team to be used in Kibana plugins related to data manipulation and verification.

<!-- INSERT GENERATED DOCS START -->

### `buildSamplerAggregation` (function)

Wraps the supplied aggregations in a sampler aggregation.
A supplied samplerShardSize (the shard_size parameter of the sampler aggregation)
of less than 1 indicates no sampling, and the aggs are returned as-is.

**Parameters:**

- aggs (`any`)
- samplerShardSize (`number`)

**returns:** Record<string, AggregationsAggregationContainer>

### `getSamplerAggregationsResponsePath` (function)

**Parameters:**

- samplerShardSize (`number`)

**returns:** string[]

### `getAggIntervals` (function)

Returns aggregation intervals for the supplied document fields.

<!-- INSERT GENERATED DOCS END -->
12 changes: 12 additions & 0 deletions x-pack/packages/ml/agg_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/agg_utils'],
};
13 changes: 13 additions & 0 deletions x-pack/packages/ml/agg_utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@kbn/ml-agg-utils",
"private": true,
"version": "1.0.0",
"main": "./target_node/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"
}
}
31 changes: 31 additions & 0 deletions x-pack/packages/ml/agg_utils/src/build_sampler_aggregation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 { buildSamplerAggregation } from './build_sampler_aggregation';

describe('buildSamplerAggregation', () => {
const testAggs = {
bytes_stats: {
stats: { field: 'bytes' },
},
};

test('returns wrapped sampler aggregation for sampler shard size of 1000', () => {
expect(buildSamplerAggregation(testAggs, 1000)).toEqual({
sample: {
sampler: {
shard_size: 1000,
},
aggs: testAggs,
},
});
});

test('returns un-sampled aggregation as-is for sampler shard size of 0', () => {
expect(buildSamplerAggregation(testAggs, 0)).toEqual(testAggs);
});
});
31 changes: 31 additions & 0 deletions x-pack/packages/ml/agg_utils/src/build_sampler_aggregation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';

/**
* Wraps the supplied aggregations in a sampler aggregation.
* A supplied samplerShardSize (the shard_size parameter of the sampler aggregation)
* of less than 1 indicates no sampling, and the aggs are returned as-is.
*/
export function buildSamplerAggregation(
aggs: any,
samplerShardSize: number
): Record<string, estypes.AggregationsAggregationContainer> {
if (samplerShardSize < 1) {
return aggs;
}

return {
sample: {
sampler: {
shard_size: samplerShardSize,
},
aggs,
},
};
}
Loading