/* ## Pie ### Parameters * query :: An object with 2 possible parameters depends on the mode: ** field: Fields to run a terms facet on. Only does anything in terms mode ** goal: How many to shoot for, only does anything in goal mode * exclude :: In terms mode, ignore these terms * donut :: Drill a big hole in the pie * tilt :: A janky 3D representation of the pie. Looks terrible 90% of the time. * legend :: Show the legend? * labels :: Label the slices of the pie? * mode :: 'terms' or 'goal' * default_field :: LOL wat? A dumb fail over field if for some reason the query object doesn't have a field * spyable :: Show the 'eye' icon that displays the last ES query for this panel */ define([ 'angular', 'app', 'underscore', 'jquery', 'kbn', 'config' ], function (angular, app, _, $, kbn) { 'use strict'; var module = angular.module('kibana.panels.pie', []); app.useModule(module); module.controller('pie', function($scope, $rootScope, querySrv, dashboard, filterSrv) { $scope.panelMeta = { editorTabs : [ {title:'Queries', src:'app/partials/querySelect.html'} ], modals : [ { description: "Inspect", icon: "icon-info-sign", partial: "app/partials/inspector.html", show: $scope.panel.spyable } ], status : "Deprecated", description : "Uses an Elasticsearch terms facet to create a pie chart. You should really only"+ " point this at not_analyzed fields for that reason. This panel is going away soon, it has"+ " been replaced by the terms panel. Please use that one instead." }; // Set and populate defaults var _d = { query : { field:"_type", goal: 100}, queries : { mode : 'all', ids : [] }, size : 10, exclude : [], donut : false, tilt : false, legend : "above", labels : true, mode : "terms", default_field : 'DEFAULT', spyable : true, }; _.defaults($scope.panel,_d); $scope.init = function() { $scope.$on('refresh',function(){$scope.get_data();}); $scope.get_data(); }; $scope.set_mode = function(mode) { switch(mode) { case 'terms': $scope.panel.query = {field:"_all"}; break; case 'goal': $scope.panel.query = {goal:100}; break; } }; $scope.set_refresh = function (state) { $scope.refresh = state; }; $scope.close_edit = function() { if($scope.refresh) { $scope.get_data(); } $scope.refresh = false; $scope.$emit('render'); }; $scope.get_data = function() { // Make sure we have everything for the request to complete if(dashboard.indices.length === 0) { return; } $scope.panelMeta.loading = true; var request = $scope.ejs.Request().indices(dashboard.indices); $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries); var queries = querySrv.getQueryObjs($scope.panel.queries.ids); // This could probably be changed to a BoolFilter var boolQuery = $scope.ejs.BoolQuery(); _.each(queries,function(q) { boolQuery = boolQuery.should(querySrv.toEjsObj(q)); }); var results; // Terms mode if ($scope.panel.mode === "terms") { request = request .facet($scope.ejs.TermsFacet('pie') .field($scope.panel.query.field || $scope.panel.default_field) .size($scope.panel.size) .exclude($scope.panel.exclude) .facetFilter($scope.ejs.QueryFilter( $scope.ejs.FilteredQuery( boolQuery, filterSrv.getBoolFilter(filterSrv.ids) )))).size(0); $scope.inspector = angular.toJson(JSON.parse(request.toString()),true); results = request.doSearch(); // Populate scope when we have results results.then(function(results) { $scope.panelMeta.loading = false; $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(); $scope.data.push(slice); k = k + 1; }); $scope.$emit('render'); }); // Goal mode } else { request = request .query(boolQuery) .filter(filterSrv.getBoolFilter(filterSrv.ids)) .size(0); $scope.inspector = angular.toJson(JSON.parse(request.toString()),true); results = request.doSearch(); results.then(function(results) { $scope.panelMeta.loading = false; var complete = results.hits.total; var remaining = $scope.panel.query.goal - complete; $scope.data = [ { label : 'Complete', data : complete, color: '#BF6730' }, { data : remaining, color: '#e2d0c4' } ]; $scope.$emit('render'); }); } }; }); module.directive('pie', function(querySrv, filterSrv) { return { restrict: 'A', link: function(scope, elem) { elem.html('
