Skip to content

Commit

Permalink
updating based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Feb 13, 2017
1 parent 7402c6c commit 40af4a3
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 99 deletions.
12 changes: 6 additions & 6 deletions src/ui/public/agg_types/controls/sub_agg.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
<div class="form-group">
<label>Metric</label>
<select
name="buckets_path"
ng-model="agg.params.buckets_path"
name="metricAgg"
ng-model="agg.params.metricAgg"
required
class="form-control">
<option
ng-repeat="respAgg in responseValueAggs track by respAgg.id"
value="{{respAgg.id}}"
ng-if="respAgg.type.name !== 'derivative'"
ng-if="respAgg.type.name !== agg.type.name"
ng-disabled="rejectAgg(respAgg)"
ng-selected="agg.params.buckets_path === respAgg.id">
ng-selected="agg.params.metricAgg === respAgg.id">
metric: {{safeMakeLabel(respAgg)}}
</option>
<option value="custom" ng-selected="agg.params.buckets_path === 'custom'">
<option value="custom" ng-selected="agg.params.metricAgg === 'custom'">
Custom Metric
</option>
</select>
</div>
<div ng-if="agg.params.buckets_path === 'custom'" class="vis-editor-agg-order-agg">
<div ng-if="agg.params.metricAgg === 'custom'" class="vis-editor-agg-order-agg">
<ng-form name="customMetricForm">
<vis-editor-agg-params
agg="agg.params.customMetric"
Expand Down
105 changes: 12 additions & 93 deletions src/ui/public/agg_types/metrics/derivative.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import AggTypesMetricsMetricAggTypeProvider from 'ui/agg_types/metrics/metric_agg_type';
import orderAggTemplate from 'ui/agg_types/controls/sub_agg.html';
import _ from 'lodash';
import $ from 'jquery';
import VisAggConfigProvider from 'ui/vis/agg_config';
import VisSchemasProvider from 'ui/vis/schemas';
import { makeNestedLabel } from './lib/make_nested_label';
import { parentPipelineAggController } from './lib/parent_pipeline_agg_controller';
import { parentPipelineAggWritter } from './lib/parent_pipeline_agg_writter';

export default function AggTypeMetricDerivativeProvider(Private) {
const DerivativeAggType = Private(AggTypesMetricsMetricAggTypeProvider);
const MetricAggType = Private(AggTypesMetricsMetricAggTypeProvider);
const AggConfig = Private(VisAggConfigProvider);
const Schemas = Private(VisSchemasProvider);

Expand All @@ -20,26 +22,10 @@ export default function AggTypeMetricDerivativeProvider(Private) {
}
])).all[0];

return new DerivativeAggType({
return new MetricAggType({
name: 'derivative',
title: 'Derivative',
makeLabel: function (aggConfig) {
if (aggConfig.params.customMetric) {
let label = aggConfig.params.customMetric.makeLabel();
if (label.includes('Derivative of ')) {
label = '2. derivative of ' + label.substring('Derivative of '.length);
}
else if (label.includes('derivative of ')) {
label = (parseInt(label.substring(0, 1)) + 1) + label.substring(1);
}
else {
label = 'Derivative of ' + label;
}
return label;
}
const metric = aggConfig.vis.aggs.find(agg => agg.id === aggConfig.params.buckets_path);
return 'Derivative of ' + metric.makeLabel();
},
makeLabel: agg => makeNestedLabel(agg, 'derivative'),
params: [
{
name: 'customMetric',
Expand All @@ -62,80 +48,13 @@ export default function AggTypeMetricDerivativeProvider(Private) {
},
{
name: 'buckets_path',
write: _.noop
},
{
name: 'metricAgg',
editor: orderAggTemplate,
controller: function ($scope, $element) {

$scope.safeMakeLabel = function (agg) {
try {
return agg.makeLabel();
} catch (e) {
return '- agg not valid -';
}
};

$scope.$watch('responseValueAggs', updateOrderAgg);
$scope.$watch('agg.params.buckets_path', updateOrderAgg);

$scope.$on('$destroy', function () {
if ($scope.aggForm && $scope.aggForm.agg) {
$scope.aggForm.agg.$setValidity('bucket', true);
}
});

// Returns true if the agg is not compatible with the terms bucket
$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 checkBuckets() {
const buckets = $scope.vis.aggs.filter(agg => agg.schema.group === 'buckets');
const bucketIsHistogram = ['date_histogram', 'histogram'].includes(buckets[0].type.name);
const canUseDerivative = buckets.length === 1 && bucketIsHistogram;
if ($scope.aggForm.agg) $scope.aggForm.agg.$setValidity('bucket', canUseDerivative);
if (canUseDerivative) {
if (buckets[0].type.name === 'histogram') {
buckets[0].params.min_doc_count = 1;
}
else {
buckets[0].params.min_doc_count = 0;
}
}
}

function updateOrderAgg() {
const agg = $scope.agg;
const params = agg.params;
const bucketsPath = params.buckets_path;
const paramDef = agg.type.params.byName.customMetric;

checkBuckets();

// we aren't creating a custom aggConfig
if (bucketsPath !== 'custom') {
params.customMetric = null;
return;
}

params.customMetric = params.customMetric || paramDef.makeAgg(agg);
}
},
write: function (agg, output) {
const vis = agg.vis;
const orderAgg = agg.params.customMetric || vis.aggs.getResponseAggById(agg.params.buckets_path);

if (agg.params.customMetric && agg.params.customMetric.type.name !== 'count') {
output.parentAggs = (output.parentAggs || []).concat(orderAgg);
}

output.params = {};
if (orderAgg.type.name === 'count') {
output.params.buckets_path = '_count';
} else {
output.params.buckets_path = orderAgg.id;
}
}
controller: parentPipelineAggController,
write: parentPipelineAggWritter
}
]
});
Expand Down
23 changes: 23 additions & 0 deletions src/ui/public/agg_types/metrics/lib/make_nested_label.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import _ from 'lodash';

const makeNestedLabel = function (aggConfig, label) {
const uppercaseLabel = _.startCase(label);
if (aggConfig.params.customMetric) {
let metricLabel = aggConfig.params.customMetric.makeLabel();
if (metricLabel.includes(`${uppercaseLabel} of `)) {
metricLabel = metricLabel.substring(`${uppercaseLabel} of `.length);
metricLabel = `2. ${label} of ${metricLabel}`;
}
else if (metricLabel.includes(`${label} of `)) {
metricLabel = (parseInt(metricLabel.substring(0, 1)) + 1) + metricLabel.substring(1);
}
else {
metricLabel = `${uppercaseLabel} of ${metricLabel}`;
}
return metricLabel;
}
const metric = aggConfig.vis.aggs.find(agg => agg.id === aggConfig.params.buckets_path);
return `${uppercaseLabel} of ${metric.makeLabel()}`;
};

export { makeNestedLabel };
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const aggFilter = ['!top_hits', '!percentiles', '!percentile_ranks', '!median', '!std_dev'];

const parentPipelineAggController = function ($scope) {

$scope.safeMakeLabel = function (agg) {
try {
return agg.makeLabel();
} catch (e) {
return '- agg not valid -';
}
};

$scope.$watch('responseValueAggs', updateOrderAgg);
$scope.$watch('agg.params.metricAgg', updateOrderAgg);

$scope.$on('$destroy', function () {
if ($scope.aggForm && $scope.aggForm.agg) {
$scope.aggForm.agg.$setValidity('bucket', true);
}
});

// Returns true if the agg is not compatible with the terms bucket
$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 checkBuckets() {
const buckets = $scope.vis.aggs.filter(agg => agg.schema.group === 'buckets');
const bucketIsHistogram = buckets.length && ['date_histogram', 'histogram'].includes(buckets[0].type.name);
const canUseAggregation = buckets.length === 1 && bucketIsHistogram;
if ($scope.aggForm.agg) $scope.aggForm.agg.$setValidity('bucket', canUseAggregation);
if (canUseAggregation) {
if (buckets[0].type.name === 'histogram') {
buckets[0].params.min_doc_count = 1;
}
else {
buckets[0].params.min_doc_count = 0;
}
}
}

function updateOrderAgg() {
const agg = $scope.agg;
const params = agg.params;
const metricAgg = params.metricAgg;
const paramDef = agg.type.params.byName.customMetric;

checkBuckets();

// we aren't creating a custom aggConfig
if (metricAgg !== 'custom') {
params.customMetric = null;
return;
}

params.customMetric = params.customMetric || paramDef.makeAgg(agg);
}
};

export { parentPipelineAggController };
17 changes: 17 additions & 0 deletions src/ui/public/agg_types/metrics/lib/parent_pipeline_agg_writter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const parentPipelineAggWritter = function (agg, output) {
const vis = agg.vis;
const orderAgg = agg.params.customMetric || vis.aggs.getResponseAggById(agg.params.metricAgg);

if (agg.params.customMetric && agg.params.customMetric.type.name !== 'count') {
output.parentAggs = (output.parentAggs || []).concat(orderAgg);
}

output.params = {};
if (orderAgg.type.name === 'count') {
output.params.buckets_path = '_count';
} else {
output.params.buckets_path = orderAgg.id;
}
};

export { parentPipelineAggWritter };

0 comments on commit 40af4a3

Please sign in to comment.