module.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. labjs = labjs.script("common/lib/jquery.flot.js")
  2. .script("common/lib/jquery.flot.time.js")
  3. angular.module('kibana.histogram', [])
  4. .directive('histogram', function() {
  5. return {
  6. restrict: 'A',
  7. link: function(scope, elem, attrs) {
  8. // Specify defaults for ALL directives
  9. var _d = {
  10. query : "*",
  11. interval: secondsToHms(calculate_interval(scope.from,scope.to,40,0)/1000),
  12. color : "#27508C",
  13. show : ['bars']
  14. }
  15. // Set ready flag and fill parameters (REQUIRED IN EVERY PANEL)
  16. scope.$watch(function () {
  17. return (attrs.params && scope.index) ? true : false;
  18. }, function (ready) {
  19. scope.ready = ready;
  20. if(ready) {
  21. scope.params = JSON.parse(attrs.params);
  22. _.each(_d, function(v, k) {
  23. scope.params[k] = _.isUndefined(scope.params[k])
  24. ? _d[k] : scope.params[k];
  25. });
  26. }
  27. });
  28. // Also get the data if time frame changes.
  29. // (REQUIRED IN EVERY PANEL)
  30. scope.$watch(function() {
  31. return angular.toJson([scope.from, scope.to, scope.ready])
  32. }, function(){
  33. if(scope.ready)
  34. if (_.isUndefined(attrs.params.interval))
  35. scope.params.interval = secondsToHms(
  36. calculate_interval(scope.from,scope.to,50,0)/1000),
  37. get_data(scope,elem,attrs);
  38. });
  39. // Re-rending the panel if it is resized,
  40. scope.$watch('data', function() {
  41. if(scope.ready)
  42. render_panel(scope,elem,attrs);
  43. });
  44. // Or if the model changes
  45. angular.element(window).bind('resize', function(){
  46. render_panel(scope,elem,attrs);
  47. });
  48. // Function for getting data
  49. function get_data(scope,elem,attrs) {
  50. var params = scope.params;
  51. var ejs = scope.ejs;
  52. var request = ejs.Request().indices(scope.index);
  53. // Build the question part of the query
  54. var query = ejs.FilteredQuery(
  55. ejs.QueryStringQuery(params.query || '*'),
  56. ejs.RangeFilter(config.timefield)
  57. .from(scope.from)
  58. .to(scope.to)
  59. .cache(false)
  60. );
  61. // Then the insert into facet and make the request
  62. var results = request
  63. .facet(ejs.DateHistogramFacet('histogram')
  64. .field(config.timefield)
  65. .interval(params.interval)
  66. .facetFilter(ejs.QueryFilter(query))
  67. )
  68. .doSearch();
  69. // Populate scope when we have results
  70. results.then(function(results) {
  71. scope.hits = results.hits.total;
  72. scope.data = results.facets.histogram.entries;
  73. });
  74. }
  75. // Function for rendering panel
  76. function render_panel(scope,elem,attrs) {
  77. // Parse our params object
  78. var params = scope.params;
  79. // Determine format
  80. var show = _.isUndefined(params.show) ? {
  81. bars: true, lines: false, points: false
  82. } : {
  83. lines: _.indexOf(params.show,'lines') < 0 ? false : true,
  84. bars: _.indexOf(params.show,'bars') < 0 ? false : true,
  85. points: _.indexOf(params.show,'points') < 0 ? false : true,
  86. }
  87. // Push null values at beginning and end of timeframe
  88. scope.graph = [
  89. [scope.from.getTime(), null],[scope.to.getTime(), null]];
  90. // Create FLOT value array
  91. _.each(scope.data, function(v, k) {
  92. scope.graph.push([v['time'],v['count']])
  93. });
  94. // Set barwidth based on specified interval
  95. var barwidth = interval_to_seconds(params.interval)*1000
  96. // Populate element
  97. $.plot(elem, [{
  98. label: _.isUndefined(params.label) ? params.query: params.label,
  99. data: scope.graph
  100. }], {
  101. legend: {
  102. position: "nw",
  103. labelFormatter: function(label, series) {
  104. return '<span class="legend">' + label + ' / ' + params.interval
  105. + '</span>';
  106. }
  107. },
  108. series: {
  109. lines: { show: show.lines, fill: false },
  110. bars: { show: show.bars, fill: 1, barWidth: barwidth/1.8 },
  111. points: { show: show.points },
  112. color: params.color,
  113. shadowSize: 1
  114. },
  115. yaxis: { min: 0, color: "#000" },
  116. xaxis: {
  117. mode: "time",
  118. timeformat: "%H:%M:%S<br>%m-%d",
  119. label: "Datetime",
  120. color: "#000",
  121. },
  122. grid: {
  123. backgroundColor: '#fff',
  124. borderWidth: 0,
  125. borderColor: '#eee',
  126. color: "#eee",
  127. hoverable: true,
  128. }
  129. });
  130. //elem.show();
  131. }
  132. }
  133. };
  134. })