From b0bae065d4f487545d795673bd295b170b9530be Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Fri, 18 Oct 2024 23:55:42 +0200 Subject: [PATCH] Move ItemBuffer to package (#196455) ## Summary Part of https://github.com/elastic/kibana/issues/186139. Bfetch exports `ItemBuffer` and `TimedItemBuffer`, the latter of which is also used inside of the content management plugin. After bfetch is removed, content management will be the sole consumer, which is why I've added the team ownership to be appex-sharedux. ### Checklist - [x] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [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 --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> --- .github/CODEOWNERS | 1 + package.json | 1 + packages/kbn-item-buffer/README.md | 4 ++++ packages/kbn-item-buffer/index.ts | 11 +++++++++++ packages/kbn-item-buffer/jest.config.js | 14 ++++++++++++++ packages/kbn-item-buffer/kibana.jsonc | 5 +++++ packages/kbn-item-buffer/package.json | 6 ++++++ .../src/__test__}/run_item_buffer_tests.ts | 0 packages/kbn-item-buffer/src/index.ts | 11 +++++++++++ .../kbn-item-buffer/src}/item_buffer.test.ts | 4 ++-- .../kbn-item-buffer/src}/item_buffer.ts | 0 .../src}/timed_item_buffer.test.ts | 4 ++-- .../kbn-item-buffer/src}/timed_item_buffer.ts | 0 packages/kbn-item-buffer/tsconfig.json | 17 +++++++++++++++++ .../{tests => }/create_batched_function.test.ts | 2 +- .../common/buffer/create_batched_function.ts | 4 ++-- src/plugins/bfetch/common/buffer/index.ts | 2 -- src/plugins/bfetch/common/index.ts | 3 +-- .../create_streaming_batched_function.ts | 9 ++------- src/plugins/bfetch/tsconfig.json | 1 + .../server/event_stream/event_stream_service.ts | 2 +- src/plugins/content_management/tsconfig.json | 2 +- tsconfig.base.json | 2 ++ yarn.lock | 4 ++++ 24 files changed, 89 insertions(+), 20 deletions(-) create mode 100644 packages/kbn-item-buffer/README.md create mode 100644 packages/kbn-item-buffer/index.ts create mode 100644 packages/kbn-item-buffer/jest.config.js create mode 100644 packages/kbn-item-buffer/kibana.jsonc create mode 100644 packages/kbn-item-buffer/package.json rename {src/plugins/bfetch/common/buffer/tests => packages/kbn-item-buffer/src/__test__}/run_item_buffer_tests.ts (100%) create mode 100644 packages/kbn-item-buffer/src/index.ts rename {src/plugins/bfetch/common/buffer/tests => packages/kbn-item-buffer/src}/item_buffer.test.ts (81%) rename {src/plugins/bfetch/common/buffer => packages/kbn-item-buffer/src}/item_buffer.ts (100%) rename {src/plugins/bfetch/common/buffer/tests => packages/kbn-item-buffer/src}/timed_item_buffer.test.ts (95%) rename {src/plugins/bfetch/common/buffer => packages/kbn-item-buffer/src}/timed_item_buffer.ts (100%) create mode 100644 packages/kbn-item-buffer/tsconfig.json rename src/plugins/bfetch/common/buffer/{tests => }/create_batched_function.test.ts (96%) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index a9c304ab9f5c5..a7a808f3d1d23 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -541,6 +541,7 @@ x-pack/plugins/observability_solution/investigate @elastic/obs-ux-management-tea packages/kbn-investigation-shared @elastic/obs-ux-management-team packages/kbn-io-ts-utils @elastic/obs-knowledge-team packages/kbn-ipynb @elastic/search-kibana +packages/kbn-item-buffer @elastic/appex-sharedux packages/kbn-jest-serializers @elastic/kibana-operations packages/kbn-journeys @elastic/kibana-operations @elastic/appex-qa packages/kbn-json-ast @elastic/kibana-operations diff --git a/package.json b/package.json index 3aea2cec6f298..1c4a15589954f 100644 --- a/package.json +++ b/package.json @@ -588,6 +588,7 @@ "@kbn/investigation-shared": "link:packages/kbn-investigation-shared", "@kbn/io-ts-utils": "link:packages/kbn-io-ts-utils", "@kbn/ipynb": "link:packages/kbn-ipynb", + "@kbn/item-buffer": "link:packages/kbn-item-buffer", "@kbn/json-schemas": "link:x-pack/packages/ml/json_schemas", "@kbn/kbn-health-gateway-status-plugin": "link:test/health_gateway/plugins/status", "@kbn/kbn-sample-panel-action-plugin": "link:test/plugin_functional/plugins/kbn_sample_panel_action", diff --git a/packages/kbn-item-buffer/README.md b/packages/kbn-item-buffer/README.md new file mode 100644 index 0000000000000..7ded185dc89df --- /dev/null +++ b/packages/kbn-item-buffer/README.md @@ -0,0 +1,4 @@ +# @kbn/item-buffer + +`ItemBuffer`: A simple buffer that collects items. Can be cleared or flushed; and can automatically flush when specified number of items is reached. +`TimedItemBuffer`: An `ItemBuffer` that flushes buffer when oldest item reaches age specified by this parameter, in milliseconds. diff --git a/packages/kbn-item-buffer/index.ts b/packages/kbn-item-buffer/index.ts new file mode 100644 index 0000000000000..92b8be8e72fe6 --- /dev/null +++ b/packages/kbn-item-buffer/index.ts @@ -0,0 +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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export { ItemBuffer, TimedItemBuffer } from './src'; +export type { ItemBufferParams, TimedItemBufferParams } from './src'; diff --git a/packages/kbn-item-buffer/jest.config.js b/packages/kbn-item-buffer/jest.config.js new file mode 100644 index 0000000000000..f553426f800bb --- /dev/null +++ b/packages/kbn-item-buffer/jest.config.js @@ -0,0 +1,14 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +module.exports = { + preset: '@kbn/test/jest_node', + rootDir: '../..', + roots: ['/packages/kbn-item-buffer'], +}; diff --git a/packages/kbn-item-buffer/kibana.jsonc b/packages/kbn-item-buffer/kibana.jsonc new file mode 100644 index 0000000000000..624939e78dbf9 --- /dev/null +++ b/packages/kbn-item-buffer/kibana.jsonc @@ -0,0 +1,5 @@ +{ + "type": "shared-common", + "id": "@kbn/item-buffer", + "owner": "@elastic/appex-sharedux" +} diff --git a/packages/kbn-item-buffer/package.json b/packages/kbn-item-buffer/package.json new file mode 100644 index 0000000000000..3fb408662c5c8 --- /dev/null +++ b/packages/kbn-item-buffer/package.json @@ -0,0 +1,6 @@ +{ + "name": "@kbn/item-buffer", + "private": true, + "version": "1.0.0", + "license": "Elastic License 2.0 OR AGPL-3.0-only OR SSPL-1.0" +} \ No newline at end of file diff --git a/src/plugins/bfetch/common/buffer/tests/run_item_buffer_tests.ts b/packages/kbn-item-buffer/src/__test__/run_item_buffer_tests.ts similarity index 100% rename from src/plugins/bfetch/common/buffer/tests/run_item_buffer_tests.ts rename to packages/kbn-item-buffer/src/__test__/run_item_buffer_tests.ts diff --git a/packages/kbn-item-buffer/src/index.ts b/packages/kbn-item-buffer/src/index.ts new file mode 100644 index 0000000000000..6ac856a0eab78 --- /dev/null +++ b/packages/kbn-item-buffer/src/index.ts @@ -0,0 +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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export * from './item_buffer'; +export * from './timed_item_buffer'; diff --git a/src/plugins/bfetch/common/buffer/tests/item_buffer.test.ts b/packages/kbn-item-buffer/src/item_buffer.test.ts similarity index 81% rename from src/plugins/bfetch/common/buffer/tests/item_buffer.test.ts rename to packages/kbn-item-buffer/src/item_buffer.test.ts index d1272f353ee05..0be567e356441 100644 --- a/src/plugins/bfetch/common/buffer/tests/item_buffer.test.ts +++ b/packages/kbn-item-buffer/src/item_buffer.test.ts @@ -7,7 +7,7 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { ItemBuffer } from '../item_buffer'; -import { runItemBufferTests } from './run_item_buffer_tests'; +import { ItemBuffer } from './item_buffer'; +import { runItemBufferTests } from './__test__/run_item_buffer_tests'; runItemBufferTests(ItemBuffer); diff --git a/src/plugins/bfetch/common/buffer/item_buffer.ts b/packages/kbn-item-buffer/src/item_buffer.ts similarity index 100% rename from src/plugins/bfetch/common/buffer/item_buffer.ts rename to packages/kbn-item-buffer/src/item_buffer.ts diff --git a/src/plugins/bfetch/common/buffer/tests/timed_item_buffer.test.ts b/packages/kbn-item-buffer/src/timed_item_buffer.test.ts similarity index 95% rename from src/plugins/bfetch/common/buffer/tests/timed_item_buffer.test.ts rename to packages/kbn-item-buffer/src/timed_item_buffer.test.ts index 34224c58eb68d..d86d775e939e6 100644 --- a/src/plugins/bfetch/common/buffer/tests/timed_item_buffer.test.ts +++ b/packages/kbn-item-buffer/src/timed_item_buffer.test.ts @@ -7,8 +7,8 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { TimedItemBuffer } from '../timed_item_buffer'; -import { runItemBufferTests } from './run_item_buffer_tests'; +import { TimedItemBuffer } from './timed_item_buffer'; +import { runItemBufferTests } from './__test__/run_item_buffer_tests'; jest.useFakeTimers({ legacyFakeTimers: true }); diff --git a/src/plugins/bfetch/common/buffer/timed_item_buffer.ts b/packages/kbn-item-buffer/src/timed_item_buffer.ts similarity index 100% rename from src/plugins/bfetch/common/buffer/timed_item_buffer.ts rename to packages/kbn-item-buffer/src/timed_item_buffer.ts diff --git a/packages/kbn-item-buffer/tsconfig.json b/packages/kbn-item-buffer/tsconfig.json new file mode 100644 index 0000000000000..2f9ddddbeea23 --- /dev/null +++ b/packages/kbn-item-buffer/tsconfig.json @@ -0,0 +1,17 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "outDir": "target/types", + "types": [ + "jest", + "node" + ] + }, + "include": [ + "**/*.ts", + ], + "exclude": [ + "target/**/*" + ], + "kbn_references": [] +} diff --git a/src/plugins/bfetch/common/buffer/tests/create_batched_function.test.ts b/src/plugins/bfetch/common/buffer/create_batched_function.test.ts similarity index 96% rename from src/plugins/bfetch/common/buffer/tests/create_batched_function.test.ts rename to src/plugins/bfetch/common/buffer/create_batched_function.test.ts index c0826375f4c49..2953eaf967c94 100644 --- a/src/plugins/bfetch/common/buffer/tests/create_batched_function.test.ts +++ b/src/plugins/bfetch/common/buffer/create_batched_function.test.ts @@ -7,7 +7,7 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { createBatchedFunction } from '../create_batched_function'; +import { createBatchedFunction } from './create_batched_function'; describe('createBatchedFunction', () => { test('calls onCall every time fn is called, calls onBatch once flushOnMaxItems reached', async () => { diff --git a/src/plugins/bfetch/common/buffer/create_batched_function.ts b/src/plugins/bfetch/common/buffer/create_batched_function.ts index 19d194d8f899e..b87d45050b3c9 100644 --- a/src/plugins/bfetch/common/buffer/create_batched_function.ts +++ b/src/plugins/bfetch/common/buffer/create_batched_function.ts @@ -7,8 +7,8 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { ItemBufferParams } from './item_buffer'; -import { TimedItemBufferParams, TimedItemBuffer } from './timed_item_buffer'; +import type { ItemBufferParams, TimedItemBufferParams } from '@kbn/item-buffer'; +import { TimedItemBuffer } from '@kbn/item-buffer'; type Fn = (...args: any) => any; diff --git a/src/plugins/bfetch/common/buffer/index.ts b/src/plugins/bfetch/common/buffer/index.ts index 4b265677e3fa7..5ec864329f456 100644 --- a/src/plugins/bfetch/common/buffer/index.ts +++ b/src/plugins/bfetch/common/buffer/index.ts @@ -7,6 +7,4 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -export * from './item_buffer'; -export * from './timed_item_buffer'; export * from './create_batched_function'; diff --git a/src/plugins/bfetch/common/index.ts b/src/plugins/bfetch/common/index.ts index c5ab7b330553e..40983f7c81374 100644 --- a/src/plugins/bfetch/common/index.ts +++ b/src/plugins/bfetch/common/index.ts @@ -9,8 +9,7 @@ export { normalizeError, removeLeadingSlash, appendQueryParam } from './util'; export type { StreamingResponseHandler } from './streaming'; -export type { ItemBufferParams, TimedItemBufferParams, BatchedFunctionParams } from './buffer'; -export { ItemBuffer, TimedItemBuffer, createBatchedFunction } from './buffer'; +export { type BatchedFunctionParams, createBatchedFunction } from './buffer'; export type { ErrorLike, BatchRequestData, BatchResponseItem, BatchItemWrapper } from './batch'; export { DISABLE_BFETCH_COMPRESSION, diff --git a/src/plugins/bfetch/public/batching/create_streaming_batched_function.ts b/src/plugins/bfetch/public/batching/create_streaming_batched_function.ts index b0a0e0eb1e0c9..799aef494a19e 100644 --- a/src/plugins/bfetch/public/batching/create_streaming_batched_function.ts +++ b/src/plugins/bfetch/public/batching/create_streaming_batched_function.ts @@ -8,13 +8,8 @@ */ import { AbortError, abortSignalToPromise, defer } from '@kbn/kibana-utils-plugin/public'; -import { - ItemBufferParams, - TimedItemBufferParams, - createBatchedFunction, - ErrorLike, - normalizeError, -} from '../../common'; +import type { ItemBufferParams, TimedItemBufferParams } from '@kbn/item-buffer'; +import { createBatchedFunction, ErrorLike, normalizeError } from '../../common'; import { fetchStreaming } from '../streaming'; import { BatchedFunc, BatchItem } from './types'; diff --git a/src/plugins/bfetch/tsconfig.json b/src/plugins/bfetch/tsconfig.json index 22c616b1bc4c9..d75e6085d4537 100644 --- a/src/plugins/bfetch/tsconfig.json +++ b/src/plugins/bfetch/tsconfig.json @@ -13,6 +13,7 @@ "@kbn/core-http-common", "@kbn/bfetch-error", "@kbn/ebt-tools", + "@kbn/item-buffer", ], "exclude": [ "target/**/*", diff --git a/src/plugins/content_management/server/event_stream/event_stream_service.ts b/src/plugins/content_management/server/event_stream/event_stream_service.ts index 74f0fde017157..ff25e9d158bb9 100644 --- a/src/plugins/content_management/server/event_stream/event_stream_service.ts +++ b/src/plugins/content_management/server/event_stream/event_stream_service.ts @@ -8,7 +8,7 @@ */ import type { CoreSetup } from '@kbn/core/server'; -import { TimedItemBuffer } from '@kbn/bfetch-plugin/common'; +import { TimedItemBuffer } from '@kbn/item-buffer'; import type { EventStreamClient, EventStreamClientFactory, diff --git a/src/plugins/content_management/tsconfig.json b/src/plugins/content_management/tsconfig.json index bba887cd887b1..bde3ff1334cbb 100644 --- a/src/plugins/content_management/tsconfig.json +++ b/src/plugins/content_management/tsconfig.json @@ -10,7 +10,7 @@ "@kbn/core-http-request-handler-context-server", "@kbn/es-query", "@kbn/core-test-helpers-kbn-server", - "@kbn/bfetch-plugin", + "@kbn/item-buffer", "@kbn/object-versioning", "@kbn/core-saved-objects-api-server-mocks", "@kbn/core-saved-objects-api-server", diff --git a/tsconfig.base.json b/tsconfig.base.json index 4464ff5f4a78c..09d1f31eceb23 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -1076,6 +1076,8 @@ "@kbn/io-ts-utils/*": ["packages/kbn-io-ts-utils/*"], "@kbn/ipynb": ["packages/kbn-ipynb"], "@kbn/ipynb/*": ["packages/kbn-ipynb/*"], + "@kbn/item-buffer": ["packages/kbn-item-buffer"], + "@kbn/item-buffer/*": ["packages/kbn-item-buffer/*"], "@kbn/jest-serializers": ["packages/kbn-jest-serializers"], "@kbn/jest-serializers/*": ["packages/kbn-jest-serializers/*"], "@kbn/journeys": ["packages/kbn-journeys"], diff --git a/yarn.lock b/yarn.lock index 40d3d20830ff0..71cdfca1203b5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5447,6 +5447,10 @@ version "0.0.0" uid "" +"@kbn/item-buffer@link:packages/kbn-item-buffer": + version "0.0.0" + uid "" + "@kbn/jest-serializers@link:packages/kbn-jest-serializers": version "0.0.0" uid ""