-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Live: broadcast events when dashboard is saved (#27583)
Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> Co-authored-by: Torkel Ödegaard <torkel@grafana.org>
- Loading branch information
1 parent
44c9aea
commit 8a5fc00
Showing
28 changed files
with
974 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package models | ||
|
||
import "github.com/centrifugal/centrifuge" | ||
|
||
// ChannelPublisher writes data into a channel | ||
type ChannelPublisher func(channel string, data []byte) error | ||
|
||
// ChannelHandler defines the core channel behavior | ||
type ChannelHandler interface { | ||
// This is called fast and often -- it must be synchrnozed | ||
GetChannelOptions(id string) centrifuge.ChannelOptions | ||
|
||
// Called when a client wants to subscribe to a channel | ||
OnSubscribe(c *centrifuge.Client, e centrifuge.SubscribeEvent) error | ||
|
||
// Called when something writes into the channel. The returned value will be broadcast if len() > 0 | ||
OnPublish(c *centrifuge.Client, e centrifuge.PublishEvent) ([]byte, error) | ||
} | ||
|
||
// ChannelHandlerProvider -- this should be implemented by any core feature | ||
type ChannelHandlerProvider interface { | ||
// This is called fast and often -- it must be synchrnozed | ||
GetHandlerForPath(path string) (ChannelHandler, error) | ||
} | ||
|
||
// DashboardActivityChannel is a service to advertise dashboard activity | ||
type DashboardActivityChannel interface { | ||
DashboardSaved(uid string, userID int64) error | ||
DashboardDeleted(uid string, userID int64) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package live | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// ChannelIdentifier is the channel id split by parts | ||
type ChannelIdentifier struct { | ||
Scope string // grafana, ds, or plugin | ||
Namespace string // feature, id, or name | ||
Path string // path within the channel handler | ||
} | ||
|
||
// ParseChannelIdentifier parses the parts from a channel id: | ||
// ${scope} / ${namespace} / ${path} | ||
func ParseChannelIdentifier(id string) (ChannelIdentifier, error) { | ||
parts := strings.SplitN(id, "/", 3) | ||
if len(parts) == 3 { | ||
return ChannelIdentifier{ | ||
Scope: parts[0], | ||
Namespace: parts[1], | ||
Path: parts[2], | ||
}, nil | ||
} | ||
return ChannelIdentifier{}, fmt.Errorf("Invalid channel id: %s", id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package live | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestParseChannelIdentifier(t *testing.T) { | ||
ident, err := ParseChannelIdentifier("aaa/bbb/ccc/ddd") | ||
if err != nil { | ||
t.FailNow() | ||
} | ||
|
||
ex := ChannelIdentifier{ | ||
Scope: "aaa", | ||
Namespace: "bbb", | ||
Path: "ccc/ddd", | ||
} | ||
|
||
if diff := cmp.Diff(ident, ex); diff != "" { | ||
t.Fatalf("Result mismatch (-want +got):\n%s", diff) | ||
} | ||
|
||
// Check an invalid identifier | ||
_, err = ParseChannelIdentifier("aaa/bbb") | ||
if err == nil { | ||
t.FailNow() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package features | ||
|
||
import ( | ||
"github.com/centrifugal/centrifuge" | ||
"github.com/grafana/grafana/pkg/models" | ||
) | ||
|
||
// BroadcastRunner will simply broadcast all events to `grafana/broadcast/*` channels | ||
// This makes no assumptions about the shape of the data and will broadcast it to anyone listening | ||
type BroadcastRunner struct{} | ||
|
||
// GetHandlerForPath called on init | ||
func (g *BroadcastRunner) GetHandlerForPath(path string) (models.ChannelHandler, error) { | ||
return g, nil // for now all channels share config | ||
} | ||
|
||
// GetChannelOptions called fast and often | ||
func (g *BroadcastRunner) GetChannelOptions(id string) centrifuge.ChannelOptions { | ||
return centrifuge.ChannelOptions{} | ||
} | ||
|
||
// OnSubscribe for now allows anyone to subscribe to any dashboard | ||
func (g *BroadcastRunner) OnSubscribe(c *centrifuge.Client, e centrifuge.SubscribeEvent) error { | ||
// anyone can subscribe | ||
return nil | ||
} | ||
|
||
// OnPublish called when an event is received from the websocket | ||
func (g *BroadcastRunner) OnPublish(c *centrifuge.Client, e centrifuge.PublishEvent) ([]byte, error) { | ||
// expect the data to be the right shape? | ||
return e.Data, nil | ||
} |
Oops, something went wrong.