dashboard_import_ctrl.ts 5.6 KB

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