module.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. ## Timepicker
  3. The timepicker panel is used to select time ranges and inform other panel of
  4. them. It also handles searching for indices that match the given time range and
  5. a pattern
  6. ### Parameters
  7. * mode :: The default mode of the panel. Options: 'relative', 'absolute' 'since' Default: 'relative'
  8. * time_options :: An array of possible time options. Default: ['5m','15m','1h','6h','12h','24h','2d','7d','30d']
  9. * timespan :: The default options selected for the relative view. Default: '15m'
  10. * timefield :: The field in which time is stored in the document.
  11. * index :: Index pattern to match. Literals should be double quoted. Default: '"logstash-"yyyy.mm.dd'
  12. * refresh: Object containing refresh parameters
  13. * enable :: true/false, enable auto refresh by default. Default: false
  14. * interval :: Seconds between auto refresh. Default: 30
  15. * min :: The lowest interval a user may set
  16. ### Group Events
  17. #### Sends
  18. * time :: Object Includes from, to and index
  19. #### Receives
  20. * get_time :: Receives an object containing a uniqueid, broadcasts to it.
  21. */
  22. angular.module('kibana.timepicker', [])
  23. .controller('timepicker', function($scope, eventBus, $timeout, timer, $http) {
  24. var _id = _.uniqueId();
  25. // Set and populate defaults
  26. var _d = {
  27. mode : "relative",
  28. time_options : ['5m','15m','1h','6h','12h','24h','2d','7d','30d'],
  29. timespan : '15m',
  30. timefield : '@timestamp',
  31. index : '"logstash-"yyyy.mm.dd',
  32. group : "default",
  33. refresh : {
  34. enable : false,
  35. interval: 30,
  36. min : 3
  37. }
  38. }
  39. _.defaults($scope.panel,_d)
  40. var _groups = _.isArray($scope.panel.group) ?
  41. $scope.panel.group : [$scope.panel.group];
  42. $scope.init = function() {
  43. // Private refresh interval that we can use for view display without causing
  44. // unnecessary refreshes during changes
  45. $scope.refresh_interval = $scope.panel.refresh.interval
  46. // Init a private time object with Date() objects depending on mode
  47. switch($scope.panel.mode) {
  48. case 'absolute':
  49. $scope.time = {
  50. from : Date.parse($scope.panel.time.from),
  51. to : Date.parse($scope.panel.time.to)
  52. }
  53. break;
  54. case 'since':
  55. $scope.time = {
  56. from : Date.parse($scope.panel.time.from),
  57. to : new Date()
  58. }
  59. break;
  60. case 'relative':
  61. $scope.time = {
  62. from : time_ago($scope.panel.timespan),
  63. to : new Date()
  64. }
  65. break;
  66. }
  67. $scope.time.field = $scope.panel.timefield;
  68. $scope.time_apply();
  69. // In the case that a panel is not ready to receive a time event, it may
  70. // request one be sent by broadcasting a 'get_time' with its _id to its group
  71. // This panel can handle multiple groups
  72. eventBus.register($scope,"get_time", function(event,id) {
  73. eventBus.broadcast($scope.$id,id,'time',$scope.time)
  74. });
  75. $scope.$watch('panel.refresh.enable', function() {$scope.refresh()});
  76. $scope.$watch('panel.refresh.interval', function() {
  77. $timeout(function(){
  78. if(_.isNumber($scope.panel.refresh.interval)) {
  79. if($scope.panel.refresh.interval < $scope.panel.refresh.min) {
  80. $scope.panel.refresh.interval = $scope.panel.refresh.min
  81. timer.cancel($scope.refresh_timer)
  82. return;
  83. }
  84. timer.cancel($scope.refresh_timer)
  85. $scope.refresh()
  86. } else {
  87. timer.cancel($scope.refresh_timer)
  88. }
  89. });
  90. });
  91. }
  92. $scope.refresh = function() {
  93. if ($scope.panel.refresh.enable) {
  94. timer.cancel($scope.refresh_timer)
  95. $scope.refresh_timer = timer.register($timeout(function() {
  96. $scope.refresh();
  97. $scope.time_apply();
  98. },$scope.panel.refresh.interval*1000
  99. ));
  100. } else {
  101. timer.cancel($scope.refresh_timer)
  102. }
  103. }
  104. $scope.set_mode = function(mode) {
  105. $scope.panel.mode = mode;
  106. $scope.panel.refresh.enable = mode === 'absolute' ?
  107. false : $scope.panel.refresh.enable
  108. }
  109. $scope.to_now = function() {
  110. $scope.timepicker.to = {
  111. time : new Date().format("HH:MM:ss"),
  112. date : new Date().format("mm/dd/yyyy")
  113. }
  114. }
  115. $scope.set_timespan = function(timespan) {
  116. $scope.panel.timespan = timespan;
  117. $scope.timepicker.from = {
  118. time : time_ago(timespan).format("HH:MM:ss"),
  119. date : time_ago(timespan).format("mm/dd/yyyy")
  120. }
  121. $scope.time_apply();
  122. }
  123. $scope.time_check = function(){
  124. var from = $scope.panel.mode === 'relative' ? time_ago($scope.panel.timespan) :
  125. Date.parse($scope.timepicker.from.date + " " + $scope.timepicker.from.time)
  126. var to = $scope.panel.mode !== 'absolute' ? new Date() :
  127. Date.parse($scope.timepicker.to.date + " " + $scope.timepicker.to.time)
  128. if (from.getTime() >= to.getTime())
  129. from = new Date(to.getTime() - 1000)
  130. // Janky 0s timeout to get around $scope queue processing view issue
  131. $timeout(function(){
  132. $scope.timepicker = {
  133. from : {
  134. time : from.format("HH:MM:ss"),
  135. date : from.format("mm/dd/yyyy")
  136. },
  137. to : {
  138. time : to.format("HH:MM:ss"),
  139. date : to.format("mm/dd/yyyy")
  140. }
  141. }
  142. });
  143. return {
  144. from : from,
  145. to : to
  146. };
  147. }
  148. $scope.time_apply = function() {
  149. // Update internal time object
  150. $scope.time = $scope.time_check();
  151. $scope.time.field = $scope.panel.timefield
  152. // Get indices for the time period, then broadcast time range and index list
  153. // in a single object. Not sure if I like this.
  154. indices($scope.time.from,$scope.time.to).then(function (p) {
  155. $scope.time.index = p.join();
  156. // Broadcast time
  157. eventBus.broadcast($scope.$id,$scope.panel.group,'time',$scope.time)
  158. });
  159. // Update panel's string representation of the time object
  160. $scope.panel.time = {
  161. from : $scope.time.from.format("mm/dd/yyyy HH:MM:ss"),
  162. to : $scope.time.to.format("mm/dd/yyyy HH:MM:ss"),
  163. index : $scope.time.index,
  164. };
  165. };
  166. // returns a promise containing an array of all indices matching the index
  167. // pattern that exist in a given range
  168. function indices(from,to) {
  169. var possible = [];
  170. _.each(date_range(from,to.add_days(1)),function(d){
  171. possible.push(d.format($scope.panel.index));
  172. });
  173. return all_indices().then(function(p) {
  174. return _.intersection(p,possible);
  175. })
  176. };
  177. // returns a promise containing an array of all indices in an elasticsearch
  178. // cluster
  179. function all_indices() {
  180. var something = $http({
  181. url: config.elasticsearch + "/_aliases",
  182. method: "GET"
  183. }).error(function(data, status, headers, config) {
  184. $scope.error = status;
  185. });
  186. return something.then(function(p) {
  187. var indices = [];
  188. _.each(p.data, function(v,k) {
  189. indices.push(k)
  190. });
  191. return indices;
  192. });
  193. }
  194. // Great, every function is ready, init.
  195. $scope.init();
  196. })