module.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. angular.module('kibana.hits', [])
  2. .controller('hits', function($scope, eventBus) {
  3. // Set and populate defaults
  4. var _d = {
  5. query : "*",
  6. group : "default",
  7. style : { "font-size": '36pt'},
  8. aggregate : true,
  9. arrangement : 'vertical'
  10. }
  11. _.defaults($scope.panel,_d)
  12. $scope.init = function () {
  13. $scope.hits = 0;
  14. eventBus.register($scope,'time', function(event,time){
  15. set_time(time)
  16. });
  17. eventBus.register($scope,'query', function(event, query) {
  18. $scope.panel.query = _.map(query,function(q) {
  19. return {query: q, label: q};
  20. })
  21. $scope.get_data();
  22. });
  23. // Now that we're all setup, request the time from our group
  24. eventBus.broadcast($scope.$id,$scope.panel.group,'get_time')
  25. }
  26. $scope.get_data = function(segment,query_id) {
  27. delete $scope.panel.error
  28. $scope.panel.loading = true;
  29. // Make sure we have everything for the request to complete
  30. if(_.isUndefined($scope.panel.index) || _.isUndefined($scope.time))
  31. return
  32. var _segment = _.isUndefined(segment) ? 0 : segment
  33. var request = $scope.ejs.Request().indices($scope.panel.index[_segment]);
  34. // Build the question part of the query
  35. var queries = [];
  36. _.each($scope.panel.query, function(v) {
  37. queries.push($scope.ejs.FilteredQuery(
  38. ejs.QueryStringQuery(v.query || '*'),
  39. ejs.RangeFilter($scope.time.field)
  40. .from($scope.time.from)
  41. .to($scope.time.to))
  42. )
  43. });
  44. // Build the facet part
  45. _.each(queries, function(v) {
  46. request = request
  47. .facet($scope.ejs.QueryFacet("query"+_.indexOf(queries,v))
  48. .query(v)
  49. ).size(0)
  50. })
  51. // TODO: Spy for hits panel
  52. //$scope.populate_modal(request);
  53. // Then run it
  54. var results = request.doSearch();
  55. // Populate scope when we have results
  56. results.then(function(results) {
  57. $scope.panel.loading = false;
  58. if(_segment == 0) {
  59. $scope.hits = 0;
  60. $scope.data = [];
  61. query_id = $scope.query_id = new Date().getTime();
  62. }
  63. // Check for error and abort if found
  64. if(!(_.isUndefined(results.error))) {
  65. $scope.panel.error = $scope.parse_error(results.error);
  66. return;
  67. }
  68. if($scope.query_id === query_id) {
  69. var i = 0;
  70. _.each(results.facets, function(v, k) {
  71. var hits = _.isUndefined($scope.data[i]) || _segment == 0 ?
  72. v.count : $scope.data[i].hits+v.count
  73. $scope.hits += v.count
  74. // Create series
  75. $scope.data[i] = {
  76. label: $scope.panel.query[i].label || "query"+(parseInt(i)+1),
  77. hits: hits
  78. };
  79. i++;
  80. });
  81. if(_segment < $scope.panel.index.length-1)
  82. $scope.get_data(_segment+1,query_id)
  83. }
  84. });
  85. }
  86. $scope.remove_query = function(q) {
  87. $scope.panel.query = _.without($scope.panel.query,q);
  88. $scope.get_data();
  89. }
  90. $scope.add_query = function(label,query) {
  91. $scope.panel.query.unshift({
  92. query: query,
  93. label: label,
  94. });
  95. $scope.get_data();
  96. }
  97. function set_time(time) {
  98. $scope.time = time;
  99. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  100. $scope.get_data();
  101. }
  102. })