module.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ## Dashcontrol
  3. Dash control allows for saving, loading and sharing of dashboards. Do not
  4. disable the dashcontrol module as a special instance of it allows for loading
  5. the default dashboard from dashboards/default
  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. * elasticsearch_saveto :: Special kibana index to save to
  18. * temp :: Allow saving of temp dashboards
  19. * temp_ttl :: How long should temp dashboards persist
  20. ### Group Events
  21. #### Sends
  22. * dashboard :: An object containing an entire dashboard to be loaded
  23. */
  24. angular.module('kibana.dashcontrol', [])
  25. .controller('dashcontrol', function($scope, $routeParams, $http, eventBus, timer, dashboard) {
  26. $scope.panel = $scope.panel || {};
  27. // Set and populate defaults
  28. var _d = {
  29. status : "Stable",
  30. group : "default",
  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. elasticsearch_saveto: $scope.config.kibana_index,
  45. temp: 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(type,($scope.elasticsearch.title || dashboard.current.title),ttl).then(
  77. function(result) {
  78. if(!_.isUndefined(result._id)) {
  79. $scope.alert('Dashboard Saved','This dashboard has been saved to Elasticsearch as "' +
  80. result._id + '"','success',5000)
  81. if(type === 'temp') {
  82. $scope.share = dashboard.share_link(dashboard.current.title,'temp',result._id)
  83. }
  84. console.log(result)
  85. } else {
  86. $scope.alert('Save failed','Dashboard could not be saved to Elasticsearch','error',5000)
  87. }
  88. })
  89. }
  90. $scope.elasticsearch_delete = function(id) {
  91. dashboard.elasticsearch_delete(id).then(
  92. function(result) {
  93. if(!_.isUndefined(result)) {
  94. if(result.found) {
  95. $scope.alert('Dashboard Deleted',id+' has been deleted','success',5000)
  96. // Find the deleted dashboard in the cached list and remove it
  97. var toDelete = _.where($scope.elasticsearch.dashboards,{_id:id})[0]
  98. $scope.elasticsearch.dashboards = _.without($scope.elasticsearch.dashboards,toDelete)
  99. } else {
  100. $scope.alert('Dashboard Not Found','Could not find '+id+' in Elasticsearch','warning',5000)
  101. }
  102. } else {
  103. $scope.alert('Dashboard Not Deleted','An error occurred deleting the dashboard',error,5000)
  104. }
  105. }
  106. )
  107. }
  108. $scope.elasticsearch_dblist = function(query) {
  109. dashboard.elasticsearch_list(query,$scope.panel.elasticsearch_size).then(
  110. function(result) {
  111. if(!_.isUndefined(result.hits)) {
  112. $scope.panel.error = false;
  113. $scope.hits = result.hits.total;
  114. $scope.elasticsearch.dashboards = result.hits.hits
  115. }
  116. })
  117. }
  118. $scope.save_gist = function() {
  119. dashboard.save_gist($scope.gist.title).then(
  120. function(link) {
  121. console.log(link)
  122. if(!_.isUndefined(link)) {
  123. $scope.gist.last = link;
  124. $scope.alert('Gist saved','You will be able to access your exported dashboard file at <a href="'+link+'">'+link+'</a> in a moment','success');
  125. } else {
  126. $scope.alert('Save failed','Gist could not be saved','error',5000)
  127. }
  128. })
  129. }
  130. $scope.gist_dblist = function(id) {
  131. dashboard.gist_list(id).then(
  132. function(files) {
  133. if(files && files.length > 0) {
  134. $scope.gist.files = files;
  135. } else {
  136. $scope.alert('Gist Failed','Could not retrieve dashboard list from gist','error',5000)
  137. }
  138. })
  139. }
  140. })
  141. .directive('dashUpload', function(timer, eventBus, dashboard){
  142. return {
  143. restrict: 'A',
  144. link: function(scope, elem, attrs) {
  145. function file_selected(evt) {
  146. var files = evt.target.files; // FileList object
  147. // files is a FileList of File objects. List some properties.
  148. var output = [];
  149. for (var i = 0, f; f = files[i]; i++) {
  150. var reader = new FileReader();
  151. reader.onload = (function(theFile) {
  152. return function(e) {
  153. dashboard.dash_load(JSON.parse(e.target.result))
  154. scope.$apply();
  155. };
  156. })(f);
  157. reader.readAsText(f);
  158. }
  159. }
  160. // Check for the various File API support.
  161. if (window.File && window.FileReader && window.FileList && window.Blob) {
  162. // Something
  163. document.getElementById('dashupload').addEventListener('change', file_selected, false);
  164. } else {
  165. alert('Sorry, the HTML5 File APIs are not fully supported in this browser.');
  166. }
  167. }
  168. }
  169. }).filter('gistid', function() {
  170. var gist_pattern = /(\d{5,})|([a-z0-9]{10,})|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  171. return function(input, scope) {
  172. //return input+"boners"
  173. if(!(_.isUndefined(input))) {
  174. var output = input.match(gist_pattern);
  175. if(!_.isNull(output) && !_.isUndefined(output))
  176. return output[0].replace(/.*\//, '');
  177. }
  178. }
  179. });;