module.js 8.5 KB

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