exporter.ts 4.2 KB

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