module.js 4.8 KB

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