1import {
2  DataFrame,
3  DataQueryResponse,
4  LiveChannelAddress,
5  LiveChannelEvent,
6  LiveChannelPresenceStatus,
7  StreamingFrameOptions,
8} from '@grafana/data';
9import { Observable } from 'rxjs';
10
11/**
12 * @alpha -- experimental
13 */
14export interface LiveDataFilter {
15  fields?: string[];
16}
17
18/**
19 * @alpha
20 */
21export interface LiveDataStreamOptions {
22  addr: LiveChannelAddress;
23  frame?: DataFrame; // initial results
24  key?: string;
25  buffer?: StreamingFrameOptions;
26  filter?: LiveDataFilter;
27}
28
29/**
30 * @alpha -- experimental
31 */
32export interface GrafanaLiveSrv {
33  /**
34   * Listen for changes to the main service
35   */
36  getConnectionState(): Observable<boolean>;
37
38  /**
39   * Watch for messages in a channel
40   */
41  getStream<T>(address: LiveChannelAddress): Observable<LiveChannelEvent<T>>;
42
43  /**
44   * Connect to a channel and return results as DataFrames
45   */
46  getDataStream(options: LiveDataStreamOptions): Observable<DataQueryResponse>;
47
48  /**
49   * For channels that support presence, this will request the current state from the server.
50   *
51   * Join and leave messages will be sent to the open stream
52   */
53  getPresence(address: LiveChannelAddress): Promise<LiveChannelPresenceStatus>;
54
55  /**
56   * Publish into a channel
57   *
58   * @alpha -- experimental
59   */
60  publish(address: LiveChannelAddress, data: any): Promise<any>;
61}
62
63let singletonInstance: GrafanaLiveSrv;
64
65/**
66 * Used during startup by Grafana to set the GrafanaLiveSrv so it is available
67 * via the {@link getGrafanaLiveSrv} to the rest of the application.
68 *
69 * @internal
70 */
71export const setGrafanaLiveSrv = (instance: GrafanaLiveSrv) => {
72  singletonInstance = instance;
73};
74
75/**
76 * Used to retrieve the GrafanaLiveSrv that allows you to subscribe to
77 * server side events and streams
78 *
79 * @alpha -- experimental
80 */
81export const getGrafanaLiveSrv = (): GrafanaLiveSrv => singletonInstance;
82