dash_import.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.info) {
  62. inputModel.info = "Select a " + input.pluginName + " data source";
  63. }
  64. inputModel.options = sources.map(val => {
  65. return {text: val.name, value: val.name};
  66. });
  67. }
  68. inputValueChanged() {
  69. this.inputsValid = true;
  70. for (let input of this.inputs) {
  71. if (!input.value) {
  72. this.inputsValid = false;
  73. }
  74. }
  75. }
  76. titleChanged() {
  77. this.backendSrv.search({query: this.dash.title}).then(res => {
  78. this.nameExists = false;
  79. for (let hit of res) {
  80. if (this.dash.title === hit.title) {
  81. this.nameExists = true;
  82. break;
  83. }
  84. }
  85. });
  86. }
  87. saveDashboard() {
  88. var inputs = this.inputs.map(input => {
  89. return {
  90. name: input.name,
  91. type: input.type,
  92. pluginId: input.pluginId,
  93. value: input.value
  94. };
  95. });
  96. return this.backendSrv.post('api/dashboards/import', {
  97. dashboard: this.dash,
  98. overwrite: true,
  99. inputs: inputs
  100. }).then(res => {
  101. this.$location.url('dashboard/' + res.importedUri);
  102. this.$scope.dismiss();
  103. });
  104. }
  105. loadJsonText() {
  106. try {
  107. this.parseError = '';
  108. var dash = JSON.parse(this.jsonText);
  109. this.onUpload(dash);
  110. } catch (err) {
  111. console.log(err);
  112. this.parseError = err.message;
  113. return;
  114. }
  115. }
  116. checkGnetDashboard() {
  117. this.gnetError = '';
  118. var match = /(^\d+$)|dashboards\/(\d+)/.exec(this.gnetUrl);
  119. var dashboardId;
  120. if (match && match[1]) {
  121. dashboardId = match[1];
  122. } else if (match && match[2]) {
  123. dashboardId = match[2];
  124. } else {
  125. this.gnetError = 'Could not find dashboard';
  126. }
  127. return this.backendSrv.get('api/gnet/dashboards/' + dashboardId).then(res => {
  128. this.gnetInfo = res;
  129. // store reference to grafana.net
  130. res.json.gnetId = res.id;
  131. this.onUpload(res.json);
  132. }).catch(err => {
  133. err.isHandled = true;
  134. this.gnetError = err.data.message || err;
  135. });
  136. }
  137. back() {
  138. this.gnetUrl = '';
  139. this.step = 1;
  140. this.gnetError = '';
  141. this.gnetInfo = '';
  142. }
  143. }
  144. export function dashImportDirective() {
  145. return {
  146. restrict: 'E',
  147. templateUrl: 'public/app/features/dashboard/import/dash_import.html',
  148. controller: DashImportCtrl,
  149. bindToController: true,
  150. controllerAs: 'ctrl',
  151. };
  152. }
  153. coreModule.directive('dashImport', dashImportDirective);