module.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. // Set and populate defaults
  25. var _d = {
  26. mode : "relative",
  27. time_options : ['5m','15m','1h','6h','12h','24h','2d','7d','30d'],
  28. timespan : '15m',
  29. timefield : '@timestamp',
  30. index : '"logstash-"yyyy.mm.dd',
  31. defaultindex : "NOINDEX",
  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) || time_ago($scope.panel.timespan),
  51. to : Date.parse($scope.panel.time.to) || new Date()
  52. }
  53. break;
  54. case 'since':
  55. $scope.time = {
  56. from : Date.parse($scope.panel.time.from) || time_ago($scope.panel.timespan),
  57. to : new Date() || 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. if(!(_.isUndefined($scope.timepicker))) {
  125. var from = $scope.panel.mode === 'relative' ? time_ago($scope.panel.timespan) :
  126. Date.parse($scope.timepicker.from.date + " " + $scope.timepicker.from.time)
  127. var to = $scope.panel.mode !== 'absolute' ? new Date() :
  128. Date.parse($scope.timepicker.to.date + " " + $scope.timepicker.to.time)
  129. } else {
  130. var from = $scope.panel.mode === 'relative' ? time_ago($scope.panel.timespan) :
  131. $scope.time.from;
  132. var to = $scope.panel.mode !== 'absolute' ? new Date() :
  133. $scope.time.to;
  134. }
  135. if (from.getTime() >= to.getTime())
  136. from = new Date(to.getTime() - 1000)
  137. // Janky 0s timeout to get around $scope queue processing view issue
  138. $timeout(function(){
  139. $scope.timepicker = {
  140. from : {
  141. time : from.format("HH:MM:ss"),
  142. date : from.format("mm/dd/yyyy")
  143. },
  144. to : {
  145. time : to.format("HH:MM:ss"),
  146. date : to.format("mm/dd/yyyy")
  147. }
  148. }
  149. });
  150. return {
  151. from : from,
  152. to : to
  153. };
  154. }
  155. $scope.time_apply = function() {
  156. // Update internal time object
  157. $scope.time = $scope.time_check();
  158. $scope.time.field = $scope.panel.timefield
  159. // Get indices for the time period, then broadcast time range and index list
  160. // in a single object. Not sure if I like this.
  161. indices($scope.time.from,$scope.time.to).then(function (p) {
  162. $scope.time.index = p.join();
  163. // Broadcast time
  164. eventBus.broadcast($scope.$id,$scope.panel.group,'time',$scope.time)
  165. });
  166. // Update panel's string representation of the time object
  167. $scope.panel.time = {
  168. from : $scope.time.from.format("mm/dd/yyyy HH:MM:ss"),
  169. to : $scope.time.to.format("mm/dd/yyyy HH:MM:ss"),
  170. index : $scope.time.index,
  171. };
  172. };
  173. // returns a promise containing an array of all indices matching the index
  174. // pattern that exist in a given range
  175. function indices(from,to) {
  176. var possible = [];
  177. _.each(date_range(from,to.add_days(1)),function(d){
  178. possible.push(d.format($scope.panel.index));
  179. });
  180. return all_indices().then(function(p) {
  181. var indices = _.intersection(p,possible);
  182. indices.reverse();
  183. return indices.length == 0 ? [$scope.panel.defaultindex] : indices;
  184. })
  185. };
  186. // returns a promise containing an array of all indices in an elasticsearch
  187. // cluster
  188. function all_indices() {
  189. var something = $http({
  190. url: config.elasticsearch + "/_aliases",
  191. method: "GET"
  192. }).error(function(data, status, headers, config) {
  193. $scope.error = status;
  194. });
  195. return something.then(function(p) {
  196. var indices = [];
  197. _.each(p.data, function(v,k) {
  198. indices.push(k)
  199. });
  200. return indices;
  201. });
  202. }
  203. // Great, every function is ready, init.
  204. $scope.init();
  205. })