services.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 event only if the sender 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. /*
  60. Service: fields
  61. Provides a global list of all seen fields for use in editor panels
  62. */
  63. .factory('fields', function($rootScope) {
  64. var fields = {
  65. list : []
  66. }
  67. $rootScope.$on('fields', function(event,f) {
  68. fields.list = _.union(f.data.all,fields.list)
  69. })
  70. return fields;
  71. })
  72. .service('kbnIndex',function($http) {
  73. // returns a promise containing an array of all indices matching the index
  74. // pattern that exist in a given range
  75. this.indices = function(from,to,pattern,interval) {
  76. var possible = [];
  77. _.each(expand_range(fake_utc(from),fake_utc(to),interval),function(d){
  78. possible.push(d.format(pattern));
  79. });
  80. return all_indices().then(function(p) {
  81. var indices = _.intersection(possible,p);
  82. indices.reverse();
  83. return indices
  84. })
  85. };
  86. // returns a promise containing an array of all indices in an elasticsearch
  87. // cluster
  88. function all_indices() {
  89. var something = $http({
  90. url: config.elasticsearch + "/_aliases",
  91. method: "GET"
  92. }).error(function(data, status, headers, config) {
  93. // Handle error condition somehow?
  94. });
  95. return something.then(function(p) {
  96. var indices = [];
  97. _.each(p.data, function(v,k) {
  98. indices.push(k)
  99. });
  100. return indices;
  101. });
  102. }
  103. // this is stupid, but there is otherwise no good way to ensure that when
  104. // I extract the date from an object that I get the UTC date. Stupid js.
  105. // I die a little inside every time I call this function.
  106. // Update: I just read this again. I died a little more inside.
  107. // Update2: More death.
  108. function fake_utc(date) {
  109. date = moment(date).clone().toDate()
  110. return moment(new Date(date.getTime() + date.getTimezoneOffset() * 60000));
  111. }
  112. // Create an array of date objects by a given interval
  113. function expand_range(start, end, interval) {
  114. if(_.contains(['hour','day','week','month','year'],interval)) {
  115. var range;
  116. start = moment(start).clone();
  117. range = [];
  118. while (start.isBefore(end)) {
  119. range.push(start.clone());
  120. switch (interval) {
  121. case 'hour':
  122. start.add('hours',1)
  123. break
  124. case 'day':
  125. start.add('days',1)
  126. break
  127. case 'week':
  128. start.add('weeks',1)
  129. break
  130. case 'month':
  131. start.add('months',1)
  132. break
  133. case 'year':
  134. start.add('years',1)
  135. break
  136. }
  137. }
  138. range.push(moment(end).clone());
  139. return range;
  140. } else {
  141. return false;
  142. }
  143. }
  144. })
  145. .service('timer', function($timeout) {
  146. // This service really just tracks a list of $timeout promises to give us a
  147. // method for cancelling them all when we need to
  148. var timers = [];
  149. this.register = function(promise) {
  150. timers.push(promise);
  151. return promise;
  152. }
  153. this.cancel = function(promise) {
  154. timers = _.without(timers,promise)
  155. $timeout.cancel(promise)
  156. }
  157. this.cancel_all = function() {
  158. _.each(timers, function(t){
  159. $timeout.cancel(t);
  160. });
  161. timers = new Array();
  162. }
  163. })
  164. .service('query', function(dashboard) {
  165. // Create an object to hold our service state on the dashboard
  166. dashboard.current.services.query = dashboard.current.services.query || {};
  167. _.defaults(dashboard.current.services.query,{
  168. idQueue : [],
  169. list : {},
  170. ids : [],
  171. });
  172. // For convenience
  173. var _q = dashboard.current.services.query;
  174. this.colors = [
  175. "#7EB26D","#EAB839","#6ED0E0","#EF843C","#E24D42","#1F78C1","#BA43A9","#705DA0", //1
  176. "#508642","#CCA300","#447EBC","#C15C17","#890F02","#0A437C","#6D1F62","#584477", //2
  177. "#B7DBAB","#F4D598","#70DBED","#F9BA8F","#F29191","#82B5D8","#E5A8E2","#AEA2E0", //3
  178. "#629E51","#E5AC0E","#64B0C8","#E0752D","#BF1B00","#0A50A1","#962D82","#614D93", //4
  179. "#9AC48A","#F2C96D","#65C5DB","#F9934E","#EA6460","#5195CE","#D683CE","#806EB7", //5
  180. "#3F6833","#967302","#2F575E","#99440A","#58140C","#052B51","#511749","#3F2B5B", //6
  181. "#E0F9D7","#FCEACA","#CFFAFF","#F9E2D2","#FCE2DE","#BADFF4","#F9D9F9","#DEDAF7" //7
  182. ];
  183. // Save a reference to this
  184. this.list = dashboard.current.services.query.list;
  185. this.ids = dashboard.current.services.query.ids;
  186. var self = this;
  187. var init = function() {
  188. if (self.ids.length == 0) {
  189. self.set({});
  190. }
  191. }
  192. // This is used both for adding queries and modifying them. If an id is passed, the query at that id is updated
  193. this.set = function(query,id) {
  194. if(!_.isUndefined(id)) {
  195. if(!_.isUndefined(self.list[id])) {
  196. _.extend(self.list[id],query);
  197. return id;
  198. } else {
  199. return false;
  200. }
  201. } else {
  202. var _id = nextId();
  203. var _query = {
  204. query: '*',
  205. alias: '',
  206. color: colorAt(_id)
  207. }
  208. _.defaults(query,_query)
  209. self.list[_id] = query;
  210. self.ids.push(_id)
  211. return id;
  212. }
  213. }
  214. this.remove = function(id) {
  215. if(!_.isUndefined(self.list[id])) {
  216. delete self.list[id];
  217. // This must happen on the full path also since _.without returns a copy
  218. self.ids = dashboard.current.services.query.ids = _.without(self.ids,id)
  219. _q.idQueue.unshift(id)
  220. _q.idQueue.sort(function(a,b){return a-b});
  221. return true;
  222. } else {
  223. return false;
  224. }
  225. }
  226. var nextId = function() {
  227. if(_q.idQueue.length > 0) {
  228. return _q.idQueue.shift()
  229. } else {
  230. return self.ids.length;
  231. }
  232. }
  233. var colorAt = function(id) {
  234. return self.colors[id % self.colors.length]
  235. }
  236. init();
  237. })
  238. .service('dashboard', function($routeParams, $http, $rootScope, ejsResource, timer) {
  239. // A hash of defaults to use when loading a dashboard
  240. var _dash = {
  241. title: "",
  242. editable: true,
  243. rows: [],
  244. services: {}
  245. };
  246. // An elasticJS client to use
  247. var ejs = ejsResource(config.elasticsearch);
  248. var gist_pattern = /(^\d{5,}$)|(^[a-z0-9]{10,}$)|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  249. // Empty dashboard object
  250. this.current = {};
  251. this.last = {};
  252. // Store a reference to this
  253. var self = this;
  254. $rootScope.$on('$routeChangeSuccess',function(){
  255. route();
  256. })
  257. var route = function() {
  258. // Is there a dashboard type and id in the URL?
  259. if(!(_.isUndefined($routeParams.type)) && !(_.isUndefined($routeParams.id))) {
  260. var _type = $routeParams.type;
  261. var _id = $routeParams.id;
  262. if(_type === 'elasticsearch')
  263. self.elasticsearch_load('dashboard',_id)
  264. if(_type === 'temp')
  265. self.elasticsearch_load('temp',_id)
  266. if(_type === 'file')
  267. self.file_load(_id)
  268. // No dashboard in the URL
  269. } else {
  270. // Check if browser supports localstorage, and if there's a dashboard
  271. if (Modernizr.localstorage &&
  272. !(_.isUndefined(localStorage['dashboard'])) &&
  273. localStorage['dashboard'] !== ''
  274. ) {
  275. var dashboard = JSON.parse(localStorage['dashboard']);
  276. _.defaults(dashboard,_dash);
  277. self.dash_load(dashboard)
  278. // No? Ok, grab default.json, its all we have now
  279. } else {
  280. self.file_load('default')
  281. }
  282. }
  283. }
  284. this.to_file = function() {
  285. var blob = new Blob([angular.toJson(self.current,true)], {type: "application/json;charset=utf-8"});
  286. // from filesaver.js
  287. saveAs(blob, self.current.title+"-"+new Date().getTime());
  288. return true;
  289. }
  290. this.set_default = function(dashboard) {
  291. if (Modernizr.localstorage) {
  292. localStorage['dashboard'] = angular.toJson(dashboard || self.current);
  293. return true;
  294. } else {
  295. return false;
  296. }
  297. }
  298. this.purge_default = function() {
  299. if (Modernizr.localstorage) {
  300. localStorage['dashboard'] = '';
  301. return true;
  302. } else {
  303. return false;
  304. }
  305. }
  306. // TOFIX: Pretty sure this breaks when you're on a saved dashboard already
  307. this.share_link = function(title,type,id) {
  308. return {
  309. location : location.href.replace(location.hash,""),
  310. type : type,
  311. id : id,
  312. link : location.href.replace(location.hash,"")+"#dashboard/"+type+"/"+id,
  313. title : title
  314. };
  315. }
  316. this.file_load = function(file) {
  317. return $http({
  318. url: "dashboards/"+file,
  319. method: "GET",
  320. }).then(function(result) {
  321. var _dashboard = result.data
  322. _.defaults(_dashboard,_dash);
  323. self.dash_load(_dashboard);
  324. return true;
  325. },function(result) {
  326. return false;
  327. });
  328. }
  329. this.elasticsearch_load = function(type,id) {
  330. var request = ejs.Request().indices(config.kibana_index).types(type);
  331. var results = request.query(
  332. ejs.IdsQuery(id)
  333. ).doSearch();
  334. return results.then(function(results) {
  335. if(_.isUndefined(results)) {
  336. return false;
  337. } else {
  338. self.dash_load(angular.fromJson(results.hits.hits[0]['_source']['dashboard']))
  339. return true;
  340. }
  341. });
  342. }
  343. this.elasticsearch_save = function(type,title,ttl) {
  344. // Clone object so we can modify it without influencing the existing obejct
  345. var save = _.clone(self.current)
  346. // Change title on object clone
  347. if (type === 'dashboard') {
  348. var id = save.title = _.isUndefined(title) ? self.current.title : title;
  349. }
  350. // Create request with id as title. Rethink this.
  351. var request = ejs.Document(config.kibana_index,type,id).source({
  352. user: 'guest',
  353. group: 'guest',
  354. title: save.title,
  355. dashboard: angular.toJson(save)
  356. })
  357. if (type === 'temp')
  358. request = request.ttl(ttl)
  359. // TOFIX: Implement error handling here
  360. return request.doIndex(
  361. // Success
  362. function(result) {
  363. return result;
  364. },
  365. // Failure
  366. function(result) {
  367. return false;
  368. }
  369. );
  370. }
  371. this.elasticsearch_delete = function(id) {
  372. return ejs.Document(config.kibana_index,'dashboard',id).doDelete(
  373. // Success
  374. function(result) {
  375. return result;
  376. },
  377. // Failure
  378. function(result) {
  379. return false;
  380. }
  381. );
  382. }
  383. this.elasticsearch_list = function(query,count) {
  384. var request = ejs.Request().indices(config.kibana_index).types('dashboard');
  385. return request.query(
  386. ejs.QueryStringQuery(query || '*')
  387. ).size(count).doSearch(
  388. // Success
  389. function(result) {
  390. return result;
  391. },
  392. // Failure
  393. function(result) {
  394. return false;
  395. }
  396. );
  397. }
  398. // TOFIX: Gist functionality
  399. this.save_gist = function(title,dashboard) {
  400. var save = _.clone(dashboard || self.current)
  401. save.title = title || self.current.title;
  402. return $http({
  403. url: "https://api.github.com/gists",
  404. method: "POST",
  405. data: {
  406. "description": save.title,
  407. "public": false,
  408. "files": {
  409. "kibana-dashboard.json": {
  410. "content": angular.toJson(save,true)
  411. }
  412. }
  413. }
  414. }).then(function(data, status, headers, config) {
  415. return data.data.html_url;
  416. }, function(data, status, headers, config) {
  417. return false;
  418. });
  419. }
  420. this.gist_list = function(id) {
  421. return $http.jsonp("https://api.github.com/gists/"+id+"?callback=JSON_CALLBACK"
  422. ).then(function(response) {
  423. var files = []
  424. _.each(response.data.data.files,function(v,k) {
  425. try {
  426. var file = JSON.parse(v.content)
  427. files.push(file)
  428. } catch(e) {
  429. // Nothing?
  430. }
  431. });
  432. return files;
  433. }, function(data, status, headers, config) {
  434. return false;
  435. });
  436. }
  437. this.dash_load = function(dashboard) {
  438. self.current = dashboard;
  439. timer.cancel_all();
  440. return true;
  441. }
  442. this.gist_id = function(string) {
  443. if(self.is_gist(string))
  444. return string.match(gist_pattern)[0].replace(/.*\//, '');
  445. }
  446. this.is_gist = function(string) {
  447. if(!_.isUndefined(string) && string != '' && !_.isNull(string.match(gist_pattern)))
  448. return string.match(gist_pattern).length > 0 ? true : false;
  449. else
  450. return false
  451. }
  452. })
  453. .service('keylistener', function($rootScope) {
  454. var keys = [];
  455. $(document).keydown(function (e) {
  456. keys[e.which] = true;
  457. });
  458. $(document).keyup(function (e) {
  459. delete keys[e.which];
  460. });
  461. this.keyActive = function(key) {
  462. return keys[key] == true;
  463. }
  464. });