dashboard_import_ctrl.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import _ from 'lodash';
  2. import config from 'app/core/config';
  3. export class DashboardImportCtrl {
  4. navModel: any;
  5. step: number;
  6. jsonText: string;
  7. parseError: string;
  8. nameExists: boolean;
  9. dash: any;
  10. inputs: any[];
  11. inputsValid: boolean;
  12. gnetUrl: string;
  13. gnetError: string;
  14. gnetInfo: any;
  15. titleTouched: boolean;
  16. hasNameValidationError: boolean;
  17. nameValidationError: any;
  18. /** @ngInject */
  19. constructor(
  20. private backendSrv,
  21. private validationSrv,
  22. navModelSrv,
  23. private $location,
  24. private $scope,
  25. $routeParams
  26. ) {
  27. this.navModel = navModelSrv.getNav('create', 'import');
  28. this.step = 1;
  29. this.nameExists = false;
  30. // check gnetId in url
  31. if ($routeParams.gnetId) {
  32. this.gnetUrl = $routeParams.gnetId;
  33. this.checkGnetDashboard();
  34. }
  35. }
  36. onUpload(dash) {
  37. this.dash = dash;
  38. this.dash.id = null;
  39. this.step = 2;
  40. this.inputs = [];
  41. if (this.dash.__inputs) {
  42. for (let input of this.dash.__inputs) {
  43. var inputModel = {
  44. name: input.name,
  45. label: input.label,
  46. info: input.description,
  47. value: input.value,
  48. type: input.type,
  49. pluginId: input.pluginId,
  50. options: [],
  51. };
  52. if (input.type === 'datasource') {
  53. this.setDatasourceOptions(input, inputModel);
  54. } else if (!inputModel.info) {
  55. inputModel.info = 'Specify a string constant';
  56. }
  57. this.inputs.push(inputModel);
  58. }
  59. }
  60. this.inputsValid = this.inputs.length === 0;
  61. this.titleChanged();
  62. }
  63. setDatasourceOptions(input, inputModel) {
  64. var sources = _.filter(config.datasources, val => {
  65. return val.type === input.pluginId;
  66. });
  67. if (sources.length === 0) {
  68. inputModel.info =
  69. 'No data sources of type ' + input.pluginName + ' found';
  70. } else if (!inputModel.info) {
  71. inputModel.info = 'Select a ' + input.pluginName + ' data source';
  72. }
  73. inputModel.options = sources.map(val => {
  74. return { text: val.name, value: val.name };
  75. });
  76. }
  77. inputValueChanged() {
  78. this.inputsValid = true;
  79. for (let input of this.inputs) {
  80. if (!input.value) {
  81. this.inputsValid = false;
  82. }
  83. }
  84. }
  85. titleChanged() {
  86. this.titleTouched = true;
  87. this.nameExists = false;
  88. this.validationSrv
  89. .validateNewDashboardOrFolderName(this.dash.title)
  90. .then(() => {
  91. this.hasNameValidationError = false;
  92. })
  93. .catch(err => {
  94. if (err.type === 'EXISTING') {
  95. this.nameExists = true;
  96. }
  97. this.hasNameValidationError = true;
  98. this.nameValidationError = err.message;
  99. });
  100. }
  101. saveDashboard() {
  102. var inputs = this.inputs.map(input => {
  103. return {
  104. name: input.name,
  105. type: input.type,
  106. pluginId: input.pluginId,
  107. value: input.value,
  108. };
  109. });
  110. return this.backendSrv
  111. .post('api/dashboards/import', {
  112. dashboard: this.dash,
  113. overwrite: true,
  114. inputs: inputs,
  115. })
  116. .then(res => {
  117. this.$location.url('dashboard/' + res.importedUri);
  118. this.$scope.dismiss();
  119. });
  120. }
  121. loadJsonText() {
  122. try {
  123. this.parseError = '';
  124. var dash = JSON.parse(this.jsonText);
  125. this.onUpload(dash);
  126. } catch (err) {
  127. console.log(err);
  128. this.parseError = err.message;
  129. return;
  130. }
  131. }
  132. checkGnetDashboard() {
  133. this.gnetError = '';
  134. var match = /(^\d+$)|dashboards\/(\d+)/.exec(this.gnetUrl);
  135. var dashboardId;
  136. if (match && match[1]) {
  137. dashboardId = match[1];
  138. } else if (match && match[2]) {
  139. dashboardId = match[2];
  140. } else {
  141. this.gnetError = 'Could not find dashboard';
  142. }
  143. return this.backendSrv
  144. .get('api/gnet/dashboards/' + dashboardId)
  145. .then(res => {
  146. this.gnetInfo = res;
  147. // store reference to grafana.com
  148. res.json.gnetId = res.id;
  149. this.onUpload(res.json);
  150. })
  151. .catch(err => {
  152. err.isHandled = true;
  153. this.gnetError = err.data.message || err;
  154. });
  155. }
  156. back() {
  157. this.gnetUrl = '';
  158. this.step = 1;
  159. this.gnetError = '';
  160. this.gnetInfo = '';
  161. }
  162. }