module.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*global FileReader:false*/
  4. /*
  5. ## Dashcontrol
  6. Dash control allows for saving, loading and sharing of dashboards. Do not
  7. disable the dashcontrol module as a special instance of it allows for loading
  8. the default dashboard from dashboards/default
  9. ### Parameters
  10. * save
  11. ** gist :: Allow saving to gist. Requires registering an oauth domain with Github
  12. ** elasticsearch :: Allow saving to a special Kibana index within Elasticsearch
  13. ** local :: Allow saving to local file
  14. * load
  15. ** gist :: Allow loading from gists
  16. ** elasticsearch :: Allow searching and loading of elasticsearch saved dashboards
  17. ** local :: Allow loading of dashboards from Elasticsearch
  18. * hide_control :: Upon save, hide this panel
  19. * elasticsearch_size :: show this many dashboards under the ES section in the load drop down
  20. * temp :: Allow saving of temp dashboards
  21. * ttl :: Enable setting ttl.
  22. * temp_ttl :: How long should temp dashboards persist
  23. */
  24. 'use strict';
  25. angular.module('kibana.dashcontrol', [])
  26. .controller('dashcontrol', function($scope, $http, timer, dashboard) {
  27. $scope.panel = $scope.panel || {};
  28. // Set and populate defaults
  29. var _d = {
  30. status : "Stable",
  31. group : "default",
  32. save : {
  33. gist: false,
  34. elasticsearch: true,
  35. local: true,
  36. 'default': true
  37. },
  38. load : {
  39. gist: true,
  40. elasticsearch: true,
  41. local: true
  42. },
  43. hide_control: false,
  44. elasticsearch_size: 20,
  45. temp: true,
  46. ttl_enable: true,
  47. temp_ttl: '30d'
  48. };
  49. _.defaults($scope.panel,_d);
  50. // A hash of defaults for the dashboard object
  51. var _dash = {
  52. title: "",
  53. editable: true,
  54. rows: [],
  55. services: {}
  56. };
  57. $scope.init = function() {
  58. $scope.gist_pattern = /(^\d{5,}$)|(^[a-z0-9]{10,}$)|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  59. $scope.gist = {};
  60. $scope.elasticsearch = {};
  61. };
  62. $scope.set_default = function() {
  63. if(dashboard.set_default()) {
  64. $scope.alert('Local Default Set',dashboard.current.title+' has been set as your local default','success',5000);
  65. } else {
  66. $scope.alert('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000);
  67. }
  68. };
  69. $scope.purge_default = function() {
  70. if(dashboard.purge_default()) {
  71. $scope.alert('Local Default Clear','Your local default dashboard has been cleared','success',5000);
  72. } else {
  73. $scope.alert('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000);
  74. }
  75. };
  76. $scope.elasticsearch_save = function(type,ttl) {
  77. dashboard.elasticsearch_save(
  78. type,
  79. ($scope.elasticsearch.title || dashboard.current.title),
  80. ($scope.panel.ttl_enable ? ttl : false)
  81. ).then(
  82. function(result) {
  83. if(!_.isUndefined(result._id)) {
  84. $scope.alert('Dashboard Saved','This dashboard has been saved to Elasticsearch as "' +
  85. result._id + '"','success',5000);
  86. if(type === 'temp') {
  87. $scope.share = dashboard.share_link(dashboard.current.title,'temp',result._id);
  88. }
  89. } else {
  90. $scope.alert('Save failed','Dashboard could not be saved to Elasticsearch','error',5000);
  91. }
  92. });
  93. };
  94. $scope.elasticsearch_delete = function(id) {
  95. dashboard.elasticsearch_delete(id).then(
  96. function(result) {
  97. if(!_.isUndefined(result)) {
  98. if(result.found) {
  99. $scope.alert('Dashboard Deleted',id+' has been deleted','success',5000);
  100. // Find the deleted dashboard in the cached list and remove it
  101. var toDelete = _.where($scope.elasticsearch.dashboards,{_id:id})[0];
  102. $scope.elasticsearch.dashboards = _.without($scope.elasticsearch.dashboards,toDelete);
  103. } else {
  104. $scope.alert('Dashboard Not Found','Could not find '+id+' in Elasticsearch','warning',5000);
  105. }
  106. } else {
  107. $scope.alert('Dashboard Not Deleted','An error occurred deleting the dashboard','error',5000);
  108. }
  109. }
  110. );
  111. };
  112. $scope.elasticsearch_dblist = function(query) {
  113. dashboard.elasticsearch_list(query,$scope.panel.elasticsearch_size).then(
  114. function(result) {
  115. if(!_.isUndefined(result.hits)) {
  116. $scope.panel.error = false;
  117. $scope.hits = result.hits.total;
  118. $scope.elasticsearch.dashboards = result.hits.hits;
  119. }
  120. });
  121. };
  122. $scope.save_gist = function() {
  123. dashboard.save_gist($scope.gist.title).then(
  124. function(link) {
  125. if(!_.isUndefined(link)) {
  126. $scope.gist.last = link;
  127. $scope.alert('Gist saved','You will be able to access your exported dashboard file at <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. });