forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
91 lines (74 loc) · 2.03 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package events
import (
"reflect"
"time"
)
// Events can be passed to external systems via for example AMQP
// Treat these events as basically DTOs so changes has to be backward compatible
type Priority string
const (
PRIO_DEBUG Priority = "DEBUG"
PRIO_INFO Priority = "INFO"
PRIO_ERROR Priority = "ERROR"
)
type Event struct {
Timestamp time.Time `json:"timestamp"`
}
type OnTheWireEvent struct {
EventType string `json:"event_type"`
Priority Priority `json:"priority"`
Timestamp time.Time `json:"timestamp"`
Payload interface{} `json:"payload"`
}
type EventBase interface {
ToOnWriteEvent() *OnTheWireEvent
}
func ToOnWriteEvent(event interface{}) (*OnTheWireEvent, error) {
eventType := reflect.TypeOf(event).Elem()
wireEvent := OnTheWireEvent{
Priority: PRIO_INFO,
EventType: eventType.Name(),
Payload: event,
}
baseField := reflect.Indirect(reflect.ValueOf(event)).FieldByName("Timestamp")
if baseField.IsValid() {
wireEvent.Timestamp = baseField.Interface().(time.Time)
} else {
wireEvent.Timestamp = time.Now()
}
return &wireEvent, nil
}
type OrgCreated struct {
Timestamp time.Time `json:"timestamp"`
Id int64 `json:"id"`
Name string `json:"name"`
}
type OrgUpdated struct {
Timestamp time.Time `json:"timestamp"`
Id int64 `json:"id"`
Name string `json:"name"`
}
type UserCreated struct {
Timestamp time.Time `json:"timestamp"`
Id int64 `json:"id"`
Name string `json:"name"`
Login string `json:"login"`
Email string `json:"email"`
}
type SignUpStarted struct {
Timestamp time.Time `json:"timestamp"`
Email string `json:"email"`
Code string `json:"code"`
}
type SignUpCompleted struct {
Timestamp time.Time `json:"timestamp"`
Name string `json:"name"`
Email string `json:"email"`
}
type UserUpdated struct {
Timestamp time.Time `json:"timestamp"`
Id int64 `json:"id"`
Name string `json:"name"`
Login string `json:"login"`
Email string `json:"email"`
}