module.js 6.8 KB

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