module.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. index_interval: "day",
  33. timed_indices : true,
  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. // In the case that a panel is not ready to receive a time event, it may
  72. // request one be sent by broadcasting a 'get_time' with its _id to its group
  73. // This panel can handle multiple groups
  74. eventBus.register($scope,"get_time", function(event,id) {
  75. eventBus.broadcast($scope.$id,id,'time',$scope.time)
  76. });
  77. // In case some other panel broadcasts a time, set us to an absolute range
  78. eventBus.register($scope,"set_time", function(event,time) {
  79. $scope.panel.mode = 'absolute';
  80. set_timepicker(time.from,time.to)
  81. $scope.time_apply()
  82. });
  83. eventBus.register($scope,"zoom", function(event,factor) {
  84. var _timespan = ($scope.time.to.getTime() - $scope.time.from.getTime());
  85. try {
  86. if($scope.panel.mode != 'absolute') {
  87. $scope.panel.mode = 'since'
  88. set_timepicker(new Date($scope.time.to.getTime() - _timespan*factor),$scope.time.to)
  89. } else {
  90. var _center = $scope.time.to - _timespan/2
  91. set_timepicker(new Date(_center - (_timespan*factor)/2),
  92. new Date(_center + (_timespan*factor)/2))
  93. }
  94. } catch (e) {
  95. console.log(e)
  96. }
  97. $scope.time_apply();
  98. });
  99. $scope.$on('render', function (){
  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.time_check = function(){
  149. // If time picker is defined (on initialization)
  150. if(!(_.isUndefined($scope.timepicker))) {
  151. var from = $scope.panel.mode === 'relative' ? time_ago($scope.panel.timespan) :
  152. new Date(Date.parse($scope.timepicker.from.date + " " + $scope.timepicker.from.time))
  153. var to = $scope.panel.mode !== 'absolute' ? new Date() :
  154. new Date(Date.parse($scope.timepicker.to.date + " " + $scope.timepicker.to.time))
  155. // Otherwise
  156. } else {
  157. var from = $scope.panel.mode === 'relative' ? time_ago($scope.panel.timespan) :
  158. $scope.time.from;
  159. var to = $scope.panel.mode !== 'absolute' ? new Date() :
  160. $scope.time.to;
  161. }
  162. if (from.getTime() >= to.getTime())
  163. from = new Date(to.getTime() - 1000)
  164. $timeout(function(){
  165. set_timepicker(from,to)
  166. });
  167. return {
  168. from : from,
  169. to : to
  170. };
  171. }
  172. $scope.time_apply = function() {
  173. // Update internal time object
  174. $scope.time = $scope.time_check();
  175. $scope.time.field = $scope.panel.timefield
  176. // Get indices for the time period, then broadcast time range and index list
  177. // in a single object. Not sure if I like this.
  178. if($scope.panel.timed_indices) {
  179. indices($scope.time.from,$scope.time.to).then(function (p) {
  180. $scope.time.index = p;
  181. eventBus.broadcast($scope.$id,$scope.panel.group,'time',$scope.time)
  182. });
  183. } else {
  184. $scope.time.index = [$scope.panel.index];
  185. eventBus.broadcast($scope.$id,$scope.panel.group,'time',$scope.time)
  186. }
  187. // Update panel's string representation of the time object
  188. $scope.panel.time = {
  189. from : $scope.time.from.format("mm/dd/yyyy HH:MM:ss"),
  190. to : $scope.time.to.format("mm/dd/yyyy HH:MM:ss"),
  191. index : $scope.time.index,
  192. };
  193. };
  194. function set_timepicker(from,to) {
  195. // Janky 0s timeout to get around $scope queue processing view issue
  196. $scope.timepicker = {
  197. from : {
  198. time : from.format("HH:MM:ss"),
  199. date : from.format("mm/dd/yyyy")
  200. },
  201. to : {
  202. time : to.format("HH:MM:ss"),
  203. date : to.format("mm/dd/yyyy")
  204. }
  205. }
  206. }
  207. // returns a promise containing an array of all indices matching the index
  208. // pattern that exist in a given range
  209. function indices(from,to) {
  210. var possible = [];
  211. _.each(expand_range(fake_utc(from),fake_utc(to),$scope.panel.index_interval),function(d){
  212. possible.push(d.format($scope.panel.index));
  213. });
  214. return all_indices().then(function(p) {
  215. var indices = _.intersection(possible,p);
  216. indices.reverse();
  217. return indices.length == 0 ? [$scope.panel.defaultindex] : indices;
  218. })
  219. };
  220. // returns a promise containing an array of all indices in an elasticsearch
  221. // cluster
  222. function all_indices() {
  223. var something = $http({
  224. url: config.elasticsearch + "/_aliases",
  225. method: "GET"
  226. }).error(function(data, status, headers, config) {
  227. $scope.error = status;
  228. });
  229. return something.then(function(p) {
  230. var indices = [];
  231. _.each(p.data, function(v,k) {
  232. indices.push(k)
  233. });
  234. return indices;
  235. });
  236. }
  237. // this is stupid, but there is otherwise no good way to ensure that when
  238. // I extract the date from an object that I'm get the UTC date. Stupid js.
  239. // I die a little inside every time I call this function.
  240. function fake_utc(date) {
  241. return new Date(date.getTime() + date.getTimezoneOffset() * 60000);
  242. }
  243. // Create an array of date objects by a given interval
  244. function expand_range(start, end, interval) {
  245. if(_.contains(['hour','day','week','month','year'],interval)) {
  246. var range;
  247. start = start.clone();
  248. range = [];
  249. while (start.isBefore(end)) {
  250. range.push(start.clone());
  251. switch (interval) {
  252. case 'hour':
  253. start.addHours(1)
  254. break
  255. case 'day':
  256. start.addDays(1)
  257. break
  258. case 'week':
  259. start.addWeeks(1)
  260. break
  261. case 'month':
  262. start.addMonths(1)
  263. break
  264. case 'year':
  265. start.addYears(1)
  266. break
  267. }
  268. }
  269. range.push(end.clone());
  270. return range;
  271. } else {
  272. return false;
  273. }
  274. }
  275. })