module.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. angular.module('kibana.histogram', [])
  2. .controller('histogram', function($scope, eventBus) {
  3. var _id = _.uniqueId();
  4. // Set and populate defaults
  5. var _d = {
  6. query : [ {query: "*", label:"Query"} ],
  7. interval: secondsToHms(calculate_interval($scope.from,$scope.to,40,0)/1000),
  8. show : ['bars'],
  9. fill : false,
  10. group : "default",
  11. }
  12. _.defaults($scope.panel,_d)
  13. $scope.init = function() {
  14. eventBus.register($scope,'time', function(event,time){set_time(time)});
  15. eventBus.register($scope,'query', function(event, query) {
  16. $scope.panel.query[0].query = query;
  17. $scope.get_data();
  18. });
  19. // Now that we're all setup, request the time from our group if we don't
  20. // have it yet
  21. if(_.isUndefined($scope.time))
  22. eventBus.broadcast($scope.$id,$scope.panel.group,'get_time')
  23. }
  24. $scope.remove_query = function(q) {
  25. $scope.panel.query = _.without($scope.panel.query,q);
  26. $scope.get_data();
  27. }
  28. $scope.add_query = function(label,query) {
  29. if(!(_.isArray($scope.panel.query)))
  30. $scope.panel.query = new Array();
  31. $scope.panel.query.unshift({
  32. query: query,
  33. label: label,
  34. });
  35. $scope.get_data();
  36. }
  37. $scope.get_data = function() {
  38. // Make sure we have everything for the request to complete
  39. if(_.isUndefined($scope.panel.index) || _.isUndefined($scope.panel.time))
  40. return
  41. var request = $scope.ejs.Request().indices($scope.panel.index);
  42. // Build the question part of the query
  43. var queries = [];
  44. _.each($scope.panel.query, function(v) {
  45. queries.push($scope.ejs.FilteredQuery(
  46. ejs.QueryStringQuery(v.query || '*'),
  47. ejs.RangeFilter($scope.panel.time.field)
  48. .from($scope.panel.time.from)
  49. .to($scope.panel.time.to))
  50. )
  51. });
  52. // Build the facet part
  53. _.each(queries, function(v) {
  54. request = request
  55. .facet($scope.ejs.DateHistogramFacet(_.indexOf(queries,v))
  56. .field($scope.panel.time.field)
  57. .interval($scope.panel.interval)
  58. .facetFilter($scope.ejs.QueryFilter(v))
  59. ).size(0)
  60. })
  61. // Then run it
  62. var results = request.doSearch();
  63. // Populate scope when we have results
  64. results.then(function(results) {
  65. $scope.hits = results.hits.total;
  66. $scope.data = [];
  67. _.each(results.facets, function(v, k) {
  68. // Null values at each end of the time range ensure we see entire range
  69. var data = [[$scope.panel.time.from.getTime(), null]];
  70. _.each(v.entries, function(v, k) {
  71. data.push([v['time'],v['count']])
  72. });
  73. data.push([$scope.panel.time.to.getTime(), null])
  74. var series = { data: {
  75. label: $scope.panel.query[k].label || k,
  76. data: data,
  77. }};
  78. if (!(_.isUndefined($scope.panel.query[k].color)))
  79. series.data.color = $scope.panel.query[k].color;
  80. $scope.data.push(series.data)
  81. });
  82. $scope.$emit('render')
  83. });
  84. }
  85. function set_time(time) {
  86. $scope.panel.time = time;
  87. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  88. $scope.panel.interval = secondsToHms(
  89. calculate_interval(time.from,time.to,50,0)/1000),
  90. $scope.get_data();
  91. }
  92. // Ready, init
  93. $scope.init();
  94. })
  95. .directive('histogram', function() {
  96. return {
  97. restrict: 'A',
  98. link: function(scope, elem, attrs, ctrl) {
  99. // Receive render events
  100. scope.$on('render',function(){
  101. render_panel();
  102. });
  103. // Re-render if the window is resized
  104. angular.element(window).bind('resize', function(){
  105. render_panel();
  106. });
  107. // Function for rendering panel
  108. function render_panel() {
  109. // Determine format
  110. var show = _.isUndefined(scope.panel.show) ? {
  111. bars: true, lines: false, points: false
  112. } : {
  113. lines: _.indexOf(scope.panel.show,'lines') < 0 ? false : true,
  114. bars: _.indexOf(scope.panel.show,'bars') < 0 ? false : true,
  115. points: _.indexOf(scope.panel.show,'points') < 0 ? false : true,
  116. stack: _.indexOf(scope.panel.show,'stack') < 0 ? null : true,
  117. }
  118. // Set barwidth based on specified interval
  119. var barwidth = interval_to_seconds(scope.panel.interval)*1000
  120. var scripts = $LAB.script("common/lib/panels/jquery.flot.js")
  121. .script("common/lib/panels/jquery.flot.time.js")
  122. .script("common/lib/panels/jquery.flot.stack.js")
  123. // Populate element. Note that jvectormap appends, does not replace.
  124. scripts.wait(function(){
  125. // Populate element
  126. $.plot(elem, scope.data, {
  127. legend: {
  128. position: "nw",
  129. labelFormatter: function(label, series) {
  130. return '<span class="legend">' + label + ' / ' +
  131. scope.panel.interval + '</span>';
  132. }
  133. },
  134. series: {
  135. stack: show.stack,
  136. lines: { show: show.lines, fill: scope.panel.fill },
  137. bars: { show: show.bars, fill: 1, barWidth: barwidth/1.8 },
  138. points: { show: show.points },
  139. shadowSize: 1
  140. },
  141. yaxis: { min: 0, color: "#000" },
  142. xaxis: {
  143. mode: "time",
  144. timeformat: "%H:%M:%S<br>%m-%d",
  145. label: "Datetime",
  146. color: "#000",
  147. },
  148. grid: {
  149. backgroundColor: '#fff',
  150. borderWidth: 0,
  151. borderColor: '#eee',
  152. color: "#eee",
  153. hoverable: true,
  154. }
  155. })
  156. })
  157. function tt(x, y, contents) {
  158. var tooltip = $('#pie-tooltip').length ?
  159. $('#pie-tooltip') : $('<div id="pie-tooltip"></div>');
  160. //var tooltip = $('#pie-tooltip')
  161. tooltip.text(contents).css({
  162. position: 'absolute',
  163. top : y + 5,
  164. left : x + 5,
  165. color : "#FFF",
  166. border : '1px solid #FFF',
  167. padding : '2px',
  168. 'font-size': '8pt',
  169. 'background-color': '#000',
  170. }).appendTo("body");
  171. }
  172. elem.bind("plothover", function (event, pos, item) {
  173. if (item) {
  174. var percent = parseFloat(item.series.percent).toFixed(1) + "%";
  175. tt(pos.pageX, pos.pageY,
  176. item.datapoint[1].toFixed(1) + " @ " +
  177. new Date(item.datapoint[0]).format(config.timeformat));
  178. } else {
  179. $("#pie-tooltip").remove();
  180. }
  181. });
  182. }
  183. }
  184. };
  185. })