module.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. angular.module('kibana.dashcontrol', [])
  2. .controller('dashcontrol', function($scope, $http, eventBus, timer) {
  3. // Set and populate defaults
  4. var _d = {
  5. group : "default",
  6. save : {
  7. gist: true,
  8. elasticsearch: true,
  9. local: true,
  10. 'default': true
  11. },
  12. load : {
  13. gist: true,
  14. elasticsearch: true,
  15. local: true,
  16. },
  17. elasticsearch_size: 20,
  18. elasticsearch_saveto: 'kibana-int'
  19. }
  20. _.defaults($scope.panel,_d);
  21. $scope.init = function() {
  22. $scope.gist_pattern = /(^\d{5,}$)|(^[a-z0-9]{10,}$)|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  23. $scope.gist = {};
  24. $scope.elasticsearch = {};
  25. }
  26. $scope.export = function() {
  27. var blob = new Blob([angular.toJson($scope.dashboards,true)], {type: "application/json;charset=utf-8"});
  28. saveAs(blob, $scope.dashboards.title+"-"+new Date().getTime());
  29. }
  30. $scope.default = function() {
  31. if (Modernizr.localstorage) {
  32. localStorage['dashboard'] = angular.toJson($scope.dashboards);
  33. $scope.alert('Success',
  34. $scope.dashboards.title + " has been set as your default dashboard",
  35. 'success',5000)
  36. } else {
  37. $scope.alert('Bummer!',
  38. "Your browser is too old for this functionality",
  39. 'error',5000);
  40. }
  41. }
  42. $scope.purge = function() {
  43. if (Modernizr.localstorage) {
  44. localStorage['dashboard'] = '';
  45. $scope.alert('Success',
  46. 'Default dashboard cleared',
  47. 'success',5000)
  48. } else {
  49. $scope.alert('Doh!',
  50. "Your browser is too old for this functionality",
  51. 'error',5000);
  52. }
  53. }
  54. $scope.save_elasticsearch = function() {
  55. var save = _.clone($scope.dashboards)
  56. save.title = $scope.elasticsearch.title;
  57. var result = $scope.ejs.Document($scope.panel.elasticsearch_saveto,'dashboard',save.title).source({
  58. user: 'guest',
  59. group: 'guest',
  60. title: save.title,
  61. dashboard: angular.toJson(save)
  62. }).doIndex();
  63. result.then(function(result) {
  64. $scope.alert('Dashboard Saved','This dashboard has been saved to Elasticsearch','success',5000)
  65. $scope.elasticsearch_dblist($scope.elasticsearch.query);
  66. $scope.elasticsearch.title = '';
  67. })
  68. }
  69. $scope.delete_elasticsearch = function(dashboard) {
  70. var result = $scope.ejs.Document($scope.panel.elasticsearch_saveto,'dashboard',dashboard._id).doDelete();
  71. result.then(function(result) {
  72. $scope.alert('Dashboard Deleted','','success',5000)
  73. $scope.elasticsearch.dashboards = _.without($scope.elasticsearch.dashboards,dashboard)
  74. })
  75. }
  76. $scope.elasticsearch_dblist = function(query) {
  77. if($scope.panel.load.elasticsearch) {
  78. var request = $scope.ejs.Request().indices($scope.panel.elasticsearch_saveto).types('dashboard');
  79. var results = request.query(
  80. $scope.ejs.QueryStringQuery(query || '*')
  81. ).size($scope.panel.elasticsearch_size).doSearch();
  82. results.then(function(results) {
  83. if(_.isUndefined(results)) {
  84. $scope.panel.error = 'Your query was unsuccessful';
  85. return;
  86. }
  87. $scope.panel.error = false;
  88. $scope.hits = results.hits.total;
  89. $scope.elasticsearch.dashboards = results.hits.hits
  90. });
  91. }
  92. }
  93. $scope.save_gist = function() {
  94. var save = _.clone($scope.dashboards)
  95. save.title = $scope.gist.title;
  96. $http({
  97. url: "https://api.github.com/gists",
  98. method: "POST",
  99. data: {
  100. "description": save.title,
  101. "public": false,
  102. "files": {
  103. "kibana-dashboard.json": {
  104. "content": angular.toJson(save,true)
  105. }
  106. }
  107. }
  108. }).success(function(data, status, headers, config) {
  109. $scope.gist.last = data.html_url;
  110. $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')
  111. }).error(function(data, status, headers, config) {
  112. $scope.alert('Unable to save','Save to gist failed for some reason','error',5000)
  113. });
  114. }
  115. $scope.gist_dblist = function(id) {
  116. $http({
  117. url: "https://api.github.com/gists/"+id,
  118. method: "GET"
  119. }).success(function(data, status, headers, config) {
  120. $scope.gist.files = []
  121. _.each(data.files,function(v,k) {
  122. try {
  123. var file = JSON.parse(v.content)
  124. $scope.gist.files.push(file)
  125. } catch(e) {
  126. $scope.alert('Gist failure','The dashboard file is invalid','warning',5000)
  127. }
  128. });
  129. }).error(function(data, status, headers, config) {
  130. $scope.alert('Gist Failed','Could not retrieve dashboard list from gist','error',5000)
  131. });
  132. }
  133. $scope.dash_load = function(dashboard) {
  134. if(!_.isObject(dashboard))
  135. dashboard = JSON.parse(dashboard)
  136. eventBus.broadcast($scope.$id,'ALL','dashboard',dashboard)
  137. timer.cancel_all();
  138. }
  139. $scope.gist_id = function(string) {
  140. if($scope.is_gist(string))
  141. return string.match($scope.gist_pattern)[0].replace(/.*\//, '');
  142. }
  143. $scope.is_gist = function(string) {
  144. if(!_.isUndefined(string) && string != '' && !_.isNull(string.match($scope.gist_pattern)))
  145. return string.match($scope.gist_pattern).length > 0 ? true : false;
  146. else
  147. return false
  148. }
  149. $scope.init();
  150. })
  151. .directive('dashUpload', function(timer, eventBus){
  152. return {
  153. restrict: 'A',
  154. link: function(scope, elem, attrs) {
  155. function file_selected(evt) {
  156. var files = evt.target.files; // FileList object
  157. // files is a FileList of File objects. List some properties.
  158. var output = [];
  159. for (var i = 0, f; f = files[i]; i++) {
  160. var reader = new FileReader();
  161. reader.onload = (function(theFile) {
  162. return function(e) {
  163. scope.dash_load(JSON.parse(e.target.result))
  164. scope.$apply();
  165. };
  166. })(f);
  167. reader.readAsText(f);
  168. }
  169. }
  170. // Check for the various File API support.
  171. if (window.File && window.FileReader && window.FileList && window.Blob) {
  172. // Something
  173. document.getElementById('dashupload').addEventListener('change', file_selected, false);
  174. } else {
  175. alert('Sorry, the HTML5 File APIs are not fully supported in this browser.');
  176. }
  177. }
  178. }
  179. }).filter('gistid', function() {
  180. var gist_pattern = /(\d{5,})|([a-z0-9]{10,})|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  181. return function(input, scope) {
  182. //return input+"boners"
  183. if(!(_.isUndefined(input))) {
  184. var output = input.match(gist_pattern);
  185. if(!_.isNull(output) && !_.isUndefined(output))
  186. return output[0].replace(/.*\//, '');
  187. }
  188. }
  189. });;