From 528edcaec650d755eb4776089146d50e1bdaf04a Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Fri, 30 Apr 2021 17:35:03 -0700 Subject: [PATCH] Live: expose publish on live interfaces (#33596) --- packages/grafana-runtime/src/services/live.ts | 7 +++++ .../live/dashboard/dashboardWatcher.ts | 26 ++++++++----------- public/app/features/live/live.ts | 14 ++++++++++ public/app/plugins/panel/live/LivePanel.tsx | 8 ++---- yarn.lock | 5 ++++ 5 files changed, 39 insertions(+), 21 deletions(-) diff --git a/packages/grafana-runtime/src/services/live.ts b/packages/grafana-runtime/src/services/live.ts index cf22c76655a..ce50bd71ecb 100644 --- a/packages/grafana-runtime/src/services/live.ts +++ b/packages/grafana-runtime/src/services/live.ts @@ -62,6 +62,13 @@ export interface GrafanaLiveSrv { * Join and leave messages will be sent to the open stream */ getPresence(address: LiveChannelAddress): Promise; + + /** + * Publish into a channel + * + * @alpha -- experimental + */ + publish(address: LiveChannelAddress, data: any): Promise; } let singletonInstance: GrafanaLiveSrv; diff --git a/public/app/features/live/dashboard/dashboardWatcher.ts b/public/app/features/live/dashboard/dashboardWatcher.ts index 34c2bb2448e..e8bf53ef365 100644 --- a/public/app/features/live/dashboard/dashboardWatcher.ts +++ b/public/app/features/live/dashboard/dashboardWatcher.ts @@ -5,10 +5,10 @@ import { AppEvents, isLiveChannelMessageEvent, isLiveChannelStatusEvent, + LiveChannelAddress, LiveChannelConnectionState, LiveChannelEvent, LiveChannelScope, - toLiveChannelId, } from '@grafana/data'; import { DashboardChangedModal } from './DashboardChangedModal'; import { DashboardEvent, DashboardEventAction } from './types'; @@ -16,10 +16,9 @@ import { CoreGrafanaLiveFeature } from '../scopes'; import { sessionId } from '../live'; import { ShowModalReactEvent } from '../../../types/events'; import { Unsubscribable } from 'rxjs'; -import { getBackendSrv } from 'app/core/services/backend_srv'; class DashboardWatcher { - channel?: string; // path to the channel + channel?: LiveChannelAddress; // path to the channel uid?: string; ignoreSave?: boolean; editing = false; @@ -36,15 +35,13 @@ class DashboardWatcher { } private sendEditingState() { - if (this.channel && this.uid) { - getBackendSrv().post(`api/live/publish`, { - channel: this.channel, - data: { - sessionId, - uid: this.uid, - action: this.editing ? DashboardEventAction.EditingStarted : DashboardEventAction.EditingCanceled, - timestamp: Date.now(), - }, + const { channel, uid } = this; + if (channel && uid) { + getGrafanaLiveSrv().publish(channel, { + sessionId, + uid, + action: this.editing ? DashboardEventAction.EditingStarted : DashboardEventAction.EditingCanceled, + timestamp: Date.now(), }); } } @@ -57,17 +54,16 @@ class DashboardWatcher { // Check for changes if (uid !== this.uid) { - const addr = { + this.channel = { scope: LiveChannelScope.Grafana, namespace: 'dashboard', path: `uid/${uid}`, }; this.leave(); if (uid) { - this.subscription = live.getStream(addr).subscribe(this.observer); + this.subscription = live.getStream(this.channel).subscribe(this.observer); } this.uid = uid; - this.channel = toLiveChannelId(addr); } } diff --git a/public/app/features/live/live.ts b/public/app/features/live/live.ts index c62957279de..aab6a25dac5 100644 --- a/public/app/features/live/live.ts +++ b/public/app/features/live/live.ts @@ -6,6 +6,7 @@ import { config, LiveDataStreamOptions, toDataQueryError, + getBackendSrv, } from '@grafana/runtime'; import { BehaviorSubject, Observable, of } from 'rxjs'; import { @@ -24,6 +25,7 @@ import { dataFrameToJSON, isLiveChannelMessageEvent, isLiveChannelStatusEvent, + toLiveChannelId, } from '@grafana/data'; import { CentrifugeLiveChannel, getErrorChannel } from './channel'; import { @@ -313,6 +315,18 @@ export class CentrifugeSrv implements GrafanaLiveSrv { getPresence(address: LiveChannelAddress): Promise { return this.getChannel(address).getPresence(); } + + /** + * Publish into a channel + * + * @alpha -- experimental + */ + publish(address: LiveChannelAddress, data: any): Promise { + 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 diff --git a/public/app/plugins/panel/live/LivePanel.tsx b/public/app/plugins/panel/live/LivePanel.tsx index 578a30c6cee..bb0d3301221 100755 --- a/public/app/plugins/panel/live/LivePanel.tsx +++ b/public/app/plugins/panel/live/LivePanel.tsx @@ -16,11 +16,10 @@ import { StreamingDataFrame, LiveChannelAddress, LiveChannelConfig, - toLiveChannelId, } from '@grafana/data'; import { TablePanel } from '../table/TablePanel'; 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 { isEqual } from 'lodash'; @@ -159,10 +158,7 @@ export class LivePanel extends PureComponent { return; } - const rsp = await getBackendSrv().post(`api/live/publish`, { - channel: toLiveChannelId(addr), - data, - }); + const rsp = await getGrafanaLiveSrv().publish(addr, data); console.log('onPublishClicked (response from publish)', rsp); }; diff --git a/yarn.lock b/yarn.lock index c6c945b8d08..53f7f6efc1c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8571,6 +8571,11 @@ caniuse-api@^3.0.0: lodash.memoize "^4.1.2" 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: version "1.0.30001205" resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001205.tgz"