services.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*global Blob:false*/
  4. 'use strict';
  5. angular.module('kibana.services', [])
  6. .service('eventBus', function($rootScope) {
  7. // An array of registed types
  8. var _types = [];
  9. this.broadcast = function(from,to,type,data) {
  10. if(_.isUndefined(data)) {
  11. data = from;
  12. }
  13. var packet = {
  14. time: new Date(),
  15. type: type,
  16. from: from,
  17. to: to,
  18. data: data
  19. };
  20. if(_.contains(_types,'$kibana_debug')) {
  21. $rootScope.$broadcast('$kibana_debug',packet);
  22. }
  23. $rootScope.$broadcast(type,{
  24. from: from,
  25. to: to,
  26. data: data
  27. });
  28. };
  29. // This sets up an $on listener that checks to see if the event (packet) is
  30. // addressed to the scope in question and runs the registered function if it
  31. // is.
  32. this.register = function(scope,type,fn) {
  33. _types = _.union(_types,[type]);
  34. scope.$on(type,function(event,packet){
  35. var _id = scope.$id;
  36. var _to = packet.to;
  37. var _from = packet.from;
  38. var _type = packet.type;
  39. var _time = packet.time;
  40. var _group = (!(_.isUndefined(scope.panel))) ? scope.panel.group : ["NONE"];
  41. if(!(_.isArray(_to))) {
  42. _to = [_to];
  43. }
  44. if(!(_.isArray(_group))) {
  45. _group = [_group];
  46. }
  47. // Transmit event only if the sender is not the receiver AND one of the following:
  48. // 1) Receiver has group in _to 2) Receiver's $id is in _to
  49. // 3) Event is addressed to ALL 4) Receiver is in ALL group
  50. if((_.intersection(_to,_group).length > 0 ||
  51. _.indexOf(_to,_id) > -1 ||
  52. _.indexOf(_group,'ALL') > -1 ||
  53. _.indexOf(_to,'ALL') > -1) &&
  54. _from !== _id
  55. ) {
  56. fn(event,packet.data,{time:_time,to:_to,from:_from,type:_type});
  57. }
  58. });
  59. };
  60. })
  61. /*
  62. Service: fields
  63. Provides a global list of all seen fields for use in editor panels
  64. */
  65. .factory('fields', function($rootScope) {
  66. var fields = {
  67. list : []
  68. };
  69. $rootScope.$on('fields', function(event,f) {
  70. fields.list = _.union(f.data.all,fields.list);
  71. });
  72. return fields;
  73. })
  74. .service('kbnIndex',function($http) {
  75. // returns a promise containing an array of all indices matching the index
  76. // pattern that exist in a given range
  77. this.indices = function(from,to,pattern,interval) {
  78. var possible = [];
  79. _.each(expand_range(fake_utc(from),fake_utc(to),interval),function(d){
  80. possible.push(d.format(pattern));
  81. });
  82. return all_indices().then(function(p) {
  83. var indices = _.intersection(possible,p);
  84. indices.reverse();
  85. return indices;
  86. });
  87. };
  88. // returns a promise containing an array of all indices in an elasticsearch
  89. // cluster
  90. function all_indices() {
  91. var something = $http({
  92. url: config.elasticsearch + "/_aliases",
  93. method: "GET"
  94. }).error(function(data, status, headers, config) {
  95. // Handle error condition somehow?
  96. });
  97. return something.then(function(p) {
  98. var indices = [];
  99. _.each(p.data, function(v,k) {
  100. indices.push(k);
  101. // Also add the aliases. Could be expensive on systems with a lot of them
  102. _.each(v.aliases, function(v, k) {
  103. indices.push(k);
  104. });
  105. });
  106. return indices;
  107. });
  108. }
  109. // this is stupid, but there is otherwise no good way to ensure that when
  110. // I extract the date from an object that I get the UTC date. Stupid js.
  111. // I die a little inside every time I call this function.
  112. // Update: I just read this again. I died a little more inside.
  113. // Update2: More death.
  114. function fake_utc(date) {
  115. date = moment(date).clone().toDate();
  116. return moment(new Date(date.getTime() + date.getTimezoneOffset() * 60000));
  117. }
  118. // Create an array of date objects by a given interval
  119. function expand_range(start, end, interval) {
  120. if(_.contains(['hour','day','week','month','year'],interval)) {
  121. var range;
  122. start = moment(start).clone();
  123. range = [];
  124. while (start.isBefore(end)) {
  125. range.push(start.clone());
  126. switch (interval) {
  127. case 'hour':
  128. start.add('hours',1);
  129. break;
  130. case 'day':
  131. start.add('days',1);
  132. break;
  133. case 'week':
  134. start.add('weeks',1);
  135. break;
  136. case 'month':
  137. start.add('months',1);
  138. break;
  139. case 'year':
  140. start.add('years',1);
  141. break;
  142. }
  143. }
  144. range.push(moment(end).clone());
  145. return range;
  146. } else {
  147. return false;
  148. }
  149. }
  150. })
  151. .service('timer', function($timeout) {
  152. // This service really just tracks a list of $timeout promises to give us a
  153. // method for cancelling them all when we need to
  154. var timers = [];
  155. this.register = function(promise) {
  156. timers.push(promise);
  157. return promise;
  158. };
  159. this.cancel = function(promise) {
  160. timers = _.without(timers,promise);
  161. $timeout.cancel(promise);
  162. };
  163. this.cancel_all = function() {
  164. _.each(timers, function(t){
  165. $timeout.cancel(t);
  166. });
  167. timers = [];
  168. };
  169. })
  170. .service('querySrv', function(dashboard, ejsResource) {
  171. // Create an object to hold our service state on the dashboard
  172. dashboard.current.services.query = dashboard.current.services.query || {};
  173. _.defaults(dashboard.current.services.query,{
  174. idQueue : [],
  175. list : {},
  176. ids : [],
  177. });
  178. // For convenience
  179. var ejs = ejsResource(config.elasticsearch);
  180. var _q = dashboard.current.services.query;
  181. this.colors = [
  182. "#7EB26D","#EAB839","#6ED0E0","#EF843C","#E24D42","#1F78C1","#BA43A9","#705DA0", //1
  183. "#508642","#CCA300","#447EBC","#C15C17","#890F02","#0A437C","#6D1F62","#584477", //2
  184. "#B7DBAB","#F4D598","#70DBED","#F9BA8F","#F29191","#82B5D8","#E5A8E2","#AEA2E0", //3
  185. "#629E51","#E5AC0E","#64B0C8","#E0752D","#BF1B00","#0A50A1","#962D82","#614D93", //4
  186. "#9AC48A","#F2C96D","#65C5DB","#F9934E","#EA6460","#5195CE","#D683CE","#806EB7", //5
  187. "#3F6833","#967302","#2F575E","#99440A","#58140C","#052B51","#511749","#3F2B5B", //6
  188. "#E0F9D7","#FCEACA","#CFFAFF","#F9E2D2","#FCE2DE","#BADFF4","#F9D9F9","#DEDAF7" //7
  189. ];
  190. // Save a reference to this
  191. var self = this;
  192. this.init = function() {
  193. _q = dashboard.current.services.query;
  194. self.list = dashboard.current.services.query.list;
  195. self.ids = dashboard.current.services.query.ids;
  196. if (self.ids.length === 0) {
  197. self.set({});
  198. }
  199. };
  200. // This is used both for adding queries and modifying them. If an id is passed, the query at that id is updated
  201. this.set = function(query,id) {
  202. if(!_.isUndefined(id)) {
  203. if(!_.isUndefined(self.list[id])) {
  204. _.extend(self.list[id],query);
  205. return id;
  206. } else {
  207. return false;
  208. }
  209. } else {
  210. var _id = nextId();
  211. var _query = {
  212. query: '*',
  213. alias: '',
  214. color: colorAt(_id),
  215. pin: false,
  216. id: _id,
  217. type: 'lucene'
  218. };
  219. _.defaults(query,_query);
  220. self.list[_id] = query;
  221. self.ids.push(_id);
  222. return _id;
  223. }
  224. };
  225. this.remove = function(id) {
  226. if(!_.isUndefined(self.list[id])) {
  227. delete self.list[id];
  228. // This must happen on the full path also since _.without returns a copy
  229. self.ids = dashboard.current.services.query.ids = _.without(self.ids,id);
  230. _q.idQueue.unshift(id);
  231. _q.idQueue.sort(function(v,k){
  232. return v-k;
  233. });
  234. return true;
  235. } else {
  236. return false;
  237. }
  238. };
  239. this.getEjsObj = function(id) {
  240. return self.toEjsObj(self.list[id]);
  241. };
  242. this.toEjsObj = function (q) {
  243. switch(q.type)
  244. {
  245. case 'lucene':
  246. return ejs.QueryStringQuery(q.query || '*');
  247. default:
  248. return _.isUndefined(q.query) ? false : ejs.QueryStringQuery(q.query || '*');
  249. }
  250. };
  251. this.findQuery = function(queryString) {
  252. return _.findWhere(self.list,{query:queryString});
  253. };
  254. var nextId = function() {
  255. if(_q.idQueue.length > 0) {
  256. return _q.idQueue.shift();
  257. } else {
  258. return self.ids.length;
  259. }
  260. };
  261. var colorAt = function(id) {
  262. return self.colors[id % self.colors.length];
  263. };
  264. self.init();
  265. })
  266. .service('filterSrv', function(dashboard, ejsResource) {
  267. // Create an object to hold our service state on the dashboard
  268. dashboard.current.services.filter = dashboard.current.services.filter || {};
  269. _.defaults(dashboard.current.services.filter,{
  270. idQueue : [],
  271. list : {},
  272. ids : []
  273. });
  274. // For convenience
  275. var ejs = ejsResource(config.elasticsearch);
  276. var _f = dashboard.current.services.filter;
  277. // Save a reference to this
  278. var self = this;
  279. // Call this whenever we need to reload the important stuff
  280. this.init = function() {
  281. // Accessors
  282. self.list = dashboard.current.services.filter.list;
  283. self.ids = dashboard.current.services.filter.ids;
  284. _f = dashboard.current.services.filter;
  285. _.each(self.getByType('time',true),function(time) {
  286. self.list[time.id].from = new Date(time.from);
  287. self.list[time.id].to = new Date(time.to);
  288. });
  289. };
  290. // This is used both for adding filters and modifying them.
  291. // If an id is passed, the filter at that id is updated
  292. this.set = function(filter,id) {
  293. _.defaults(filter,{mandate:'must'});
  294. filter.active = true;
  295. if(!_.isUndefined(id)) {
  296. if(!_.isUndefined(self.list[id])) {
  297. _.extend(self.list[id],filter);
  298. return id;
  299. } else {
  300. return false;
  301. }
  302. } else {
  303. if(_.isUndefined(filter.type)) {
  304. return false;
  305. } else {
  306. var _id = nextId();
  307. var _filter = {
  308. alias: '',
  309. id: _id
  310. };
  311. _.defaults(filter,_filter);
  312. self.list[_id] = filter;
  313. self.ids.push(_id);
  314. return _id;
  315. }
  316. }
  317. };
  318. this.getBoolFilter = function(ids) {
  319. // A default match all filter, just in case there are no other filters
  320. var bool = ejs.BoolFilter().must(ejs.MatchAllFilter());
  321. var either_bool = ejs.BoolFilter().must(ejs.MatchAllFilter());
  322. _.each(ids,function(id) {
  323. if(self.list[id].active) {
  324. switch(self.list[id].mandate)
  325. {
  326. case 'mustNot':
  327. bool = bool.mustNot(self.getEjsObj(id));
  328. break;
  329. case 'either':
  330. either_bool = either_bool.should(self.getEjsObj(id));
  331. break;
  332. default:
  333. bool = bool.must(self.getEjsObj(id));
  334. }
  335. }
  336. });
  337. return bool.must(either_bool);
  338. };
  339. this.getEjsObj = function(id) {
  340. return self.toEjsObj(self.list[id]);
  341. };
  342. this.toEjsObj = function (filter) {
  343. if(!filter.active) {
  344. return false;
  345. }
  346. switch(filter.type)
  347. {
  348. case 'time':
  349. return ejs.RangeFilter(filter.field)
  350. .from(filter.from)
  351. .to(filter.to);
  352. case 'range':
  353. return ejs.RangeFilter(filter.field)
  354. .from(filter.from)
  355. .to(filter.to);
  356. case 'querystring':
  357. return ejs.QueryFilter(ejs.QueryStringQuery(filter.query)).cache(true);
  358. case 'terms':
  359. return ejs.TermsFilter(filter.field,filter.value);
  360. case 'exists':
  361. return ejs.ExistsFilter(filter.field);
  362. case 'missing':
  363. return ejs.MissingFilter(filter.field);
  364. default:
  365. return false;
  366. }
  367. };
  368. this.getByType = function(type,inactive) {
  369. return _.pick(self.list,self.idsByType(type,inactive));
  370. };
  371. this.removeByType = function(type) {
  372. var ids = self.idsByType(type);
  373. _.each(ids,function(id) {
  374. self.remove(id);
  375. });
  376. return ids;
  377. };
  378. this.idsByType = function(type,inactive) {
  379. var _require = inactive ? {type:type} : {type:type,active:true};
  380. return _.pluck(_.where(self.list,_require),'id');
  381. };
  382. // This special function looks for all time filters, and returns a time range according to the mode
  383. this.timeRange = function(mode) {
  384. var _t = _.where(self.list,{type:'time',active:true});
  385. if(_t.length === 0) {
  386. return false;
  387. }
  388. switch(mode) {
  389. case "min":
  390. return {
  391. from: new Date(_.max(_.pluck(_t,'from'))),
  392. to: new Date(_.min(_.pluck(_t,'to')))
  393. };
  394. case "max":
  395. return {
  396. from: new Date(_.min(_.pluck(_t,'from'))),
  397. to: new Date(_.max(_.pluck(_t,'to')))
  398. };
  399. default:
  400. return false;
  401. }
  402. };
  403. this.remove = function(id) {
  404. if(!_.isUndefined(self.list[id])) {
  405. delete self.list[id];
  406. // This must happen on the full path also since _.without returns a copy
  407. self.ids = dashboard.current.services.filter.ids = _.without(self.ids,id);
  408. _f.idQueue.unshift(id);
  409. _f.idQueue.sort(function(v,k){return v-k;});
  410. return true;
  411. } else {
  412. return false;
  413. }
  414. };
  415. var nextId = function() {
  416. if(_f.idQueue.length > 0) {
  417. return _f.idQueue.shift();
  418. } else {
  419. return self.ids.length;
  420. }
  421. };
  422. // Now init
  423. self.init();
  424. })
  425. .service('dashboard', function($routeParams, $http, $rootScope, $injector, ejsResource, timer, kbnIndex) {
  426. // A hash of defaults to use when loading a dashboard
  427. var _dash = {
  428. title: "",
  429. editable: true,
  430. rows: [],
  431. services: {},
  432. index: {
  433. interval: 'none',
  434. pattern: '_all',
  435. default: '_all'
  436. },
  437. };
  438. // An elasticJS client to use
  439. var ejs = ejsResource(config.elasticsearch);
  440. var gist_pattern = /(^\d{5,}$)|(^[a-z0-9]{10,}$)|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  441. // Store a reference to this
  442. var self = this;
  443. var filterSrv,querySrv;
  444. this.current = {};
  445. this.last = {};
  446. $rootScope.$on('$routeChangeSuccess',function(){
  447. // Clear the current dashboard to prevent reloading
  448. self.current = {};
  449. self.indices = [];
  450. route();
  451. });
  452. var route = function() {
  453. // Is there a dashboard type and id in the URL?
  454. if(!(_.isUndefined($routeParams.type)) && !(_.isUndefined($routeParams.id))) {
  455. var _type = $routeParams.type;
  456. var _id = $routeParams.id;
  457. switch(_type) {
  458. case ('elasticsearch'):
  459. self.elasticsearch_load('dashboard',_id);
  460. break;
  461. case ('temp'):
  462. self.elasticsearch_load('temp',_id);
  463. break;
  464. case ('file'):
  465. self.file_load(_id);
  466. break;
  467. default:
  468. self.file_load('default.json');
  469. }
  470. // No dashboard in the URL
  471. } else {
  472. // Check if browser supports localstorage, and if there's a dashboard
  473. if (window.Modernizr.localstorage &&
  474. !(_.isUndefined(window.localStorage['dashboard'])) &&
  475. window.localStorage['dashboard'] !== ''
  476. ) {
  477. var dashboard = JSON.parse(window.localStorage['dashboard']);
  478. _.defaults(dashboard,_dash);
  479. self.dash_load(dashboard);
  480. // No? Ok, grab default.json, its all we have now
  481. } else {
  482. self.file_load('default.json');
  483. }
  484. }
  485. };
  486. // Since the dashboard is responsible for index computation, we can compute and assign the indices
  487. // here before telling the panels to refresh
  488. this.refresh = function() {
  489. if(self.current.index.interval !== 'none') {
  490. if(filterSrv.idsByType('time').length > 0) {
  491. var _range = filterSrv.timeRange('min');
  492. kbnIndex.indices(_range.from,_range.to,
  493. self.current.index.pattern,self.current.index.interval
  494. ).then(function (p) {
  495. if(p.length > 0) {
  496. self.indices = p;
  497. } else {
  498. self.indices = [self.current.index.default];
  499. }
  500. $rootScope.$broadcast('refresh');
  501. });
  502. } else {
  503. // This is not optimal, we should be getting the entire index list here, or at least every
  504. // index that possibly matches the pattern
  505. self.indices = [self.current.index.default];
  506. $rootScope.$broadcast('refresh');
  507. }
  508. } else {
  509. self.indices = [self.current.index.default];
  510. $rootScope.$broadcast('refresh');
  511. }
  512. };
  513. this.dash_load = function(dashboard) {
  514. // Cancel all timers
  515. timer.cancel_all();
  516. // If not using time based indices, use the default index
  517. if(dashboard.index.interval === 'none') {
  518. self.indices = [dashboard.index.default];
  519. }
  520. self.current = _.clone(dashboard);
  521. // Ok, now that we've setup the current dashboard, we can inject our services
  522. querySrv = $injector.get('querySrv');
  523. filterSrv = $injector.get('filterSrv');
  524. // Make sure these re-init
  525. querySrv.init();
  526. filterSrv.init();
  527. if(dashboard.index.interval !== 'none' && filterSrv.idsByType('time').length === 0) {
  528. self.refresh();
  529. }
  530. return true;
  531. };
  532. this.gist_id = function(string) {
  533. if(self.is_gist(string)) {
  534. return string.match(gist_pattern)[0].replace(/.*\//, '');
  535. }
  536. };
  537. this.is_gist = function(string) {
  538. if(!_.isUndefined(string) && string !== '' && !_.isNull(string.match(gist_pattern))) {
  539. return string.match(gist_pattern).length > 0 ? true : false;
  540. } else {
  541. return false;
  542. }
  543. };
  544. this.to_file = function() {
  545. var blob = new Blob([angular.toJson(self.current,true)], {type: "application/json;charset=utf-8"});
  546. // from filesaver.js
  547. window.saveAs(blob, self.current.title+"-"+new Date().getTime());
  548. return true;
  549. };
  550. this.set_default = function(dashboard) {
  551. if (window.Modernizr.localstorage) {
  552. window.localStorage['dashboard'] = angular.toJson(dashboard || self.current);
  553. return true;
  554. } else {
  555. return false;
  556. }
  557. };
  558. this.purge_default = function() {
  559. if (window.Modernizr.localstorage) {
  560. window.localStorage['dashboard'] = '';
  561. return true;
  562. } else {
  563. return false;
  564. }
  565. };
  566. // TOFIX: Pretty sure this breaks when you're on a saved dashboard already
  567. this.share_link = function(title,type,id) {
  568. return {
  569. location : window.location.href.replace(window.location.hash,""),
  570. type : type,
  571. id : id,
  572. link : window.location.href.replace(window.location.hash,"")+"#dashboard/"+type+"/"+id,
  573. title : title
  574. };
  575. };
  576. this.file_load = function(file) {
  577. return $http({
  578. url: "dashboards/"+file,
  579. method: "GET",
  580. }).then(function(result) {
  581. var _dashboard = result.data;
  582. _.defaults(_dashboard,_dash);
  583. self.dash_load(_dashboard);
  584. return true;
  585. },function(result) {
  586. return false;
  587. });
  588. };
  589. this.elasticsearch_load = function(type,id) {
  590. var request = ejs.Request().indices(config.kibana_index).types(type);
  591. var results = request.query(
  592. ejs.IdsQuery(id)
  593. ).doSearch();
  594. return results.then(function(results) {
  595. if(_.isUndefined(results)) {
  596. return false;
  597. } else {
  598. self.dash_load(angular.fromJson(results.hits.hits[0]['_source']['dashboard']));
  599. return true;
  600. }
  601. });
  602. };
  603. this.elasticsearch_save = function(type,title,ttl) {
  604. // Clone object so we can modify it without influencing the existing obejct
  605. var save = _.clone(self.current);
  606. var id;
  607. // Change title on object clone
  608. if (type === 'dashboard') {
  609. id = save.title = _.isUndefined(title) ? self.current.title : title;
  610. }
  611. // Create request with id as title. Rethink this.
  612. var request = ejs.Document(config.kibana_index,type,id).source({
  613. user: 'guest',
  614. group: 'guest',
  615. title: save.title,
  616. dashboard: angular.toJson(save)
  617. });
  618. request = type === 'temp' ? request.ttl(ttl) : request;
  619. // TOFIX: Implement error handling here
  620. return request.doIndex(
  621. // Success
  622. function(result) {
  623. return result;
  624. },
  625. // Failure
  626. function(result) {
  627. return false;
  628. }
  629. );
  630. };
  631. this.elasticsearch_delete = function(id) {
  632. return ejs.Document(config.kibana_index,'dashboard',id).doDelete(
  633. // Success
  634. function(result) {
  635. return result;
  636. },
  637. // Failure
  638. function(result) {
  639. return false;
  640. }
  641. );
  642. };
  643. this.elasticsearch_list = function(query,count) {
  644. var request = ejs.Request().indices(config.kibana_index).types('dashboard');
  645. return request.query(
  646. ejs.QueryStringQuery(query || '*')
  647. ).size(count).doSearch(
  648. // Success
  649. function(result) {
  650. return result;
  651. },
  652. // Failure
  653. function(result) {
  654. return false;
  655. }
  656. );
  657. };
  658. // TOFIX: Gist functionality
  659. this.save_gist = function(title,dashboard) {
  660. var save = _.clone(dashboard || self.current);
  661. save.title = title || self.current.title;
  662. return $http({
  663. url: "https://api.github.com/gists",
  664. method: "POST",
  665. data: {
  666. "description": save.title,
  667. "public": false,
  668. "files": {
  669. "kibana-dashboard.json": {
  670. "content": angular.toJson(save,true)
  671. }
  672. }
  673. }
  674. }).then(function(data, status, headers, config) {
  675. return data.data.html_url;
  676. }, function(data, status, headers, config) {
  677. return false;
  678. });
  679. };
  680. this.gist_list = function(id) {
  681. return $http.jsonp("https://api.github.com/gists/"+id+"?callback=JSON_CALLBACK"
  682. ).then(function(response) {
  683. var files = [];
  684. _.each(response.data.data.files,function(v,k) {
  685. try {
  686. var file = JSON.parse(v.content);
  687. files.push(file);
  688. } catch(e) {
  689. // Nothing?
  690. }
  691. });
  692. return files;
  693. }, function(data, status, headers, config) {
  694. return false;
  695. });
  696. };
  697. });