module.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. angular.module('kibana.histogram', [])
  2. .controller('histogram', function($scope, eventBus) {
  3. // Set and populate defaults
  4. var _d = {
  5. query : [ {query: "*", label:"Query"} ],
  6. interval : secondsToHms(calculate_interval($scope.from,$scope.to,40,0)/1000),
  7. show : ['bars','y-axis','x-axis','legend'],
  8. fill : 3,
  9. timezone : 'browser', // browser, utc or a standard timezone
  10. spyable : true,
  11. zoomlinks : true,
  12. group : "default",
  13. }
  14. _.defaults($scope.panel,_d)
  15. $scope.init = function() {
  16. eventBus.register($scope,'time', function(event,time){$scope.set_time(time)});
  17. eventBus.register($scope,'query', function(event, query) {
  18. if(_.isArray(query)) {
  19. $scope.panel.query = _.map(query,function(q) {
  20. return {query: q, label: q};
  21. })
  22. } else {
  23. $scope.panel.query[0] = {query: query, label: query}
  24. }
  25. $scope.get_data();
  26. });
  27. // Now that we're all setup, request the time from our group if we don't
  28. // have it yet
  29. if(_.isUndefined($scope.time))
  30. eventBus.broadcast($scope.$id,$scope.panel.group,'get_time')
  31. }
  32. $scope.remove_query = function(q) {
  33. $scope.panel.query = _.without($scope.panel.query,q);
  34. $scope.get_data();
  35. }
  36. $scope.add_query = function(label,query) {
  37. if(!(_.isArray($scope.panel.query)))
  38. $scope.panel.query = new Array();
  39. $scope.panel.query.unshift({
  40. query: query,
  41. label: label,
  42. });
  43. $scope.get_data();
  44. }
  45. $scope.get_data = function(segment,query_id) {
  46. // Make sure we have everything for the request to complete
  47. if(_.isUndefined($scope.panel.index) || _.isUndefined($scope.time))
  48. return
  49. var _segment = _.isUndefined(segment) ? 0 : segment
  50. $scope.panel.loading = true;
  51. var request = $scope.ejs.Request().indices($scope.panel.index[_segment]);
  52. // Build the question part of the query
  53. var queries = [];
  54. _.each($scope.panel.query, function(v) {
  55. queries.push($scope.ejs.FilteredQuery(
  56. ejs.QueryStringQuery(v.query || '*'),
  57. ejs.RangeFilter($scope.time.field)
  58. .from($scope.time.from)
  59. .to($scope.time.to))
  60. )
  61. });
  62. // Build the facet part
  63. _.each(queries, function(v) {
  64. request = request
  65. .facet($scope.ejs.DateHistogramFacet("chart"+_.indexOf(queries,v))
  66. .field($scope.time.field)
  67. .interval($scope.panel.interval)
  68. .facetFilter($scope.ejs.QueryFilter(v))
  69. ).size(0)
  70. })
  71. $scope.populate_modal(request);
  72. // Then run it
  73. var results = request.doSearch();
  74. // Populate scope when we have results
  75. results.then(function(results) {
  76. $scope.panel.loading = false;
  77. if(_segment == 0) {
  78. $scope.hits = 0;
  79. $scope.data = [];
  80. query_id = $scope.query_id = new Date().getTime();
  81. }
  82. if($scope.query_id === query_id) {
  83. var i = 0;
  84. _.each(results.facets, function(v, k) {
  85. // If this isn't a date histogram it must be a QueryFacet, get the
  86. // count and return
  87. if(v._type !== 'date_histogram') {
  88. //$scope.hits += v.count;
  89. return
  90. }
  91. // Null values at each end of the time range ensure we see entire range
  92. if(_.isUndefined($scope.data[i]) || _segment == 0) {
  93. var data = [[$scope.time.from.getTime(), null],[$scope.time.to.getTime(), null]];
  94. var hits = 0;
  95. } else {
  96. var data = $scope.data[i].data
  97. var hits = $scope.data[i].hits
  98. }
  99. // Assemble segments
  100. var segment_data = [];
  101. _.each(v.entries, function(v, k) {
  102. segment_data.push([v['time'],v['count']])
  103. hits += v['count'];
  104. $scope.hits += v['count'];
  105. });
  106. data.splice.apply(data,[1,0].concat(segment_data))
  107. // Create the flot series
  108. var series = {
  109. data: {
  110. label: $scope.panel.query[i].label || "query"+(parseInt(i)+1),
  111. data: data,
  112. hits: hits
  113. },
  114. };
  115. if (!(_.isUndefined($scope.panel.query[i].color)))
  116. series.data.color = $scope.panel.query[i].color;
  117. $scope.data[i] = series.data
  118. i++;
  119. });
  120. eventBus.broadcast($scope.$id,$scope.panel.group,'hits',$scope.hits)
  121. $scope.$emit('render')
  122. if(_segment < $scope.panel.index.length-1) {
  123. $scope.get_data(_segment+1,query_id)
  124. }
  125. }
  126. });
  127. }
  128. // function $scope.zoom
  129. // factor :: Zoom factor, so 0.5 = cuts timespan in half, 2 doubles timespan
  130. $scope.zoom = function(factor) {
  131. eventBus.broadcast($scope.$id,$scope.panel.group,'zoom',factor)
  132. }
  133. // I really don't like this function, too much dom manip. Break out into directive?
  134. $scope.populate_modal = function(request) {
  135. $scope.modal = {
  136. title: "Inspector",
  137. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  138. 'curl -XGET '+config.elasticsearch+'/'+$scope.panel.index+"/_search?pretty -d'\n"+
  139. angular.toJson(JSON.parse(request.toString()),true)+
  140. "'</pre>",
  141. }
  142. }
  143. $scope.set_time = function(time) {
  144. $scope.time = time;
  145. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  146. $scope.panel.interval = secondsToHms(
  147. calculate_interval(time.from,time.to,50,0)/1000);
  148. $scope.get_data();
  149. }
  150. })
  151. .directive('histogram', function(eventBus) {
  152. return {
  153. restrict: 'A',
  154. link: function(scope, elem, attrs, ctrl) {
  155. var height = scope.panel.height || scope.row.height;
  156. elem.html('<center><img src="common/img/load_big.gif"></center>')
  157. // Receive render events
  158. scope.$on('render',function(){
  159. render_panel();
  160. });
  161. // Re-render if the window is resized
  162. angular.element(window).bind('resize', function(){
  163. render_panel();
  164. });
  165. // Function for rendering panel
  166. function render_panel() {
  167. // Determine format
  168. var show = _.isUndefined(scope.panel.show) ? {
  169. bars: true, lines: false, points: false
  170. } : {
  171. lines: _.indexOf(scope.panel.show,'lines') < 0 ? false : true,
  172. bars: _.indexOf(scope.panel.show,'bars') < 0 ? false : true,
  173. points: _.indexOf(scope.panel.show,'points') < 0 ? false : true,
  174. stack: _.indexOf(scope.panel.show,'stack') < 0 ? null : true,
  175. legend: _.indexOf(scope.panel.show,'legend') < 0 ? false : true,
  176. 'x-axis': _.indexOf(scope.panel.show,'x-axis') < 0 ? false : true,
  177. 'y-axis': _.indexOf(scope.panel.show,'y-axis') < 0 ? false : true,
  178. }
  179. // Set barwidth based on specified interval
  180. var barwidth = interval_to_seconds(scope.panel.interval)*1000
  181. var scripts = $LAB.script("common/lib/panels/jquery.flot.js")
  182. .script("common/lib/panels/jquery.flot.time.js")
  183. .script("common/lib/panels/jquery.flot.stack.js")
  184. .script("common/lib/panels/jquery.flot.selection.js")
  185. .script("common/lib/panels/timezone.js")
  186. // Populate element. Note that jvectormap appends, does not replace.
  187. scripts.wait(function(){
  188. // Populate element
  189. try {
  190. var plot = $.plot(elem, scope.data, {
  191. legend: {
  192. show: false,
  193. },
  194. series: {
  195. stack: show.stack,
  196. lines: { show: show.lines, fill: scope.panel.fill/10 },
  197. bars: { show: show.bars, fill: 1, barWidth: barwidth/1.8 },
  198. points: { show: show.points, fill: 1, fillColor: false},
  199. shadowSize: 1
  200. },
  201. yaxis: { show: show['y-axis'], min: 0, color: "#000" },
  202. xaxis: {
  203. timezone: scope.panel.timezone,
  204. show: show['x-axis'],
  205. mode: "time",
  206. timeformat: time_format(scope.panel.interval),
  207. label: "Datetime",
  208. color: "#000",
  209. },
  210. selection: {
  211. mode: "x"
  212. },
  213. grid: {
  214. backgroundColor: '#fff',
  215. borderWidth: 0,
  216. borderColor: '#eee',
  217. color: "#eee",
  218. hoverable: true,
  219. },
  220. colors: ['#EB6841','#00A0B0','#6A4A3C','#EDC951','#CC333F']
  221. })
  222. scope.legend = [];
  223. _.each(plot.getData(),function(series) {
  224. scope.legend.push(_.pick(series,'label','color','hits'))
  225. })
  226. // Work around for missing legend at initialization
  227. if(!scope.$$phase)
  228. scope.$apply()
  229. } catch(e) {
  230. elem.text(e)
  231. }
  232. })
  233. }
  234. function time_format(interval) {
  235. var _int = interval_to_seconds(interval)
  236. if(_int >= 2628000)
  237. return "%m/%y"
  238. if(_int >= 86400)
  239. return "%m/%d/%y"
  240. if(_int >= 60)
  241. return "%H:%M<br>%m/%d"
  242. else
  243. return "%H:%M:%S"
  244. }
  245. function tt(x, y, contents) {
  246. var tooltip = $('#pie-tooltip').length ?
  247. $('#pie-tooltip') : $('<div id="pie-tooltip"></div>');
  248. //var tooltip = $('#pie-tooltip')
  249. tooltip.text(contents).css({
  250. position: 'absolute',
  251. top : y + 5,
  252. left : x + 5,
  253. color : "#FFF",
  254. border : '1px solid #FFF',
  255. padding : '2px',
  256. 'font-size': '8pt',
  257. 'background-color': '#000',
  258. }).appendTo("body");
  259. }
  260. elem.bind("plothover", function (event, pos, item) {
  261. if (item) {
  262. var percent = parseFloat(item.series.percent).toFixed(1) + "%";
  263. tt(pos.pageX, pos.pageY,
  264. item.datapoint[1].toFixed(1) + " @ " +
  265. new Date(item.datapoint[0]).format('mm/dd HH:MM:ss'));
  266. } else {
  267. $("#pie-tooltip").remove();
  268. }
  269. });
  270. elem.bind("plotselected", function (event, ranges) {
  271. scope.time.from = new Date(ranges.xaxis.from);
  272. scope.time.to = new Date(ranges.xaxis.to)
  273. eventBus.broadcast(scope.$id,scope.panel.group,'set_time',scope.time)
  274. });
  275. }
  276. };
  277. })