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