Skip to content

Commit

Permalink
[TSVB] Change term sorting to match Kibana Core (#14679)
Browse files Browse the repository at this point in the history
* [TSVB] Change term sorting to make Kibana Core

* removing last bucket changes

* Update tests to reflect new sorting metric
  • Loading branch information
simianhacker authored Nov 9, 2017
1 parent 50fcc36 commit 680cf1a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,13 @@ describe('splitByTerms(req, panel, series)', () => {
field: 'host',
size: 10,
order: {
'avgmetric-SORT > SORT': 'desc'
'avgmetric-SORT': 'desc'
}
},
aggs: {
'avgmetric-SORT': {
aggs: {
SORT: {
avg: {
field: 'cpu'
}
}
},
filter: {
range: {
timestamp: {
format: 'epoch_millis',
gte: 1483232355000,
lte: 1483232400000
}
}
avg: {
field: 'cpu'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,22 @@
import _ from 'lodash';
import basicAggs from '../../../../../common/basic_aggs';
import getBucketSize from '../../helpers/get_bucket_size';
import getTimerange from '../../helpers/get_timerange';
import getIntervalAndTimefield from '../../get_interval_and_timefield';
import getBucketsPath from '../../helpers/get_buckets_path';
import bucketTransform from '../../helpers/bucket_transform';

export default function splitByTerm(req, panel, series) {
return next => doc => {
if (series.split_mode === 'terms' && series.terms_field) {
const { timeField, interval } = getIntervalAndTimefield(panel, series);
const {
bucketSize
} = getBucketSize(req, interval);
const {
to
} = getTimerange(req);

const direction = series.terms_direction || 'desc';

_.set(doc, `aggs.${series.id}.terms.field`, series.terms_field);
_.set(doc, `aggs.${series.id}.terms.size`, series.terms_size);
const metric = series.metrics.find(item => item.id === series.terms_order_by);
if (metric && metric.type !== 'count' && ~basicAggs.indexOf(metric.type)) {
const sortAggKey = `${series.terms_order_by}-SORT`;
const fn = bucketTransform[metric.type];
const bucketPath = getBucketsPath(series.terms_order_by, series.metrics)
.replace(series.terms_order_by, `${sortAggKey} > SORT`);
.replace(series.terms_order_by, sortAggKey);
_.set(doc, `aggs.${series.id}.terms.order`, { [bucketPath]: direction });
_.set(doc, `aggs.${series.id}.aggs`, {
[sortAggKey]: {
filter: {
range: {
[timeField]: {
gte: to.valueOf() - (bucketSize * 1500),
lte: to.valueOf(),
format: 'epoch_millis'
}
}
},
aggs: { SORT: fn(metric) }
}
});
_.set(doc, `aggs.${series.id}.aggs`, { [sortAggKey]: fn(metric) });
} else if (['_term', '_count'].includes(series.terms_order_by)) {
_.set(doc, `aggs.${series.id}.terms.order`, { [series.terms_order_by]: direction });
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { get, set, last } from 'lodash';

import basicAggs from '../../../../../common/basic_aggs';
import getBucketSize from '../../helpers/get_bucket_size';
import getTimerange from '../../helpers/get_timerange';
import getIntervalAndTimefield from '../../get_interval_and_timefield';
import getBucketsPath from '../../helpers/get_buckets_path';
import bucketTransform from '../../helpers/bucket_transform';

Expand All @@ -15,32 +12,16 @@ export default function pivot(req, panel) {
set(doc, 'aggs.pivot.terms.size', panel.pivot_rows);
if (sort) {
const series = panel.series.find(item => item.id === sort.column);
const { timeField, interval } = getIntervalAndTimefield(panel, series);
const { bucketSize } = getBucketSize(req, interval);
const { to } = getTimerange(req);
const metric = series && last(series.metrics);
if (metric && metric.type === 'count') {
set(doc, 'aggs.pivot.terms.order', { _count: sort.order });
} else if (metric && basicAggs.includes(metric.type)) {
const sortAggKey = `${metric.id}-SORT`;
const fn = bucketTransform[metric.type];
const bucketPath = getBucketsPath(metric.id, series.metrics)
.replace(metric.id, `${sortAggKey} > SORT`);
.replace(metric.id, sortAggKey);
set(doc, `aggs.pivot.terms.order`, { [bucketPath]: sort.order });
set(doc, `aggs.pivot.aggs`, {
[sortAggKey]: {
filter: {
range: {
[timeField]: {
gte: to.valueOf() - (bucketSize * 1500),
lte: to.valueOf(),
format: 'epoch_millis'
}
}
},
aggs: { SORT: fn(metric) }
}
});
set(doc, `aggs.pivot.aggs`, { [sortAggKey]: fn(metric) });
} else {
set(doc, 'aggs.pivot.terms.order', { _term: get(sort, 'order', 'asc') });
}
Expand Down

0 comments on commit 680cf1a

Please sign in to comment.