-
Notifications
You must be signed in to change notification settings - Fork 229
/
dataloaders.go
222 lines (187 loc) · 7.19 KB
/
dataloaders.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package graphqlapp
import (
context "context"
"io"
"github.com/google/uuid"
"github.com/target/goalert/alert"
"github.com/target/goalert/alert/alertmetrics"
"github.com/target/goalert/dataloader"
"github.com/target/goalert/escalation"
"github.com/target/goalert/heartbeat"
"github.com/target/goalert/notification"
"github.com/target/goalert/notificationchannel"
"github.com/target/goalert/schedule"
"github.com/target/goalert/schedule/rotation"
"github.com/target/goalert/service"
"github.com/target/goalert/user"
"github.com/target/goalert/user/contactmethod"
"github.com/pkg/errors"
)
type dataLoaderKey int
const (
dataLoaderKeyUnknown = dataLoaderKey(iota)
dataLoaderKeyAlert
dataLoaderKeyAlertState
dataLoaderKeyEP
dataLoaderKeyRotation
dataLoaderKeySchedule
dataLoaderKeyService
dataLoaderKeyUser
dataLoaderKeyCM
dataLoaderKeyHeartbeatMonitor
dataLoaderKeyNotificationMessageStatus
dataLoaderKeyNC
dataLoaderAlertMetrics
dataLoaderAlertFeedback
dataLoaderKeyLast // always keep as last
)
func (a *App) registerLoaders(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, dataLoaderKeyAlert, dataloader.NewStoreLoaderInt(ctx, a.AlertStore.FindMany))
ctx = context.WithValue(ctx, dataLoaderKeyAlertState, dataloader.NewStoreLoaderInt(ctx, a.AlertStore.State))
ctx = context.WithValue(ctx, dataLoaderKeyEP, dataloader.NewStoreLoader(ctx, a.PolicyStore.FindManyPolicies))
ctx = context.WithValue(ctx, dataLoaderKeyRotation, dataloader.NewStoreLoader(ctx, a.RotationStore.FindMany))
ctx = context.WithValue(ctx, dataLoaderKeySchedule, dataloader.NewStoreLoader(ctx, a.ScheduleStore.FindMany))
ctx = context.WithValue(ctx, dataLoaderKeyService, dataloader.NewStoreLoader(ctx, a.ServiceStore.FindMany))
ctx = context.WithValue(ctx, dataLoaderKeyUser, dataloader.NewStoreLoader(ctx, a.UserStore.FindMany))
ctx = context.WithValue(ctx, dataLoaderKeyCM, dataloader.NewStoreLoader(ctx, a.CMStore.FindMany))
ctx = context.WithValue(ctx, dataLoaderKeyNotificationMessageStatus, dataloader.NewStoreLoader(ctx, a.NotificationStore.FindManyMessageStatuses))
ctx = context.WithValue(ctx, dataLoaderKeyHeartbeatMonitor, dataloader.NewStoreLoader(ctx, a.HeartbeatStore.FindMany))
ctx = context.WithValue(ctx, dataLoaderKeyNC, dataloader.NewStoreLoader(ctx, a.NCStore.FindMany))
ctx = context.WithValue(ctx, dataLoaderAlertMetrics, dataloader.NewStoreLoaderInt(ctx, a.AlertMetricsStore.FindMetrics))
ctx = context.WithValue(ctx, dataLoaderAlertFeedback, dataloader.NewStoreLoaderInt(ctx, a.AlertStore.Feedback))
return ctx
}
func (a *App) closeLoaders(ctx context.Context) {
for key := dataLoaderKeyUnknown; key < dataLoaderKeyLast; key++ {
loader, ok := ctx.Value(key).(io.Closer)
if !ok {
continue
}
loader.Close()
}
}
func (app *App) FindOneNotificationMessageStatus(ctx context.Context, id string) (*notification.SendResult, error) {
loader, ok := ctx.Value(dataLoaderKeyNotificationMessageStatus).(*dataloader.Loader[string, notification.SendResult])
if !ok {
ms, err := app.NotificationStore.FindManyMessageStatuses(ctx, []string{id})
if err != nil {
return nil, err
}
return &ms[0], nil
}
return loader.FetchOne(ctx, id)
}
func (app *App) FindOneAlertFeedback(ctx context.Context, id int) (*alert.Feedback, error) {
loader, ok := ctx.Value(dataLoaderAlertFeedback).(*dataloader.Loader[int, alert.Feedback])
if !ok {
feedback, err := app.AlertStore.Feedback(ctx, []int{id})
if err != nil {
return nil, err
}
if len(feedback) == 0 {
return nil, nil
}
return &feedback[0], nil
}
return loader.FetchOne(ctx, id)
}
func (app *App) FindOneRotation(ctx context.Context, id string) (*rotation.Rotation, error) {
loader, ok := ctx.Value(dataLoaderKeyRotation).(*dataloader.Loader[string, rotation.Rotation])
if !ok {
return app.RotationStore.FindRotation(ctx, id)
}
return loader.FetchOne(ctx, id)
}
func (app *App) FindOneSchedule(ctx context.Context, id string) (*schedule.Schedule, error) {
loader, ok := ctx.Value(dataLoaderKeySchedule).(*dataloader.Loader[string, schedule.Schedule])
if !ok {
return app.ScheduleStore.FindOne(ctx, id)
}
return loader.FetchOne(ctx, id)
}
func (app *App) FindOneUser(ctx context.Context, id string) (*user.User, error) {
loader, ok := ctx.Value(dataLoaderKeyUser).(*dataloader.Loader[string, user.User])
if !ok {
return app.UserStore.FindOne(ctx, id)
}
return loader.FetchOne(ctx, id)
}
func (app *App) FindOneAlertMetric(ctx context.Context, id int) (*alertmetrics.Metric, error) {
loader, ok := ctx.Value(dataLoaderAlertMetrics).(*dataloader.Loader[int, alertmetrics.Metric])
if !ok {
m, err := app.AlertMetricsStore.FindMetrics(ctx, []int{id})
if err != nil {
return nil, err
}
if len(m) == 0 {
return nil, nil
}
return &m[0], nil
}
return loader.FetchOne(ctx, id)
}
// FindOneCM will return a single contact method for the given id, using the contexts dataloader if enabled.
func (app *App) FindOneCM(ctx context.Context, id string) (*contactmethod.ContactMethod, error) {
loader, ok := ctx.Value(dataLoaderKeyCM).(*dataloader.Loader[string, contactmethod.ContactMethod])
if !ok {
return app.CMStore.FindOne(ctx, id)
}
return loader.FetchOne(ctx, id)
}
// FindOneNC will return a single notification channel for the given id, using the contexts dataloader if enabled.
func (app *App) FindOneNC(ctx context.Context, id uuid.UUID) (*notificationchannel.Channel, error) {
loader, ok := ctx.Value(dataLoaderKeyNC).(*dataloader.Loader[string, notificationchannel.Channel])
if !ok {
return app.NCStore.FindOne(ctx, id)
}
return loader.FetchOne(ctx, id.String())
}
func (app *App) FindOnePolicy(ctx context.Context, id string) (*escalation.Policy, error) {
loader, ok := ctx.Value(dataLoaderKeyEP).(*dataloader.Loader[string, escalation.Policy])
if !ok {
return app.PolicyStore.FindOnePolicyTx(ctx, nil, id)
}
return loader.FetchOne(ctx, id)
}
func (app *App) FindOneService(ctx context.Context, id string) (*service.Service, error) {
loader, ok := ctx.Value(dataLoaderKeyService).(*dataloader.Loader[string, service.Service])
if !ok {
return app.ServiceStore.FindOne(ctx, id)
}
return loader.FetchOne(ctx, id)
}
func (app *App) FindOneAlertState(ctx context.Context, alertID int) (*alert.State, error) {
loader, ok := ctx.Value(dataLoaderKeyAlertState).(*dataloader.Loader[int, alert.State])
if !ok {
epState, err := app.AlertStore.State(ctx, []int{alertID})
if err != nil {
return nil, err
}
if len(epState) == 0 {
return nil, errors.New("no current epState for alert")
}
return &epState[0], nil
}
return loader.FetchOne(ctx, alertID)
}
func (app *App) FindOneAlert(ctx context.Context, id int) (*alert.Alert, error) {
loader, ok := ctx.Value(dataLoaderKeyAlert).(*dataloader.Loader[int, alert.Alert])
if !ok {
return app.AlertStore.FindOne(ctx, id)
}
return loader.FetchOne(ctx, id)
}
func (app *App) FindOneHeartbeatMonitor(ctx context.Context, id string) (*heartbeat.Monitor, error) {
loader, ok := ctx.Value(dataLoaderKeyHeartbeatMonitor).(*dataloader.Loader[string, heartbeat.Monitor])
if !ok {
hb, err := app.HeartbeatStore.FindMany(ctx, []string{id})
if err != nil {
return nil, err
}
if len(hb) == 0 {
return nil, nil
}
return &hb[0], nil
}
return loader.FetchOne(ctx, id)
}