index.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { config, getBackendSrv, getGrafanaLiveSrv, setGrafanaLiveSrv } from '@grafana/runtime';
  2. import { liveTimer } from 'app/features/dashboard/dashgrid/liveTimer';
  3. import { contextSrv } from '../../core/services/context_srv';
  4. import { CentrifugeService } from './centrifuge/service';
  5. import { CentrifugeServiceWorkerProxy } from './centrifuge/serviceWorkerProxy';
  6. import { GrafanaLiveService } from './live';
  7. export const sessionId =
  8. (window as any)?.grafanaBootData?.user?.id +
  9. '/' +
  10. Date.now().toString(16) +
  11. '/' +
  12. Math.random().toString(36).substring(2, 15);
  13. export function initGrafanaLive() {
  14. const centrifugeServiceDeps = {
  15. appUrl: `${window.location.origin}${config.appSubUrl}`,
  16. orgId: contextSrv.user.orgId,
  17. orgRole: contextSrv.user.orgRole,
  18. liveEnabled: config.liveEnabled,
  19. sessionId,
  20. dataStreamSubscriberReadiness: liveTimer.ok.asObservable(),
  21. };
  22. const centrifugeSrv = config.featureToggles['live-service-web-worker']
  23. ? new CentrifugeServiceWorkerProxy(centrifugeServiceDeps)
  24. : new CentrifugeService(centrifugeServiceDeps);
  25. setGrafanaLiveSrv(
  26. new GrafanaLiveService({
  27. centrifugeSrv,
  28. backendSrv: getBackendSrv(),
  29. })
  30. );
  31. }
  32. export function getGrafanaLiveCentrifugeSrv() {
  33. return getGrafanaLiveSrv() as GrafanaLiveService;
  34. }