Skip to content

Commit

Permalink
Create cumulative cardinality aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
wylieconlon committed Feb 12, 2020
1 parent 3e95a84 commit 32f821d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/legacy/core_plugins/data/public/search/aggs/agg_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { geoBoundsMetricAgg } from './metrics/geo_bounds';
import { geoCentroidMetricAgg } from './metrics/geo_centroid';
import { percentileRanksMetricAgg } from './metrics/percentile_ranks';
import { derivativeMetricAgg } from './metrics/derivative';
import { cumulativeCardinalityMetricAgg } from './metrics/cumulative_cardinality';
import { cumulativeSumMetricAgg } from './metrics/cumulative_sum';
import { movingAvgMetricAgg } from './metrics/moving_avg';
import { serialDiffMetricAgg } from './metrics/serial_diff';
Expand Down Expand Up @@ -64,6 +65,7 @@ export const aggTypes = {
percentileRanksMetricAgg,
topHitMetricAgg,
derivativeMetricAgg,
cumulativeCardinalityMetricAgg,
cumulativeSumMetricAgg,
movingAvgMetricAgg,
serialDiffMetricAgg,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { i18n } from '@kbn/i18n';
import { npStart } from 'ui/new_platform';
import { MetricAggType, IMetricAggConfig, MetricAggParam } from './metric_agg_type';
import { IAggConfigs } from '../agg_configs';
import { METRIC_TYPES } from './metric_agg_types';
import { KBN_FIELD_TYPES } from '../../../../../../../plugins/data/public';

const cumulativeCardinalityTitle = i18n.translate(
'data.search.aggs.metrics.cumulativeCardinalityTitle',
{
defaultMessage: 'Cumulative Unique Count',
}
);

export const cumulativeCardinalityMetricAgg = new MetricAggType({
name: METRIC_TYPES.CUMULATIVE_CARDINALITY,
title: cumulativeCardinalityTitle,
makeLabel: agg => {
return i18n.translate('data.search.aggs.metrics.uniqueCountLabel', {
defaultMessage: 'Cumulative Unique count of {field}',
values: { field: agg.getParam('field')?.name },
});
},
params: [
{
name: 'field',
type: 'field',
write: (
agg: IMetricAggConfig,
output: Record<string, any>,
aggConfigs?: IAggConfigs
): void => {
if (!aggConfigs) {
return;
}
const metric = aggConfigs.createAggConfig({
id: agg.id + '-parent',
enabled: true,
type: METRIC_TYPES.CARDINALITY,
schema: 'metric',
params: {
field: agg.getParam('field'),
missing: 0,
},
});
output.parentAggs = (output.parentAggs || []).concat(metric);
output.params = {
buckets_path: metric.id,
};
},
},
] as Array<MetricAggParam<IMetricAggConfig>>,
getFormat() {
const fieldFormatsService = npStart.plugins.data.fieldFormats;

return fieldFormatsService.getDefaultInstance(KBN_FIELD_TYPES.NUMBER);
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export enum METRIC_TYPES {
MIN_BUCKET = 'min_bucket',
SUM_BUCKET = 'sum_bucket',
COUNT = 'count',
CUMULATIVE_CARDINALITY = 'cumulative_cardinality',
CUMULATIVE_SUM = 'cumulative_sum',
DERIVATIVE = 'derivative',
GEO_BOUNDS = 'geo_bounds',
Expand Down

0 comments on commit 32f821d

Please sign in to comment.