exporter.ts 3.4 KB

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