module.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*global FileReader:false*/
  4. /*
  5. ## Dashcontrol
  6. ### Parameters
  7. * save
  8. ** gist :: Allow saving to gist. Requires registering an oauth domain with Github
  9. ** elasticsearch :: Allow saving to a special Kibana index within Elasticsearch
  10. ** local :: Allow saving to local file
  11. * load
  12. ** gist :: Allow loading from gists
  13. ** elasticsearch :: Allow searching and loading of elasticsearch saved dashboards
  14. ** local :: Allow loading of dashboards from Elasticsearch
  15. * hide_control :: Upon save, hide this panel
  16. * elasticsearch_size :: show this many dashboards under the ES section in the load drop down
  17. * temp :: Allow saving of temp dashboards
  18. * ttl :: Enable setting ttl.
  19. * temp_ttl :: How long should temp dashboards persist
  20. */
  21. 'use strict';
  22. angular.module('kibana.dashcontrol', [])
  23. .controller('dashcontrol', function($scope, $http, timer, dashboard) {
  24. $scope.panelMeta = {
  25. status : "Stable",
  26. description : "This panel allows for saving, loading, exporting and sharing dashboard schemas."
  27. };
  28. $scope.panel = $scope.panel || {};
  29. // Set and populate defaults
  30. var _d = {
  31. save : {
  32. gist: false,
  33. elasticsearch: true,
  34. local: true,
  35. 'default': true
  36. },
  37. load : {
  38. gist: true,
  39. elasticsearch: true,
  40. local: true
  41. },
  42. hide_control: false,
  43. elasticsearch_size: 20,
  44. temp: true,
  45. ttl_enable: true,
  46. temp_ttl: '30d'
  47. };
  48. _.defaults($scope.panel,_d);
  49. // A hash of defaults for the dashboard object
  50. var _dash = {
  51. title: "",
  52. editable: true,
  53. rows: [],
  54. services: {}
  55. };
  56. $scope.init = function() {
  57. $scope.gist_pattern = /(^\d{5,}$)|(^[a-z0-9]{10,}$)|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  58. $scope.gist = {};
  59. $scope.elasticsearch = {};
  60. };
  61. $scope.set_default = function() {
  62. if(dashboard.set_default()) {
  63. $scope.alert('Local Default Set',dashboard.current.title+' has been set as your local default','success',5000);
  64. } else {
  65. $scope.alert('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000);
  66. }
  67. };
  68. $scope.purge_default = function() {
  69. if(dashboard.purge_default()) {
  70. $scope.alert('Local Default Clear','Your local default dashboard has been cleared','success',5000);
  71. } else {
  72. $scope.alert('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000);
  73. }
  74. };
  75. $scope.elasticsearch_save = function(type,ttl) {
  76. dashboard.elasticsearch_save(
  77. type,
  78. ($scope.elasticsearch.title || dashboard.current.title),
  79. ($scope.panel.ttl_enable ? ttl : false)
  80. ).then(
  81. function(result) {
  82. if(!_.isUndefined(result._id)) {
  83. $scope.alert('Dashboard Saved','This dashboard has been saved to Elasticsearch as "' +
  84. result._id + '"','success',5000);
  85. if(type === 'temp') {
  86. $scope.share = dashboard.share_link(dashboard.current.title,'temp',result._id);
  87. }
  88. } else {
  89. $scope.alert('Save failed','Dashboard could not be saved to Elasticsearch','error',5000);
  90. }
  91. });
  92. };
  93. $scope.elasticsearch_delete = function(id) {
  94. dashboard.elasticsearch_delete(id).then(
  95. function(result) {
  96. if(!_.isUndefined(result)) {
  97. if(result.found) {
  98. $scope.alert('Dashboard Deleted',id+' has been deleted','success',5000);
  99. // Find the deleted dashboard in the cached list and remove it
  100. var toDelete = _.where($scope.elasticsearch.dashboards,{_id:id})[0];
  101. $scope.elasticsearch.dashboards = _.without($scope.elasticsearch.dashboards,toDelete);
  102. } else {
  103. $scope.alert('Dashboard Not Found','Could not find '+id+' in Elasticsearch','warning',5000);
  104. }
  105. } else {
  106. $scope.alert('Dashboard Not Deleted','An error occurred deleting the dashboard','error',5000);
  107. }
  108. }
  109. );
  110. };
  111. $scope.elasticsearch_dblist = function(query) {
  112. dashboard.elasticsearch_list(query,$scope.panel.elasticsearch_size).then(
  113. function(result) {
  114. if(!_.isUndefined(result.hits)) {
  115. $scope.panel.error = false;
  116. $scope.hits = result.hits.total;
  117. $scope.elasticsearch.dashboards = result.hits.hits;
  118. }
  119. });
  120. };
  121. $scope.save_gist = function() {
  122. dashboard.save_gist($scope.gist.title).then(
  123. function(link) {
  124. if(!_.isUndefined(link)) {
  125. $scope.gist.last = link;
  126. $scope.alert('Gist saved','You will be able to access your exported dashboard file at '+
  127. '<a href="'+link+'">'+link+'</a> in a moment','success');
  128. } else {
  129. $scope.alert('Save failed','Gist could not be saved','error',5000);
  130. }
  131. });
  132. };
  133. $scope.gist_dblist = function(id) {
  134. dashboard.gist_list(id).then(
  135. function(files) {
  136. if(files && files.length > 0) {
  137. $scope.gist.files = files;
  138. } else {
  139. $scope.alert('Gist Failed','Could not retrieve dashboard list from gist','error',5000);
  140. }
  141. });
  142. };
  143. })
  144. .directive('dashUpload', function(timer, dashboard){
  145. return {
  146. restrict: 'A',
  147. link: function(scope, elem, attrs) {
  148. function file_selected(evt) {
  149. var files = evt.target.files; // FileList object
  150. // files is a FileList of File objects. List some properties.
  151. var output = [];
  152. var readerOnload = function(theFile) {
  153. return function(e) {
  154. dashboard.dash_load(JSON.parse(e.target.result));
  155. scope.$apply();
  156. };
  157. };
  158. for (var i = 0, f; f = files[i]; i++) {
  159. var reader = new FileReader();
  160. reader.onload = (readerOnload)(f);
  161. reader.readAsText(f);
  162. }
  163. }
  164. // Check for the various File API support.
  165. if (window.File && window.FileReader && window.FileList && window.Blob) {
  166. // Something
  167. document.getElementById('dashupload').addEventListener('change', file_selected, false);
  168. } else {
  169. alert('Sorry, the HTML5 File APIs are not fully supported in this browser.');
  170. }
  171. }
  172. };
  173. }).filter('gistid', function() {
  174. var gist_pattern = /(\d{5,})|([a-z0-9]{10,})|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  175. return function(input, scope) {
  176. //return input+"boners"
  177. if(!(_.isUndefined(input))) {
  178. var output = input.match(gist_pattern);
  179. if(!_.isNull(output) && !_.isUndefined(output)) {
  180. return output[0].replace(/.*\//, '');
  181. }
  182. }
  183. };
  184. });