exporter.ts 4.1 KB

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