Live: expose publish on live interfaces (#33596)

This commit is contained in:
Ryan McKinley 2021-04-30 17:35:03 -07:00 committed by GitHub
parent 69f2a43063
commit 528edcaec6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 21 deletions

View File

@ -62,6 +62,13 @@ export interface GrafanaLiveSrv {
* Join and leave messages will be sent to the open stream * Join and leave messages will be sent to the open stream
*/ */
getPresence(address: LiveChannelAddress): Promise<LiveChannelPresenceStatus>; getPresence(address: LiveChannelAddress): Promise<LiveChannelPresenceStatus>;
/**
* Publish into a channel
*
* @alpha -- experimental
*/
publish(address: LiveChannelAddress, data: any): Promise<any>;
} }
let singletonInstance: GrafanaLiveSrv; let singletonInstance: GrafanaLiveSrv;

View File

@ -5,10 +5,10 @@ import {
AppEvents, AppEvents,
isLiveChannelMessageEvent, isLiveChannelMessageEvent,
isLiveChannelStatusEvent, isLiveChannelStatusEvent,
LiveChannelAddress,
LiveChannelConnectionState, LiveChannelConnectionState,
LiveChannelEvent, LiveChannelEvent,
LiveChannelScope, LiveChannelScope,
toLiveChannelId,
} from '@grafana/data'; } from '@grafana/data';
import { DashboardChangedModal } from './DashboardChangedModal'; import { DashboardChangedModal } from './DashboardChangedModal';
import { DashboardEvent, DashboardEventAction } from './types'; import { DashboardEvent, DashboardEventAction } from './types';
@ -16,10 +16,9 @@ import { CoreGrafanaLiveFeature } from '../scopes';
import { sessionId } from '../live'; import { sessionId } from '../live';
import { ShowModalReactEvent } from '../../../types/events'; import { ShowModalReactEvent } from '../../../types/events';
import { Unsubscribable } from 'rxjs'; import { Unsubscribable } from 'rxjs';
import { getBackendSrv } from 'app/core/services/backend_srv';
class DashboardWatcher { class DashboardWatcher {
channel?: string; // path to the channel channel?: LiveChannelAddress; // path to the channel
uid?: string; uid?: string;
ignoreSave?: boolean; ignoreSave?: boolean;
editing = false; editing = false;
@ -36,15 +35,13 @@ class DashboardWatcher {
} }
private sendEditingState() { private sendEditingState() {
if (this.channel && this.uid) { const { channel, uid } = this;
getBackendSrv().post(`api/live/publish`, { if (channel && uid) {
channel: this.channel, getGrafanaLiveSrv().publish(channel, {
data: { sessionId,
sessionId, uid,
uid: this.uid, action: this.editing ? DashboardEventAction.EditingStarted : DashboardEventAction.EditingCanceled,
action: this.editing ? DashboardEventAction.EditingStarted : DashboardEventAction.EditingCanceled, timestamp: Date.now(),
timestamp: Date.now(),
},
}); });
} }
} }
@ -57,17 +54,16 @@ class DashboardWatcher {
// Check for changes // Check for changes
if (uid !== this.uid) { if (uid !== this.uid) {
const addr = { this.channel = {
scope: LiveChannelScope.Grafana, scope: LiveChannelScope.Grafana,
namespace: 'dashboard', namespace: 'dashboard',
path: `uid/${uid}`, path: `uid/${uid}`,
}; };
this.leave(); this.leave();
if (uid) { if (uid) {
this.subscription = live.getStream(addr).subscribe(this.observer); this.subscription = live.getStream(this.channel).subscribe(this.observer);
} }
this.uid = uid; this.uid = uid;
this.channel = toLiveChannelId(addr);
} }
} }

View File

@ -6,6 +6,7 @@ import {
config, config,
LiveDataStreamOptions, LiveDataStreamOptions,
toDataQueryError, toDataQueryError,
getBackendSrv,
} from '@grafana/runtime'; } from '@grafana/runtime';
import { BehaviorSubject, Observable, of } from 'rxjs'; import { BehaviorSubject, Observable, of } from 'rxjs';
import { import {
@ -24,6 +25,7 @@ import {
dataFrameToJSON, dataFrameToJSON,
isLiveChannelMessageEvent, isLiveChannelMessageEvent,
isLiveChannelStatusEvent, isLiveChannelStatusEvent,
toLiveChannelId,
} from '@grafana/data'; } from '@grafana/data';
import { CentrifugeLiveChannel, getErrorChannel } from './channel'; import { CentrifugeLiveChannel, getErrorChannel } from './channel';
import { import {
@ -313,6 +315,18 @@ export class CentrifugeSrv implements GrafanaLiveSrv {
getPresence(address: LiveChannelAddress): Promise<LiveChannelPresenceStatus> { getPresence(address: LiveChannelAddress): Promise<LiveChannelPresenceStatus> {
return this.getChannel(address).getPresence(); return this.getChannel(address).getPresence();
} }
/**
* Publish into a channel
*
* @alpha -- experimental
*/
publish(address: LiveChannelAddress, data: any): Promise<any> {
return getBackendSrv().post(`api/live/publish`, {
channel: toLiveChannelId(address),
data,
});
}
} }
// This is used to give a unique key for each stream. The actual value does not matter // This is used to give a unique key for each stream. The actual value does not matter

View File

@ -16,11 +16,10 @@ import {
StreamingDataFrame, StreamingDataFrame,
LiveChannelAddress, LiveChannelAddress,
LiveChannelConfig, LiveChannelConfig,
toLiveChannelId,
} from '@grafana/data'; } from '@grafana/data';
import { TablePanel } from '../table/TablePanel'; import { TablePanel } from '../table/TablePanel';
import { LivePanelOptions, MessageDisplayMode } from './types'; import { LivePanelOptions, MessageDisplayMode } from './types';
import { config, getBackendSrv, getGrafanaLiveSrv } from '@grafana/runtime'; import { config, getGrafanaLiveSrv } from '@grafana/runtime';
import { css, cx } from '@emotion/css'; import { css, cx } from '@emotion/css';
import { isEqual } from 'lodash'; import { isEqual } from 'lodash';
@ -159,10 +158,7 @@ export class LivePanel extends PureComponent<Props, State> {
return; return;
} }
const rsp = await getBackendSrv().post(`api/live/publish`, { const rsp = await getGrafanaLiveSrv().publish(addr, data);
channel: toLiveChannelId(addr),
data,
});
console.log('onPublishClicked (response from publish)', rsp); console.log('onPublishClicked (response from publish)', rsp);
}; };

View File

@ -8571,6 +8571,11 @@ caniuse-api@^3.0.0:
lodash.memoize "^4.1.2" lodash.memoize "^4.1.2"
lodash.uniq "^4.5.0" lodash.uniq "^4.5.0"
caniuse-db@1.0.30000772:
version "1.0.30000772"
resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30000772.tgz#51aae891768286eade4a3d8319ea76d6a01b512b"
integrity sha1-UarokXaChureSj2DGep21qAbUSs=
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001020, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001125, caniuse-lite@^1.0.30001181: caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001020, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001125, caniuse-lite@^1.0.30001181:
version "1.0.30001205" version "1.0.30001205"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001205.tgz" resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001205.tgz"