module.js 8.5 KB

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