module.js 6.3 KB

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