module.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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: '_all'
  12. * timed_indices :: Should kibana generate a list of indices to try based on selected time range?
  13. * defaultindex :: Index to failover to if index not found
  14. * refresh: Object containing refresh parameters
  15. * enable :: true/false, enable auto refresh by default. Default: false
  16. * interval :: Seconds between auto refresh. Default: 30
  17. * min :: The lowest interval a user may set
  18. ### Group Events
  19. #### Sends
  20. * time :: Object Includes from, to and index
  21. #### Receives
  22. * get_time :: Receives an object containing a uniqueid, broadcasts to it.
  23. */
  24. angular.module('kibana.timepicker', [])
  25. .controller('timepicker', function($scope, eventBus, $timeout, timer, $http) {
  26. // Set and populate defaults
  27. var _d = {
  28. mode : "relative",
  29. time_options : ['5m','15m','1h','6h','12h','24h','2d','7d','30d'],
  30. timespan : '15m',
  31. timefield : '@timestamp',
  32. index : '_all',
  33. defaultindex : "_all",
  34. index_interval: "day",
  35. timed_indices : false,
  36. group : "default",
  37. refresh : {
  38. enable : false,
  39. interval: 30,
  40. min : 3
  41. }
  42. }
  43. _.defaults($scope.panel,_d)
  44. var _groups = _.isArray($scope.panel.group) ?
  45. $scope.panel.group : [$scope.panel.group];
  46. $scope.init = function() {
  47. // Private refresh interval that we can use for view display without causing
  48. // unnecessary refreshes during changes
  49. $scope.refresh_interval = $scope.panel.refresh.interval
  50. // Init a private time object with Date() objects depending on mode
  51. switch($scope.panel.mode) {
  52. case 'absolute':
  53. $scope.time = {
  54. from : new Date(Date.parse($scope.panel.time.from)) || time_ago($scope.panel.timespan),
  55. to : new Date(Date.parse($scope.panel.time.to)) || new Date()
  56. }
  57. break;
  58. case 'since':
  59. $scope.time = {
  60. from : new Date(Date.parse($scope.panel.time.from)) || time_ago($scope.panel.timespan),
  61. to : new Date() || new Date()
  62. }
  63. break;
  64. case 'relative':
  65. $scope.time = {
  66. from : time_ago($scope.panel.timespan),
  67. to : new Date()
  68. }
  69. break;
  70. }
  71. $scope.time.field = $scope.panel.timefield;
  72. $scope.time_apply();
  73. // Start refresh timer if enabled
  74. if ($scope.panel.refresh.enable)
  75. $scope.set_interval($scope.panel.refresh.interval);
  76. // In the case that a panel is not ready to receive a time event, it may
  77. // request one be sent by broadcasting a 'get_time' with its _id to its group
  78. // This panel can handle multiple groups
  79. eventBus.register($scope,"get_time", function(event,id) {
  80. eventBus.broadcast($scope.$id,id,'time',$scope.time)
  81. });
  82. // In case some other panel broadcasts a time, set us to an absolute range
  83. eventBus.register($scope,"set_time", function(event,time) {
  84. $scope.panel.mode = 'absolute';
  85. set_timepicker(time.from,time.to)
  86. $scope.time_apply()
  87. });
  88. eventBus.register($scope,"zoom", function(event,factor) {
  89. var _timespan = ($scope.time.to.getTime() - $scope.time.from.getTime());
  90. try {
  91. if($scope.panel.mode != 'absolute') {
  92. $scope.panel.mode = 'since'
  93. set_timepicker(new Date($scope.time.to.getTime() - _timespan*factor),$scope.time.to)
  94. } else {
  95. var _center = $scope.time.to - _timespan/2
  96. set_timepicker(new Date(_center - (_timespan*factor)/2),
  97. new Date(_center + (_timespan*factor)/2))
  98. }
  99. } catch (e) {
  100. console.log(e)
  101. }
  102. $scope.time_apply();
  103. });
  104. $scope.$on('render', function (){
  105. $scope.time_apply();
  106. });
  107. }
  108. $scope.set_interval = function (refresh_interval) {
  109. $scope.panel.refresh.interval = refresh_interval
  110. if(_.isNumber($scope.panel.refresh.interval)) {
  111. if($scope.panel.refresh.interval < $scope.panel.refresh.min) {
  112. $scope.panel.refresh.interval = $scope.panel.refresh.min
  113. timer.cancel($scope.refresh_timer)
  114. return;
  115. }
  116. timer.cancel($scope.refresh_timer)
  117. $scope.refresh()
  118. } else {
  119. timer.cancel($scope.refresh_timer)
  120. }
  121. }
  122. $scope.refresh = function() {
  123. if ($scope.panel.refresh.enable) {
  124. timer.cancel($scope.refresh_timer)
  125. $scope.refresh_timer = timer.register($timeout(function() {
  126. $scope.refresh();
  127. $scope.time_apply();
  128. },$scope.panel.refresh.interval*1000
  129. ));
  130. } else {
  131. timer.cancel($scope.refresh_timer)
  132. }
  133. }
  134. $scope.set_mode = function(mode) {
  135. $scope.panel.mode = mode;
  136. $scope.panel.refresh.enable = mode === 'absolute' ?
  137. false : $scope.panel.refresh.enable
  138. }
  139. $scope.to_now = function() {
  140. $scope.timepicker.to = {
  141. time : new Date().format("HH:MM:ss"),
  142. date : new Date().format("mm/dd/yyyy")
  143. }
  144. }
  145. $scope.set_timespan = function(timespan) {
  146. $scope.panel.timespan = timespan;
  147. $scope.timepicker.from = {
  148. time : time_ago(timespan).format("HH:MM:ss"),
  149. date : time_ago(timespan).format("mm/dd/yyyy")
  150. }
  151. $scope.time_apply();
  152. }
  153. $scope.time_check = function(){
  154. // If time picker is defined (on initialization)
  155. if(!(_.isUndefined($scope.timepicker))) {
  156. var from = $scope.panel.mode === 'relative' ? time_ago($scope.panel.timespan) :
  157. new Date(Date.parse($scope.timepicker.from.date + " " + $scope.timepicker.from.time))
  158. var to = $scope.panel.mode !== 'absolute' ? new Date() :
  159. new Date(Date.parse($scope.timepicker.to.date + " " + $scope.timepicker.to.time))
  160. // Otherwise
  161. } else {
  162. var from = $scope.panel.mode === 'relative' ? time_ago($scope.panel.timespan) :
  163. $scope.time.from;
  164. var to = $scope.panel.mode !== 'absolute' ? new Date() :
  165. $scope.time.to;
  166. }
  167. if (from.getTime() >= to.getTime())
  168. from = new Date(to.getTime() - 1000)
  169. $timeout(function(){
  170. set_timepicker(from,to)
  171. });
  172. return {
  173. from : from,
  174. to : to
  175. };
  176. }
  177. $scope.time_apply = function() {
  178. // Update internal time object
  179. $scope.time = $scope.time_check();
  180. $scope.time.field = $scope.panel.timefield
  181. // Get indices for the time period, then broadcast time range and index list
  182. // in a single object. Not sure if I like this.
  183. if($scope.panel.timed_indices) {
  184. indices($scope.time.from,$scope.time.to).then(function (p) {
  185. $scope.time.index = p;
  186. eventBus.broadcast($scope.$id,$scope.panel.group,'time',$scope.time)
  187. });
  188. } else {
  189. $scope.time.index = [$scope.panel.index];
  190. eventBus.broadcast($scope.$id,$scope.panel.group,'time',$scope.time)
  191. }
  192. // Update panel's string representation of the time object
  193. $scope.panel.time = {
  194. from : $scope.time.from.format("mm/dd/yyyy HH:MM:ss"),
  195. to : $scope.time.to.format("mm/dd/yyyy HH:MM:ss"),
  196. index : $scope.time.index,
  197. };
  198. };
  199. function set_timepicker(from,to) {
  200. // Janky 0s timeout to get around $scope queue processing view issue
  201. $scope.timepicker = {
  202. from : {
  203. time : from.format("HH:MM:ss"),
  204. date : from.format("mm/dd/yyyy")
  205. },
  206. to : {
  207. time : to.format("HH:MM:ss"),
  208. date : to.format("mm/dd/yyyy")
  209. }
  210. }
  211. }
  212. // returns a promise containing an array of all indices matching the index
  213. // pattern that exist in a given range
  214. function indices(from,to) {
  215. var possible = [];
  216. _.each(expand_range(fake_utc(from),fake_utc(to),$scope.panel.index_interval),function(d){
  217. possible.push(d.format($scope.panel.index));
  218. });
  219. return all_indices().then(function(p) {
  220. var indices = _.intersection(possible,p);
  221. indices.reverse();
  222. return indices.length == 0 ? [$scope.panel.defaultindex] : indices;
  223. })
  224. };
  225. // returns a promise containing an array of all indices in an elasticsearch
  226. // cluster
  227. function all_indices() {
  228. var something = $http({
  229. url: config.elasticsearch + "/_aliases",
  230. method: "GET"
  231. }).error(function(data, status, headers, config) {
  232. $scope.error = status;
  233. });
  234. return something.then(function(p) {
  235. var indices = [];
  236. _.each(p.data, function(v,k) {
  237. indices.push(k)
  238. });
  239. return indices;
  240. });
  241. }
  242. // this is stupid, but there is otherwise no good way to ensure that when
  243. // I extract the date from an object that I'm get the UTC date. Stupid js.
  244. // I die a little inside every time I call this function.
  245. function fake_utc(date) {
  246. return new Date(date.getTime() + date.getTimezoneOffset() * 60000);
  247. }
  248. // Create an array of date objects by a given interval
  249. function expand_range(start, end, interval) {
  250. if(_.contains(['hour','day','week','month','year'],interval)) {
  251. var range;
  252. start = start.clone();
  253. range = [];
  254. while (start.isBefore(end)) {
  255. range.push(start.clone());
  256. switch (interval) {
  257. case 'hour':
  258. start.addHours(1)
  259. break
  260. case 'day':
  261. start.addDays(1)
  262. break
  263. case 'week':
  264. start.addWeeks(1)
  265. break
  266. case 'month':
  267. start.addMonths(1)
  268. break
  269. case 'year':
  270. start.addYears(1)
  271. break
  272. }
  273. }
  274. range.push(end.clone());
  275. return range;
  276. } else {
  277. return false;
  278. }
  279. }
  280. })