module.js 2.0 KB

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