PluginDashboards.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { extend } from 'lodash';
  2. import React, { PureComponent } from 'react';
  3. import { AppEvents, PluginMeta, DataSourceApi } from '@grafana/data';
  4. import { getBackendSrv } from '@grafana/runtime';
  5. import { appEvents } from 'app/core/core';
  6. import DashboardsTable from 'app/features/datasources/DashboardsTable';
  7. import { PluginDashboard } from 'app/types';
  8. interface Props {
  9. plugin: PluginMeta;
  10. datasource?: DataSourceApi;
  11. }
  12. interface State {
  13. dashboards: PluginDashboard[];
  14. loading: boolean;
  15. }
  16. export class PluginDashboards extends PureComponent<Props, State> {
  17. constructor(props: Props) {
  18. super(props);
  19. this.state = {
  20. loading: true,
  21. dashboards: [],
  22. };
  23. }
  24. async componentDidMount() {
  25. const pluginId = this.props.plugin.id;
  26. getBackendSrv()
  27. .get(`/api/plugins/${pluginId}/dashboards`)
  28. .then((dashboards: any) => {
  29. this.setState({ dashboards, loading: false });
  30. });
  31. }
  32. importAll = () => {
  33. this.importNext(0);
  34. };
  35. private importNext = (index: number) => {
  36. const { dashboards } = this.state;
  37. return this.import(dashboards[index], true).then(() => {
  38. if (index + 1 < dashboards.length) {
  39. return new Promise<void>((resolve) => {
  40. setTimeout(() => {
  41. this.importNext(index + 1).then(() => {
  42. resolve();
  43. });
  44. }, 500);
  45. });
  46. } else {
  47. return Promise.resolve();
  48. }
  49. });
  50. };
  51. import = (dash: PluginDashboard, overwrite: boolean) => {
  52. const { plugin, datasource } = this.props;
  53. const installCmd: any = {
  54. pluginId: plugin.id,
  55. path: dash.path,
  56. overwrite: overwrite,
  57. inputs: [],
  58. };
  59. if (datasource) {
  60. installCmd.inputs.push({
  61. name: '*',
  62. type: 'datasource',
  63. pluginId: datasource.meta.id,
  64. value: datasource.name,
  65. });
  66. }
  67. return getBackendSrv()
  68. .post(`/api/dashboards/import`, installCmd)
  69. .then((res: PluginDashboard) => {
  70. appEvents.emit(AppEvents.alertSuccess, ['Dashboard Imported', dash.title]);
  71. extend(dash, res);
  72. this.setState({ dashboards: [...this.state.dashboards] });
  73. });
  74. };
  75. remove = (dash: PluginDashboard) => {
  76. getBackendSrv()
  77. .delete('/api/dashboards/uid/' + dash.uid)
  78. .then(() => {
  79. dash.imported = false;
  80. this.setState({ dashboards: [...this.state.dashboards] });
  81. });
  82. };
  83. render() {
  84. const { loading, dashboards } = this.state;
  85. if (loading) {
  86. return <div>loading...</div>;
  87. }
  88. if (!dashboards || !dashboards.length) {
  89. return <div>No dashboards are included with this plugin</div>;
  90. }
  91. return (
  92. <div className="gf-form-group">
  93. <DashboardsTable dashboards={dashboards} onImport={this.import} onRemove={this.remove} />
  94. </div>
  95. );
  96. }
  97. }