mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
Merge pull request #3950 from spalger/fix/3926
[aggTypes/dateHistogram] propertly validate custom intervals
This commit is contained in:
commit
1390506ff9
5 changed files with 42 additions and 14 deletions
|
@ -21,6 +21,7 @@ define(function (require) {
|
|||
}
|
||||
|
||||
require('filters/field_type');
|
||||
require('components/validateDateInterval');
|
||||
|
||||
return new BucketAggType({
|
||||
name: 'date_histogram',
|
||||
|
|
|
@ -5,6 +5,8 @@ define(function (require) {
|
|||
var BucketAggType = Private(require('components/agg_types/buckets/_bucket_agg_type'));
|
||||
var createFilter = Private(require('components/agg_types/buckets/create_filter/histogram'));
|
||||
|
||||
require('components/validateDateInterval');
|
||||
|
||||
return new BucketAggType({
|
||||
name: 'histogram',
|
||||
title: 'Histogram',
|
||||
|
|
|
@ -20,8 +20,10 @@
|
|||
</select>
|
||||
<input
|
||||
type="text"
|
||||
name="customInterval"
|
||||
ng-model="agg.params.customInterval"
|
||||
ng-change="agg.write()"
|
||||
validate-date-interval
|
||||
ng-change="aggForm.customInterval.$valid && agg.write()"
|
||||
ng-if="agg.params.interval.val == 'custom'"
|
||||
class="form-control"
|
||||
required />
|
||||
|
|
23
src/kibana/components/validateDateInterval.js
Normal file
23
src/kibana/components/validateDateInterval.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
define(function (require) {
|
||||
var parseInterval = require('utils/parse_interval');
|
||||
|
||||
require('modules')
|
||||
.get('kibana')
|
||||
.directive('validateDateInterval', function () {
|
||||
return {
|
||||
restrict: 'A',
|
||||
require: 'ngModel',
|
||||
link: function ($scope, $el, attrs, ngModelCntrl) {
|
||||
|
||||
ngModelCntrl.$parsers.push(check);
|
||||
ngModelCntrl.$formatters.push(check);
|
||||
|
||||
function check(value) {
|
||||
ngModelCntrl.$setValidity('dateInterval', parseInterval(value) != null);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
});
|
|
@ -3,20 +3,19 @@ define(function (require) {
|
|||
var moment = require('moment');
|
||||
var datemath = require('utils/datemath');
|
||||
|
||||
return function parseInterval(interval) {
|
||||
// Assume interval is in the form (value)(unit), such as "1h"
|
||||
var regex = new RegExp('^([0-9\\.]*)\\s*(' + datemath.units.join('|') + ')$');
|
||||
var matches = regex.exec(interval);
|
||||
var value;
|
||||
var unit;
|
||||
// Assume interval is in the form (value)(unit), such as "1h"
|
||||
var INTERVAL_STRING_RE = new RegExp('^([0-9\\.]*)\\s*(' + datemath.units.join('|') + ')$');
|
||||
|
||||
if (matches && matches.length) {
|
||||
value = parseFloat(matches[1]) || 1;
|
||||
unit = matches[2];
|
||||
}
|
||||
return function parseInterval(interval) {
|
||||
var matches = String(interval).trim().match(INTERVAL_STRING_RE);
|
||||
|
||||
if (!matches) return null;
|
||||
|
||||
try {
|
||||
interval = moment.duration(value, unit);
|
||||
var value = parseFloat(matches[1]) || 1;
|
||||
var unit = matches[2];
|
||||
|
||||
var duration = moment.duration(value, unit);
|
||||
|
||||
// There is an error with moment, where if you have a fractional interval between 0 and 1, then when you add that
|
||||
// interval to an existing moment object, it will remain unchanged, which causes problems in the ordered_x_keys
|
||||
|
@ -26,9 +25,10 @@ define(function (require) {
|
|||
// the start date, you get the same exact date (instead of being ahead by 12 hours). So instead of returning
|
||||
// a duration corresponding to 0.5 hours, we return a duration corresponding to 12 hours.
|
||||
var selectedUnit = _.find(datemath.units, function (unit) {
|
||||
return Math.abs(interval.as(unit)) >= 1;
|
||||
return Math.abs(duration.as(unit)) >= 1;
|
||||
});
|
||||
return moment.duration(interval.as(selectedUnit), selectedUnit);
|
||||
|
||||
return moment.duration(duration.as(selectedUnit), selectedUnit);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue