1package models
2
3import (
4	"context"
5	"encoding/json"
6	"time"
7
8	"github.com/grafana/grafana-plugin-sdk-go/backend"
9)
10
11// ChannelPublisher writes data into a channel. Note that permissions are not checked.
12type ChannelPublisher func(orgID int64, channel string, data []byte) error
13
14// ChannelClientCount will return the number of clients for a channel
15type ChannelClientCount func(orgID int64, channel string) (int, error)
16
17// SubscribeEvent contains subscription data.
18type SubscribeEvent struct {
19	Channel string
20	Path    string
21}
22
23// SubscribeReply is a reaction to SubscribeEvent.
24type SubscribeReply struct {
25	Presence  bool
26	JoinLeave bool
27	Recover   bool
28	Data      json.RawMessage
29}
30
31// PublishEvent contains publication data.
32type PublishEvent struct {
33	Channel string
34	Path    string
35	Data    json.RawMessage
36}
37
38// PublishReply is a reaction to PublishEvent.
39type PublishReply struct {
40	// By default, it's a handler responsibility to publish data
41	// into a stream upon OnPublish but returning a data here
42	// will make Grafana Live publish data itself (i.e. stream handler
43	// just works as permission proxy in this case).
44	Data json.RawMessage
45	// HistorySize sets a stream history size.
46	HistorySize int
47	// HistoryTTL is a time that messages will live in stream history.
48	HistoryTTL time.Duration
49}
50
51// ChannelHandler defines the core channel behavior
52type ChannelHandler interface {
53	// OnSubscribe is called when a client wants to subscribe to a channel
54	OnSubscribe(ctx context.Context, user *SignedInUser, e SubscribeEvent) (SubscribeReply, backend.SubscribeStreamStatus, error)
55
56	// OnPublish is called when a client writes a message to the channel websocket.
57	OnPublish(ctx context.Context, user *SignedInUser, e PublishEvent) (PublishReply, backend.PublishStreamStatus, error)
58}
59
60// ChannelHandlerFactory should be implemented by all core features.
61type ChannelHandlerFactory interface {
62	// GetHandlerForPath gets a ChannelHandler for a path.
63	// This is called fast and often -- it must be synchronized
64	GetHandlerForPath(path string) (ChannelHandler, error)
65}
66
67// DashboardActivityChannel is a service to advertise dashboard activity
68type DashboardActivityChannel interface {
69	// Called when a dashboard is saved -- this includes the error so we can support a
70	// gitops workflow that knows if the value was saved to the local database or not
71	// in many cases all direct save requests will fail, but the request should be forwarded
72	// to any gitops observers
73	DashboardSaved(orgID int64, user *UserDisplayDTO, message string, dashboard *Dashboard, err error) error
74
75	// Called when a dashboard is deleted
76	DashboardDeleted(orgID int64, user *UserDisplayDTO, uid string) error
77
78	// Experimental! Indicate is GitOps is active.  This really means
79	// someone is subscribed to the `grafana/dashboards/gitops` channel
80	HasGitOpsObserver(orgID int64) bool
81}
82
83type LiveMessage struct {
84	Id        int64
85	OrgId     int64
86	Channel   string
87	Data      json.RawMessage
88	Published time.Time
89}
90
91type SaveLiveMessageQuery struct {
92	OrgId   int64
93	Channel string
94	Data    json.RawMessage
95}
96
97type GetLiveMessageQuery struct {
98	OrgId   int64
99	Channel string
100}
101