ExportStatus.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { css } from '@emotion/css';
  2. import React, { useEffect, useState } from 'react';
  3. import { GrafanaTheme2, isLiveChannelMessageEvent, isLiveChannelStatusEvent, LiveChannelScope } from '@grafana/data';
  4. import { getBackendSrv, getGrafanaLiveSrv } from '@grafana/runtime';
  5. import { Button, useTheme2 } from '@grafana/ui';
  6. import { ExportStartButton } from './ExportStartButton';
  7. interface ExportStatusMessage {
  8. running: boolean;
  9. target: string;
  10. started: number;
  11. finished: number;
  12. update: number;
  13. count: number;
  14. current: number;
  15. last: string;
  16. status: string;
  17. }
  18. export const ExportStatus = () => {
  19. const styles = getStyles(useTheme2());
  20. const [status, setStatus] = useState<ExportStatusMessage>();
  21. useEffect(() => {
  22. const subscription = getGrafanaLiveSrv()
  23. .getStream<ExportStatusMessage>({
  24. scope: LiveChannelScope.Grafana,
  25. namespace: 'broadcast',
  26. path: 'export',
  27. })
  28. .subscribe({
  29. next: (evt) => {
  30. if (isLiveChannelMessageEvent(evt)) {
  31. setStatus(evt.message);
  32. } else if (isLiveChannelStatusEvent(evt)) {
  33. setStatus(evt.message);
  34. }
  35. },
  36. });
  37. return () => {
  38. subscription.unsubscribe();
  39. };
  40. }, []);
  41. if (!status) {
  42. return (
  43. <div className={styles.wrap}>
  44. <ExportStartButton />
  45. </div>
  46. );
  47. }
  48. return (
  49. <div className={styles.wrap}>
  50. <pre>{JSON.stringify(status, null, 2)}</pre>
  51. {Boolean(!status.running) && <ExportStartButton />}
  52. {Boolean(status.running) && (
  53. <Button
  54. variant="secondary"
  55. onClick={() => {
  56. getBackendSrv().post('/api/admin/export/stop');
  57. }}
  58. >
  59. Stop
  60. </Button>
  61. )}
  62. </div>
  63. );
  64. };
  65. const getStyles = (theme: GrafanaTheme2) => {
  66. return {
  67. wrap: css`
  68. border: 4px solid red;
  69. `,
  70. running: css`
  71. border: 4px solid green;
  72. `,
  73. };
  74. };