dashboard_import_ctrl.ts 3.8 KB

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