module.js 9.7 KB

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