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 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. $scope.panel.query.unshift({
  30. query: query,
  31. label: label,
  32. });
  33. $scope.get_data();
  34. }
  35. $scope.get_data = function() {
  36. // Make sure we have everything for the request to complete
  37. if(_.isUndefined($scope.panel.index) || _.isUndefined($scope.panel.time))
  38. return
  39. var request = $scope.ejs.Request().indices($scope.panel.index);
  40. // Build the question part of the query
  41. var queries = [];
  42. _.each($scope.panel.query, function(v) {
  43. queries.push($scope.ejs.FilteredQuery(
  44. ejs.QueryStringQuery(v.query || '*'),
  45. ejs.RangeFilter($scope.panel.time.field)
  46. .from($scope.panel.time.from)
  47. .to($scope.panel.time.to))
  48. )
  49. });
  50. // Build the facet part
  51. _.each(queries, function(v) {
  52. request = request
  53. .facet($scope.ejs.DateHistogramFacet(_.indexOf(queries,v))
  54. .field($scope.panel.time.field)
  55. .interval($scope.panel.interval)
  56. .facetFilter($scope.ejs.QueryFilter(v))
  57. ).size(0)
  58. })
  59. // Then run it
  60. var results = request.doSearch();
  61. // Populate scope when we have results
  62. results.then(function(results) {
  63. $scope.hits = results.hits.total;
  64. $scope.data = [];
  65. _.each(results.facets, function(v, k) {
  66. // Null values at each end of the time range ensure we see entire range
  67. var data = [[$scope.panel.time.from.getTime(), null]];
  68. _.each(v.entries, function(v, k) {
  69. data.push([v['time'],v['count']])
  70. });
  71. data.push([$scope.panel.time.to.getTime(), null])
  72. var series = { data: {
  73. label: $scope.panel.query[k].label || k,
  74. data: data,
  75. }};
  76. if (!(_.isUndefined($scope.panel.query[k].color)))
  77. series.data.color = $scope.panel.query[k].color;
  78. $scope.data.push(series.data)
  79. });
  80. $scope.$emit('render')
  81. });
  82. }
  83. function set_time(time) {
  84. $scope.panel.time = time;
  85. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  86. $scope.panel.interval = secondsToHms(
  87. calculate_interval(time.from,time.to,50,0)/1000),
  88. $scope.get_data();
  89. }
  90. // Ready, init
  91. $scope.init();
  92. })
  93. .directive('histogram', function() {
  94. return {
  95. restrict: 'A',
  96. link: function(scope, elem, attrs, ctrl) {
  97. // If the data or row state changes, re-render
  98. scope.$on('render',function(){
  99. render_panel();
  100. });
  101. // Re-render if the window is resized
  102. angular.element(window).bind('resize', function(){
  103. render_panel();
  104. });
  105. // Function for rendering panel
  106. function render_panel() {
  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. })