controllers.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. 'use strict';
  4. angular.module('kibana-dash.controllers', [])
  5. .controller('SearchCtrl', function($scope, $location, $http, $timeout, ejsResource) {
  6. $scope.config = config;
  7. $scope.dashboards = dashboards
  8. $scope.timespan = config.timespan
  9. $scope.from = time_ago($scope.timespan);
  10. $scope.to = new Date();
  11. $scope.time_options = ['5m','15m','1h','6h','12h','24h','2d','7d','30d'];
  12. $scope.counter = 0;
  13. $scope.playing = true;
  14. $scope.play = function(){
  15. $scope.counter++;
  16. $scope.to = new Date();
  17. $scope.from = time_ago($scope.timespan);
  18. $scope.$root.$eval()
  19. mytimeout = $timeout($scope.play,config.refresh);
  20. }
  21. $scope.pause = function(){
  22. if($scope.playing) {
  23. $scope.playing = false;
  24. $timeout.cancel(mytimeout);
  25. } else {
  26. $scope.playing = true;
  27. mytimeout = $timeout($scope.play,config.refresh);
  28. }
  29. }
  30. var mytimeout = $timeout($scope.play,config.refresh);
  31. // If from/to to change, update index list
  32. $scope.$watch(function() {
  33. return angular.toJson([$scope.from, $scope.to])
  34. }, function(){
  35. indices($scope.from,$scope.to).then(function (p) {
  36. $scope.index = p.join();
  37. });
  38. });
  39. // point to your ElasticSearch server
  40. var ejs = $scope.ejs = ejsResource(config.elasticsearch);
  41. $scope.set_timespan = function(timespan) {
  42. $scope.timespan = timespan;
  43. $scope.from = time_ago($scope.timespan);
  44. }
  45. // returns a promise containing an array of all indices matching the index
  46. // pattern that exist in a given range
  47. function indices(from,to) {
  48. var possible = [];
  49. _.each(date_range(from,to.add_days(1)),function(d){
  50. possible.push(d.format(config.indexpattern));
  51. });
  52. return all_indices().then(function(p) {
  53. return _.intersection(p,possible);
  54. })
  55. };
  56. // returns a promise containing an array of all indices in an elasticsearch
  57. // cluster
  58. function all_indices() {
  59. var something = $http({
  60. url: config.elasticsearch + "/_aliases",
  61. method: "GET"
  62. }).error(function(data, status, headers, config) {
  63. $scope.error = status;
  64. });
  65. return something.then(function(p) {
  66. var indices = [];
  67. _.each(p.data, function(v,k) {
  68. indices.push(k)
  69. });
  70. return indices;
  71. });
  72. }
  73. });