Teasing apart the branch walking from the hierarchial tooltip

This commit is contained in:
Chris Cowan 2014-10-22 12:05:31 -07:00
parent 6f857658c6
commit 1e1cedf6aa
4 changed files with 111 additions and 34 deletions

View file

@ -0,0 +1,44 @@
define(function (require) {
var _ = require('lodash');
return function (leaf) {
// walk up the branch for each parent
function walk(item, memo) {
// record the the depth
var depth = item.depth - 1;
// Using the aggConfig determin what the field name is. If the aggConfig
// doesn't exist (which means it's an _all agg) then use the level for
// the field name
var col = item.aggConfig;
var field = (col && col.params && col.params.field && col.params.field.name)
|| (col && col.label)
|| ('level ' + item.depth);
// Set the bucket name, and use the converter to format the field if
// the field exists.
var bucket = item.name;
if (col && col.field && col.field.format && col.field.format.convert) {
bucket = col.field.format.convert(bucket);
}
// Add the row to the tooltipScope.rows
memo.unshift({
aggConfig: col,
depth: depth,
field: field,
bucket: bucket,
metric: item.value
});
// If the item has a parent and it's also a child then continue walking
// up the branch
if (item.parent && item.parent.parent) {
return walk(item.parent, memo);
} else {
return memo;
}
}
return walk(leaf, []);
};
});

View file

@ -3,6 +3,7 @@ define(function (require) {
var _ = require('lodash');
var $ = require('jquery');
var $tooltip = $(require('text!plugins/vis_types/tooltips/pie.html'));
var collectBranch = require('components/visualize/_collect_branch');
var $tooltipScope = $rootScope.$new();
$compile($tooltip)($tooltipScope);
@ -18,40 +19,15 @@ define(function (require) {
sum = parent.value;
}
var rows = $tooltipScope.rows = [];
// Collect the current leaf and parents into an array of values
var rows = collectBranch(datum);
// walk up the branch for each parent
(function walk(item) {
// record the the depth
var i = item.depth - 1;
// Using the aggConfig determin what the field name is. If the aggConfig
// doesn't exist (which means it's an _all agg) then use the level for
// the field name
var col = item.aggConfig;
var field = (col && col.params && col.params.field && col.params.field.name)
|| (col && col.label)
|| ('level ' + datum.depth);
// Set the bucket name, and use the converter to format the field if
// the field exists.
var bucket = item.name;
if (col && col.field) bucket = col.field.format.convert(bucket);
// Add the row to the tooltipScope.rows
rows.unshift({
spacer: $sce.trustAsHtml(_.repeat(' ', i)),
field: field,
bucket: bucket,
metric: item.value + ' (' + Math.round((item.value / sum) * 100) + '%)'
});
// If the item has a parent and it's also a child then continue walking
// up the branch
if (item.parent && item.parent.parent) {
walk(item.parent);
}
})(datum);
// Map those values to what the tooltipSource.rows format.
$tooltipScope.rows = _.map(rows, function (row) {
row.spacer = $sce.trustAsHtml(_.repeat(' ', row.depth));
row.metric = row.metric + ' (' + Math.round((row.metric / sum) * 100) + '%)';
return row;
});
$tooltipScope.metricCol = _.find(columns, { categoryName: 'metric' });

View file

@ -147,7 +147,8 @@
'specs/visualize/_extract_buckets',
'specs/visualize/_transform_aggregation',
'specs/visualize/_create_raw_data',
'specs/visualize/_array_to_linked_list'
'specs/visualize/_array_to_linked_list',
'specs/visualize/_collect_branch'
], function (kibana, sinon) {
kibana.load(function () {
var xhr = sinon.useFakeXMLHttpRequest();

View file

@ -0,0 +1,56 @@
define(function (require) {
var collectBranch = require('components/visualize/_collect_branch');
describe('collectBranch()', function () {
var results;
var convert = function (name) {
return 'converted:' + name;
};
beforeEach(function () {
results = collectBranch({
name: 'bucket3',
depth: 3,
value: 6,
field: { format: { convert: convert } },
aggConfig: { params: { field: { name: 'field3' } } },
parent: {
name: 'bucket2',
depth: 2,
value: 12,
aggConfig: { label: 'field2' },
parent: {
name: 'bucket1',
depth: 1,
value: 24,
parent: {}
}
}
});
});
it('should return an array with bucket objects', function () {
expect(results).to.be.an(Array);
expect(results).to.have.length(3);
expect(results[0]).to.have.property('metric', 24);
expect(results[0]).to.have.property('depth', 0);
expect(results[0]).to.have.property('bucket', 'bucket1');
expect(results[0]).to.have.property('field', 'level 1');
expect(results[0]).to.have.property('aggConfig');
expect(results[1]).to.have.property('metric', 12);
expect(results[1]).to.have.property('depth', 1);
expect(results[1]).to.have.property('bucket', 'bucket2');
expect(results[1]).to.have.property('field', 'field2');
expect(results[1]).to.have.property('aggConfig');
expect(results[2]).to.have.property('metric', 6);
expect(results[2]).to.have.property('depth', 2);
expect(results[2]).to.have.property('bucket', 'bucket3');
expect(results[2]).to.have.property('field', 'field3');
expect(results[2]).to.have.property('aggConfig');
});
});
});