module.js 5.2 KB

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