Skip to content

Commit

Permalink
prevent to sort terms on top_hits
Browse files Browse the repository at this point in the history
  • Loading branch information
scampi committed Oct 11, 2016
1 parent c6494cc commit b365ddc
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 18 deletions.
13 changes: 0 additions & 13 deletions src/ui/public/agg_types/__tests__/buckets/_terms.js

This file was deleted.

156 changes: 156 additions & 0 deletions src/ui/public/agg_types/__tests__/buckets/terms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import expect from 'expect.js';
import ngMock from 'ng_mock';
import AggTypesIndexProvider from 'ui/agg_types/index';

describe('Terms Agg', function () {
describe('order agg editor UI', function () {

let $rootScope;

function init({ responseValueAggs = [] }) {
ngMock.module('kibana');
ngMock.inject(function (Private, $controller, _$rootScope_) {
const terms = Private(AggTypesIndexProvider).byName.terms;
const orderAggController = terms.params.byName.orderAgg.controller;

$rootScope = _$rootScope_;
$rootScope.agg = {
id: 'test',
params: {},
type: terms,
vis: {
aggs: []
}
};
$rootScope.responseValueAggs = responseValueAggs;
$controller(orderAggController, { $scope: $rootScope });
$rootScope.$digest();
});
}

it('defaults to the first metric agg', function () {
init({
responseValueAggs: [
{
id: 'agg1',
type: {
name: 'count'
}
},
{
id: 'agg2',
type: {
name: 'count'
}
}
]
});
expect($rootScope.agg.params.orderBy).to.be('agg1');
});

it('defaults to the first metric agg that is compatible with the terms bucket', function () {
init({
responseValueAggs: [
{
id: 'agg1',
type: {
name: 'top_hits'
}
},
{
id: 'agg2',
type: {
name: 'percentiles'
}
},
{
id: 'agg3',
type: {
name: 'median'
}
},
{
id: 'agg4',
type: {
name: 'std_dev'
}
},
{
id: 'agg5',
type: {
name: 'count'
}
}
]
});
expect($rootScope.agg.params.orderBy).to.be('agg5');
});

it('defaults to the custom metric if no agg is compatible', function () {
init({
responseValueAggs: [
{
id: 'agg1',
type: {
name: 'top_hits'
}
}
]
});
expect($rootScope.agg.params.orderBy).to.be('custom');
});

it('selects "custom metric" if there are no metric aggs', function () {
init({});
expect($rootScope.agg.params.orderBy).to.be('custom');
});

it('is emptied if the selected metric becomes incompatible', function () {
init({
responseValueAggs: [
{
id: 'agg1',
type: {
name: 'count'
}
}
]
});
expect($rootScope.agg.params.orderBy).to.be('agg1');
$rootScope.responseValueAggs = [
{
id: 'agg1',
type: {
name: 'top_hits'
}
}
];
$rootScope.$digest();
expect($rootScope.agg.params.orderBy).to.be(null);
});

it('is emptied if the selected metric is removed', function () {
init({
responseValueAggs: [
{
id: 'agg1',
type: {
name: 'count'
}
}
]
});
expect($rootScope.agg.params.orderBy).to.be('agg1');
$rootScope.responseValueAggs = [];
$rootScope.$digest();
expect($rootScope.agg.params.orderBy).to.be(null);
});

it('adds "custom metric" option');
it('lists all metric agg responses');
it('lists individual values of a multi-value metric');
it('displays a metric editor if "custom metric" is selected');
it('saves the "custom metric" to state and refreshes from it');
it('invalidates the form if the metric agg form is not complete');
});
});
19 changes: 15 additions & 4 deletions src/ui/public/agg_types/buckets/terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ export default function TermsAggDefinition(Private) {
let createFilter = Private(AggTypesBucketsCreateFilterTermsProvider);
const routeBasedNotifier = Private(routeBasedNotifierProvider);

const aggFilter = ['!top_hits', '!percentiles', '!median', '!std_dev'];
let orderAggSchema = (new Schemas([
{
group: 'none',
name: 'orderAgg',
title: 'Order Agg',
aggFilter: ['!percentiles', '!median', '!std_dev']
aggFilter: aggFilter
}
])).all[0];

Expand Down Expand Up @@ -95,9 +96,14 @@ export default function TermsAggDefinition(Private) {
$scope.$watch('responseValueAggs', updateOrderAgg);
$scope.$watch('agg.params.orderBy', updateOrderAgg);

$scope.rejectAgg = function (agg) {
// aggFilter elements all starts with a '!'
// so the index of agg.type.name in a filter is 1 if it is included
return Boolean(aggFilter.find((filter) => filter.indexOf(agg.type.name) === 1));
};

function updateOrderAgg() {
let agg = $scope.agg;
let aggs = agg.vis.aggs;
let params = agg.params;
let orderBy = params.orderBy;
let paramDef = agg.type.params.byName.orderAgg;
Expand All @@ -106,7 +112,11 @@ export default function TermsAggDefinition(Private) {
if (!orderBy && prevOrderBy === INIT) {
// abort until we get the responseValueAggs
if (!$scope.responseValueAggs) return;
params.orderBy = (_.first($scope.responseValueAggs) || { id: 'custom' }).id;
let respAgg = _($scope.responseValueAggs).filter((agg) => !$scope.rejectAgg(agg)).first();
if (!respAgg) {
respAgg = { id: 'custom' };
}
params.orderBy = respAgg.id;
return;
}

Expand All @@ -123,7 +133,8 @@ export default function TermsAggDefinition(Private) {
}

// ensure that orderBy is set to a valid agg
if (!_.find($scope.responseValueAggs, { id: orderBy })) {
const respAgg = _($scope.responseValueAggs).filter((agg) => !$scope.rejectAgg(agg)).find({ id: orderBy });
if (!respAgg) {
params.orderBy = null;
}
return;
Expand Down
3 changes: 2 additions & 1 deletion src/ui/public/agg_types/controls/order_agg.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<option
ng-repeat="respAgg in responseValueAggs track by respAgg.id"
value="{{respAgg.id}}"
ng-disabled="rejectAgg(respAgg)"
ng-selected="agg.params.orderBy === respAgg.id">
metric: {{safeMakeLabel(respAgg)}}
</option>
Expand All @@ -27,4 +28,4 @@
group-name="'metrics'">
</vis-editor-agg-params>
</div>
</div>
</div>

0 comments on commit b365ddc

Please sign in to comment.