-
Notifications
You must be signed in to change notification settings - Fork 85
/
watch.go
403 lines (336 loc) · 10.5 KB
/
watch.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright 2019 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package main
import (
"database/sql"
"errors"
"net/http"
"time"
"github.com/moov-io/base"
moovhttp "github.com/moov-io/base/http"
"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
)
var (
errNoWatchID = errors.New("no watchID found")
)
func getWatchID(w http.ResponseWriter, r *http.Request) string {
v, ok := mux.Vars(r)["watchID"]
if !ok || v == "" {
moovhttp.Problem(w, errNoWatchID)
return ""
}
return v
}
type watchRequest struct {
AuthToken string `json:"authToken"`
Webhook string `json:"webhook"`
}
// watchRepository holds information about each company and/or customer that another service wants notifications
// for every time we re-download data.
type watchRepository interface {
// getWatchesCursor returns a watchCursor which traverses both customer and company watches
getWatchesCursor(logger log.Logger, batchSize int) *watchCursor
// Company watches
addCompanyWatch(companyID string, params watchRequest) (string, error)
addCompanyNameWatch(name string, webhook string, authToken string) (string, error)
removeCompanyWatch(companyID string, watchID string) error
removeCompanyNameWatch(watchID string) error
// Customer watches
addCustomerWatch(customerID string, params watchRequest) (string, error)
addCustomerNameWatch(name string, webhook string, authToken string) (string, error)
removeCustomerWatch(customerID string, watchID string) error
removeCustomerNameWatch(watchID string) error
}
type sqliteWatchRepository struct {
db *sql.DB
logger log.Logger
}
func (r *sqliteWatchRepository) close() error {
return r.db.Close()
}
func (r *sqliteWatchRepository) getWatchesCursor(logger log.Logger, batchSize int) *watchCursor {
return &watchCursor{
batchSize: batchSize,
db: r.db,
logger: logger,
}
}
// Company methods
func (r *sqliteWatchRepository) addCompanyWatch(companyID string, params watchRequest) (string, error) {
if companyID == "" {
return "", errNoCompanyID
}
id := base.ID()
query := `insert into company_watches (id, company_id, webhook, auth_token, created_at) values (?, ?, ?, ?, ?)`
stmt, err := r.db.Prepare(query)
if err != nil {
return "", err
}
defer stmt.Close()
_, err = stmt.Exec(id, companyID, params.Webhook, params.AuthToken, time.Now())
if err != nil {
return "", err
}
return id, nil
}
func (r *sqliteWatchRepository) removeCompanyWatch(companyID string, watchID string) error {
if watchID == "" {
return errNoWatchID
}
query := `update company_watches set deleted_at = ? where company_id = ? and id = ? and deleted_at is null`
stmt, err := r.db.Prepare(query)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(time.Now(), companyID, watchID)
return err
}
func (r *sqliteWatchRepository) addCompanyNameWatch(name string, webhook string, authToken string) (string, error) {
query := `insert into company_name_watches (id, name, webhook, auth_token, created_at) values (?, ?, ?, ?, ?);`
stmt, err := r.db.Prepare(query)
if err != nil {
return "", err
}
defer stmt.Close()
id := base.ID()
_, err = stmt.Exec(id, name, webhook, authToken, time.Now())
if err != nil {
return "", err
}
return id, nil
}
func (r *sqliteWatchRepository) removeCompanyNameWatch(watchID string) error {
if watchID == "" {
return errNoWatchID
}
query := `update company_name_watches set deleted_at = ? where id = ? and deleted_at is null`
stmt, err := r.db.Prepare(query)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(time.Now(), watchID)
return err
}
// Customer methods
func (r *sqliteWatchRepository) addCustomerWatch(customerID string, params watchRequest) (string, error) {
if customerID == "" {
return "", errNoCustomerID
}
id := base.ID()
query := `insert into customer_watches (id, customer_id, webhook, auth_token, created_at) values (?, ?, ?, ?, ?)`
stmt, err := r.db.Prepare(query)
if err != nil {
return "", err
}
defer stmt.Close()
_, err = stmt.Exec(id, customerID, params.Webhook, params.AuthToken, time.Now())
if err != nil {
return "", err
}
return id, nil
}
func (r *sqliteWatchRepository) removeCustomerWatch(customerID string, watchID string) error {
if watchID == "" {
return errNoWatchID
}
query := `update customer_watches set deleted_at = ? where customer_id = ? and id = ? and deleted_at is null`
stmt, err := r.db.Prepare(query)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(time.Now(), customerID, watchID)
return err
}
func (r *sqliteWatchRepository) addCustomerNameWatch(name string, webhook string, authToken string) (string, error) {
query := `insert into customer_name_watches (id, name, webhook, auth_token, created_at) values (?, ?, ?, ?, ?);`
stmt, err := r.db.Prepare(query)
if err != nil {
return "", err
}
defer stmt.Close()
id := base.ID()
_, err = stmt.Exec(id, name, webhook, authToken, time.Now())
if err != nil {
return "", err
}
return id, nil
}
func (r *sqliteWatchRepository) removeCustomerNameWatch(watchID string) error {
if watchID == "" {
return errNoWatchID
}
query := `update customer_name_watches set deleted_at = ? where id = ? and deleted_at is null`
stmt, err := r.db.Prepare(query)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(time.Now(), watchID)
return err
}
type watch struct {
id string
customerID, customerName string
companyID, companyName string
webhook string
authToken string
}
type watchCursor struct {
batchSize int
db *sql.DB
logger log.Logger
// '*NewerThan' values represent the minimum (oldest) created_at value to return in the batch.
//
// These values start at "zero time" (an empty time.Time) and progresses towards time.Now()
// with each batch by being set to the batch's newest time.
customerNewerThan, customerNameNewerThan time.Time
companyNewerThan, companyNameNewerThan time.Time
}
// Next returns a batch of watches that will be sent off to their respective webhook URL.
func (cur *watchCursor) Next() ([]watch, error) {
var watches []watch
limit := cur.batchSize / 4 // 4 SQL queries
if cur.batchSize < 4 {
limit = 1 // return one if batchSize is invalid
}
// Companies
companyWatches, err := cur.getCompanyBatch(limit)
if err != nil {
cur.logger.Log("watchCursor", "problem reading company watches", "error", err)
}
watches = append(watches, companyWatches...)
companyNameWatches, err := cur.getCompanyNameBatch(limit)
if err != nil {
cur.logger.Log("watchCursor", "problem reading company name watches", "error", err)
}
watches = append(watches, companyNameWatches...)
// Customers
customerWatches, err := cur.getCustomerBatch(limit)
if err != nil {
cur.logger.Log("watchCursor", "problem reading customer watches", "error", err)
}
watches = append(watches, customerWatches...)
customerNameWatches, err := cur.getCustomerNameBatch(limit)
if err != nil {
cur.logger.Log("watchCursor", "problem reading customer name watches", "error", err)
}
watches = append(watches, customerNameWatches...)
return watches, nil
}
func (cur *watchCursor) getCompanyBatch(limit int) ([]watch, error) {
query := `select id, company_id, webhook, auth_token, created_at from company_watches where created_at > ? and deleted_at is null order by created_at asc limit ?`
stmt, err := cur.db.Prepare(query)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(cur.companyNewerThan, limit)
if err != nil {
return nil, err
}
defer rows.Close()
max := cur.companyNewerThan
var watches []watch
for rows.Next() {
var createdAt time.Time
var watch watch
if err := rows.Scan(&watch.id, &watch.companyID, &watch.webhook, &watch.authToken, &createdAt); err == nil {
watches = append(watches, watch)
}
if createdAt.After(max) {
// advance max to newest time
max = createdAt
}
}
cur.companyNewerThan = max
return watches, rows.Err()
}
func (cur *watchCursor) getCompanyNameBatch(limit int) ([]watch, error) {
query := `select id, name, webhook, auth_token, created_at from company_name_watches where created_at > ? and deleted_at is null order by created_at asc limit ?`
stmt, err := cur.db.Prepare(query)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(cur.companyNameNewerThan, limit)
if err != nil {
return nil, err
}
defer rows.Close()
max := cur.companyNameNewerThan
var watches []watch
for rows.Next() {
var createdAt time.Time
var watch watch
if err := rows.Scan(&watch.id, &watch.companyName, &watch.webhook, &watch.authToken, &createdAt); err == nil {
watches = append(watches, watch)
}
if createdAt.After(max) {
// advance max to newest time
max = createdAt
}
}
cur.companyNameNewerThan = max
return watches, rows.Err()
}
func (cur *watchCursor) getCustomerBatch(limit int) ([]watch, error) {
query := `select id, customer_id, webhook, auth_token, created_at from customer_watches where created_at > ? and deleted_at is null order by created_at asc limit ?`
stmt, err := cur.db.Prepare(query)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(cur.customerNewerThan, limit)
if err != nil {
return nil, err
}
defer rows.Close()
max := cur.customerNewerThan
var watches []watch
for rows.Next() {
var createdAt time.Time
var watch watch
if err := rows.Scan(&watch.id, &watch.customerID, &watch.webhook, &watch.authToken, &createdAt); err == nil {
watches = append(watches, watch)
}
if createdAt.After(max) {
// advance max to newest time
max = createdAt
}
}
cur.customerNewerThan = max
return watches, rows.Err()
}
func (cur *watchCursor) getCustomerNameBatch(limit int) ([]watch, error) {
query := `select id, name, webhook, auth_token, created_at from customer_name_watches where created_at > ? and deleted_at is null order by created_at asc limit ?`
stmt, err := cur.db.Prepare(query)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(cur.customerNameNewerThan, limit)
if err != nil {
return nil, err
}
defer rows.Close()
max := cur.customerNameNewerThan
var watches []watch
for rows.Next() {
var createdAt time.Time
var watch watch
if err := rows.Scan(&watch.id, &watch.customerName, &watch.webhook, &watch.authToken, &createdAt); err == nil {
watches = append(watches, watch)
}
if createdAt.After(max) {
// advance max to newest time
max = createdAt
}
}
cur.customerNameNewerThan = max
return watches, rows.Err()
}