module.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. angular.module('kibana.dashcontrol', [])
  2. .controller('dashcontrol', function($scope, $routeParams, $http, eventBus, timer) {
  3. $scope.panel = $scope.panel || {};
  4. // Set and populate defaults
  5. var _d = {
  6. group : "default",
  7. save : {
  8. gist: false,
  9. elasticsearch: true,
  10. local: true,
  11. 'default': true
  12. },
  13. load : {
  14. gist: true,
  15. elasticsearch: true,
  16. local: true,
  17. },
  18. elasticsearch_size: 20,
  19. elasticsearch_saveto: $scope.config.kibana_index,
  20. temp_ttl: '30d',
  21. }
  22. _.defaults($scope.panel,_d);
  23. $scope.init = function() {
  24. // A hash of defaults for the dashboard object
  25. var _dash = {
  26. title: "",
  27. editable: true,
  28. rows: [],
  29. }
  30. // Long ugly if statement for figuring out which dashboard to load on init
  31. // If there is no dashboard defined, find one
  32. if(_.isUndefined($scope.dashboards)) {
  33. // First check the URL for a path to a dashboard
  34. if(!(_.isUndefined($routeParams.type)) && !(_.isUndefined($routeParams.id))) {
  35. var _type = $routeParams.type;
  36. var _id = $routeParams.id;
  37. if(_type === 'elasticsearch')
  38. $scope.elasticsearch_load('dashboard',_id)
  39. if(_type === 'temp')
  40. $scope.elasticsearch_load('temp',_id)
  41. // No dashboard in the URL
  42. } else {
  43. // Check if browser supports localstorage, and if there's a dashboard
  44. if (Modernizr.localstorage &&
  45. !(_.isUndefined(localStorage['dashboard'])) &&
  46. localStorage['dashboard'] !== ''
  47. ) {
  48. var dashboard = JSON.parse(localStorage['dashboard']);
  49. _.defaults(dashboard,_dash);
  50. $scope.dash_load(JSON.stringify(dashboard))
  51. // No? Ok, grab default.json, its all we have now
  52. } else {
  53. $http({
  54. url: "default.json",
  55. method: "GET",
  56. }).success(function(data, status, headers, config) {
  57. var dashboard = data
  58. _.defaults(dashboard,_dash);
  59. $scope.dash_load(JSON.stringify(dashboard))
  60. }).error(function(data, status, headers, config) {
  61. $scope.alert('Default dashboard missing!','Could not locate default.json','error')
  62. });
  63. }
  64. }
  65. }
  66. $scope.gist_pattern = /(^\d{5,}$)|(^[a-z0-9]{10,}$)|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  67. $scope.gist = {};
  68. $scope.elasticsearch = {};
  69. }
  70. $scope.export = function() {
  71. var blob = new Blob([angular.toJson($scope.dashboards,true)], {type: "application/json;charset=utf-8"});
  72. saveAs(blob, $scope.dashboards.title+"-"+new Date().getTime());
  73. }
  74. $scope.default = function() {
  75. if (Modernizr.localstorage) {
  76. localStorage['dashboard'] = angular.toJson($scope.dashboards);
  77. $scope.alert('Success',
  78. $scope.dashboards.title + " has been set as your default dashboard",
  79. 'success',5000)
  80. } else {
  81. $scope.alert('Bummer!',
  82. "Your browser is too old for this functionality",
  83. 'error',5000);
  84. }
  85. }
  86. $scope.share_link = function(title,type,id) {
  87. $scope.share = {
  88. location : location.href.replace(location.hash,""),
  89. type : type,
  90. id : id,
  91. link : location.href.replace(location.hash,"")+"#dashboard/"+type+"/"+id,
  92. title : title,
  93. };
  94. }
  95. $scope.purge = function() {
  96. if (Modernizr.localstorage) {
  97. localStorage['dashboard'] = '';
  98. $scope.alert('Success',
  99. 'Default dashboard cleared',
  100. 'success',5000)
  101. } else {
  102. $scope.alert('Doh!',
  103. "Your browser is too old for this functionality",
  104. 'error',5000);
  105. }
  106. }
  107. $scope.elasticsearch_save = function(type) {
  108. // Clone object so we can modify it without influencing the existing obejct
  109. var save = _.clone($scope.dashboards)
  110. // Change title on object clone
  111. if(type === 'dashboard')
  112. var id = save.title = $scope.elasticsearch.title;
  113. // Create request with id as title. Rethink this.
  114. var request = $scope.ejs.Document($scope.panel.elasticsearch_saveto,type,id).source({
  115. user: 'guest',
  116. group: 'guest',
  117. title: save.title,
  118. dashboard: angular.toJson(save)
  119. })
  120. if(type === 'temp')
  121. request = request.ttl($scope.panel.temp_ttl)
  122. var result = request.doIndex();
  123. var id = result.then(function(result) {
  124. $scope.alert('Dashboard Saved','This dashboard has been saved to Elasticsearch','success',5000)
  125. $scope.elasticsearch_dblist($scope.elasticsearch.query);
  126. $scope.elasticsearch.title = '';
  127. if(type === 'temp')
  128. $scope.share_link($scope.dashboards.title,'temp',result._id)
  129. })
  130. }
  131. $scope.elasticsearch_delete = function(dashboard) {
  132. var result = $scope.ejs.Document($scope.panel.elasticsearch_saveto,'dashboard',dashboard._id).doDelete();
  133. result.then(function(result) {
  134. $scope.alert('Dashboard Deleted','','success',5000)
  135. $scope.elasticsearch.dashboards = _.without($scope.elasticsearch.dashboards,dashboard)
  136. })
  137. }
  138. $scope.elasticsearch_load = function(type,id) {
  139. var request = $scope.ejs.Request().indices($scope.panel.elasticsearch_saveto).types(type);
  140. var results = request.query(
  141. $scope.ejs.IdsQuery(id)
  142. ).size($scope.panel.elasticsearch_size).doSearch();
  143. results.then(function(results) {
  144. if(_.isUndefined(results)) {
  145. $scope.panel.error = 'Your query was unsuccessful';
  146. return;
  147. }
  148. $scope.panel.error = false;
  149. $scope.dash_load(results.hits.hits[0]['_source']['dashboard'])
  150. });
  151. }
  152. $scope.elasticsearch_dblist = function(query) {
  153. if($scope.panel.load.elasticsearch) {
  154. var request = $scope.ejs.Request().indices($scope.panel.elasticsearch_saveto).types('dashboard');
  155. var results = request.query(
  156. $scope.ejs.QueryStringQuery(query || '*')
  157. ).size($scope.panel.elasticsearch_size).doSearch();
  158. results.then(function(results) {
  159. if(_.isUndefined(results)) {
  160. $scope.panel.error = 'Your query was unsuccessful';
  161. return;
  162. }
  163. $scope.panel.error = false;
  164. $scope.hits = results.hits.total;
  165. $scope.elasticsearch.dashboards = results.hits.hits
  166. });
  167. }
  168. }
  169. $scope.save_gist = function() {
  170. var save = _.clone($scope.dashboards)
  171. save.title = $scope.gist.title;
  172. $http({
  173. url: "https://api.github.com/gists",
  174. method: "POST",
  175. data: {
  176. "description": save.title,
  177. "public": false,
  178. "files": {
  179. "kibana-dashboard.json": {
  180. "content": angular.toJson(save,true)
  181. }
  182. }
  183. }
  184. }).success(function(data, status, headers, config) {
  185. $scope.gist.last = data.html_url;
  186. $scope.alert('Gist saved','You will be able to access your exported dashboard file at <a href="'+data.html_url+'">'+data.html_url+'</a> in a moment','success')
  187. }).error(function(data, status, headers, config) {
  188. $scope.alert('Unable to save','Save to gist failed for some reason','error',5000)
  189. });
  190. }
  191. $scope.gist_dblist = function(id) {
  192. $http.jsonp("https://api.github.com/gists/"+id+"?callback=JSON_CALLBACK"
  193. ).success(function(response) {
  194. $scope.gist.files = []
  195. _.each(response.data.files,function(v,k) {
  196. try {
  197. var file = JSON.parse(v.content)
  198. $scope.gist.files.push(file)
  199. } catch(e) {
  200. $scope.alert('Gist failure','The dashboard file is invalid','warning',5000)
  201. }
  202. });
  203. }).error(function(data, status, headers, config) {
  204. $scope.alert('Gist Failed','Could not retrieve dashboard list from gist','error',5000)
  205. });
  206. }
  207. $scope.dash_load = function(dashboard) {
  208. if(!_.isObject(dashboard))
  209. dashboard = JSON.parse(dashboard)
  210. eventBus.broadcast($scope.$id,'ALL','dashboard',dashboard)
  211. timer.cancel_all();
  212. }
  213. $scope.gist_id = function(string) {
  214. if($scope.is_gist(string))
  215. return string.match($scope.gist_pattern)[0].replace(/.*\//, '');
  216. }
  217. $scope.is_gist = function(string) {
  218. if(!_.isUndefined(string) && string != '' && !_.isNull(string.match($scope.gist_pattern)))
  219. return string.match($scope.gist_pattern).length > 0 ? true : false;
  220. else
  221. return false
  222. }
  223. $scope.init();
  224. })
  225. .directive('dashUpload', function(timer, eventBus){
  226. return {
  227. restrict: 'A',
  228. link: function(scope, elem, attrs) {
  229. function file_selected(evt) {
  230. var files = evt.target.files; // FileList object
  231. // files is a FileList of File objects. List some properties.
  232. var output = [];
  233. for (var i = 0, f; f = files[i]; i++) {
  234. var reader = new FileReader();
  235. reader.onload = (function(theFile) {
  236. return function(e) {
  237. scope.dash_load(JSON.parse(e.target.result))
  238. scope.$apply();
  239. };
  240. })(f);
  241. reader.readAsText(f);
  242. }
  243. }
  244. // Check for the various File API support.
  245. if (window.File && window.FileReader && window.FileList && window.Blob) {
  246. // Something
  247. document.getElementById('dashupload').addEventListener('change', file_selected, false);
  248. } else {
  249. alert('Sorry, the HTML5 File APIs are not fully supported in this browser.');
  250. }
  251. }
  252. }
  253. }).filter('gistid', function() {
  254. var gist_pattern = /(\d{5,})|([a-z0-9]{10,})|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  255. return function(input, scope) {
  256. //return input+"boners"
  257. if(!(_.isUndefined(input))) {
  258. var output = input.match(gist_pattern);
  259. if(!_.isNull(output) && !_.isUndefined(output))
  260. return output[0].replace(/.*\//, '');
  261. }
  262. }
  263. });;