dashboard_import_ctrl.ts 5.4 KB

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