Live: cleanup and simple changes (#28028)

This commit is contained in:
Ryan McKinley
2020-10-04 23:53:52 -07:00
committed by GitHub
parent 0ffd9a9a3c
commit 726bb4477b
12 changed files with 98 additions and 61 deletions

View File

@@ -62,7 +62,7 @@ export class LivePanel extends PureComponent<Props, State> {
startSubscription = () => {
const { scope, namespace, path } = this.props;
const channel = getGrafanaLiveSrv().getChannel(scope, namespace, path);
const channel = getGrafanaLiveSrv().getChannel({ scope, namespace, path });
if (this.state.channel === channel) {
return; // no change!
}

View File

@@ -1,12 +1,12 @@
import {
LiveChannelConfig,
LiveChannel,
LiveChannelScope,
LiveChannelStatusEvent,
LiveChannelEvent,
LiveChannelEventType,
LiveChannelConnectionState,
LiveChannelPresenceStatus,
LiveChannelAddress,
} from '@grafana/data';
import Centrifuge, {
JoinLeaveContext,
@@ -26,9 +26,7 @@ export class CentrifugeLiveChannel<TMessage = any, TPublish = any> implements Li
readonly opened = Date.now();
readonly id: string;
readonly scope: LiveChannelScope;
readonly namespace: string;
readonly path: string;
readonly addr: LiveChannelAddress;
readonly stream = new Subject<LiveChannelEvent<TMessage>>();
@@ -37,11 +35,9 @@ export class CentrifugeLiveChannel<TMessage = any, TPublish = any> implements Li
subscription?: Centrifuge.Subscription;
shutdownCallback?: () => void;
constructor(id: string, scope: LiveChannelScope, namespace: string, path: string) {
constructor(id: string, addr: LiveChannelAddress) {
this.id = id;
this.scope = scope;
this.namespace = namespace;
this.path = path;
this.addr = addr;
this.currentStatus = {
type: LiveChannelEventType.Status,
id,
@@ -61,15 +57,25 @@ export class CentrifugeLiveChannel<TMessage = any, TPublish = any> implements Li
const events: SubscriptionEvents = {
// This means a message was received from the server
publish: (ctx: PublicationContext) => {
this.stream.next({
type: LiveChannelEventType.Message,
message: prepare(ctx.data),
});
try {
const message = prepare(ctx.data);
if (message) {
this.stream.next({
type: LiveChannelEventType.Message,
message,
});
}
// Clear any error messages
if (this.currentStatus.error) {
// Clear any error messages
if (this.currentStatus.error) {
this.currentStatus.timestamp = Date.now();
delete this.currentStatus.error;
this.sendStatus();
}
} catch (err) {
console.log('publish error', config.path, err);
this.currentStatus.error = err;
this.currentStatus.timestamp = Date.now();
delete this.currentStatus.error;
this.sendStatus();
}
},
@@ -81,6 +87,7 @@ export class CentrifugeLiveChannel<TMessage = any, TPublish = any> implements Li
subscribe: (ctx: SubscribeSuccessContext) => {
this.currentStatus.timestamp = Date.now();
this.currentStatus.state = LiveChannelConnectionState.Connected;
delete this.currentStatus.error;
this.sendStatus();
},
unsubscribe: (ctx: UnsubscribeContext) => {
@@ -159,19 +166,11 @@ export class CentrifugeLiveChannel<TMessage = any, TPublish = any> implements Li
}
}
export function getErrorChannel(
msg: string,
id: string,
scope: LiveChannelScope,
namespace: string,
path: string
): LiveChannel {
export function getErrorChannel(msg: string, id: string, addr: LiveChannelAddress): LiveChannel {
return {
id,
opened: Date.now(),
scope,
namespace,
path,
addr,
// return an error
getStream: () =>

View File

@@ -56,7 +56,11 @@ class DashboardWatcher {
// Check for changes
if (uid !== this.uid) {
this.leave();
this.channel = live.getChannel(LiveChannelScope.Grafana, 'dashboard', uid);
this.channel = live.getChannel({
scope: LiveChannelScope.Grafana,
namespace: 'dashboard',
path: uid,
});
this.channel.getStream().subscribe(this.observer);
this.uid = uid;
}

View File

@@ -2,7 +2,7 @@ import Centrifuge from 'centrifuge/dist/centrifuge.protobuf';
import SockJS from 'sockjs-client';
import { GrafanaLiveSrv, setGrafanaLiveSrv, getGrafanaLiveSrv, config } from '@grafana/runtime';
import { BehaviorSubject } from 'rxjs';
import { LiveChannel, LiveChannelScope } from '@grafana/data';
import { LiveChannel, LiveChannelScope, LiveChannelAddress } from '@grafana/data';
import { CentrifugeLiveChannel, getErrorChannel } from './channel';
import {
GrafanaLiveScope,
@@ -84,23 +84,19 @@ export class CentrifugeSrv implements GrafanaLiveSrv {
* Get a channel. If the scope, namespace, or path is invalid, a shutdown
* channel will be returned with an error state indicated in its status
*/
getChannel<TMessage, TPublish>(
scopeId: LiveChannelScope,
namespace: string,
path: string
): LiveChannel<TMessage, TPublish> {
const id = `${scopeId}/${namespace}/${path}`;
getChannel<TMessage, TPublish = any>(addr: LiveChannelAddress): LiveChannel<TMessage, TPublish> {
const id = `${addr.scope}/${addr.namespace}/${addr.path}`;
let channel = this.open.get(id);
if (channel != null) {
return channel;
}
const scope = this.scopes[scopeId];
const scope = this.scopes[addr.scope];
if (!scope) {
return getErrorChannel('invalid scope', id, scopeId, namespace, path);
return getErrorChannel('invalid scope', id, addr);
}
channel = new CentrifugeLiveChannel(id, scopeId, namespace, path);
channel = new CentrifugeLiveChannel(id, addr);
channel.shutdownCallback = () => {
this.open.delete(id); // remove it from the list of open channels
};
@@ -117,13 +113,14 @@ export class CentrifugeSrv implements GrafanaLiveSrv {
}
private async initChannel(scope: GrafanaLiveScope, channel: CentrifugeLiveChannel): Promise<void> {
const support = await scope.getChannelSupport(channel.namespace);
const { addr } = channel;
const support = await scope.getChannelSupport(addr.namespace);
if (!support) {
throw new Error(channel.namespace + 'does not support streaming');
throw new Error(channel.addr.namespace + 'does not support streaming');
}
const config = support.getChannelConfig(channel.path);
const config = support.getChannelConfig(addr.path);
if (!config) {
throw new Error('unknown path: ' + channel.path);
throw new Error('unknown path: ' + addr.path);
}
if (config.canPublish?.()) {
channel.publish = (data: any) => this.centrifuge.publish(channel.id, data);

View File

@@ -21,7 +21,7 @@ const samples: Array<SelectableValue<string>> = [
{ label: 'Show buckets', description: 'List the avaliable buckets (table)', value: 'buckets()' },
{
label: 'Simple query',
description: 'filter by measurment and field',
description: 'filter by measurement and field',
value: `from(bucket: "db/rp")
|> range(start: v.timeRangeStart, stop:v.timeRangeStop)
|> filter(fn: (r) =>