module.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // If the data or row state changes, re-render
  80. scope.$watch(function () {
  81. return angular.toJson([scope.data, scope.row])
  82. }, function() {
  83. if(!(_.isUndefined(scope.data)))
  84. render_panel(scope,elem,attrs);
  85. });
  86. // Re-render if the window is resized
  87. angular.element(window).bind('resize', function(){
  88. render_panel(scope,elem,attrs);
  89. });
  90. // Function for rendering panel
  91. function render_panel(scope,elem,attrs) {
  92. // Determine format
  93. var show = _.isUndefined(scope.panel.show) ? {
  94. bars: true, lines: false, points: false
  95. } : {
  96. lines: _.indexOf(scope.panel.show,'lines') < 0 ? false : true,
  97. bars: _.indexOf(scope.panel.show,'bars') < 0 ? false : true,
  98. points: _.indexOf(scope.panel.show,'points') < 0 ? false : true,
  99. stack: _.indexOf(scope.panel.show,'stack') < 0 ? null : true,
  100. }
  101. // Set barwidth based on specified interval
  102. var barwidth = interval_to_seconds(scope.panel.interval)*1000
  103. var scripts = $LAB.script("common/lib/panels/jquery.flot.js")
  104. .script("common/lib/panels/jquery.flot.time.js")
  105. .script("common/lib/panels/jquery.flot.stack.js")
  106. // Populate element. Note that jvectormap appends, does not replace.
  107. scripts.wait(function(){
  108. // Populate element
  109. $.plot(elem, scope.data, {
  110. legend: {
  111. position: "nw",
  112. labelFormatter: function(label, series) {
  113. return '<span class="legend">' + label + ' / ' +
  114. scope.panel.interval + '</span>';
  115. }
  116. },
  117. series: {
  118. stack: show.stack,
  119. lines: { show: show.lines, fill: scope.panel.fill },
  120. bars: { show: show.bars, fill: 1, barWidth: barwidth/1.8 },
  121. points: { show: show.points },
  122. shadowSize: 1
  123. },
  124. yaxis: { min: 0, color: "#000" },
  125. xaxis: {
  126. mode: "time",
  127. timeformat: "%H:%M:%S<br>%m-%d",
  128. label: "Datetime",
  129. color: "#000",
  130. },
  131. grid: {
  132. backgroundColor: '#fff',
  133. borderWidth: 0,
  134. borderColor: '#eee',
  135. color: "#eee",
  136. hoverable: true,
  137. }
  138. })
  139. })
  140. }
  141. }
  142. };
  143. })