module.js 7.1 KB

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