dashboard_import_ctrl.ts 4.9 KB

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