controllers.js 2.6 KB

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