Live: support streaming results out-of-the-box (#32821)

This commit is contained in:
Ryan McKinley
2021-04-09 21:17:22 +02:00
committed by GitHub
parent 2d7e980da7
commit b96e45299d
20 changed files with 179 additions and 242 deletions
+19 -10
View File
@@ -4,12 +4,14 @@ import (
"context"
"fmt"
"net/http"
"strconv"
"sync"
"time"
"github.com/centrifugal/centrifuge"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/live"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
@@ -306,11 +308,11 @@ func publishStatusToHTTPError(status backend.PublishStreamStatus) (int, string)
}
// GetChannelHandler gives thread-safe access to the channel.
func (g *GrafanaLive) GetChannelHandler(user *models.SignedInUser, channel string) (models.ChannelHandler, Channel, error) {
func (g *GrafanaLive) GetChannelHandler(user *models.SignedInUser, channel string) (models.ChannelHandler, live.Channel, error) {
// Parse the identifier ${scope}/${namespace}/${path}
addr := ParseChannel(channel)
addr := live.ParseChannel(channel)
if !addr.IsValid() {
return nil, Channel{}, fmt.Errorf("invalid channel: %q", channel)
return nil, live.Channel{}, fmt.Errorf("invalid channel: %q", channel)
}
g.channelsMu.RLock()
@@ -349,15 +351,15 @@ func (g *GrafanaLive) GetChannelHandler(user *models.SignedInUser, channel strin
// It gives thread-safe access to the channel.
func (g *GrafanaLive) GetChannelHandlerFactory(user *models.SignedInUser, scope string, namespace string) (models.ChannelHandlerFactory, error) {
switch scope {
case ScopeGrafana:
case live.ScopeGrafana:
return g.handleGrafanaScope(user, namespace)
case ScopePlugin:
case live.ScopePlugin:
return g.handlePluginScope(user, namespace)
case ScopeDatasource:
case live.ScopeDatasource:
return g.handleDatasourceScope(user, namespace)
case ScopeStream:
case live.ScopeStream:
return g.handleStreamScope(user, namespace)
case ScopePush:
case live.ScopePush:
return g.handlePushScope(user, namespace)
default:
return nil, fmt.Errorf("invalid scope: %q", scope)
@@ -403,7 +405,14 @@ func (g *GrafanaLive) handlePushScope(_ *models.SignedInUser, namespace string)
func (g *GrafanaLive) handleDatasourceScope(user *models.SignedInUser, namespace string) (models.ChannelHandlerFactory, error) {
ds, err := g.DatasourceCache.GetDatasourceByUID(namespace, user, false)
if err != nil {
return nil, fmt.Errorf("error getting datasource: %w", err)
// the namespace may be an ID
id, _ := strconv.ParseInt(namespace, 10, 64)
if id > 0 {
ds, err = g.DatasourceCache.GetDatasource(id, user, false)
}
if err != nil {
return nil, fmt.Errorf("error getting datasource: %w", err)
}
}
streamHandler, err := g.getStreamPlugin(ds.Type)
if err != nil {
@@ -430,7 +439,7 @@ func (g *GrafanaLive) IsEnabled() bool {
}
func (g *GrafanaLive) HandleHTTPPublish(ctx *models.ReqContext, cmd dtos.LivePublishCmd) response.Response {
addr := ParseChannel(cmd.Channel)
addr := live.ParseChannel(cmd.Channel)
if !addr.IsValid() {
return response.Error(http.StatusBadRequest, "Bad channel address", nil)
}