CrawlerStatus.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 { CrawlerStartButton } from './CrawlerStartButton';
  7. interface CrawlerStatusMessage {
  8. state: string;
  9. started: string;
  10. finished: string;
  11. complete: number;
  12. queue: number;
  13. last: string;
  14. }
  15. export const CrawlerStatus = () => {
  16. const styles = getStyles(useTheme2());
  17. const [status, setStatus] = useState<CrawlerStatusMessage>();
  18. useEffect(() => {
  19. const subscription = getGrafanaLiveSrv()
  20. .getStream<CrawlerStatusMessage>({
  21. scope: LiveChannelScope.Grafana,
  22. namespace: 'broadcast',
  23. path: 'crawler',
  24. })
  25. .subscribe({
  26. next: (evt) => {
  27. if (isLiveChannelMessageEvent(evt)) {
  28. setStatus(evt.message);
  29. } else if (isLiveChannelStatusEvent(evt)) {
  30. setStatus(evt.message);
  31. }
  32. },
  33. });
  34. return () => {
  35. subscription.unsubscribe();
  36. };
  37. }, []);
  38. if (!status) {
  39. return (
  40. <div className={styles.wrap}>
  41. No status (never run)
  42. <br />
  43. <CrawlerStartButton />
  44. </div>
  45. );
  46. }
  47. return (
  48. <div className={styles.wrap}>
  49. <pre>{JSON.stringify(status, null, 2)}</pre>
  50. {status.state !== 'running' && <CrawlerStartButton />}
  51. {status.state !== 'stopped' && (
  52. <Button
  53. variant="secondary"
  54. onClick={() => {
  55. getBackendSrv().post('/api/admin/crawler/stop');
  56. }}
  57. >
  58. Stop
  59. </Button>
  60. )}
  61. </div>
  62. );
  63. };
  64. const getStyles = (theme: GrafanaTheme2) => {
  65. return {
  66. wrap: css`
  67. border: 4px solid red;
  68. `,
  69. running: css`
  70. border: 4px solid green;
  71. `,
  72. };
  73. };