Skip to content

Commit

Permalink
do not try to get the doc_values field of an IP field
Browse files Browse the repository at this point in the history
  • Loading branch information
scampi committed Nov 18, 2016
1 parent bc1c90a commit d697f11
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/test_utils/stub_index_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import getComputedFields from 'ui/index_patterns/_get_computed_fields';
import RegistryFieldFormatsProvider from 'ui/registry/field_formats';
import IndexPatternsFlattenHitProvider from 'ui/index_patterns/_flatten_hit';
import IndexPatternsFieldProvider from 'ui/index_patterns/_field';
import initDefaultFieldProps from '../core_plugins/kibana/server/lib/init_default_field_props';

export default function (Private) {
let fieldFormats = Private(RegistryFieldFormatsProvider);
let flattenHit = Private(IndexPatternsFlattenHitProvider);
Expand Down Expand Up @@ -42,7 +44,7 @@ export default function (Private) {
this.fields = new IndexedArray({
index: ['name'],
group: ['type'],
initialSet: fields.map(function (field) {
initialSet: initDefaultFieldProps(fields).map(function (field) {
return new Field(this, field);
}, this)
});
Expand Down
169 changes: 169 additions & 0 deletions src/ui/public/agg_types/__tests__/metrics/top_hit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import expect from 'expect.js';
import ngMock from 'ng_mock';
import TopHitProvider from 'ui/agg_types/metrics/top_hit';
import VisProvider from 'ui/vis';
import StubbedIndexPattern from 'fixtures/stubbed_logstash_index_pattern';

describe('Top hit metric', function () {
let aggDsl;
let topHitMetric;
let aggConfig;

function init({ field, sortOrder }) {
ngMock.module('kibana');
ngMock.inject(function (Private) {
const Vis = Private(VisProvider);
const indexPattern = Private(StubbedIndexPattern);
topHitMetric = Private(TopHitProvider);

const params = {};
if (field) {
params.field = field;
}
if (sortOrder) {
params.sortOrder = sortOrder;
}
const vis = new Vis(indexPattern, {
title: 'New Visualization',
type: 'metric',
params: {
fontSize: 60,
handleNoResults: true
},
aggs: [
{
id: '1',
type: 'top_hits',
schema: 'metric',
params
}
],
listeners: {}
});

// Grab the aggConfig off the vis (we don't actually use the vis for anything else)
aggConfig = vis.aggs[0];
aggDsl = aggConfig.toDsl();
});
}

it ('should return a label prefixed with Last if sorting in descending order', function () {
init({ field: 'bytes' });
expect(topHitMetric.makeLabel(aggConfig)).to.eql('Last bytes');
});

it ('should return a label prefixed with First if sorting in ascending order', function () {
init({
field: 'bytes',
sortOrder: {
val: 'asc'
}
});
expect(topHitMetric.makeLabel(aggConfig)).to.eql('First bytes');
});

it ('should request both for the source and doc_values fields', function () {
init({ field: 'bytes' });
expect(aggDsl.top_hits._source).to.be('bytes');
expect(aggDsl.top_hits.docvalue_fields).to.eql([ 'bytes' ]);
});

it ('should only request for the source if the field is an IP', function () {
init({ field: 'ip' });
expect(aggDsl.top_hits._source).to.be('ip');
expect(aggDsl.top_hits.docvalue_fields).to.be(undefined);
});

describe('try to get the value from the top hit', function () {
it ('should return null if there is no hit', function () {
const bucket = {
'1': {
hits: {
hits: []
}
}
};

init({ field: '@tags' });
expect(topHitMetric.getValue(aggConfig, bucket)).to.be(null);
});

it ('should return null if the field has no value', function () {
const bucket = {
'1': {
hits: {
hits: [
{
_source: {
bytes: 123
}
}
]
}
}
};

init({ field: '@tags' });
expect(topHitMetric.getValue(aggConfig, bucket)).to.be(null);
});

it ('should return the field value from the top hit', function () {
const bucket = {
'1': {
hits: {
hits: [
{
_source: {
'@tags': 'aaa'
}
}
]
}
}
};

init({ field: '@tags' });
expect(topHitMetric.getValue(aggConfig, bucket)).to.be('aaa');
});

it ('should return a stringified representation of the field value if it is an object', function () {
const bucket = {
'1': {
hits: {
hits: [
{
_source: {
'@tags': {
label: 'aaa'
}
}
}
]
}
}
};

init({ field: '@tags' });
expect(topHitMetric.getValue(aggConfig, bucket)).to.be(JSON.stringify({ label: 'aaa' }, null, ' '));
});

it ('should return a stringified representation of the field if it has more than one values', function () {
const bucket = {
'1': {
hits: {
hits: [
{
_source: {
'@tags': [ 'aaa', 'bbb' ]
}
}
]
}
}
};

init({ field: '@tags' });
expect(topHitMetric.getValue(aggConfig, bucket)).to.be(JSON.stringify([ 'aaa', 'bbb' ], null, ' '));
});
});
});
4 changes: 3 additions & 1 deletion src/ui/public/agg_types/metrics/top_hit.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export default function AggTypeMetricTopProvider(Private) {
}
};
} else {
if (field.doc_values) {
// do not try to get the doc_values for IP fields, since it is
// an internal representataion of the IP and so of no use for display.
if (field.type !== 'ip' && field.doc_values) {
output.params.docvalue_fields = [ field.name ];
}
output.params._source = field.name;
Expand Down

0 comments on commit d697f11

Please sign in to comment.