2020-11-07 20:06:48 -09:00
|
|
|
package events
|
|
|
|
|
|
2021-02-04 08:34:27 -09:00
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
"unicode"
|
|
|
|
|
)
|
2020-11-13 14:40:33 -09:00
|
|
|
|
2020-11-07 20:06:48 -09:00
|
|
|
type Event interface {
|
2021-06-09 17:02:20 -08:00
|
|
|
Name(Event) string
|
|
|
|
|
Data(Event) string
|
2021-02-04 08:34:27 -09:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 17:02:20 -08:00
|
|
|
type baseEvent struct{}
|
2021-02-04 08:34:27 -09:00
|
|
|
|
2021-06-09 17:02:20 -08:00
|
|
|
func (e *baseEvent) Name(evt Event) string {
|
2021-02-04 08:34:27 -09:00
|
|
|
str := strings.TrimPrefix(reflect.TypeOf(evt).String(), "*events.")
|
2021-06-09 17:02:20 -08:00
|
|
|
return str[:0] + string(unicode.ToLower(rune(str[0]))) + str[1:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *baseEvent) Data(evt Event) string {
|
2021-02-04 08:34:27 -09:00
|
|
|
data, _ := json.Marshal(evt)
|
|
|
|
|
return string(data)
|
2020-11-07 20:06:48 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ScanStatus struct {
|
2021-02-04 08:34:27 -09:00
|
|
|
baseEvent
|
2020-11-15 20:33:17 -09:00
|
|
|
Scanning bool `json:"scanning"`
|
|
|
|
|
Count int64 `json:"count"`
|
|
|
|
|
FolderCount int64 `json:"folderCount"`
|
2020-11-07 20:06:48 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type KeepAlive struct {
|
2021-02-04 08:34:27 -09:00
|
|
|
baseEvent
|
2020-11-07 20:06:48 -09:00
|
|
|
TS int64 `json:"ts"`
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 12:09:01 -08:00
|
|
|
type ServerStart struct {
|
2021-06-09 17:02:20 -08:00
|
|
|
baseEvent
|
2021-06-15 12:09:01 -08:00
|
|
|
StartTime time.Time `json:"startTime"`
|
2021-06-21 09:42:14 -08:00
|
|
|
Version string `json:"version"`
|
2021-06-09 17:02:20 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-15 12:09:01 -08:00
|
|
|
const Any = "*"
|
|
|
|
|
|
|
|
|
|
type RefreshResource struct {
|
2021-02-04 08:34:27 -09:00
|
|
|
baseEvent
|
2021-06-15 12:09:01 -08:00
|
|
|
resources map[string][]string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (rr *RefreshResource) With(resource string, ids ...string) *RefreshResource {
|
|
|
|
|
if rr.resources == nil {
|
|
|
|
|
rr.resources = make(map[string][]string)
|
|
|
|
|
}
|
2021-06-16 06:23:34 -08:00
|
|
|
if len(ids) == 0 {
|
|
|
|
|
rr.resources[resource] = append(rr.resources[resource], Any)
|
|
|
|
|
}
|
2023-02-02 07:10:28 -09:00
|
|
|
rr.resources[resource] = append(rr.resources[resource], ids...)
|
2021-06-15 12:09:01 -08:00
|
|
|
return rr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (rr *RefreshResource) Data(evt Event) string {
|
|
|
|
|
if rr.resources == nil {
|
|
|
|
|
return `{"*":"*"}`
|
|
|
|
|
}
|
|
|
|
|
r := evt.(*RefreshResource)
|
|
|
|
|
data, _ := json.Marshal(r.resources)
|
|
|
|
|
return string(data)
|
2020-11-13 14:40:33 -09:00
|
|
|
}
|