module.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. ## Derivequeries
  3. Broadcasts an array of queries based on the results of a terms facet
  4. ### Parameters
  5. * label :: The label to stick over the field
  6. * query :: A string to use as a filter for the terms facet
  7. * field :: the field to facet on
  8. * size :: how many queries to generate
  9. * fields :: a list of fields known to us
  10. * query_mode :: how to create query
  11. ### Group Events
  12. #### Sends
  13. * query :: Always broadcast as an array, even in multi: false
  14. * get_time :: Request the time object from the timepicker
  15. #### Receives
  16. * query :: An array of queries. This is probably needs to be fixed.
  17. * time :: populate index and time
  18. * fields :: A list of fields known to us
  19. */
  20. angular.module('kibana.derivequeries', [])
  21. .controller('derivequeries', function($scope, $rootScope, query, eventBus, fields, dashboard, filterSrv) {
  22. // Set and populate defaults
  23. var _d = {
  24. loading : false,
  25. status : "Beta",
  26. label : "Search",
  27. query : "*",
  28. ids : [],
  29. group : "default",
  30. field : '_type',
  31. fields : [],
  32. spyable : true,
  33. size : 5,
  34. mode : 'terms only',
  35. exclude : [],
  36. history : [],
  37. remember: 10 // max: 100, angular strap can't take a variable for items param
  38. }
  39. _.defaults($scope.panel,_d);
  40. $scope.init = function() {
  41. $scope.panel.fields = fields.list
  42. }
  43. $scope.get_data = function() {
  44. update_history($scope.panel.query);
  45. // Make sure we have everything for the request to complete
  46. if(dashboard.indices.length == 0) {
  47. return
  48. }
  49. $scope.panel.loading = true;
  50. var request = $scope.ejs.Request().indices(dashboard.indices);
  51. // Terms mode
  52. request = request
  53. .facet(ejs.TermsFacet('query')
  54. .field($scope.panel.field)
  55. .size($scope.panel['size'])
  56. .exclude($scope.panel.exclude)
  57. .facetFilter(ejs.QueryFilter(
  58. ejs.FilteredQuery(
  59. ejs.QueryStringQuery($scope.panel.query || '*'),
  60. filterSrv.getBoolFilter(filterSrv.ids)
  61. )))).size(0)
  62. $scope.populate_modal(request);
  63. var results = request.doSearch();
  64. // Populate scope when we have results
  65. results.then(function(results) {
  66. $scope.panel.loading = false;
  67. var data = [];
  68. if ($scope.panel.query === '' || $scope.panel.mode === 'terms only') {
  69. var suffix = '';
  70. } else if ($scope.panel.mode === 'AND') {
  71. var suffix = ' AND (' + $scope.panel.query + ')';
  72. } else if ($scope.panel.mode === 'OR') {
  73. var suffix = ' OR (' + $scope.panel.query + ')';
  74. }
  75. var ids = [];
  76. _.each(results.facets.query.terms, function(v) {
  77. var _q = $scope.panel.field+':"'+v.term+'"'+suffix;
  78. // if it isn't in the list, remove it
  79. var _iq = query.findQuery(_q)
  80. if(!_iq) {
  81. ids.push(query.set({query:_q}));
  82. } else {
  83. ids.push(_iq.id);
  84. }
  85. });
  86. _.each(_.difference($scope.panel.ids,ids),function(id){
  87. query.remove(id)
  88. })
  89. $scope.panel.ids = ids;
  90. dashboard.refresh();
  91. });
  92. }
  93. $scope.set_refresh = function (state) {
  94. $scope.refresh = state;
  95. }
  96. $scope.close_edit = function() {
  97. if($scope.refresh)
  98. $scope.get_data();
  99. $scope.refresh = false;
  100. }
  101. $scope.populate_modal = function(request) {
  102. $scope.modal = {
  103. title: "Inspector",
  104. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  105. 'curl -XGET '+config.elasticsearch+'/'+dashboard.indices+"/_search?pretty -d'\n"+
  106. angular.toJson(JSON.parse(request.toString()),true)+
  107. "'</pre>",
  108. }
  109. }
  110. var update_history = function(query) {
  111. query = _.isArray(query) ? query : [query];
  112. if($scope.panel.remember > 0) {
  113. $scope.panel.history = _.union(query.reverse(),$scope.panel.history)
  114. var _length = $scope.panel.history.length
  115. if(_length > $scope.panel.remember) {
  116. $scope.panel.history = $scope.panel.history.slice(0,$scope.panel.remember)
  117. }
  118. }
  119. }
  120. });