module.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. angular.module('kibana.table', [])
  2. .controller('table', function($scope, $rootScope, $location) {
  3. var _id = _.uniqueId();
  4. // Set and populate defaults
  5. var _d = {
  6. query : "*",
  7. size : 100,
  8. sort : ['@timestamp','desc'],
  9. group : "default",
  10. }
  11. _.defaults($scope.panel,_d)
  12. $scope.init = function () {
  13. $scope.$on(_id+"-time", function(event,time){set_time(time)});
  14. $scope.$on($scope.panel.group+"-time", function(event,time){set_time(time)});
  15. $scope.$on($scope.panel.group+"-query", function(event, query) {
  16. $scope.panel.query = query;
  17. $scope.get_data();
  18. });
  19. $scope.$watch(function() {
  20. return angular.toJson($scope.panel.sort)
  21. }, function(){$scope.get_data()});
  22. // Now that we're all setup, request the time from our group
  23. $rootScope.$broadcast($scope.panel.group+"-get_time",_id)
  24. }
  25. $scope.toggle_sort = function() {
  26. $scope.panel.sort[1] = $scope.panel.sort[1] == 'asc' ? 'desc' : 'asc';
  27. }
  28. $scope.get_data = function() {
  29. // Make sure we have everything for the request to complete
  30. if(_.isUndefined($scope.panel.index) || _.isUndefined($scope.panel.time))
  31. return
  32. var request = $scope.ejs.Request().indices($scope.panel.index);
  33. var results = request
  34. .query(ejs.FilteredQuery(
  35. ejs.QueryStringQuery($scope.panel.query || '*'),
  36. ejs.RangeFilter(config.timefield)
  37. .from($scope.panel.time.from)
  38. .to($scope.panel.time.to)
  39. .cache(false)
  40. )
  41. )
  42. .size($scope.panel.size)
  43. .sort($scope.panel.sort[0],$scope.panel.sort[1])
  44. .doSearch();
  45. // Populate scope when we have results
  46. results.then(function(results) {
  47. if(_.isUndefined(results)) {
  48. $scope.panel.error = 'Your query was unsuccessful';
  49. return;
  50. }
  51. $scope.panel.error = false;
  52. $scope.hits = results.hits.total;
  53. $scope.data = results.hits.hits;
  54. // Broadcast a list of all fields. Note that receivers of field array
  55. // events should be able to receive from multiple sources, merge, dedupe
  56. // and sort on the fly.
  57. if (!(_.isUndefined($scope.panel.group)))
  58. $rootScope.$broadcast(
  59. $scope.panel.group+"-fields", {
  60. all : get_all_fields(results),
  61. sort : $scope.panel.sort
  62. });
  63. });
  64. }
  65. function set_time(time) {
  66. $scope.panel.time = time;
  67. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  68. $scope.get_data();
  69. }
  70. $scope.init();
  71. })