module.js 6.6 KB

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