module.js 6.5 KB

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