exporter.ts 3.7 KB

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