-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Consolidate redundant
time_buckets
into @kbn/ml-time-buckets
. (
#178756) ## Summary Follow up to #46227. Consolidates multiple copies of `time_buckets.js` into `@kbn/ml-time-buckets`. The scope of this PR is just to consolidate the files. In follow ups we still need to: Refactor JS to TS and get rid of the code that uses this using "dependency cache" in the `ml` plugin. ### 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
Showing
67 changed files
with
358 additions
and
1,311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# @kbn/ml-time-buckets | ||
|
||
`TimeBuckets` is a helper class for wrapping the concept of an "Interval", which describes a timespan that will separate buckets of time, for example the interval between points on a time series chart. | ||
|
||
Back in 2019 for Kibana New Platform it was decided that the original `TimeBuckets` would not longer be exposed from Kibana itself, it was therefor copied over to the `ml` plugin (see https://github.com/elastic/kibana/issues/44249). Over time, before we had the package system, several copies of this class spread over more plugins. All these usage are now consolidated into this package. | ||
|
||
In the meantime, the original `TimeBuckets` class has been reworked and migrated to TS (https://github.com/elastic/kibana/issues/60130). In contrast to the original idea, it is again available as an export from Kibana's `data` plugin. Because of this we might want to look into using the original `TimeBuckets` again if it solves our use cases. |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export type { TimeBucketsConfig, TimeBucketsInterval, TimeRangeBounds } from './time_buckets'; | ||
export { getBoundsRoundedToInterval, TimeBuckets } from './time_buckets'; | ||
export { useTimeBuckets } from './use_time_buckets'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
rootDir: '../../../..', | ||
roots: ['<rootDir>/x-pack/packages/ml/time_buckets'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"type": "shared-common", | ||
"id": "@kbn/ml-time-buckets", | ||
"owner": "@elastic/ml-ui" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "@kbn/ml-time-buckets", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "Elastic License 2.0" | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* 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 type { Moment } from 'moment'; | ||
|
||
/** | ||
* Represents the minimum and maximum time bounds for a time range. | ||
*/ | ||
export interface TimeRangeBounds { | ||
/** | ||
* The minimum bound of the time range (optional). | ||
*/ | ||
min?: Moment; | ||
/** | ||
* The maximum bound of the time range (optional). | ||
*/ | ||
max?: Moment; | ||
} | ||
|
||
/** | ||
* Defines the structure for time intervals used within TimeBuckets. | ||
*/ | ||
export declare interface TimeBucketsInterval { | ||
/** | ||
* Returns the interval in milliseconds. | ||
*/ | ||
asMilliseconds: () => number; | ||
/** | ||
* Returns the interval in seconds. | ||
*/ | ||
asSeconds: () => number; | ||
/** | ||
* The string expression representing the interval. | ||
*/ | ||
expression: string; | ||
} | ||
|
||
/** | ||
* Configuration options for initializing TimeBuckets. | ||
*/ | ||
export interface TimeBucketsConfig { | ||
/** | ||
* The maximum number of bars to display on the histogram. | ||
*/ | ||
'histogram:maxBars': number; | ||
/** | ||
* The targeted number of bars for the histogram. | ||
*/ | ||
'histogram:barTarget': number; | ||
/** | ||
* The date format string. | ||
*/ | ||
dateFormat: string; | ||
/** | ||
* The scaled date format strings. | ||
*/ | ||
'dateFormat:scaled': string[][]; | ||
} | ||
|
||
/** | ||
* Represents a configurable utility class for working with time buckets. | ||
*/ | ||
export declare class TimeBuckets { | ||
/** | ||
* Creates an instance of TimeBuckets. | ||
* @param timeBucketsConfig - Configuration for the TimeBuckets instance. | ||
*/ | ||
constructor(timeBucketsConfig: TimeBucketsConfig); | ||
|
||
/** | ||
* Sets the target number of bars for the histogram. | ||
* @param barTarget - The target bar count. | ||
*/ | ||
public setBarTarget(barTarget: number): void; | ||
|
||
/** | ||
* Sets the maximum number of bars for the histogram. | ||
* @param maxBars - The maximum bar count. | ||
*/ | ||
public setMaxBars(maxBars: number): void; | ||
|
||
/** | ||
* Sets the interval for the time buckets. | ||
* @param interval - The interval expression, e.g., "1h" for one hour. | ||
*/ | ||
public setInterval(interval: string): void; | ||
|
||
/** | ||
* Sets the bounds of the time range for the buckets. | ||
* @param bounds - The minimum and maximum time bounds. | ||
*/ | ||
public setBounds(bounds: TimeRangeBounds): void; | ||
|
||
/** | ||
* Gets the current bounds of the time range. | ||
* @returns The current time range bounds. | ||
*/ | ||
public getBounds(): { min: Moment; max: Moment }; | ||
|
||
/** | ||
* Retrieves the configured interval for the buckets. | ||
* @returns The current interval settings for the buckets. | ||
*/ | ||
public getInterval(): TimeBucketsInterval; | ||
|
||
/** | ||
* Calculates the nearest interval that is a multiple of a specified divisor. | ||
* @param divisorSecs - The divisor in seconds. | ||
* @returns The nearest interval as a multiple of the divisor. | ||
*/ | ||
public getIntervalToNearestMultiple(divisorSecs: number): TimeBucketsInterval; | ||
|
||
/** | ||
* Retrieves the date format that should be used for scaled intervals. | ||
* @returns The scaled date format string. | ||
*/ | ||
public getScaledDateFormat(): string; | ||
} | ||
|
||
/** | ||
* Adjusts the given time range bounds to align with the specified interval. | ||
* @param bounds The current time range bounds. | ||
* @param interval The interval to align the time range bounds with. | ||
* @param inclusiveEnd Whether the end of the range should be inclusive. | ||
* @returns The adjusted time range bounds. | ||
*/ | ||
export declare function getBoundsRoundedToInterval( | ||
bounds: TimeRangeBounds, | ||
interval: TimeBucketsInterval, | ||
inclusiveEnd?: boolean | ||
): Required<TimeRangeBounds>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"extends": "../../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types", | ||
"types": [ | ||
"jest", | ||
"node", | ||
"react" | ||
] | ||
}, | ||
"include": [ | ||
"**/*.ts", | ||
"**/*.tsx", | ||
], | ||
"exclude": [ | ||
"target/**/*" | ||
], | ||
"kbn_references": [ | ||
"@kbn/datemath", | ||
"@kbn/data-plugin", | ||
"@kbn/core-ui-settings-browser", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.