-
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.
feat(slo): add events chart (#170896)
- Loading branch information
Showing
28 changed files
with
531 additions
and
88 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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,3 @@ | ||
# @kbn/calculate-auto | ||
|
||
Empty package generated by @kbn/generate |
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,9 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { calculateAuto } from './src/calculate_auto'; |
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,13 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test/jest_node', | ||
rootDir: '../..', | ||
roots: ['<rootDir>/packages/kbn-calculate-auto'], | ||
}; |
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/calculate-auto", | ||
"owner": "@elastic/obs-ux-management-team" | ||
} |
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/calculate-auto", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "SSPL-1.0 OR Elastic License 2.0" | ||
} |
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,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 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. | ||
*/ | ||
|
||
import { calculateAuto } from './calculate_auto'; | ||
import moment, { isDuration } from 'moment'; | ||
|
||
describe('calculateAuto.near(bucket, duration)', () => { | ||
it('should calculate the bucket size for 15 minutes', () => { | ||
const bucketSizeDuration = calculateAuto.near(100, moment.duration(15, 'minutes')); | ||
expect(bucketSizeDuration).not.toBeUndefined(); | ||
expect(isDuration(bucketSizeDuration)).toBeTruthy(); | ||
expect(bucketSizeDuration!.asSeconds()).toBe(10); | ||
}); | ||
it('should calculate the bucket size for an hour', () => { | ||
const bucketSizeDuration = calculateAuto.near(100, moment.duration(1, 'hour')); | ||
expect(bucketSizeDuration).not.toBeUndefined(); | ||
expect(isDuration(bucketSizeDuration)).toBeTruthy(); | ||
expect(bucketSizeDuration!.asSeconds()).toBe(30); | ||
}); | ||
it('should calculate the bucket size for a day', () => { | ||
const bucketSizeDuration = calculateAuto.near(100, moment.duration(1, 'day')); | ||
expect(bucketSizeDuration).not.toBeUndefined(); | ||
expect(isDuration(bucketSizeDuration)).toBeTruthy(); | ||
expect(bucketSizeDuration!.asMinutes()).toBe(10); | ||
}); | ||
}); |
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,83 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import moment, { isDuration, Duration } from 'moment'; | ||
const d = moment.duration; | ||
|
||
const roundingRules = [ | ||
[d(500, 'ms'), d(100, 'ms')], | ||
[d(5, 'second'), d(1, 'second')], | ||
[d(7.5, 'second'), d(5, 'second')], | ||
[d(15, 'second'), d(10, 'second')], | ||
[d(45, 'second'), d(30, 'second')], | ||
[d(3, 'minute'), d(1, 'minute')], | ||
[d(9, 'minute'), d(5, 'minute')], | ||
[d(20, 'minute'), d(10, 'minute')], | ||
[d(45, 'minute'), d(30, 'minute')], | ||
[d(2, 'hour'), d(1, 'hour')], | ||
[d(6, 'hour'), d(3, 'hour')], | ||
[d(24, 'hour'), d(12, 'hour')], | ||
[d(1, 'week'), d(1, 'd')], | ||
[d(3, 'week'), d(1, 'week')], | ||
[d(1, 'year'), d(1, 'month')], | ||
[d(Infinity, 'year'), d(1, 'year')], | ||
]; | ||
|
||
const reverseRoundingRules = [...roundingRules].reverse(); | ||
type CheckFunction = (bound: Duration, interval: Duration, target: number) => Duration | undefined; | ||
|
||
function findRule(rules: Duration[][], check: CheckFunction, last?: boolean) { | ||
function pickInterval(buckets: number, duration: Duration) { | ||
const target = duration.asMilliseconds() / buckets; | ||
let lastResult = null; | ||
|
||
for (const [end, start] of rules) { | ||
const result = check(end, start, target); | ||
|
||
if (result == null) { | ||
if (!last) continue; | ||
if (lastResult) return lastResult; | ||
break; | ||
} | ||
|
||
if (!last) return result; | ||
lastResult = result; | ||
} | ||
|
||
// fallback to just a number of milliseconds, ensure ms is >= 1 | ||
const ms = Math.max(Math.floor(target), 1); | ||
return moment.duration(ms, 'ms'); | ||
} | ||
|
||
return (buckets: number, duration: Duration) => { | ||
const interval = pickInterval(buckets, duration); | ||
if (isDuration(interval)) return interval; | ||
}; | ||
} | ||
|
||
export const calculateAuto = { | ||
near: findRule( | ||
reverseRoundingRules, | ||
function near(bound, interval, target) { | ||
if (isDuration(bound) && bound.asMilliseconds() > target) return interval; | ||
}, | ||
true | ||
), | ||
lessThan: findRule( | ||
reverseRoundingRules, | ||
function lessThan(_bound: Duration, interval: Duration, target: number) { | ||
if (interval.asMilliseconds() < target) return interval; | ||
} | ||
), | ||
atLeast: findRule( | ||
reverseRoundingRules, | ||
function atLeast(_bound: Duration, interval: Duration, target: number) { | ||
if (interval.asMilliseconds() <= target) return interval; | ||
} | ||
), | ||
}; |
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,17 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types", | ||
"types": [ | ||
"jest", | ||
"node" | ||
] | ||
}, | ||
"include": [ | ||
"**/*.ts", | ||
], | ||
"exclude": [ | ||
"target/**/*" | ||
], | ||
"kbn_references": [] | ||
} |
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
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
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
Oops, something went wrong.