dash_import.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import kbn from 'app/core/utils/kbn';
  3. import coreModule from 'app/core/core_module';
  4. import appEvents from 'app/core/app_events';
  5. import config from 'app/core/config';
  6. import _ from 'lodash';
  7. export class DashImportCtrl {
  8. step: number;
  9. jsonText: string;
  10. parseError: string;
  11. nameExists: boolean;
  12. dash: any;
  13. inputs: any[];
  14. inputsValid: boolean;
  15. gnetUrl: string;
  16. gnetError: string;
  17. gnetInfo: any;
  18. /** @ngInject */
  19. constructor(private backendSrv, private $location, private $scope, private $routeParams) {
  20. this.step = 1;
  21. this.nameExists = false;
  22. // check gnetId in url
  23. if ($routeParams.gnetId) {
  24. this.gnetUrl = $routeParams.gnetId ;
  25. this.checkGnetDashboard();
  26. }
  27. }
  28. onUpload(dash) {
  29. this.dash = dash;
  30. this.dash.id = null;
  31. this.step = 2;
  32. this.inputs = [];
  33. if (this.dash.__inputs) {
  34. for (let input of this.dash.__inputs) {
  35. var inputModel = {
  36. name: input.name,
  37. label: input.label,
  38. info: input.description,
  39. value: input.value,
  40. type: input.type,
  41. pluginId: input.pluginId,
  42. options: []
  43. };
  44. if (input.type === 'datasource') {
  45. this.setDatasourceOptions(input, inputModel);
  46. } else if (!inputModel.info) {
  47. inputModel.info = 'Specify a string constant';
  48. }
  49. this.inputs.push(inputModel);
  50. }
  51. }
  52. this.inputsValid = this.inputs.length === 0;
  53. this.titleChanged();
  54. }
  55. setDatasourceOptions(input, inputModel) {
  56. var sources = _.filter(config.datasources, val => {
  57. return val.type === input.pluginId;
  58. });
  59. if (sources.length === 0) {
  60. inputModel.info = "No data sources of type " + input.pluginName + " found";
  61. } else if (inputModel.description) {
  62. inputModel.info = inputModel.description;
  63. } else {
  64. inputModel.info = "Select a " + input.pluginName + " data source";
  65. }
  66. inputModel.options = sources.map(val => {
  67. return {text: val.name, value: val.name};
  68. });
  69. }
  70. inputValueChanged() {
  71. this.inputsValid = true;
  72. for (let input of this.inputs) {
  73. if (!input.value) {
  74. this.inputsValid = false;
  75. }
  76. }
  77. }
  78. titleChanged() {
  79. this.backendSrv.search({query: this.dash.title}).then(res => {
  80. this.nameExists = false;
  81. for (let hit of res) {
  82. if (this.dash.title === hit.title) {
  83. this.nameExists = true;
  84. break;
  85. }
  86. }
  87. });
  88. }
  89. saveDashboard() {
  90. var inputs = this.inputs.map(input => {
  91. return {
  92. name: input.name,
  93. type: input.type,
  94. pluginId: input.pluginId,
  95. value: input.value
  96. };
  97. });
  98. return this.backendSrv.post('api/dashboards/import', {
  99. dashboard: this.dash,
  100. overwrite: true,
  101. inputs: inputs
  102. }).then(res => {
  103. this.$location.url('dashboard/' + res.importedUri);
  104. this.$scope.dismiss();
  105. });
  106. }
  107. loadJsonText() {
  108. try {
  109. this.parseError = '';
  110. var dash = JSON.parse(this.jsonText);
  111. this.onUpload(dash);
  112. } catch (err) {
  113. console.log(err);
  114. this.parseError = err.message;
  115. return;
  116. }
  117. }
  118. checkGnetDashboard() {
  119. this.gnetError = '';
  120. var match = /(^\d+$)|dashboards\/(\d+)/.exec(this.gnetUrl);
  121. var dashboardId;
  122. if (match && match[1]) {
  123. dashboardId = match[1];
  124. } else if (match && match[2]) {
  125. dashboardId = match[2];
  126. } else {
  127. this.gnetError = 'Could not find dashboard';
  128. }
  129. return this.backendSrv.get('api/gnet/dashboards/' + dashboardId).then(res => {
  130. this.gnetInfo = res;
  131. // store reference to grafana.net
  132. res.json.gnetId = res.id;
  133. this.onUpload(res.json);
  134. }).catch(err => {
  135. err.isHandled = true;
  136. this.gnetError = err.data.message || err;
  137. });
  138. }
  139. back() {
  140. this.gnetUrl = '';
  141. this.step = 1;
  142. this.gnetError = '';
  143. this.gnetInfo = '';
  144. }
  145. }
  146. export function dashImportDirective() {
  147. return {
  148. restrict: 'E',
  149. templateUrl: 'public/app/features/dashboard/import/dash_import.html',
  150. controller: DashImportCtrl,
  151. bindToController: true,
  152. controllerAs: 'ctrl',
  153. };
  154. }
  155. coreModule.directive('dashImport', dashImportDirective);