services.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. 'use strict';
  4. angular.module('kibana.services', [])
  5. .service('eventBus', function($rootScope) {
  6. // An array of registed types
  7. var _types = []
  8. this.broadcast = function(from,to,type,data) {
  9. if(_.isUndefined(data))
  10. var data = from
  11. var packet = {
  12. time: new Date(),
  13. type: type,
  14. from: from,
  15. to: to,
  16. data: data
  17. }
  18. if(_.contains(_types,'$kibana_debug'))
  19. $rootScope.$broadcast('$kibana_debug',packet);
  20. //console.log('Sent: '+type + ' to ' + to + ' from ' + from + ': ' + angular.toJson(data))
  21. $rootScope.$broadcast(type,{
  22. from: from,
  23. to: to,
  24. data: data
  25. });
  26. }
  27. // This sets up an $on listener that checks to see if the event (packet) is
  28. // addressed to the scope in question and runs the registered function if it
  29. // is.
  30. this.register = function(scope,type,fn) {
  31. _types = _.union(_types,[type])
  32. scope.$on(type,function(event,packet){
  33. var _id = scope.$id;
  34. var _to = packet.to;
  35. var _from = packet.from;
  36. var _type = packet.type
  37. var _time = packet.time
  38. var _group = (!(_.isUndefined(scope.panel))) ? scope.panel.group : ["NONE"]
  39. //console.log('registered:' + type + " for " + scope.panel.title + " " + scope.$id)
  40. if(!(_.isArray(_to)))
  41. _to = [_to];
  42. if(!(_.isArray(_group)))
  43. _group = [_group];
  44. // Transmit even only if the send is not the receiver AND one of the following:
  45. // 1) Receiver has group in _to 2) Receiver's $id is in _to
  46. // 3) Event is addressed to ALL 4) Receiver is in ALL group
  47. if((_.intersection(_to,_group).length > 0 ||
  48. _.indexOf(_to,_id) > -1 ||
  49. _.indexOf(_group,'ALL') > -1 ||
  50. _.indexOf(_to,'ALL') > -1) &&
  51. _from !== _id
  52. ) {
  53. //console.log('Got: '+type + ' from ' + _from + ' to ' + _to + ': ' + angular.toJson(packet.data))
  54. fn(event,packet.data,{time:_time,to:_to,from:_from,type:_type});
  55. }
  56. });
  57. }
  58. })
  59. /* Service: fields
  60. Provides a global list of all seen fields for use in editor panels
  61. */
  62. .factory('fields', function($rootScope) {
  63. var fields = {
  64. list : []
  65. }
  66. $rootScope.$on('fields', function(event,f) {
  67. fields.list = _.union(f.data.all,fields.list)
  68. })
  69. return fields;
  70. })
  71. .service('kbnIndex',function($http) {
  72. // returns a promise containing an array of all indices matching the index
  73. // pattern that exist in a given range
  74. this.indices = function(from,to,pattern,interval) {
  75. var possible = [];
  76. _.each(expand_range(fake_utc(from),fake_utc(to),interval),function(d){
  77. possible.push(d.format(pattern));
  78. });
  79. return all_indices().then(function(p) {
  80. var indices = _.intersection(possible,p);
  81. indices.reverse();
  82. return indices
  83. })
  84. };
  85. // returns a promise containing an array of all indices in an elasticsearch
  86. // cluster
  87. function all_indices() {
  88. var something = $http({
  89. url: config.elasticsearch + "/_aliases",
  90. method: "GET"
  91. }).error(function(data, status, headers, config) {
  92. // Handle error condition somehow?
  93. });
  94. return something.then(function(p) {
  95. var indices = [];
  96. _.each(p.data, function(v,k) {
  97. indices.push(k)
  98. });
  99. return indices;
  100. });
  101. }
  102. // this is stupid, but there is otherwise no good way to ensure that when
  103. // I extract the date from an object that I'm get the UTC date. Stupid js.
  104. // I die a little inside every time I call this function.
  105. // Update: I just read this again. I died a little more inside.
  106. // Update2: More death.
  107. function fake_utc(date) {
  108. date = moment(date).clone().toDate()
  109. return moment(new Date(date.getTime() + date.getTimezoneOffset() * 60000));
  110. }
  111. // Create an array of date objects by a given interval
  112. function expand_range(start, end, interval) {
  113. if(_.contains(['hour','day','week','month','year'],interval)) {
  114. var range;
  115. start = moment(start).clone();
  116. range = [];
  117. while (start.isBefore(end)) {
  118. range.push(start.clone());
  119. switch (interval) {
  120. case 'hour':
  121. start.add('hours',1)
  122. break
  123. case 'day':
  124. start.add('days',1)
  125. break
  126. case 'week':
  127. start.add('weeks',1)
  128. break
  129. case 'month':
  130. start.add('months',1)
  131. break
  132. case 'year':
  133. start.add('years',1)
  134. break
  135. }
  136. }
  137. range.push(moment(end).clone());
  138. return range;
  139. } else {
  140. return false;
  141. }
  142. }
  143. })
  144. .service('timer', function($timeout) {
  145. // This service really just tracks a list of $timeout promises to give us a
  146. // method for cancelling them all when we need to
  147. var timers = [];
  148. this.register = function(promise) {
  149. timers.push(promise);
  150. return promise;
  151. }
  152. this.cancel = function(promise) {
  153. timers = _.without(timers,promise)
  154. $timeout.cancel(promise)
  155. }
  156. this.cancel_all = function() {
  157. _.each(timers, function(t){
  158. $timeout.cancel(t);
  159. });
  160. timers = new Array();
  161. }
  162. })
  163. .service('keylistener', function($rootScope) {
  164. var keys = [];
  165. $(document).keydown(function (e) {
  166. keys[e.which] = true;
  167. });
  168. $(document).keyup(function (e) {
  169. delete keys[e.which];
  170. });
  171. this.keyActive = function(key) {
  172. return keys[key] == true;
  173. }
  174. });