module.js 9.0 KB

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