2013-01-30 21:39:57 -07:00
|
|
|
angular.module('kibana.pie', [])
|
2013-02-07 15:05:55 -07:00
|
|
|
.controller('pie', function($scope, eventBus) {
|
2013-01-30 21:39:57 -07:00
|
|
|
|
|
|
|
|
// Set and populate defaults
|
|
|
|
|
var _d = {
|
2013-02-18 09:18:36 -07:00
|
|
|
query : { field:"_all", query:"*", goal: 1},
|
2013-02-18 10:53:25 -07:00
|
|
|
size : 10,
|
2013-01-30 21:39:57 -07:00
|
|
|
exclude : [],
|
|
|
|
|
donut : false,
|
|
|
|
|
tilt : false,
|
|
|
|
|
legend : true,
|
2013-02-05 16:24:46 -07:00
|
|
|
labels : true,
|
2013-02-08 15:18:35 -07:00
|
|
|
mode : "terms",
|
|
|
|
|
group : "default",
|
|
|
|
|
default_field : '_all'
|
2013-01-30 21:39:57 -07:00
|
|
|
}
|
2013-02-05 14:30:08 -07:00
|
|
|
_.defaults($scope.panel,_d)
|
|
|
|
|
|
|
|
|
|
$scope.init = function() {
|
2013-02-07 15:05:55 -07:00
|
|
|
eventBus.register($scope,'time', function(event,time){set_time(time)});
|
2013-02-08 15:18:35 -07:00
|
|
|
eventBus.register($scope,'query', function(event, query) {
|
2013-02-18 09:18:36 -07:00
|
|
|
if($scope.panel.mode !== 'query') {
|
2013-02-05 14:30:08 -07:00
|
|
|
$scope.panel.query.query = query;
|
2013-02-21 15:26:48 -07:00
|
|
|
$scope.panel.query.query = _.isArray(query) ? query[0] : query;
|
|
|
|
|
} else {
|
|
|
|
|
if(_.isArray(query))
|
|
|
|
|
$scope.panel.query = _.map(query,function(q) {
|
|
|
|
|
return {query: q, label: q}})
|
|
|
|
|
else
|
|
|
|
|
$scope.panel.query[0] = {query: query, label: query}
|
2013-02-08 15:18:35 -07:00
|
|
|
}
|
2013-02-21 15:26:48 -07:00
|
|
|
$scope.get_data();
|
2013-02-08 15:18:35 -07:00
|
|
|
});
|
2013-02-05 14:30:08 -07:00
|
|
|
// Now that we're all setup, request the time from our group
|
2013-02-07 15:05:55 -07:00
|
|
|
eventBus.broadcast($scope.$id,$scope.panel.group,'get_time')
|
2013-01-31 08:54:20 -07:00
|
|
|
}
|
|
|
|
|
|
2013-02-08 15:57:24 -07:00
|
|
|
|
|
|
|
|
$scope.remove_query = function(q) {
|
|
|
|
|
if($scope.panel.mode !== 'query')
|
|
|
|
|
return false;
|
|
|
|
|
$scope.panel.query = _.without($scope.panel.query,q);
|
|
|
|
|
$scope.get_data();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-08 15:18:35 -07:00
|
|
|
$scope.add_query = function(label,query) {
|
|
|
|
|
if($scope.panel.mode !== 'query')
|
|
|
|
|
return false;
|
|
|
|
|
$scope.panel.query.unshift({
|
|
|
|
|
query: query,
|
|
|
|
|
label: label,
|
|
|
|
|
});
|
|
|
|
|
$scope.get_data();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-22 14:57:47 -07:00
|
|
|
$scope.set_mode = function(mode) {
|
|
|
|
|
switch(mode)
|
|
|
|
|
{
|
|
|
|
|
case 'terms':
|
|
|
|
|
$scope.panel.query = {query:"*",field:"_all"};
|
|
|
|
|
break;
|
|
|
|
|
case 'query':
|
|
|
|
|
$scope.panel.query = [{query:"*",label:"*"}];
|
|
|
|
|
break;
|
|
|
|
|
case 'goal':
|
|
|
|
|
$scope.panel.query = {query:"*",goal:100};
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-30 21:39:57 -07:00
|
|
|
$scope.get_data = function() {
|
2013-02-05 14:30:08 -07:00
|
|
|
// Make sure we have everything for the request to complete
|
2013-02-13 14:51:56 -07:00
|
|
|
if(_.isUndefined($scope.panel.index) || _.isUndefined($scope.time))
|
2013-02-04 17:05:36 -07:00
|
|
|
return
|
2013-02-05 14:30:08 -07:00
|
|
|
|
2013-02-27 10:00:39 -07:00
|
|
|
$scope.panel.loading = true;
|
2013-02-05 14:30:08 -07:00
|
|
|
var request = $scope.ejs.Request().indices($scope.panel.index);
|
2013-01-30 21:39:57 -07:00
|
|
|
|
|
|
|
|
// If we have an array, use query facet
|
2013-02-08 15:18:35 -07:00
|
|
|
if($scope.panel.mode == "query") {
|
|
|
|
|
if(!(_.isArray($scope.panel.query)))
|
|
|
|
|
$scope.panel.query = [$scope.panel.query];
|
2013-01-30 21:39:57 -07:00
|
|
|
var queries = [];
|
|
|
|
|
// Build the question part of the query
|
|
|
|
|
_.each($scope.panel.query, function(v) {
|
|
|
|
|
queries.push(ejs.FilteredQuery(
|
|
|
|
|
ejs.QueryStringQuery(v.query || '*'),
|
2013-02-22 13:55:46 -07:00
|
|
|
ejs.RangeFilter($scope.time.field)
|
2013-02-13 14:51:56 -07:00
|
|
|
.from($scope.time.from)
|
|
|
|
|
.to($scope.time.to))
|
2013-01-30 21:39:57 -07:00
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Then the insert into facet and make the request
|
|
|
|
|
_.each(queries, function(v) {
|
|
|
|
|
request = request.facet(ejs.QueryFacet(_.indexOf(queries,v))
|
|
|
|
|
.query(v)
|
|
|
|
|
.facetFilter(ejs.QueryFilter(v))
|
|
|
|
|
)
|
|
|
|
|
})
|
2013-03-11 17:13:47 -07:00
|
|
|
$scope.populate_modal(request);
|
2013-01-30 21:39:57 -07:00
|
|
|
var results = request.doSearch();
|
|
|
|
|
|
|
|
|
|
// Populate scope when we have results
|
|
|
|
|
results.then(function(results) {
|
2013-02-27 10:00:39 -07:00
|
|
|
$scope.panel.loading = false;
|
2013-01-30 21:39:57 -07:00
|
|
|
$scope.hits = results.hits.total;
|
|
|
|
|
$scope.data = [];
|
|
|
|
|
_.each(results.facets, function(v, k) {
|
|
|
|
|
var series = {};
|
2013-02-08 15:18:35 -07:00
|
|
|
var label = _.isUndefined($scope.panel.query[k].label) ?
|
|
|
|
|
$scope.panel.query[k].query : $scope.panel.query[k].label
|
|
|
|
|
var slice = { label : label, data : v.count };
|
2013-01-30 21:39:57 -07:00
|
|
|
if (!(_.isUndefined($scope.panel.query[k].color)))
|
|
|
|
|
slice.color = $scope.panel.query[k].color;
|
|
|
|
|
$scope.data.push(slice)
|
|
|
|
|
});
|
2013-02-12 16:25:39 -07:00
|
|
|
$scope.$emit('render');
|
2013-01-30 21:39:57 -07:00
|
|
|
});
|
|
|
|
|
// If we don't have an array, assume its a term facet.
|
2013-02-18 09:18:36 -07:00
|
|
|
} else if ($scope.panel.mode == "terms") {
|
2013-03-11 17:13:47 -07:00
|
|
|
request = request
|
2013-01-30 21:39:57 -07:00
|
|
|
.facet(ejs.TermsFacet('pie')
|
2013-02-08 15:18:35 -07:00
|
|
|
.field($scope.panel.query.field || $scope.panel.default_field)
|
2013-01-30 21:39:57 -07:00
|
|
|
.size($scope.panel['size'])
|
|
|
|
|
.exclude($scope.panel.exclude)
|
|
|
|
|
.facetFilter(ejs.QueryFilter(
|
|
|
|
|
ejs.FilteredQuery(
|
|
|
|
|
ejs.QueryStringQuery($scope.panel.query.query || '*'),
|
2013-02-22 13:55:46 -07:00
|
|
|
ejs.RangeFilter($scope.time.field)
|
2013-02-13 14:51:56 -07:00
|
|
|
.from($scope.time.from)
|
|
|
|
|
.to($scope.time.to)
|
2013-01-30 21:39:57 -07:00
|
|
|
.cache(false)
|
|
|
|
|
)))).size(0)
|
2013-03-11 17:13:47 -07:00
|
|
|
|
|
|
|
|
$scope.populate_modal(request);
|
|
|
|
|
|
|
|
|
|
var results = request.doSearch();
|
2013-01-30 21:39:57 -07:00
|
|
|
|
|
|
|
|
// Populate scope when we have results
|
|
|
|
|
results.then(function(results) {
|
2013-02-27 10:00:39 -07:00
|
|
|
$scope.panel.loading = false;
|
2013-01-30 21:39:57 -07:00
|
|
|
$scope.hits = results.hits.total;
|
|
|
|
|
$scope.data = [];
|
|
|
|
|
var k = 0;
|
|
|
|
|
_.each(results.facets.pie.terms, function(v) {
|
|
|
|
|
var slice = { label : v.term, data : v.count };
|
|
|
|
|
$scope.data.push();
|
|
|
|
|
if(!(_.isUndefined($scope.panel.colors))
|
|
|
|
|
&& _.isArray($scope.panel.colors)
|
|
|
|
|
&& $scope.panel.colors.length > 0) {
|
|
|
|
|
slice.color = $scope.panel.colors[k%$scope.panel.colors.length];
|
|
|
|
|
}
|
|
|
|
|
$scope.data.push(slice)
|
|
|
|
|
k = k + 1;
|
|
|
|
|
});
|
2013-02-12 16:25:39 -07:00
|
|
|
$scope.$emit('render');
|
2013-01-30 21:39:57 -07:00
|
|
|
});
|
2013-02-18 09:18:36 -07:00
|
|
|
} else {
|
2013-03-11 17:13:47 -07:00
|
|
|
request = request
|
2013-02-18 09:18:36 -07:00
|
|
|
.query(ejs.QueryStringQuery($scope.panel.query.query || '*'))
|
2013-02-22 13:55:46 -07:00
|
|
|
.filter(ejs.RangeFilter($scope.time.field)
|
2013-02-18 09:18:36 -07:00
|
|
|
.from($scope.time.from)
|
|
|
|
|
.to($scope.time.to)
|
|
|
|
|
.cache(false))
|
|
|
|
|
.size(0)
|
|
|
|
|
.doSearch();
|
2013-03-11 17:13:47 -07:00
|
|
|
|
|
|
|
|
$scope.populate_modal(request);
|
|
|
|
|
|
|
|
|
|
var results = request.doSearch();
|
|
|
|
|
|
2013-02-18 09:18:36 -07:00
|
|
|
results.then(function(results) {
|
2013-02-27 10:00:39 -07:00
|
|
|
$scope.panel.loading = false;
|
2013-02-18 09:18:36 -07:00
|
|
|
var complete = results.hits.total;
|
|
|
|
|
var remaining = $scope.panel.query.goal - complete;
|
|
|
|
|
$scope.data = [
|
|
|
|
|
{ label : 'Complete', data : complete, color: '#51A351' },
|
|
|
|
|
{ data : remaining, color: '#EEE'}]
|
|
|
|
|
$scope.$emit('render');
|
|
|
|
|
});
|
2013-01-30 21:39:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-11 17:13:47 -07:00
|
|
|
$scope.populate_modal = function(request) {
|
|
|
|
|
$scope.modal = {
|
|
|
|
|
title: "Pie Inspector",
|
|
|
|
|
body : "<h5>Last Elasticsearch Query</h5><pre>"+
|
|
|
|
|
'curl -XGET '+config.elasticsearch+'/'+$scope.panel.index+"/_search?pretty -d'\n"+
|
|
|
|
|
angular.toJson(JSON.parse(request.toString()),true)+
|
|
|
|
|
"'</pre>",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-05 14:30:08 -07:00
|
|
|
function set_time(time) {
|
2013-02-13 14:51:56 -07:00
|
|
|
$scope.time = time;
|
2013-02-05 14:30:08 -07:00
|
|
|
$scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
|
|
|
|
|
$scope.get_data();
|
2013-02-04 17:05:36 -07:00
|
|
|
}
|
2013-03-05 09:54:29 -07:00
|
|
|
|
2013-01-30 21:39:57 -07:00
|
|
|
})
|
|
|
|
|
.directive('pie', function() {
|
|
|
|
|
return {
|
|
|
|
|
restrict: 'A',
|
|
|
|
|
link: function(scope, elem, attrs) {
|
|
|
|
|
|
2013-03-08 10:04:38 -07:00
|
|
|
elem.html('<center><img src="common/img/load_big.gif"></center>')
|
|
|
|
|
|
2013-02-12 16:25:39 -07:00
|
|
|
// Receive render events
|
|
|
|
|
scope.$on('render',function(){
|
|
|
|
|
render_panel();
|
2013-01-30 21:39:57 -07:00
|
|
|
});
|
|
|
|
|
|
2013-02-01 14:16:55 -07:00
|
|
|
// Or if the window is resized
|
2013-01-30 21:39:57 -07:00
|
|
|
angular.element(window).bind('resize', function(){
|
2013-02-12 16:25:39 -07:00
|
|
|
render_panel();
|
2013-01-30 21:39:57 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Function for rendering panel
|
2013-02-12 16:25:39 -07:00
|
|
|
function render_panel() {
|
2013-02-12 08:42:31 -07:00
|
|
|
var scripts = $LAB.script("common/lib/panels/jquery.flot.js")
|
|
|
|
|
.script("common/lib/panels/jquery.flot.pie.js")
|
|
|
|
|
|
2013-02-18 09:18:36 -07:00
|
|
|
if(scope.panel.mode === 'goal')
|
|
|
|
|
var label = {
|
|
|
|
|
show: scope.panel.labels,
|
|
|
|
|
radius: 0,
|
|
|
|
|
formatter: function(label, series){
|
|
|
|
|
var font = parseInt(scope.row.height.replace('px',''))/10 + String('px')
|
|
|
|
|
if(!(_.isUndefined(label)))
|
|
|
|
|
return '<div style="font-size:'+font+';font-weight:bold;text-align:center;padding:2px;color:black;">'+
|
|
|
|
|
Math.round(series.percent)+'%</div>';
|
|
|
|
|
else
|
|
|
|
|
return ''
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
var label = {
|
|
|
|
|
show: scope.panel.labels,
|
|
|
|
|
radius: 2/3,
|
|
|
|
|
formatter: function(label, series){
|
|
|
|
|
return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'+
|
|
|
|
|
label+'<br/>'+Math.round(series.percent)+'%</div>';
|
|
|
|
|
},
|
|
|
|
|
threshold: 0.1
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-30 21:39:57 -07:00
|
|
|
var pie = {
|
|
|
|
|
series: {
|
|
|
|
|
pie: {
|
|
|
|
|
innerRadius: scope.panel.donut ? 0.4 : 0,
|
|
|
|
|
tilt: scope.panel.tilt ? 0.45 : 1,
|
|
|
|
|
radius: 1,
|
|
|
|
|
show: true,
|
|
|
|
|
combine: {
|
|
|
|
|
color: '#999',
|
|
|
|
|
label: 'The Rest'
|
|
|
|
|
},
|
2013-02-18 09:18:36 -07:00
|
|
|
label: label
|
2013-01-30 21:39:57 -07:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
//grid: { hoverable: true, clickable: true },
|
2013-02-05 16:24:46 -07:00
|
|
|
grid: { hoverable: true, clickable: true },
|
2013-03-08 10:04:38 -07:00
|
|
|
legend: { show: scope.panel.legend },
|
|
|
|
|
colors: ['#EB6841','#00A0B0','#6A4A3C','#EDC951','#CC333F']
|
2013-01-30 21:39:57 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Populate element
|
2013-02-01 14:16:55 -07:00
|
|
|
if(elem.is(":visible")){
|
2013-02-12 08:42:31 -07:00
|
|
|
scripts.wait(function(){
|
|
|
|
|
$.plot(elem, scope.data, pie);
|
|
|
|
|
});
|
2013-02-01 14:16:55 -07:00
|
|
|
}
|
2013-02-05 16:24:46 -07:00
|
|
|
|
|
|
|
|
function piett(x, y, contents) {
|
|
|
|
|
var tooltip = $('#pie-tooltip').length ?
|
|
|
|
|
$('#pie-tooltip') : $('<div id="pie-tooltip"></div>');
|
|
|
|
|
tooltip.text(contents).css({
|
|
|
|
|
position: 'absolute',
|
2013-02-06 14:14:17 -07:00
|
|
|
top : y + 10,
|
|
|
|
|
left : x + 10,
|
2013-02-05 16:24:46 -07:00
|
|
|
color : "#FFF",
|
|
|
|
|
border : '1px solid #FFF',
|
|
|
|
|
padding : '2px',
|
|
|
|
|
'font-size': '8pt',
|
|
|
|
|
'background-color': '#000',
|
|
|
|
|
}).appendTo("body");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elem.bind("plothover", function (event, pos, item) {
|
|
|
|
|
if (item) {
|
2013-02-05 19:20:32 -07:00
|
|
|
var percent = parseFloat(item.series.percent).toFixed(1) + "%";
|
2013-02-18 09:18:36 -07:00
|
|
|
piett(pos.pageX, pos.pageY, percent + " " + (item.series.label||""));
|
2013-02-05 16:24:46 -07:00
|
|
|
} else {
|
|
|
|
|
$("#pie-tooltip").remove();
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-01-30 21:39:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
})
|