-
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.
Change the implementation to use singleRercentileRank agg config
- Loading branch information
Showing
36 changed files
with
445 additions
and
191 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
17 changes: 17 additions & 0 deletions
17
...plugins/data/common/search/aggs/metrics/__snapshots__/single_percentile_rank.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
150 changes: 150 additions & 0 deletions
150
src/plugins/data/common/search/aggs/metrics/single_percentile_rank.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,150 @@ | ||
/* | ||
* 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 { AggConfigs, IAggConfigs } from '../agg_configs'; | ||
import { mockAggTypesRegistry } from '../test_helpers'; | ||
import { METRIC_TYPES } from './metric_agg_types'; | ||
|
||
describe('AggTypeMetricSinglePercentileRankProvider class', () => { | ||
let aggConfigs: IAggConfigs; | ||
|
||
beforeEach(() => { | ||
const typesRegistry = mockAggTypesRegistry(); | ||
const field = { | ||
name: 'bytes', | ||
}; | ||
const indexPattern = { | ||
id: '1234', | ||
title: 'logstash-*', | ||
fields: { | ||
getByName: () => field, | ||
filter: () => [field], | ||
}, | ||
} as any; | ||
|
||
aggConfigs = new AggConfigs( | ||
indexPattern, | ||
[ | ||
{ | ||
id: METRIC_TYPES.SINGLE_PERCENTILE_RANK, | ||
type: METRIC_TYPES.SINGLE_PERCENTILE_RANK, | ||
schema: 'metric', | ||
params: { | ||
field: 'bytes', | ||
value: 1024, | ||
}, | ||
}, | ||
], | ||
{ | ||
typesRegistry, | ||
} | ||
); | ||
}); | ||
|
||
it('requests the percentile ranks aggregation in the Elasticsearch query DSL', () => { | ||
const dsl: Record<string, any> = aggConfigs.toDsl(); | ||
|
||
expect(dsl.single_percentile_rank.percentile_ranks.field).toEqual('bytes'); | ||
expect(dsl.single_percentile_rank.percentile_ranks.values).toEqual([1024]); | ||
}); | ||
|
||
it('points to right value within multi metric for value bucket path', () => { | ||
expect(aggConfigs.byId(METRIC_TYPES.SINGLE_PERCENTILE_RANK)!.getValueBucketPath()).toEqual( | ||
`${METRIC_TYPES.SINGLE_PERCENTILE_RANK}.1024` | ||
); | ||
}); | ||
|
||
it('converts the response', () => { | ||
const agg = aggConfigs.getResponseAggs()[0]; | ||
|
||
expect( | ||
agg.getValue({ | ||
[agg.id]: { | ||
values: { | ||
'1024.0': 123, | ||
}, | ||
}, | ||
}) | ||
).toEqual(1.23); | ||
}); | ||
|
||
it('should not throw error for empty buckets', () => { | ||
const agg = aggConfigs.getResponseAggs()[0]; | ||
expect(agg.getValue({})).toEqual(NaN); | ||
}); | ||
|
||
it('produces the expected expression ast', () => { | ||
const agg = aggConfigs.getResponseAggs()[0]; | ||
expect(agg.toExpressionAst()).toMatchInlineSnapshot(` | ||
Object { | ||
"chain": Array [ | ||
Object { | ||
"arguments": Object { | ||
"enabled": Array [ | ||
true, | ||
], | ||
"field": Array [ | ||
"bytes", | ||
], | ||
"id": Array [ | ||
"single_percentile_rank", | ||
], | ||
"schema": Array [ | ||
"metric", | ||
], | ||
"value": Array [ | ||
1024, | ||
], | ||
}, | ||
"function": "aggSinglePercentileRank", | ||
"type": "function", | ||
}, | ||
], | ||
"type": "expression", | ||
} | ||
`); | ||
}); | ||
|
||
it('supports scripted fields', () => { | ||
const typesRegistry = mockAggTypesRegistry(); | ||
const field = { | ||
name: 'bytes', | ||
scripted: true, | ||
language: 'painless', | ||
script: 'return 456', | ||
}; | ||
const indexPattern = { | ||
id: '1234', | ||
title: 'logstash-*', | ||
fields: { | ||
getByName: () => field, | ||
filter: () => [field], | ||
}, | ||
} as any; | ||
|
||
aggConfigs = new AggConfigs( | ||
indexPattern, | ||
[ | ||
{ | ||
id: METRIC_TYPES.SINGLE_PERCENTILE_RANK, | ||
type: METRIC_TYPES.SINGLE_PERCENTILE_RANK, | ||
schema: 'metric', | ||
params: { | ||
field: 'bytes', | ||
value: 1024, | ||
}, | ||
}, | ||
], | ||
{ | ||
typesRegistry, | ||
} | ||
); | ||
|
||
expect(aggConfigs.toDsl()).toMatchSnapshot(); | ||
}); | ||
}); |
70 changes: 70 additions & 0 deletions
70
src/plugins/data/common/search/aggs/metrics/single_percentile_rank.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,70 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import { aggSinglePercentileRankFnName } from './single_percentile_rank_fn'; | ||
import { MetricAggType } from './metric_agg_type'; | ||
import { METRIC_TYPES } from './metric_agg_types'; | ||
import type { IResponseAggConfig } from './lib/get_response_agg_config_class'; | ||
import { KBN_FIELD_TYPES } from '../../..'; | ||
import { BaseAggParams } from '../types'; | ||
|
||
const singlePercentileTitle = i18n.translate('data.search.aggs.metrics.singlePercentileRankTitle', { | ||
defaultMessage: 'Percentile rank', | ||
}); | ||
|
||
export interface AggParamsSinglePercentileRank extends BaseAggParams { | ||
field: string; | ||
value: number; | ||
} | ||
|
||
export const getSinglePercentileRankMetricAgg = () => { | ||
return new MetricAggType<IResponseAggConfig>({ | ||
name: METRIC_TYPES.SINGLE_PERCENTILE_RANK, | ||
expressionName: aggSinglePercentileRankFnName, | ||
dslName: 'percentile_ranks', | ||
title: singlePercentileTitle, | ||
valueType: 'number', | ||
makeLabel(aggConfig) { | ||
return i18n.translate('data.search.aggs.metrics.singlePercentileRankLabel', { | ||
defaultMessage: 'Percentile rank of {field}', | ||
values: { field: aggConfig.getFieldDisplayName() }, | ||
}); | ||
}, | ||
getValueBucketPath(aggConfig) { | ||
return `${aggConfig.id}.${aggConfig.params.value}`; | ||
}, | ||
getSerializedFormat(agg) { | ||
return { | ||
id: 'percent', | ||
}; | ||
}, | ||
params: [ | ||
{ | ||
name: 'field', | ||
type: 'field', | ||
filterFieldTypes: [KBN_FIELD_TYPES.NUMBER, KBN_FIELD_TYPES.HISTOGRAM], | ||
}, | ||
{ | ||
name: 'value', | ||
default: 0, | ||
write: (agg, output) => { | ||
output.params.values = [agg.params.value]; | ||
}, | ||
}, | ||
], | ||
getValue(agg, bucket) { | ||
let valueKey = String(agg.params.value); | ||
if (Number.isInteger(agg.params.value)) { | ||
valueKey += '.0'; | ||
} | ||
const { values } = bucket[agg.id] ?? {}; | ||
return values ? values[valueKey] / 100 : NaN; | ||
}, | ||
}); | ||
}; |
Oops, something went wrong.