-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathdial.go
669 lines (594 loc) · 17.7 KB
/
dial.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
package sqlite
import (
"context"
"crypto/rand"
"database/sql"
"encoding/hex"
"fmt"
"io"
"strings"
"time"
"github.com/benbjohnson/wtf"
)
// DialService represents a service for managing dials.
type DialService struct {
db *DB
}
// NewDialService returns a new instance of DialService.
func NewDialService(db *DB) *DialService {
return &DialService{db: db}
}
// FindDialByID retrieves a single dial by ID along with associated memberships.
// Only the dial owner & members can see a dial. Returns ENOTFOUND if dial does
// not exist or user does not have permission to view it.
func (s *DialService) FindDialByID(ctx context.Context, id int) (*wtf.Dial, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
// Fetch dial object and attach owner user.
dial, err := findDialByID(ctx, tx, id)
if err != nil {
return nil, err
} else if err := attachDialAssociations(ctx, tx, dial); err != nil {
return nil, err
}
return dial, nil
}
// FindDials retrieves a list of dials based on a filter. Only returns dials
// that the user owns or is a member of.
//
// Also returns a count of total matching dials which may different from the
// number of returned dials if the "Limit" field is set.
func (s *DialService) FindDials(ctx context.Context, filter wtf.DialFilter) ([]*wtf.Dial, int, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, 0, err
}
defer tx.Rollback()
// Fetch list of matching dial objects.
dials, n, err := findDials(ctx, tx, filter)
if err != nil {
return dials, n, err
}
// Iterate over dials and attach associated owner user.
// This should be batched up if using a remote database server.
for _, dial := range dials {
if err := attachDialAssociations(ctx, tx, dial); err != nil {
return dials, n, err
}
}
return dials, n, nil
}
// CreateDial creates a new dial and assigns the current user as the owner.
// The owner will automatically be added as a member of the new dial.
func (s *DialService) CreateDial(ctx context.Context, dial *wtf.Dial) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
// Assign dial to the current user.
// Return an error if the user is not currently logged in.
userID := wtf.UserIDFromContext(ctx)
if userID == 0 {
return wtf.Errorf(wtf.EUNAUTHORIZED, "You must be logged in to create a dial.")
}
dial.UserID = wtf.UserIDFromContext(ctx)
// Create dial and attach associated owner user.
if err := createDial(ctx, tx, dial); err != nil {
return err
} else if err := attachDialAssociations(ctx, tx, dial); err != nil {
return err
}
return tx.Commit()
}
// UpdateDial updates an existing dial by ID. Only the dial owner can update a dial.
// Returns the new dial state even if there was an error during update.
//
// Returns ENOTFOUND if dial does not exist. Returns EUNAUTHORIZED if user
// is not the dial owner.
func (s *DialService) UpdateDial(ctx context.Context, id int, upd wtf.DialUpdate) (*wtf.Dial, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
// Update the dial object and attach associated user to returned dial.
dial, err := updateDial(ctx, tx, id, upd)
if err != nil {
return dial, err
} else if err := attachDialAssociations(ctx, tx, dial); err != nil {
return dial, err
}
return dial, tx.Commit()
}
// DeleteDial permanently removes a dial by ID. Only the dial owner may delete
// a dial. Returns ENOTFOUND if dial does not exist. Returns EUNAUTHORIZED if
// user is not the dial owner.
func (s *DialService) DeleteDial(ctx context.Context, id int) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if err := deleteDial(ctx, tx, id); err != nil {
return err
}
return tx.Commit()
}
// Sets the value of the user's membership in a dial. This works the same
// as calling UpdateDialMembership() although it doesn't require that the
// user know their membership ID. Only the dial ID.
//
// Returns ENOTFOUND if the membership does not exist.
func (s *DialService) SetDialMembershipValue(ctx context.Context, dialID, value int) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
// Fetch current user.
userID := wtf.UserIDFromContext(ctx)
// Find user's membership.
memberships, _, err := findDialMemberships(ctx, tx, wtf.DialMembershipFilter{
DialID: &dialID,
UserID: &userID,
})
if err != nil {
return err
} else if len(memberships) == 0 {
return wtf.Errorf(wtf.ENOTFOUND, "User is not a member of this dial.")
}
// Update value on membership.
if _, err := updateDialMembership(ctx, tx, memberships[0].ID, wtf.DialMembershipUpdate{Value: &value}); err != nil {
return err
}
return tx.Commit()
}
// DialValues returns a list of all stored historical values for a dial.
// This is only used for testing.
func (s *DialService) DialValues(ctx context.Context, id int) ([]int, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
// Execute query to read all values in order for a dial.
rows, err := tx.QueryContext(ctx, `
SELECT value
FROM dial_values
WHERE dial_id = ?
ORDER BY "timestamp"
`, id)
if err != nil {
return nil, FormatError(err)
}
// Iterate over rows and append to list of values.
var a []int
for rows.Next() {
var v int
if err := rows.Scan(&v); err != nil {
return nil, FormatError(err)
}
a = append(a, v)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
return a, nil
}
// AverageDialValueReport returns a report of the average dial value across
// all dials that the user is a member of. Average values are computed
// between start & end time and are slotted into given intervals. The
// minimum interval size is one minute.
func (s *DialService) AverageDialValueReport(ctx context.Context, start, end time.Time, interval time.Duration) (*wtf.DialValueReport, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
// Ensure start/end line up with the interval unit.
start = start.Truncate(interval).UTC()
end = end.Truncate(interval).UTC()
// Compute the number of slots between start & end.
slotN := int(end.Sub(start) / interval)
report := &wtf.DialValueReport{
Records: make([]*wtf.DialValueRecord, slotN),
}
// Fetch all dials which user is a member or owner.
dials, _, err := findDials(ctx, tx, wtf.DialFilter{})
if err != nil {
return nil, fmt.Errorf("find dials: %w", err)
}
// Iterate over each dial and compute value at each slot.
valuesSlice := make([][]int, len(dials))
for i, dial := range dials {
values, err := findDialValueSlotsBetween(ctx, tx, dial.ID, start, end, interval)
if err != nil {
return nil, fmt.Errorf("dial values between: id=%d err=%w", dial.ID, err)
}
valuesSlice[i] = values
}
// Compute average for each slot.
for i := 0; i < slotN; i++ {
var avg int
if len(dials) != 0 {
var sum int
for j := range dials {
sum += valuesSlice[j][i]
}
avg = sum / len(valuesSlice)
}
// Append record for avg value at a given time.
report.Records[i] = &wtf.DialValueRecord{
Timestamp: start.Add(time.Duration(i) * interval),
Value: avg,
}
}
return report, nil
}
// findDialByID is a helper function to retrieve a dial by ID.
// Returns ENOTFOUND if dial doesn't exist.
func findDialByID(ctx context.Context, tx *Tx, id int) (*wtf.Dial, error) {
dials, _, err := findDials(ctx, tx, wtf.DialFilter{ID: &id})
if err != nil {
return nil, err
} else if len(dials) == 0 {
return nil, &wtf.Error{Code: wtf.ENOTFOUND, Message: "Dial not found."}
}
return dials[0], nil
}
// checkDialExists returns nil if a dial does not exist. Otherwise returns ENOTFOUND.
// This is used to avoid permissions checks when inserting related objects.
//
// Unfortunately, SQLite provides poor FOREIGN KEY error descriptions but
// otherwise we would just use those.
func checkDialExists(ctx context.Context, tx *Tx, id int) error {
var n int
if err := tx.QueryRowContext(ctx, `SELECT COUNT(1) FROM dials WHERE id = ?`, id).Scan(&n); err != nil {
return FormatError(err)
} else if n == 0 {
return &wtf.Error{Code: wtf.ENOTFOUND, Message: "Dial not found."}
}
return nil
}
// findDials retrieves a list of matching dials. Also returns a total matching
// count which may different from the number of results if filter.Limit is set.
func findDials(ctx context.Context, tx *Tx, filter wtf.DialFilter) (_ []*wtf.Dial, n int, err error) {
// Build WHERE clause. Each part of the WHERE clause is AND-ed together.
// Values are appended to an arg list to avoid SQL injection.
where, args := []string{"1 = 1"}, []interface{}{}
if v := filter.ID; v != nil {
where, args = append(where, "id = ?"), append(args, *v)
}
// Limit to dials user is a member of unless searching by invite code.
if v := filter.InviteCode; v != nil {
where, args = append(where, "invite_code = ?"), append(args, *v)
} else {
userID := wtf.UserIDFromContext(ctx)
where = append(where, `(
id IN (SELECT dial_id FROM dial_memberships dm WHERE dm.user_id = ?)
)`)
args = append(args, userID)
}
// Execue query with limiting WHERE clause and LIMIT/OFFSET injected.
rows, err := tx.QueryContext(ctx, `
SELECT
id,
user_id,
name,
value,
invite_code,
created_at,
updated_at,
COUNT(*) OVER()
FROM dials
WHERE `+strings.Join(where, " AND ")+`
ORDER BY id ASC
`+FormatLimitOffset(filter.Limit, filter.Offset),
args...,
)
if err != nil {
return nil, n, FormatError(err)
}
defer rows.Close()
// Iterate over rows and deserialize into Dial objects.
dials := make([]*wtf.Dial, 0)
for rows.Next() {
var dial wtf.Dial
if rows.Scan(
&dial.ID,
&dial.UserID,
&dial.Name,
&dial.Value,
&dial.InviteCode,
(*NullTime)(&dial.CreatedAt),
(*NullTime)(&dial.UpdatedAt),
&n,
); err != nil {
return nil, 0, err
}
dials = append(dials, &dial)
}
if err := rows.Err(); err != nil {
return nil, 0, err
}
return dials, n, nil
}
// createDial creates a new dial.
func createDial(ctx context.Context, tx *Tx, dial *wtf.Dial) error {
// Generate a random invite code.
inviteCode := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, inviteCode); err != nil {
return err
}
dial.InviteCode = hex.EncodeToString(inviteCode)
// Set timestamps to current time.
dial.CreatedAt = tx.now
dial.UpdatedAt = dial.CreatedAt
// Perform basic field validation.
if err := dial.Validate(); err != nil {
return err
}
// Insert row into database.
result, err := tx.ExecContext(ctx, `
INSERT INTO dials (
user_id,
name,
invite_code,
created_at,
updated_at
)
VALUES (?, ?, ?, ?, ?)
`,
dial.UserID,
dial.Name,
dial.InviteCode,
(*NullTime)(&dial.CreatedAt),
(*NullTime)(&dial.UpdatedAt),
)
if err != nil {
return FormatError(err)
}
// Read back new dial ID into caller argument.
if dial.ID, err = lastInsertID(result); err != nil {
return err
}
// Record initial value to history table.
if err := insertDialValue(ctx, tx, dial.ID, dial.Value, dial.CreatedAt); err != nil {
return fmt.Errorf("insert initial value: %w", err)
}
// Create self membership automatically.
if err := createDialMembership(ctx, tx, &wtf.DialMembership{
DialID: dial.ID,
UserID: dial.UserID,
}); err != nil {
return fmt.Errorf("create self-membership: %w", err)
}
return nil
}
// updateDial updates a dial by ID. Returns the new state of the dial after update.
func updateDial(ctx context.Context, tx *Tx, id int, upd wtf.DialUpdate) (*wtf.Dial, error) {
// Fetch current object state. Return an error if current user is not owner.
dial, err := findDialByID(ctx, tx, id)
if err != nil {
return dial, err
} else if !wtf.CanEditDial(ctx, dial) {
return dial, wtf.Errorf(wtf.EUNAUTHORIZED, "You must be the owner can edit a dial.")
}
// Update fields, if set.
if v := upd.Name; v != nil {
dial.Name = *v
}
dial.UpdatedAt = tx.now
// Perform basic field validation.
if err := dial.Validate(); err != nil {
return dial, err
}
// Execute update query.
if _, err := tx.ExecContext(ctx, `
UPDATE dials
SET name = ?,
updated_at = ?
WHERE id = ?
`,
dial.Name,
(*NullTime)(&dial.UpdatedAt),
id,
); err != nil {
return dial, FormatError(err)
}
return dial, nil
}
// deleteDial permanently deletes a dial by ID. Returns EUNAUTHORIZED if user
// does not own the dial.
func deleteDial(ctx context.Context, tx *Tx, id int) error {
// Verify object exists & the current user is the owner.
if dial, err := findDialByID(ctx, tx, id); err != nil {
return err
} else if !wtf.CanEditDial(ctx, dial) {
return wtf.Errorf(wtf.EUNAUTHORIZED, "Only the owner can delete a dial.")
}
// Remove row from database.
if _, err := tx.ExecContext(ctx, `DELETE FROM dials WHERE id = ?`, id); err != nil {
return FormatError(err)
}
return nil
}
// refreshDialValue recomputes the WTF level of a dial by ID and saves it in dials.value.
func refreshDialValue(ctx context.Context, tx *Tx, id int) error {
// Fetch current dial value.
var oldValue int
if err := tx.QueryRowContext(ctx, `SELECT value FROM dials WHERE id = ? `, id).Scan(&oldValue); err == sql.ErrNoRows {
return nil // no dial, skip
} else if err != nil {
return FormatError(err)
}
// Compute average value from dial memberships.
var newValue int
if err := tx.QueryRowContext(ctx, `
SELECT CAST(ROUND(IFNULL(AVG(value), 0)) AS INTEGER)
FROM dial_memberships
WHERE dial_id = ?
`,
id,
).Scan(
&newValue,
); err != nil && err != sql.ErrNoRows {
return FormatError(err)
}
// Exit if the value will not change.
if oldValue == newValue {
return nil
}
// Update value on dial.
if _, err := tx.ExecContext(ctx, `
UPDATE dials
SET value = ?,
updated_at = ?
WHERE id = ?
`,
newValue,
(*NullTime)(&tx.now),
id,
); err != nil {
return FormatError(err)
}
// Record historical value into "dial_values" table.
if err := insertDialValue(ctx, tx, id, newValue, tx.now); err != nil {
return fmt.Errorf("insert historical value: %w", err)
}
// Publish event to notify other members that the value has changed.
if err := publishDialEvent(ctx, tx, id, wtf.Event{
Type: wtf.EventTypeDialValueChanged,
Payload: &wtf.DialValueChangedPayload{
ID: id,
Value: newValue,
},
}); err != nil {
return fmt.Errorf("publish dial event: %w", err)
}
return nil
}
// insertDialValue records a dial value at specific point in time.
func insertDialValue(ctx context.Context, tx *Tx, id int, value int, timestamp time.Time) error {
// Reduce our precision to only one update per minute.
timestamp = timestamp.Truncate(1 * time.Minute)
// Insert a new record or update an existing record for the dial at the given timestamp.
if _, err := tx.ExecContext(ctx, `
INSERT INTO dial_values (dial_id, "timestamp", value)
VALUES (?, ?, ?)
ON CONFLICT (dial_id, "timestamp") DO UPDATE SET value = ?
`,
id, (*NullTime)(×tamp), value, value,
); err != nil {
return FormatError(err)
}
return nil
}
// findDialValueSlotsBetween returns the value of a dial at given intervals in a time range.
//
// This function is implemented naively so that we build a set of slots, insert
// values when they've changed, and then we backfill the empty slots with the
// previous value.
//
// There's probably a fancier way to do this in SQL but this was pretty easy.
func findDialValueSlotsBetween(ctx context.Context, tx *Tx, id int, start, end time.Time, interval time.Duration) ([]int, error) {
values := make([]int, end.Sub(start)/interval)
if len(values) == 0 {
return values, nil
}
// Mark slots empty. We'll fill them in later.
for i := range values {
values[i] = -1
}
// Determine initial value at start of report time range.
var value int
if err := tx.QueryRowContext(ctx, `
SELECT value
FROM dial_values
WHERE dial_id = ?
AND "timestamp" <= ?
ORDER BY "timestamp" DESC
LIMIT 1
`,
id,
(*NullTime)(&start),
).Scan(
&value,
); err != nil && err != sql.ErrNoRows {
return nil, err
}
values[0] = value
// Find all values between start & end.
rows, err := tx.QueryContext(ctx, `
SELECT value, "timestamp"
FROM dial_values
WHERE dial_id = ?
AND "timestamp" >= ?
AND "timestamp" < ?
ORDER BY "timestamp" ASC
`,
id,
(*NullTime)(&start),
(*NullTime)(&end),
)
if err != nil {
return nil, FormatError(err)
}
defer rows.Close()
// Iterate over rows and assign values to slots.
for rows.Next() {
var timestamp time.Time
if rows.Scan(&value, (*NullTime)(×tamp)); err != nil {
return nil, err
}
i := int(timestamp.Sub(start) / interval)
values[i] = value
}
if err := rows.Err(); err != nil {
return nil, err
} else if err := rows.Close(); err != nil {
return nil, err
}
// Iterate over values to fill empty slots.
var lastValue int
for i, v := range values {
if v != -1 {
lastValue = v
continue
}
values[i] = lastValue
}
return values, nil
}
// publishDialEvent publishes event to the dial members.
func publishDialEvent(ctx context.Context, tx *Tx, id int, event wtf.Event) error {
// Find all users who are members of the dial.
rows, err := tx.QueryContext(ctx, `SELECT user_id FROM dial_memberships WHERE dial_id = ?`, id)
if err != nil {
return FormatError(err)
}
defer rows.Close()
// Iterate over users and publish event.
for rows.Next() {
var userID int
if err := rows.Scan(&userID); err != nil {
return err
}
tx.db.EventService.PublishEvent(userID, event)
}
if err := rows.Err(); err != nil {
return err
}
return nil
}
// attachDialAssociations is a helper function to look up and attach the owner user to the dial.
func attachDialAssociations(ctx context.Context, tx *Tx, dial *wtf.Dial) (err error) {
if dial.User, err = findUserByID(ctx, tx, dial.UserID); err != nil {
return fmt.Errorf("attach dial user: %w", err)
}
return nil
}