-
Notifications
You must be signed in to change notification settings - Fork 1
/
getters.go
175 lines (140 loc) · 3.82 KB
/
getters.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package query
import (
"errors"
"fmt"
"time"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/google/uuid"
"github.com/patrickmn/go-cache"
"gorm.io/gorm"
)
var (
// getterCache caches the results for all the getters in this file.
getterCache = cache.New(time.Second*90, time.Minute*5)
immutableCache = cache.New(cache.NoExpiration, time.Hour*12)
)
func FlushGettersCache() {
getterCache.Flush()
immutableCache.Flush()
}
type GetterOption uint8
const (
GetterOptionNoCache GetterOption = iota + 1
)
type GetterOptions []GetterOption
func (t GetterOptions) IsSet(option GetterOption) bool {
for _, opt := range t {
if opt == option {
return true
}
}
return false
}
func findCachedEntity[T any](ctx context.Context, id string) (*T, error) {
return findEntityByField[T](ctx, "id", id)
}
func cacheKey[T any](field, key string) string {
var v T
return fmt.Sprintf("%T:%s=%s", v, field, key)
}
func findEntityByField[T any](ctx context.Context, field, key string, opts ...GetterOption) (*T, error) {
if !GetterOptions(opts).IsSet(GetterOptionNoCache) {
if value, ok := getterCache.Get(cacheKey[T](field, key)); ok {
if cache, ok := value.(*T); ok {
return cache, nil
} else {
return nil, fmt.Errorf("unexpected cached value type: %T", value)
}
}
}
var resource T
if err := ctx.DB().Where(fmt.Sprintf("%s = ?", field), key).First(&resource).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
getterCache.SetDefault(cacheKey[T](field, key), &resource)
return &resource, nil
}
func GetCachedComponent(ctx context.Context, id string) (*models.Component, error) {
component, err := findCachedEntity[models.Component](ctx, id)
if err != nil {
return nil, err
}
return component, nil
}
func GetCachedConfig(ctx context.Context, id string) (*models.ConfigItem, error) {
config, err := findCachedEntity[models.ConfigItem](ctx, id)
if err != nil {
return nil, err
}
return config, nil
}
func GetCachedIncident(ctx context.Context, id string) (*models.Incident, error) {
incident, err := findCachedEntity[models.Incident](ctx, id)
if err != nil {
return nil, err
}
return incident, nil
}
func FindCachedAgent(ctx context.Context, identifier string) (*models.Agent, error) {
var field string
if _, err := uuid.Parse(identifier); err == nil {
field = "id"
} else {
field = "name"
}
agent, err := findEntityByField[models.Agent](ctx, field, identifier)
if err != nil {
return nil, err
}
return agent, nil
}
func FindCachedCheck(ctx context.Context, id string) (*models.Check, error) {
check, err := findCachedEntity[models.Check](ctx, id)
if err != nil {
return nil, err
}
return check, nil
}
func FindCachedCanary(ctx context.Context, id string) (*models.Canary, error) {
canary, err := findCachedEntity[models.Canary](ctx, id)
if err != nil {
return nil, err
}
return canary, nil
}
// FindPerson looks up a person by the given identifier which can either be
// - UUID
// - email
func FindPerson(ctx context.Context, identifier string, opts ...GetterOption) (*models.Person, error) {
var field string
if _, err := uuid.Parse(identifier); err == nil {
field = "id"
} else {
field = "email"
}
person, err := findEntityByField[models.Person](ctx, field, identifier, opts...)
if err != nil {
return nil, err
}
return person, nil
}
// FindTeam looks up a team by the given identifier which can either be
// - UUID
// - team name
func FindTeam(ctx context.Context, identifier string, opts ...GetterOption) (*models.Team, error) {
var field string
if _, err := uuid.Parse(identifier); err == nil {
field = "id"
} else {
field = "name"
}
team, err := findEntityByField[models.Team](ctx, field, identifier, opts...)
if err != nil {
return nil, err
}
return team, nil
}