forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OBS-UX-MNGMT] - Add P95, P99, and RATE Aggs to the Custom Threshold …
…rule (elastic#173728) ## Summary Fixes elastic#172945 by adding P95, P99, and RATE Aggs to the Custom Threshold rule <img width="575" alt="Screenshot 2023-12-20 at 12 31 50" src="https://github.com/elastic/kibana/assets/6838659/fd9f33fc-97c5-4cf8-b15e-3134048c861a"> <img width="565" alt="Screenshot 2023-12-20 at 12 31 57" src="https://github.com/elastic/kibana/assets/6838659/56047c84-a6e5-4a8a-a05f-b6c1358a7297"> <img width="601" alt="Screenshot 2023-12-20 at 12 36 16" src="https://github.com/elastic/kibana/assets/6838659/af7c291d-320f-4cb1-b62f-46e29f596bcd">
- Loading branch information
Showing
25 changed files
with
1,201 additions
and
95 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
138 changes: 138 additions & 0 deletions
138
...bility/public/components/custom_threshold/components/rule_condition_chart/helpers.test.ts
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,138 @@ | ||
/* | ||
* 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 { | ||
Aggregators, | ||
CustomThresholdExpressionMetric, | ||
} from '../../../../../common/custom_threshold_rule/types'; | ||
import { getBufferThreshold, getLensOperationFromRuleMetric, lensFieldFormatter } from './helpers'; | ||
const useCases = [ | ||
[ | ||
{ | ||
aggType: Aggregators.SUM, | ||
field: 'system.cpu.user.pct', | ||
filter: '', | ||
name: '', | ||
}, | ||
'sum(system.cpu.user.pct)', | ||
], | ||
[ | ||
{ | ||
aggType: Aggregators.MAX, | ||
field: 'system.cpu.user.pct', | ||
filter: '', | ||
name: '', | ||
}, | ||
'max(system.cpu.user.pct)', | ||
], | ||
[ | ||
{ | ||
aggType: Aggregators.MIN, | ||
field: 'system.cpu.user.pct', | ||
filter: '', | ||
name: '', | ||
}, | ||
'min(system.cpu.user.pct)', | ||
], | ||
[ | ||
{ | ||
aggType: Aggregators.AVERAGE, | ||
field: 'system.cpu.user.pct', | ||
filter: '', | ||
name: '', | ||
}, | ||
'average(system.cpu.user.pct)', | ||
], | ||
[ | ||
{ | ||
aggType: Aggregators.COUNT, | ||
field: 'system.cpu.user.pct', | ||
filter: '', | ||
name: '', | ||
}, | ||
'count(___records___)', | ||
], | ||
[ | ||
{ | ||
aggType: Aggregators.CARDINALITY, | ||
field: 'system.cpu.user.pct', | ||
filter: '', | ||
name: '', | ||
}, | ||
'unique_count(system.cpu.user.pct)', | ||
], | ||
[ | ||
{ | ||
aggType: Aggregators.P95, | ||
field: 'system.cpu.user.pct', | ||
filter: '', | ||
name: '', | ||
}, | ||
'percentile(system.cpu.user.pct, percentile=95)', | ||
], | ||
[ | ||
{ | ||
aggType: Aggregators.P99, | ||
field: 'system.cpu.user.pct', | ||
filter: '', | ||
name: '', | ||
}, | ||
'percentile(system.cpu.user.pct, percentile=99)', | ||
], | ||
[ | ||
{ | ||
aggType: Aggregators.RATE, | ||
field: 'system.network.in.bytes', | ||
filter: '', | ||
name: '', | ||
}, | ||
`counter_rate(max(system.network.in.bytes), kql='')`, | ||
], | ||
[ | ||
{ | ||
aggType: Aggregators.RATE, | ||
field: 'system.network.in.bytes', | ||
filter: 'host.name : "foo"', | ||
name: '', | ||
}, | ||
`counter_rate(max(system.network.in.bytes), kql='host.name : foo')`, | ||
], | ||
]; | ||
|
||
test.each(useCases)('returns the correct operation from %p. => %p', (metric, expectedValue) => { | ||
return expect(getLensOperationFromRuleMetric(metric as CustomThresholdExpressionMetric)).toEqual( | ||
expectedValue | ||
); | ||
}); | ||
|
||
describe('getBufferThreshold', () => { | ||
const testData = [ | ||
{ threshold: undefined, buffer: '0.00' }, | ||
{ threshold: 0.1, buffer: '0.12' }, | ||
{ threshold: 0.01, buffer: '0.02' }, | ||
{ threshold: 0.001, buffer: '0.01' }, | ||
{ threshold: 0.00098, buffer: '0.01' }, | ||
{ threshold: 130, buffer: '143.00' }, | ||
]; | ||
|
||
it.each(testData)('getBufferThreshold($threshold) = $buffer', ({ threshold, buffer }) => { | ||
expect(getBufferThreshold(threshold)).toBe(buffer); | ||
}); | ||
}); | ||
|
||
describe('lensFieldFormatter', () => { | ||
const testData = [ | ||
{ metrics: [{ field: 'system.bytes' }], format: 'bits' }, | ||
{ metrics: [{ field: 'system.pct' }], format: 'percent' }, | ||
{ metrics: [{ field: 'system.host.cores' }], format: 'number' }, | ||
{ metrics: [{ field: undefined }], format: 'number' }, | ||
]; | ||
it.each(testData)('getBufferThreshold($threshold) = $buffer', ({ metrics, format }) => { | ||
expect(lensFieldFormatter(metrics as unknown as CustomThresholdExpressionMetric[])).toBe( | ||
format | ||
); | ||
}); | ||
}); |
69 changes: 69 additions & 0 deletions
69
...servability/public/components/custom_threshold/components/rule_condition_chart/helpers.ts
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,69 @@ | ||
/* | ||
* 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 { | ||
Aggregators, | ||
CustomThresholdExpressionMetric, | ||
} from '../../../../../common/custom_threshold_rule/types'; | ||
|
||
export const getLensOperationFromRuleMetric = (metric: CustomThresholdExpressionMetric): string => { | ||
const { aggType, field, filter } = metric; | ||
let operation: string = aggType; | ||
const operationArgs: string[] = []; | ||
const aggFilter = JSON.stringify(filter || '').replace(/"|\\/g, ''); | ||
|
||
if (aggType === Aggregators.RATE) { | ||
return `counter_rate(max(${field}), kql='${aggFilter}')`; | ||
} | ||
|
||
if (aggType === Aggregators.AVERAGE) operation = 'average'; | ||
if (aggType === Aggregators.CARDINALITY) operation = 'unique_count'; | ||
if (aggType === Aggregators.P95 || aggType === Aggregators.P99) operation = 'percentile'; | ||
if (aggType === Aggregators.COUNT) operation = 'count'; | ||
|
||
let sourceField = field; | ||
|
||
if (aggType === Aggregators.COUNT) { | ||
sourceField = '___records___'; | ||
} | ||
|
||
operationArgs.push(sourceField || ''); | ||
|
||
if (aggType === Aggregators.P95) { | ||
operationArgs.push('percentile=95'); | ||
} | ||
|
||
if (aggType === Aggregators.P99) { | ||
operationArgs.push('percentile=99'); | ||
} | ||
|
||
if (aggFilter) operationArgs.push(`kql='${aggFilter}'`); | ||
|
||
return operation + '(' + operationArgs.join(', ') + ')'; | ||
}; | ||
|
||
export const getBufferThreshold = (threshold?: number): string => | ||
(Math.ceil((threshold || 0) * 1.1 * 100) / 100).toFixed(2).toString(); | ||
|
||
export const LensFieldFormat = { | ||
NUMBER: 'number', | ||
PERCENT: 'percent', | ||
BITS: 'bits', | ||
} as const; | ||
|
||
export const lensFieldFormatter = ( | ||
metrics: CustomThresholdExpressionMetric[] | ||
): typeof LensFieldFormat[keyof typeof LensFieldFormat] => { | ||
if (metrics.length < 1 || !metrics[0].field) return LensFieldFormat.NUMBER; | ||
const firstMetricField = metrics[0].field; | ||
if (firstMetricField.endsWith('.pct')) return LensFieldFormat.PERCENT; | ||
if (firstMetricField.endsWith('.bytes')) return LensFieldFormat.BITS; | ||
return LensFieldFormat.NUMBER; | ||
}; | ||
|
||
export const isRate = (metrics: CustomThresholdExpressionMetric[]): boolean => | ||
Boolean(metrics.length > 0 && metrics[0].aggType === Aggregators.RATE); |
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.