dashboard_import_ctrl.ts 4.1 KB

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