module.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 : Date.parse($scope.panel.time.from) || time_ago($scope.panel.timespan),
  53. to : Date.parse($scope.panel.time.to) || new Date()
  54. }
  55. break;
  56. case 'since':
  57. $scope.time = {
  58. from : 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. $scope.$watch('panel.refresh.enable', function() {$scope.refresh()});
  78. $scope.$watch('panel.refresh.interval', function() {
  79. $timeout(function(){
  80. if(_.isNumber($scope.panel.refresh.interval)) {
  81. if($scope.panel.refresh.interval < $scope.panel.refresh.min) {
  82. $scope.panel.refresh.interval = $scope.panel.refresh.min
  83. timer.cancel($scope.refresh_timer)
  84. return;
  85. }
  86. timer.cancel($scope.refresh_timer)
  87. $scope.refresh()
  88. } else {
  89. timer.cancel($scope.refresh_timer)
  90. }
  91. });
  92. });
  93. }
  94. $scope.refresh = function() {
  95. if ($scope.panel.refresh.enable) {
  96. timer.cancel($scope.refresh_timer)
  97. $scope.refresh_timer = timer.register($timeout(function() {
  98. $scope.refresh();
  99. $scope.time_apply();
  100. },$scope.panel.refresh.interval*1000
  101. ));
  102. } else {
  103. timer.cancel($scope.refresh_timer)
  104. }
  105. }
  106. $scope.set_mode = function(mode) {
  107. $scope.panel.mode = mode;
  108. $scope.panel.refresh.enable = mode === 'absolute' ?
  109. false : $scope.panel.refresh.enable
  110. }
  111. $scope.to_now = function() {
  112. $scope.timepicker.to = {
  113. time : new Date().format("HH:MM:ss"),
  114. date : new Date().format("mm/dd/yyyy")
  115. }
  116. }
  117. $scope.set_timespan = function(timespan) {
  118. $scope.panel.timespan = timespan;
  119. $scope.timepicker.from = {
  120. time : time_ago(timespan).format("HH:MM:ss"),
  121. date : time_ago(timespan).format("mm/dd/yyyy")
  122. }
  123. $scope.time_apply();
  124. }
  125. $scope.time_check = function(){
  126. if(!(_.isUndefined($scope.timepicker))) {
  127. var from = $scope.panel.mode === 'relative' ? time_ago($scope.panel.timespan) :
  128. Date.parse($scope.timepicker.from.date + " " + $scope.timepicker.from.time)
  129. var to = $scope.panel.mode !== 'absolute' ? new Date() :
  130. Date.parse($scope.timepicker.to.date + " " + $scope.timepicker.to.time)
  131. } else {
  132. var from = $scope.panel.mode === 'relative' ? time_ago($scope.panel.timespan) :
  133. $scope.time.from;
  134. var to = $scope.panel.mode !== 'absolute' ? new Date() :
  135. $scope.time.to;
  136. }
  137. if (from.getTime() >= to.getTime())
  138. from = new Date(to.getTime() - 1000)
  139. // Janky 0s timeout to get around $scope queue processing view issue
  140. $timeout(function(){
  141. $scope.timepicker = {
  142. from : {
  143. time : from.format("HH:MM:ss"),
  144. date : from.format("mm/dd/yyyy")
  145. },
  146. to : {
  147. time : to.format("HH:MM:ss"),
  148. date : to.format("mm/dd/yyyy")
  149. }
  150. }
  151. });
  152. return {
  153. from : from,
  154. to : to
  155. };
  156. }
  157. $scope.time_apply = function() {
  158. // Update internal time object
  159. $scope.time = $scope.time_check();
  160. $scope.time.field = $scope.panel.timefield
  161. // Get indices for the time period, then broadcast time range and index list
  162. // in a single object. Not sure if I like this.
  163. if($scope.panel.timed_indices) {
  164. indices($scope.time.from,$scope.time.to).then(function (p) {
  165. $scope.time.index = p;
  166. eventBus.broadcast($scope.$id,$scope.panel.group,'time',$scope.time)
  167. });
  168. } else {
  169. $scope.time.index = $scope.panel.index;
  170. eventBus.broadcast($scope.$id,$scope.panel.group,'time',$scope.time)
  171. }
  172. // Update panel's string representation of the time object
  173. $scope.panel.time = {
  174. from : $scope.time.from.format("mm/dd/yyyy HH:MM:ss"),
  175. to : $scope.time.to.format("mm/dd/yyyy HH:MM:ss"),
  176. index : $scope.time.index,
  177. };
  178. };
  179. // returns a promise containing an array of all indices matching the index
  180. // pattern that exist in a given range
  181. function indices(from,to) {
  182. var possible = [];
  183. _.each(expand_range(fake_utc(from),fake_utc(to),$scope.panel.index_interval),function(d){
  184. possible.push(d.format($scope.panel.index));
  185. });
  186. return all_indices().then(function(p) {
  187. var indices = _.intersection(possible,p);
  188. indices.reverse();
  189. return indices.length == 0 ? [$scope.panel.defaultindex] : indices;
  190. })
  191. };
  192. // returns a promise containing an array of all indices in an elasticsearch
  193. // cluster
  194. function all_indices() {
  195. var something = $http({
  196. url: config.elasticsearch + "/_aliases",
  197. method: "GET"
  198. }).error(function(data, status, headers, config) {
  199. $scope.error = status;
  200. });
  201. return something.then(function(p) {
  202. var indices = [];
  203. _.each(p.data, function(v,k) {
  204. indices.push(k)
  205. });
  206. return indices;
  207. });
  208. }
  209. // this is stupid, but there is otherwise no good way to ensure that when
  210. // I extract the date from an object that I'm get the UTC date. Stupid js.
  211. // I die a little inside every time I call this function.
  212. function fake_utc(date) {
  213. return new Date(date.getTime() + date.getTimezoneOffset() * 60000);
  214. }
  215. // Create an array of date objects by a given interval
  216. function expand_range(start, end, interval) {
  217. if(_.contains(['hour','day','week','month','year'],interval)) {
  218. var range;
  219. start = start.clone();
  220. range = [];
  221. while (start.isBefore(end)) {
  222. range.push(start.clone());
  223. switch (interval) {
  224. case 'hour':
  225. start.addHours(1)
  226. break
  227. case 'day':
  228. start.addDays(1)
  229. break
  230. case 'week':
  231. start.addWeeks(1)
  232. break
  233. case 'month':
  234. start.addMonths(1)
  235. break
  236. case 'year':
  237. start.addYears(1)
  238. break
  239. }
  240. }
  241. range.push(end.clone());
  242. return range;
  243. } else {
  244. return false;
  245. }
  246. }
  247. // Great, every function is ready, init.
  248. $scope.init();
  249. })