module.js 9.7 KB

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