module.js 9.5 KB

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