Skip to content

Commit

Permalink
corrected tests and rely on the field's formatter instead of trying t…
Browse files Browse the repository at this point in the history
…o return a nice string
  • Loading branch information
scampi committed Nov 20, 2016
1 parent eda9610 commit b465438
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 46 deletions.
55 changes: 49 additions & 6 deletions src/ui/public/agg_types/__tests__/metrics/top_hit.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('Top hit metric', function () {
expect(topHitMetric.getValue(aggConfig, bucket)).to.be(null);
});

it ('should return null if the field has no value', function () {
it ('should return undefined if the field does not appear in the source', function () {
const bucket = {
'1': {
hits: {
Expand All @@ -104,7 +104,7 @@ describe('Top hit metric', function () {
};

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

it ('should return the field value from the top hit', function () {
Expand All @@ -126,7 +126,7 @@ describe('Top hit metric', function () {
expect(topHitMetric.getValue(aggConfig, bucket)).to.be('aaa');
});

it ('should return a stringified representation of the field value if it is an object', function () {
it ('should return the object if the field value is an object', function () {
const bucket = {
'1': {
hits: {
Expand All @@ -144,10 +144,10 @@ describe('Top hit metric', function () {
};

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

it ('should return a stringified representation of the field if it has more than one values', function () {
it ('should return an array if the field has more than one values', function () {
const bucket = {
'1': {
hits: {
Expand All @@ -163,7 +163,50 @@ describe('Top hit metric', function () {
};

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

it ('should get the value from the doc_values field if the source does not have that field', function () {
const bucket = {
'1': {
hits: {
hits: [
{
_source: {
},
fields: {
'machine.os': [ 'linux' ]
}
}
]
}
}
};

init({ field: 'machine.os' });
expect(topHitMetric.getValue(aggConfig, bucket)).to.be('linux');
});

it ('should return null if the field is not in the source nor in the doc_values field', function () {
const bucket = {
'1': {
hits: {
hits: [
{
_source: {
nope: 'aaa'
},
fields: {
nope: [ 'aaa' ]
}
}
]
}
}
};

init({ field: 'machine.os' });
expect(topHitMetric.getValue(aggConfig, bucket)).to.be(null);
});
});
});
63 changes: 23 additions & 40 deletions src/ui/public/agg_types/metrics/top_hit.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,13 @@
import _ from 'lodash';
import AggTypesAggTypeProvider from 'ui/agg_types/agg_type';
import MetricAggTypeProvider from 'ui/agg_types/metrics/metric_agg_type';
import RegistryFieldFormatsProvider from 'ui/registry/field_formats';
import topSortEditor from 'ui/agg_types/controls/top_sort.html';

export default function AggTypeMetricTopProvider(Private) {
const AggType = Private(AggTypesAggTypeProvider);
const MetricAggType = Private(MetricAggTypeProvider);
const fieldFormats = Private(RegistryFieldFormatsProvider);

_.class(TopHitAggType).inherits(AggType);
function TopHitAggType(config) {
TopHitAggType.Super.call(this, config);
}

TopHitAggType.prototype.getValue = function (agg, bucket) {
const hits = _.get(bucket, `${agg.id}.hits.hits`);
if (!hits || !hits.length) {
return null;
}
const path = agg.params.field.name;
let values = agg.vis.indexPattern.formatField(hits[0], path);
if (!_.isArray(values)) {
values = [ values ];
}

if (!values.length && hits[0].fields) {
// no values found in the source, check the doc_values fields
values = hits[0].fields[path] || [];
}

switch (values.length) {
case 0:
return null;
case 1:
return _.isObject(values[0]) ? JSON.stringify(values[0], null, ' ') : values [0];
default:
return JSON.stringify(values, null, ' ');
}
};

TopHitAggType.prototype.getFormat = function (agg) {
return fieldFormats.getDefaultInstance('string');
};

return new TopHitAggType({
return new MetricAggType({
name: 'top_hits',
title: 'Top Hit',
makeLabel: function (aggConfig) {
Expand Down Expand Up @@ -87,7 +52,7 @@ export default function AggTypeMetricTopProvider(Private) {
name: 'sortField',
type: 'field',
editor: null,
filterFieldTypes: ['number', 'date', 'ip', 'string'],
filterFieldTypes: [ 'number', 'date', 'ip', 'string' ],
default: function (agg) {
return agg.vis.indexPattern.timeFieldName;
},
Expand Down Expand Up @@ -130,6 +95,24 @@ export default function AggTypeMetricTopProvider(Private) {
}
}
}
]
],
getValue(agg, bucket) {
const hits = _.get(bucket, `${agg.id}.hits.hits`);
if (!hits || !hits.length) {
return null;
}
const path = agg.params.field.name;
let values = agg.vis.indexPattern.flattenHit(hits[0])[path];

if ((values === '' || values === null || values === undefined) && hits[0].fields) {
// no values found in the source, check the doc_values fields
values = hits[0].fields[path] || [];
if (!values.length) {
return null;
}
}

return values;
}
});
};

0 comments on commit b465438

Please sign in to comment.