module.js 7.2 KB

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