module.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*
  4. ## Hits
  5. A variety of representations of the hits a query matches
  6. ### Parameters
  7. * query :: An array of queries. No labels here, just an array of strings. Maybe
  8. there should be labels. Probably.
  9. * style :: A hash of css styles
  10. * arrangement :: How should I arrange the query results? 'horizontal' or 'vertical'
  11. * chart :: Show a chart? 'none', 'bar', 'pie'
  12. * donut :: Only applies to 'pie' charts. Punches a hole in the chart for some reason
  13. * tilt :: Only 'pie' charts. Janky 3D effect. Looks terrible 90% of the time.
  14. * lables :: Only 'pie' charts. Labels on the pie?
  15. */
  16. 'use strict';
  17. angular.module('kibana.hits', [])
  18. .controller('hits', function($scope, querySrv, dashboard, filterSrv) {
  19. // Set and populate defaults
  20. var _d = {
  21. status : "Beta",
  22. query : ["*"],
  23. group : "default",
  24. style : { "font-size": '10pt'},
  25. arrangement : 'horizontal',
  26. chart : 'bar',
  27. counter_pos : 'above',
  28. donut : false,
  29. tilt : false,
  30. labels : true
  31. };
  32. _.defaults($scope.panel,_d);
  33. $scope.init = function () {
  34. $scope.hits = 0;
  35. $scope.$on('refresh',function(){
  36. $scope.get_data();
  37. });
  38. $scope.get_data();
  39. };
  40. $scope.get_data = function(segment,query_id) {
  41. delete $scope.panel.error;
  42. $scope.panel.loading = true;
  43. // Make sure we have everything for the request to complete
  44. if(dashboard.indices.length === 0) {
  45. return;
  46. }
  47. var _segment = _.isUndefined(segment) ? 0 : segment;
  48. var request = $scope.ejs.Request().indices(dashboard.indices[_segment]);
  49. // Build the question part of the query
  50. _.each(querySrv.ids, function(id) {
  51. var _q = $scope.ejs.FilteredQuery(
  52. querySrv.getEjsObj(id),
  53. filterSrv.getBoolFilter(filterSrv.ids));
  54. request = request
  55. .facet($scope.ejs.QueryFacet(id)
  56. .query(_q)
  57. ).size(0);
  58. });
  59. // TODO: Spy for hits panel
  60. //$scope.populate_modal(request);
  61. // Then run it
  62. var results = request.doSearch();
  63. // Populate scope when we have results
  64. results.then(function(results) {
  65. $scope.panel.loading = false;
  66. if(_segment === 0) {
  67. $scope.hits = 0;
  68. $scope.data = [];
  69. query_id = $scope.query_id = new Date().getTime();
  70. }
  71. // Check for error and abort if found
  72. if(!(_.isUndefined(results.error))) {
  73. $scope.panel.error = $scope.parse_error(results.error);
  74. return;
  75. }
  76. // Convert facet ids to numbers
  77. var facetIds = _.map(_.keys(results.facets),function(k){return parseInt(k, 10);});
  78. // Make sure we're still on the same query/queries
  79. if($scope.query_id === query_id &&
  80. _.intersection(facetIds,querySrv.ids).length === querySrv.ids.length
  81. ) {
  82. var i = 0;
  83. _.each(querySrv.ids, function(id) {
  84. var v = results.facets[id];
  85. var hits = _.isUndefined($scope.data[i]) || _segment === 0 ?
  86. v.count : $scope.data[i].hits+v.count;
  87. $scope.hits += v.count;
  88. // Create series
  89. $scope.data[i] = {
  90. info: querySrv.list[id],
  91. id: id,
  92. hits: hits,
  93. data: [[i,hits]]
  94. };
  95. i++;
  96. });
  97. $scope.$emit('render');
  98. if(_segment < dashboard.indices.length-1) {
  99. $scope.get_data(_segment+1,query_id);
  100. }
  101. }
  102. });
  103. };
  104. $scope.set_refresh = function (state) {
  105. $scope.refresh = state;
  106. };
  107. $scope.close_edit = function() {
  108. if($scope.refresh) {
  109. $scope.get_data();
  110. }
  111. $scope.refresh = false;
  112. $scope.$emit('render');
  113. };
  114. function set_time(time) {
  115. $scope.time = time;
  116. $scope.get_data();
  117. }
  118. }).directive('hitsChart', function(querySrv) {
  119. return {
  120. restrict: 'A',
  121. link: function(scope, elem, attrs, ctrl) {
  122. // Receive render events
  123. scope.$on('render',function(){
  124. render_panel();
  125. });
  126. // Re-render if the window is resized
  127. angular.element(window).bind('resize', function(){
  128. render_panel();
  129. });
  130. // Function for rendering panel
  131. function render_panel() {
  132. // IE doesn't work without this
  133. elem.css({height:scope.panel.height||scope.row.height});
  134. try {
  135. _.each(scope.data,function(series) {
  136. series.label = series.info.alias;
  137. series.color = series.info.color;
  138. });
  139. } catch(e) {return;}
  140. var scripts = $LAB.script("common/lib/panels/jquery.flot.js").wait()
  141. .script("common/lib/panels/jquery.flot.pie.js");
  142. // Populate element.
  143. scripts.wait(function(){
  144. // Populate element
  145. try {
  146. // Add plot to scope so we can build out own legend
  147. if(scope.panel.chart === 'bar') {
  148. scope.plot = $.plot(elem, scope.data, {
  149. legend: { show: false },
  150. series: {
  151. lines: { show: false, },
  152. bars: { show: true, fill: 1, barWidth: 0.8, horizontal: false },
  153. shadowSize: 1
  154. },
  155. yaxis: { show: true, min: 0, color: "#c8c8c8" },
  156. xaxis: { show: false },
  157. grid: {
  158. borderWidth: 0,
  159. borderColor: '#eee',
  160. color: "#eee",
  161. hoverable: true,
  162. },
  163. colors: querySrv.colors
  164. });
  165. }
  166. if(scope.panel.chart === 'pie') {
  167. scope.plot = $.plot(elem, scope.data, {
  168. legend: { show: false },
  169. series: {
  170. pie: {
  171. innerRadius: scope.panel.donut ? 0.4 : 0,
  172. tilt: scope.panel.tilt ? 0.45 : 1,
  173. radius: 1,
  174. show: true,
  175. combine: {
  176. color: '#999',
  177. label: 'The Rest'
  178. },
  179. stroke: {
  180. width: 0
  181. },
  182. label: {
  183. show: scope.panel.labels,
  184. radius: 2/3,
  185. formatter: function(label, series){
  186. return '<div ng-click="build_search(panel.query.field,\''+label+'\') "style="font-size:8pt;text-align:center;padding:2px;color:white;">'+
  187. label+'<br/>'+Math.round(series.percent)+'%</div>';
  188. },
  189. threshold: 0.1
  190. }
  191. }
  192. },
  193. //grid: { hoverable: true, clickable: true },
  194. grid: { hoverable: true, clickable: true },
  195. colors: querySrv.colors
  196. });
  197. }
  198. // Work around for missing legend at initialization
  199. if(!scope.$$phase) {
  200. scope.$apply();
  201. }
  202. } catch(e) {
  203. elem.text(e);
  204. }
  205. });
  206. }
  207. function tt(x, y, contents) {
  208. var tooltip = $('#pie-tooltip').length ?
  209. $('#pie-tooltip') : $('<div id="pie-tooltip"></div>');
  210. //var tooltip = $('#pie-tooltip')
  211. tooltip.html(contents).css({
  212. position: 'absolute',
  213. top : y + 5,
  214. left : x + 5,
  215. color : "#c8c8c8",
  216. padding : '10px',
  217. 'font-size': '11pt',
  218. 'font-weight' : 200,
  219. 'background-color': '#1f1f1f',
  220. 'border-radius': '5px',
  221. }).appendTo("body");
  222. }
  223. elem.bind("plothover", function (event, pos, item) {
  224. if (item) {
  225. var value = scope.panel.chart === 'bar' ?
  226. item.datapoint[1] : item.datapoint[1][0][1];
  227. tt(pos.pageX, pos.pageY,
  228. "<div style='vertical-align:middle;border-radius:10px;display:inline-block;background:"+
  229. item.series.color+";height:20px;width:20px'></div> "+value.toFixed(0));
  230. } else {
  231. $("#pie-tooltip").remove();
  232. }
  233. });
  234. }
  235. };
  236. });