exporter.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import config from 'app/core/config';
  2. import _ from 'lodash';
  3. import { DashboardModel } from '../dashboard_model';
  4. export class DashboardExporter {
  5. constructor(private datasourceSrv) {}
  6. makeExportable(dashboard: DashboardModel) {
  7. // clean up repeated rows and panels,
  8. // this is done on the live real dashboard instance, not on a clone
  9. // so we need to undo this
  10. // this is pretty hacky and needs to be changed
  11. dashboard.cleanUpRepeats();
  12. var saveModel = dashboard.getSaveModelClone();
  13. saveModel.id = null;
  14. // undo repeat cleanup
  15. dashboard.processRepeats();
  16. var inputs = [];
  17. var requires = {};
  18. var datasources = {};
  19. var promises = [];
  20. var variableLookup: any = {};
  21. for (let variable of saveModel.templating.list) {
  22. variableLookup[variable.name] = variable;
  23. }
  24. var templateizeDatasourceUsage = obj => {
  25. // ignore data source properties that contain a variable
  26. if (obj.datasource && obj.datasource.indexOf('$') === 0) {
  27. if (variableLookup[obj.datasource.substring(1)]) {
  28. return;
  29. }
  30. }
  31. promises.push(
  32. this.datasourceSrv.get(obj.datasource).then(ds => {
  33. if (ds.meta.builtIn) {
  34. return;
  35. }
  36. var refName = 'DS_' + ds.name.replace(' ', '_').toUpperCase();
  37. datasources[refName] = {
  38. name: refName,
  39. label: ds.name,
  40. description: '',
  41. type: 'datasource',
  42. pluginId: ds.meta.id,
  43. pluginName: ds.meta.name,
  44. };
  45. obj.datasource = '${' + refName + '}';
  46. requires['datasource' + ds.meta.id] = {
  47. type: 'datasource',
  48. id: ds.meta.id,
  49. name: ds.meta.name,
  50. version: ds.meta.info.version || '1.0.0',
  51. };
  52. })
  53. );
  54. };
  55. // check up panel data sources
  56. for (let panel of saveModel.panels) {
  57. if (panel.datasource !== undefined) {
  58. templateizeDatasourceUsage(panel);
  59. }
  60. if (panel.targets) {
  61. for (let target of panel.targets) {
  62. if (target.datasource !== undefined) {
  63. templateizeDatasourceUsage(target);
  64. }
  65. }
  66. }
  67. var panelDef = config.panels[panel.type];
  68. if (panelDef) {
  69. requires['panel' + panelDef.id] = {
  70. type: 'panel',
  71. id: panelDef.id,
  72. name: panelDef.name,
  73. version: panelDef.info.version,
  74. };
  75. }
  76. }
  77. // templatize template vars
  78. for (let variable of saveModel.templating.list) {
  79. if (variable.type === 'query') {
  80. templateizeDatasourceUsage(variable);
  81. variable.options = [];
  82. variable.current = {};
  83. variable.refresh = variable.refresh > 0 ? variable.refresh : 1;
  84. }
  85. }
  86. // templatize annotations vars
  87. for (let annotationDef of saveModel.annotations.list) {
  88. templateizeDatasourceUsage(annotationDef);
  89. }
  90. // add grafana version
  91. requires['grafana'] = {
  92. type: 'grafana',
  93. id: 'grafana',
  94. name: 'Grafana',
  95. version: config.buildInfo.version,
  96. };
  97. return Promise.all(promises)
  98. .then(() => {
  99. _.each(datasources, (value, key) => {
  100. inputs.push(value);
  101. });
  102. // templatize constants
  103. for (let variable of saveModel.templating.list) {
  104. if (variable.type === 'constant') {
  105. var refName = 'VAR_' + variable.name.replace(' ', '_').toUpperCase();
  106. inputs.push({
  107. name: refName,
  108. type: 'constant',
  109. label: variable.label || variable.name,
  110. value: variable.current.value,
  111. description: '',
  112. });
  113. // update current and option
  114. variable.query = '${' + refName + '}';
  115. variable.options[0] = variable.current = {
  116. value: variable.query,
  117. text: variable.query,
  118. };
  119. }
  120. }
  121. // make inputs and requires a top thing
  122. var newObj = {};
  123. newObj['__inputs'] = inputs;
  124. newObj['__requires'] = _.sortBy(requires, ['id']);
  125. _.defaults(newObj, saveModel);
  126. return newObj;
  127. })
  128. .catch(err => {
  129. console.log('Export failed:', err);
  130. return {
  131. error: err,
  132. };
  133. });
  134. }
  135. }