module.js 7.0 KB

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