-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
426 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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 _ from 'lodash'; | ||
import { VisProvider } from '../../vis'; | ||
import { aggTypes } from '..'; | ||
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern'; | ||
|
||
// eslint-disable-next-line @elastic/kibana-custom/no-default-export | ||
export default function AggParamWriterHelper(Private) { | ||
const Vis = Private(VisProvider); | ||
const stubbedLogstashIndexPattern = Private(FixturesStubbedLogstashIndexPatternProvider); | ||
|
||
/** | ||
* Helper object for writing aggParams. Specify an aggType and it will find a vis & schema, and | ||
* wire up the supporting objects required to feed in parameters, and get #write() output. | ||
* | ||
* Use cases: | ||
* - Verify that the interval parameter of the histogram visualization casts its input to a number | ||
* ```js | ||
* it('casts to a number', function () { | ||
* let writer = new AggParamWriter({ aggType: 'histogram' }); | ||
* let output = writer.write({ interval : '100/10' }); | ||
* expect(output.params.interval).to.be.a('number'); | ||
* expect(output.params.interval).to.be(100); | ||
* }); | ||
* ``` | ||
* | ||
* @class AggParamWriter | ||
* @param {object} opts - describe the properties of this paramWriter | ||
* @param {string} opts.aggType - the name of the aggType we want to test. ('histogram', 'filter', etc.) | ||
*/ | ||
function AggParamWriter(opts) { | ||
const self = this; | ||
|
||
self.aggType = opts.aggType; | ||
if (_.isString(self.aggType)) { | ||
self.aggType = aggTypes.byName[self.aggType]; | ||
} | ||
|
||
// not configurable right now, but totally required | ||
self.indexPattern = stubbedLogstashIndexPattern; | ||
|
||
// the schema that the aggType satisfies | ||
self.visAggSchema = null; | ||
|
||
self.vis = new Vis(self.indexPattern, { | ||
type: 'histogram', | ||
aggs: [{ | ||
id: 1, | ||
type: self.aggType.name, | ||
params: {} | ||
}] | ||
}); | ||
} | ||
|
||
AggParamWriter.prototype.write = function (paramValues) { | ||
const self = this; | ||
paramValues = _.clone(paramValues); | ||
|
||
if (self.aggType.params.byName.field && !paramValues.field) { | ||
// pick a field rather than force a field to be specified everywhere | ||
if (self.aggType.type === 'metrics') { | ||
paramValues.field = _.sample(self.indexPattern.fields.byType.number); | ||
} else { | ||
const type = self.aggType.params.byName.field.filterFieldTypes || 'string'; | ||
let field; | ||
do { | ||
field = _.sample(self.indexPattern.fields.byType[type]); | ||
} while (!field.aggregatable); | ||
paramValues.field = field.name; | ||
} | ||
} | ||
|
||
|
||
const aggConfig = self.vis.aggs[0]; | ||
aggConfig.setParams(paramValues); | ||
|
||
return aggConfig.write(self.vis.aggs); | ||
}; | ||
|
||
return AggParamWriter; | ||
|
||
} |
151 changes: 151 additions & 0 deletions
151
src/ui/public/agg_types/__tests__/buckets/_histogram.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* 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 expect from 'expect.js'; | ||
import ngMock from 'ng_mock'; | ||
import { aggTypes } from '../..'; | ||
import AggParamWriterProvider from '../agg_param_writer'; | ||
|
||
const histogram = aggTypes.byName.histogram; | ||
describe('Histogram Agg', function () { | ||
|
||
describe('ordered', function () { | ||
|
||
it('is ordered', function () { | ||
expect(histogram.ordered).to.be.ok(); | ||
}); | ||
|
||
it('is not ordered by date', function () { | ||
expect(histogram.ordered).to.not.have.property('date'); | ||
}); | ||
}); | ||
|
||
|
||
describe('params', function () { | ||
let paramWriter; | ||
|
||
beforeEach(ngMock.module('kibana')); | ||
beforeEach(ngMock.inject(function (Private) { | ||
const AggParamWriter = Private(AggParamWriterProvider); | ||
paramWriter = new AggParamWriter({ aggType: 'histogram' }); | ||
})); | ||
|
||
describe('interval', function () { | ||
// reads aggConfig.params.interval, writes to dsl.interval | ||
|
||
it('accepts a whole number', function () { | ||
const output = paramWriter.write({ interval: 100 }); | ||
expect(output.params).to.have.property('interval', 100); | ||
}); | ||
|
||
it('accepts a decimal number', function () { | ||
const output = paramWriter.write({ interval: 0.1 }); | ||
expect(output.params).to.have.property('interval', 0.1); | ||
}); | ||
|
||
it('accepts a decimal number string', function () { | ||
const output = paramWriter.write({ interval: '0.1' }); | ||
expect(output.params).to.have.property('interval', 0.1); | ||
}); | ||
|
||
it('accepts a whole number string', function () { | ||
const output = paramWriter.write({ interval: '10' }); | ||
expect(output.params).to.have.property('interval', 10); | ||
}); | ||
|
||
it('fails on non-numeric values', function () { | ||
// template validation prevents this from users, not devs | ||
const output = paramWriter.write({ interval: [] }); | ||
expect(isNaN(output.params.interval)).to.be.ok(); | ||
}); | ||
}); | ||
|
||
describe('min_doc_count', function () { | ||
it('casts true values to 0', function () { | ||
let output = paramWriter.write({ min_doc_count: true }); | ||
expect(output.params).to.have.property('min_doc_count', 0); | ||
|
||
output = paramWriter.write({ min_doc_count: 'yes' }); | ||
expect(output.params).to.have.property('min_doc_count', 0); | ||
|
||
output = paramWriter.write({ min_doc_count: 1 }); | ||
expect(output.params).to.have.property('min_doc_count', 0); | ||
|
||
output = paramWriter.write({ min_doc_count: {} }); | ||
expect(output.params).to.have.property('min_doc_count', 0); | ||
}); | ||
|
||
it('writes 1 for falsy values', function () { | ||
let output = paramWriter.write({ min_doc_count: '' }); | ||
expect(output.params).to.have.property('min_doc_count', 1); | ||
|
||
output = paramWriter.write({ min_doc_count: null }); | ||
expect(output.params).to.have.property('min_doc_count', 1); | ||
|
||
output = paramWriter.write({ min_doc_count: undefined }); | ||
expect(output.params).to.have.property('min_doc_count', 1); | ||
}); | ||
}); | ||
|
||
describe('extended_bounds', function () { | ||
it('writes when only eb.min is set', function () { | ||
const output = paramWriter.write({ | ||
min_doc_count: true, | ||
extended_bounds: { min: 0 } | ||
}); | ||
expect(output.params.extended_bounds).to.have.property('min', 0); | ||
expect(output.params.extended_bounds).to.have.property('max', undefined); | ||
}); | ||
|
||
it('writes when only eb.max is set', function () { | ||
const output = paramWriter.write({ | ||
min_doc_count: true, | ||
extended_bounds: { max: 0 } | ||
}); | ||
expect(output.params.extended_bounds).to.have.property('min', undefined); | ||
expect(output.params.extended_bounds).to.have.property('max', 0); | ||
}); | ||
|
||
it('writes when both eb.min and eb.max are set', function () { | ||
const output = paramWriter.write({ | ||
min_doc_count: true, | ||
extended_bounds: { min: 99, max: 100 } | ||
}); | ||
expect(output.params.extended_bounds).to.have.property('min', 99); | ||
expect(output.params.extended_bounds).to.have.property('max', 100); | ||
}); | ||
|
||
it('does not write when nothing is set', function () { | ||
const output = paramWriter.write({ | ||
min_doc_count: true, | ||
extended_bounds: {} | ||
}); | ||
expect(output.params).to.not.have.property('extended_bounds'); | ||
}); | ||
|
||
it('does not write when min_doc_count is false', function () { | ||
const output = paramWriter.write({ | ||
min_doc_count: false, | ||
extended_bounds: { min: 99, max: 100 } | ||
}); | ||
expect(output.params).to.not.have.property('extended_bounds'); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.