exporter.ts 3.7 KB

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