module.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. angular.module('kibana.histogram', [])
  2. .controller('histogram', function($scope, $rootScope) {
  3. // Set and populate defaults
  4. var _d = {
  5. query : "*",
  6. interval: secondsToHms(calculate_interval($scope.from,$scope.to,40,0)/1000),
  7. color : "#27508C",
  8. show : ['bars'],
  9. fill : false,
  10. }
  11. _.each(_d, function(v, k) {
  12. $scope.panel[k] = _.isUndefined($scope.panel[k])
  13. ? _d[k] : $scope.panel[k];
  14. });
  15. $scope.get_data = function() {
  16. // Make sure we have everything for the request to complete
  17. if(_.isUndefined($scope.panel.time))
  18. return
  19. var request = $scope.ejs.Request().indices($scope.index);
  20. // Build the question part of the query
  21. var queries = [];
  22. _.each($scope.panel.query, function(v) {
  23. queries.push($scope.ejs.FilteredQuery(
  24. ejs.QueryStringQuery(v.query || '*'),
  25. ejs.RangeFilter(config.timefield)
  26. .from($scope.panel.time.from)
  27. .to($scope.panel.time.to)
  28. .cache(false))
  29. )
  30. });
  31. // Build the facet part
  32. _.each(queries, function(v) {
  33. request = request
  34. .facet($scope.ejs.DateHistogramFacet(_.indexOf(queries,v))
  35. .field(config.timefield)
  36. .interval($scope.panel.interval)
  37. .facetFilter($scope.ejs.QueryFilter(v))
  38. ).size(0)
  39. })
  40. // Then run it
  41. var results = request.doSearch();
  42. // Populate scope when we have results
  43. results.then(function(results) {
  44. $scope.hits = results.hits.total;
  45. // Null values at each end of the time range make sure we see entire range
  46. $scope.data = [];
  47. _.each(results.facets, function(v, k) {
  48. var series = {};
  49. var data = [[$scope.panel.time.from.getTime(), null]];
  50. _.each(v.entries, function(v, k) {
  51. data.push([v['time'],v['count']])
  52. });
  53. data.push([$scope.panel.time.to.getTime(), null])
  54. series.data = {
  55. label: $scope.panel.query[k].label,
  56. data: data,
  57. };
  58. if (!(_.isUndefined($scope.panel.query[k].color)))
  59. series.data.color = $scope.panel.query[k].color;
  60. $scope.data.push(series.data)
  61. });
  62. });
  63. }
  64. if (!(_.isUndefined($scope.panel.group))) {
  65. $scope.$on($scope.panel.group+"-query", function(event, query) {
  66. $scope.panel.query[0].query = query;
  67. $scope.get_data();
  68. });
  69. $scope.$on($scope.panel.group+"-time", function(event, time) {
  70. $scope.panel.time = time;
  71. $scope.panel.interval = secondsToHms(
  72. calculate_interval(time.from,time.to,50,0)/1000),
  73. $scope.get_data();
  74. });
  75. }
  76. // Now that we're all setup, request the time from our group
  77. $rootScope.$broadcast($scope.panel.group+"-get_time")
  78. })
  79. .directive('histogram', function() {
  80. return {
  81. restrict: 'A',
  82. link: function(scope, elem, attrs, ctrl) {
  83. // If the data or row state changes, re-render
  84. scope.$watch(function () {
  85. return angular.toJson([scope.data, scope.row])
  86. }, function() {
  87. if(!(_.isUndefined(scope.data)))
  88. render_panel(scope,elem,attrs);
  89. });
  90. // Re-render if the window is resized
  91. angular.element(window).bind('resize', function(){
  92. render_panel(scope,elem,attrs);
  93. });
  94. // Function for rendering panel
  95. function render_panel(scope,elem,attrs) {
  96. // Determine format
  97. var show = _.isUndefined(scope.panel.show) ? {
  98. bars: true, lines: false, points: false
  99. } : {
  100. lines: _.indexOf(scope.panel.show,'lines') < 0 ? false : true,
  101. bars: _.indexOf(scope.panel.show,'bars') < 0 ? false : true,
  102. points: _.indexOf(scope.panel.show,'points') < 0 ? false : true,
  103. stack: _.indexOf(scope.panel.show,'stack') < 0 ? null : true,
  104. }
  105. // Set barwidth based on specified interval
  106. var barwidth = interval_to_seconds(scope.panel.interval)*1000
  107. var scripts = $LAB.script("common/lib/panels/jquery.flot.js")
  108. .script("common/lib/panels/jquery.flot.time.js")
  109. .script("common/lib/panels/jquery.flot.stack.js")
  110. // Populate element. Note that jvectormap appends, does not replace.
  111. scripts.wait(function(){
  112. // Populate element
  113. $.plot(elem, scope.data, {
  114. legend: {
  115. position: "nw",
  116. labelFormatter: function(label, series) {
  117. return '<span class="legend">' + label + ' / ' +
  118. scope.panel.interval + '</span>';
  119. }
  120. },
  121. series: {
  122. stack: show.stack,
  123. lines: { show: show.lines, fill: scope.panel.fill },
  124. bars: { show: show.bars, fill: 1, barWidth: barwidth/1.8 },
  125. points: { show: show.points },
  126. shadowSize: 1
  127. },
  128. yaxis: { min: 0, color: "#000" },
  129. xaxis: {
  130. mode: "time",
  131. timeformat: "%H:%M:%S<br>%m-%d",
  132. label: "Datetime",
  133. color: "#000",
  134. },
  135. grid: {
  136. backgroundColor: '#fff',
  137. borderWidth: 0,
  138. borderColor: '#eee',
  139. color: "#eee",
  140. hoverable: true,
  141. }
  142. })
  143. })
  144. }
  145. }
  146. };
  147. })