module.js 6.4 KB

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