grafana/public/app/features/live/live.ts

63 lines
1.7 KiB
TypeScript
Raw Normal View History

import { BackendSrv, GrafanaLiveSrv, LiveDataStreamOptions } from '@grafana/runtime';
import { CentrifugeSrv } from './centrifuge/service';
import { Observable } from 'rxjs';
2021-04-23 16:21:38 -05:00
import {
DataQueryResponse,
2021-04-23 16:21:38 -05:00
LiveChannelAddress,
LiveChannelEvent,
LiveChannelPresenceStatus,
toLiveChannelId,
2021-04-23 16:21:38 -05:00
} from '@grafana/data';
type GrafanaLiveServiceDeps = {
centrifugeSrv: CentrifugeSrv;
backendSrv: BackendSrv;
};
2020-09-23 10:02:01 -05:00
export class GrafanaLiveService implements GrafanaLiveSrv {
constructor(private deps: GrafanaLiveServiceDeps) {}
/**
* Listen for changes to the connection state
*/
getConnectionState(): Observable<boolean> {
return this.deps.centrifugeSrv.getConnectionState();
}
2020-09-23 10:02:01 -05:00
/**
* Connect to a channel and return results as DataFrames
2020-09-23 10:02:01 -05:00
*/
getDataStream(options: LiveDataStreamOptions): Observable<DataQueryResponse> {
return this.deps.centrifugeSrv.getDataStream(options);
2020-09-23 10:02:01 -05:00
}
/**
* Watch for messages in a channel
*/
getStream<T>(address: LiveChannelAddress): Observable<LiveChannelEvent<T>> {
return this.deps.centrifugeSrv.getStream<T>(address);
2020-09-23 10:02:01 -05:00
}
/**
* Publish into a channel
*
* @alpha -- experimental
*/
async publish(address: LiveChannelAddress, data: any): Promise<any> {
return this.deps.backendSrv.post(`api/live/publish`, {
channel: toLiveChannelId(address), // orgId is from user
data,
});
}
/**
* For channels that support presence, this will request the current state from the server.
*
* Join and leave messages will be sent to the open stream
*/
async getPresence(address: LiveChannelAddress): Promise<LiveChannelPresenceStatus> {
return this.deps.centrifugeSrv.getPresence(address);
2021-04-23 16:21:38 -05:00
}
}