/
event_logs.go
702 lines (622 loc) · 24.7 KB
/
event_logs.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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
package db
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/keegancsmith/sqlf"
"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/sourcegraph/sourcegraph/cmd/frontend/types"
"github.com/sourcegraph/sourcegraph/internal/db/dbconn"
"github.com/sourcegraph/sourcegraph/internal/db/dbutil"
intSecrets "github.com/sourcegraph/sourcegraph/internal/secrets"
"github.com/sourcegraph/sourcegraph/internal/timeutil"
"github.com/sourcegraph/sourcegraph/internal/version"
)
const (
integrationSource = "CODEHOSTINTEGRATION"
)
type eventLogs struct{}
// Event contains information needed for logging an event.
type Event struct {
Name string
URL string
UserID uint32
AnonymousUserID string
Argument json.RawMessage
Source string
Timestamp time.Time
}
func (*eventLogs) Insert(ctx context.Context, e *Event) error {
argument := e.Argument
if argument == nil {
argument = json.RawMessage([]byte(`{}`))
}
argData, err := intSecrets.CryptObject.EncryptBytesIfPossible(argument)
if err != nil {
return err
}
_, err = dbconn.Global.ExecContext(
ctx,
"INSERT INTO event_logs(name, url, user_id, anonymous_user_id, source, argument, version, timestamp) VALUES($1, $2, $3, $4, $5, $6, $7, $8)",
e.Name,
e.URL,
e.UserID,
e.AnonymousUserID,
e.Source,
argData,
version.Version(),
e.Timestamp.UTC(),
)
if err != nil {
return errors.Wrap(err, "INSERT")
}
return nil
}
func (*eventLogs) getBySQL(ctx context.Context, querySuffix *sqlf.Query) ([]*types.Event, error) {
q := sqlf.Sprintf("SELECT id, name, url, user_id, anonymous_user_id, source, argument, version, timestamp FROM event_logs %s", querySuffix)
rows, err := dbconn.Global.QueryContext(ctx, q.Query(sqlf.PostgresBindVar), q.Args()...)
if err != nil {
return nil, err
}
defer rows.Close()
events := []*types.Event{}
for rows.Next() {
r := types.Event{}
args, secErr := intSecrets.CryptObject.DecryptIfPossible(r.Argument)
if secErr != nil {
return nil, err
}
r.Argument = args
err := rows.Scan(&r.ID, &r.Name, &r.URL, &r.UserID, &r.AnonymousUserID, &r.Source, &r.Argument, &r.Version, &r.Timestamp)
if err != nil {
return nil, err
}
events = append(events, &r)
}
if err = rows.Err(); err != nil {
return nil, err
}
return events, nil
}
// EventLogsListOptions specifies the options for listing event logs.
type EventLogsListOptions struct {
// UserID specifies the user whose events should be included.
UserID int32
*LimitOffset
}
// ListAll gets all event logs in descending order of timestamp.
func (l *eventLogs) ListAll(ctx context.Context, opt EventLogsListOptions) ([]*types.Event, error) {
conds := []*sqlf.Query{sqlf.Sprintf("TRUE")}
if opt.UserID != 0 {
conds = append(conds, sqlf.Sprintf("user_id = %d", opt.UserID))
}
return l.getBySQL(ctx, sqlf.Sprintf("WHERE %s ORDER BY timestamp DESC %s", sqlf.Join(conds, "AND"), opt.LimitOffset.SQL()))
}
// CountByUserID gets a count of events logged by a given user.
func (l *eventLogs) CountByUserID(ctx context.Context, userID int32) (int, error) {
return l.countBySQL(ctx, sqlf.Sprintf("WHERE user_id = %d", userID))
}
// CountByUserIDAndEventName gets a count of events logged by a given user and with a given event name.
func (l *eventLogs) CountByUserIDAndEventName(ctx context.Context, userID int32, name string) (int, error) {
return l.countBySQL(ctx, sqlf.Sprintf("WHERE user_id = %d AND name = %s", userID, name))
}
// CountByUserIDAndEventNamePrefix gets a count of events logged by a given user and with a given event name prefix.
func (l *eventLogs) CountByUserIDAndEventNamePrefix(ctx context.Context, userID int32, namePrefix string) (int, error) {
return l.countBySQL(ctx, sqlf.Sprintf("WHERE user_id = %d AND name LIKE %s", userID, namePrefix+"%"))
}
// CountByUserIDAndEventNames gets a count of events logged by a given user that match a list of given event names.
func (l *eventLogs) CountByUserIDAndEventNames(ctx context.Context, userID int32, names []string) (int, error) {
items := []*sqlf.Query{}
for _, v := range names {
items = append(items, sqlf.Sprintf("%s", v))
}
return l.countBySQL(ctx, sqlf.Sprintf("WHERE user_id = %d AND name IN (%s)", userID, sqlf.Join(items, ",")))
}
// countBySQL gets a count of event logs.
func (*eventLogs) countBySQL(ctx context.Context, querySuffix *sqlf.Query) (int, error) {
q := sqlf.Sprintf("SELECT COUNT(*) FROM event_logs %s", querySuffix)
r := dbconn.Global.QueryRowContext(ctx, q.Query(sqlf.PostgresBindVar), q.Args()...)
var count int
err := r.Scan(&count)
return count, err
}
// MaxTimestampByUserID gets the max timestamp among event logs for a given user.
func (l *eventLogs) MaxTimestampByUserID(ctx context.Context, userID int32) (*time.Time, error) {
return l.maxTimestampBySQL(ctx, sqlf.Sprintf("WHERE user_id = %d", userID))
}
// MaxTimestampByUserIDAndSource gets the max timestamp among event logs for a given user and event source.
func (l *eventLogs) MaxTimestampByUserIDAndSource(ctx context.Context, userID int32, source string) (*time.Time, error) {
return l.maxTimestampBySQL(ctx, sqlf.Sprintf("WHERE user_id = %d AND source = %s", userID, source))
}
// maxTimestampBySQL gets the max timestamp among event logs.
func (*eventLogs) maxTimestampBySQL(ctx context.Context, querySuffix *sqlf.Query) (*time.Time, error) {
q := sqlf.Sprintf("SELECT MAX(timestamp) FROM event_logs %s", querySuffix)
r := dbconn.Global.QueryRowContext(ctx, q.Query(sqlf.PostgresBindVar), q.Args()...)
var t time.Time
err := r.Scan(&dbutil.NullTime{Time: &t})
if t.IsZero() {
return nil, err
}
return &t, err
}
// UsageValue is a single count of usage for a time period starting on a given date.
type UsageValue struct {
Start time.Time
Count int
}
// PeriodType is the type of period in which to count events and unique users.
type PeriodType string
const (
// Daily is used to get a count of events or unique users within a day.
Daily PeriodType = "daily"
// Weekly is used to get a count of events or unique users within a week.
Weekly PeriodType = "weekly"
// Monthly is used to get a count of events or unique users within a month.
Monthly PeriodType = "monthly"
)
// intervalByPeriodType is a map of generate_series step values by period type.
var intervalByPeriodType = map[PeriodType]*sqlf.Query{
Daily: sqlf.Sprintf("'1 day'"),
Weekly: sqlf.Sprintf("'1 week'"),
Monthly: sqlf.Sprintf("'1 month'"),
}
// periodByPeriodType is a map of SQL fragments that produce a timestamp bucket by period
// type. This assumes the existence of a field named `timestamp` in the enclosing query.
var periodByPeriodType = map[PeriodType]*sqlf.Query{
Daily: sqlf.Sprintf("DATE_TRUNC('day', timestamp)"),
Weekly: sqlf.Sprintf("DATE_TRUNC('week', timestamp + '1 day'::interval) - '1 day'::interval"),
Monthly: sqlf.Sprintf("DATE_TRUNC('month', timestamp)"),
}
// calcStartDate calculates the the starting date of a number of periods given the period type.
// from the current time supplied as `now`. Returns a second false value if the period type is
// illegal.
func calcStartDate(now time.Time, periodType PeriodType, periods int) (time.Time, bool) {
periodsAgo := periods - 1
switch periodType {
case Daily:
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC).AddDate(0, 0, -periodsAgo), true
case Weekly:
return timeutil.StartOfWeek(now, periodsAgo), true
case Monthly:
return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC).AddDate(0, -periodsAgo, 0), true
}
return time.Time{}, false
}
// calcEndDate calculates the the ending date of a number of periods given the period type.
// Returns a second false value if the period type is illegal.
func calcEndDate(startDate time.Time, periodType PeriodType, periods int) (time.Time, bool) {
periodsAgo := periods - 1
switch periodType {
case Daily:
return startDate.AddDate(0, 0, periodsAgo), true
case Weekly:
return startDate.AddDate(0, 0, 7*periodsAgo), true
case Monthly:
return startDate.AddDate(0, periodsAgo, 0), true
}
return time.Time{}, false
}
// CountUniqueUsersOptions provides options for counting unique users.
type CountUniqueUsersOptions struct {
// If true, only include registered users. Otherwise, include all users.
RegisteredOnly bool
// If true, only include code host integration users. Otherwise, include all users.
IntegrationOnly bool
// If set, adds additional restrictions on the event types.
EventFilters *EventFilterOptions
}
// EventFilterOptions provides options for filtering events.
type EventFilterOptions struct {
// If set, only include events with a given prefix.
ByEventNamePrefix string
// If set, only include events with the given name.
ByEventName string
// If not empty, only include events that matche a list of given event names
ByEventNames []string
// Must be used with ByEventName
//
// If set, only include events that match a specified condition.
ByEventNameWithCondition *sqlf.Query
}
// EventArgumentMatch provides the options for matching an event with
// a specific JSON value passed as an argument.
type EventArgumentMatch struct {
// The name of the JSON key to match against.
ArgumentName string
// The actual value passed to the JSON key to match.
ArgumentValue string
}
// PercentileValue is a slice of Nth percentile values calculated from a field of events
// in a time period starting on a given date.
type PercentileValue struct {
Start time.Time
Values []float64
}
// CountUniqueUsersPerPeriod provides a count of unique active users in a given time span, broken up into periods of
// a given type. The value of `now` should be the current time in UTC. Returns an array of length `periods`, with one
// entry for each period in the time span.
func (l *eventLogs) CountUniqueUsersPerPeriod(ctx context.Context, periodType PeriodType, now time.Time, periods int, opt *CountUniqueUsersOptions) ([]UsageValue, error) {
startDate, ok := calcStartDate(now, periodType, periods)
if !ok {
return nil, fmt.Errorf("periodType must be \"daily\", \"weekly\", or \"monthly\". Got %s", periodType)
}
endDate, ok := calcEndDate(startDate, periodType, periods)
if !ok {
return nil, fmt.Errorf("periodType must be \"daily\", \"weekly\", or \"monthly\". Got %s", periodType)
}
conds := []*sqlf.Query{sqlf.Sprintf("TRUE")}
if opt != nil {
if opt.RegisteredOnly {
conds = append(conds, sqlf.Sprintf("user_id > 0"))
}
if opt.IntegrationOnly {
conds = append(conds, sqlf.Sprintf("source = %s", integrationSource))
}
if opt.EventFilters != nil {
if opt.EventFilters.ByEventNamePrefix != "" {
conds = append(conds, sqlf.Sprintf("name LIKE %s", opt.EventFilters.ByEventNamePrefix+"%"))
}
if opt.EventFilters.ByEventName != "" {
conds = append(conds, sqlf.Sprintf("name = %s", opt.EventFilters.ByEventName))
}
if opt.EventFilters.ByEventNameWithCondition != nil {
conds = append(conds, opt.EventFilters.ByEventNameWithCondition)
}
if len(opt.EventFilters.ByEventNames) > 0 {
items := []*sqlf.Query{}
for _, v := range opt.EventFilters.ByEventNames {
items = append(items, sqlf.Sprintf("%s", v))
}
conds = append(conds, sqlf.Sprintf("name IN (%s)", sqlf.Join(items, ",")))
}
}
}
return l.countUniqueUsersPerPeriodBySQL(ctx, intervalByPeriodType[periodType], periodByPeriodType[periodType], startDate, endDate, conds)
}
func (l *eventLogs) countUniqueUsersPerPeriodBySQL(ctx context.Context, interval, period *sqlf.Query, startDate, endDate time.Time, conds []*sqlf.Query) ([]UsageValue, error) {
return l.countPerPeriodBySQL(ctx, sqlf.Sprintf("DISTINCT CASE WHEN user_id = 0 THEN anonymous_user_id ELSE CAST(user_id AS TEXT) END"), interval, period, startDate, endDate, conds)
}
func (l *eventLogs) countPerPeriodBySQL(ctx context.Context, countExpr, interval, period *sqlf.Query, startDate, endDate time.Time, conds []*sqlf.Query) ([]UsageValue, error) {
allPeriods := sqlf.Sprintf("SELECT generate_series((%s)::timestamp, (%s)::timestamp, (%s)::interval) AS period", startDate, endDate, interval)
countByPeriod := sqlf.Sprintf(`SELECT (%s) AS period, COUNT(%s) AS count
FROM event_logs
WHERE (%s)
GROUP BY period`, period, countExpr, sqlf.Join(conds, ") AND ("))
q := sqlf.Sprintf(`WITH all_periods AS (%s), count_by_period AS (%s)
SELECT all_periods.period, COALESCE(count, 0)
FROM all_periods
LEFT OUTER JOIN count_by_period ON all_periods.period = (count_by_period.period)::timestamp
ORDER BY period DESC`, allPeriods, countByPeriod)
rows, err := dbconn.Global.QueryContext(ctx, q.Query(sqlf.PostgresBindVar), q.Args()...)
if err != nil {
return nil, err
}
defer rows.Close()
counts := []UsageValue{}
for rows.Next() {
var v UsageValue
err := rows.Scan(&v.Start, &v.Count)
if err != nil {
return nil, err
}
v.Start = v.Start.UTC()
counts = append(counts, v)
}
if err = rows.Err(); err != nil {
return nil, err
}
return counts, nil
}
// CountUniqueUsersAll provides a count of unique active users in a given time span.
func (l *eventLogs) CountUniqueUsersAll(ctx context.Context, startDate, endDate time.Time) (int, error) {
return l.countUniqueUsersBySQL(ctx, startDate, endDate, nil)
}
// CountUniqueUsersByEventNamePrefix provides a count of unique active users in a given time span that logged an event with a given prefix.
func (l *eventLogs) CountUniqueUsersByEventNamePrefix(ctx context.Context, startDate, endDate time.Time, namePrefix string) (int, error) {
return l.countUniqueUsersBySQL(ctx, startDate, endDate, sqlf.Sprintf("AND name LIKE %s ", namePrefix+"%"))
}
// CountUniqueUsersByEventName provides a count of unique active users in a given time span that logged a given event.
func (l *eventLogs) CountUniqueUsersByEventName(ctx context.Context, startDate, endDate time.Time, name string) (int, error) {
return l.countUniqueUsersBySQL(ctx, startDate, endDate, sqlf.Sprintf("AND name = %s", name))
}
// CountUniqueUsersByEventNames provides a count of unique active users in a given time span that logged any event that matches a list of given event names
func (l *eventLogs) CountUniqueUsersByEventNames(ctx context.Context, startDate, endDate time.Time, names []string) (int, error) {
items := []*sqlf.Query{}
for _, v := range names {
items = append(items, sqlf.Sprintf("%s", v))
}
return l.countUniqueUsersBySQL(ctx, startDate, endDate, sqlf.Sprintf("AND name IN (%s)", sqlf.Join(items, ",")))
}
func (*eventLogs) countUniqueUsersBySQL(ctx context.Context, startDate, endDate time.Time, querySuffix *sqlf.Query) (int, error) {
if querySuffix == nil {
querySuffix = sqlf.Sprintf("")
}
q := sqlf.Sprintf(`SELECT COUNT(DISTINCT CASE WHEN user_id = 0 THEN anonymous_user_id ELSE CAST(user_id AS TEXT) END)
FROM event_logs
WHERE (DATE(TIMEZONE('UTC'::text, timestamp)) >= %s) AND (DATE(TIMEZONE('UTC'::text, timestamp)) <= %s) %s`, startDate, endDate, querySuffix)
r := dbconn.Global.QueryRowContext(ctx, q.Query(sqlf.PostgresBindVar), q.Args()...)
var count int
err := r.Scan(&count)
return count, err
}
func (l *eventLogs) ListUniqueUsersAll(ctx context.Context, startDate, endDate time.Time) ([]int32, error) {
rows, err := dbconn.Global.QueryContext(ctx, `SELECT user_id
FROM event_logs
WHERE user_id > 0 AND DATE(TIMEZONE('UTC'::text, timestamp)) >= $1 AND DATE(TIMEZONE('UTC'::text, timestamp)) <= $2
GROUP BY user_id`, startDate, endDate)
if err != nil {
return nil, err
}
var users []int32
defer rows.Close()
for rows.Next() {
var userID int32
err := rows.Scan(&userID)
if err != nil {
return nil, err
}
users = append(users, userID)
}
if err = rows.Err(); err != nil {
return nil, err
}
return users, nil
}
// UsersUsageCounts returns a list of UserUsageCounts for all active users that produced 'SearchResultsQueried' and any
// '%codeintel%' events in the event_logs table.
func (l *eventLogs) UsersUsageCounts(ctx context.Context) (counts []types.UserUsageCounts, err error) {
rows, err := dbconn.Global.QueryContext(ctx, usersUsageCountsQuery)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var c types.UserUsageCounts
err := rows.Scan(
&c.Date,
&c.UserID,
&dbutil.NullInt32{N: &c.SearchCount},
&dbutil.NullInt32{N: &c.CodeIntelCount},
)
if err != nil {
return nil, err
}
counts = append(counts, c)
}
if err = rows.Err(); err != nil {
return nil, err
}
return counts, nil
}
const usersUsageCountsQuery = `
-- source: internal/db/event_logs.go:UsersUsageCounts
SELECT
DATE(timestamp),
user_id,
COUNT(*) FILTER (WHERE event_logs.name ='SearchResultsQueried') as search_count,
COUNT(*) FILTER (WHERE event_logs.name LIKE '%codeintel%') as codeintel_count
FROM event_logs
GROUP BY 1, 2
ORDER BY 1 DESC, 2 ASC;
`
func (l *eventLogs) SiteUsage(ctx context.Context) (types.SiteUsageSummary, error) {
return l.siteUsage(ctx, time.Now().UTC())
}
func (l *eventLogs) siteUsage(ctx context.Context, now time.Time) (summary types.SiteUsageSummary, err error) {
query := sqlf.Sprintf(siteUsageQuery, now, now, now, now)
err = dbconn.Global.QueryRowContext(
ctx,
query.Query(sqlf.PostgresBindVar),
query.Args()...,
).Scan(
&summary.Month,
&summary.Week,
&summary.Day,
&summary.UniquesMonth,
&summary.UniquesWeek,
&summary.UniquesDay,
&summary.RegisteredUniquesMonth,
&summary.RegisteredUniquesWeek,
&summary.RegisteredUniquesDay,
&summary.IntegrationUniquesMonth,
&summary.IntegrationUniquesWeek,
&summary.IntegrationUniquesDay,
&summary.ManageUniquesMonth,
&summary.CodeUniquesMonth,
&summary.VerifyUniquesMonth,
&summary.MonitorUniquesMonth,
&summary.ManageUniquesWeek,
&summary.CodeUniquesWeek,
&summary.VerifyUniquesWeek,
&summary.MonitorUniquesWeek,
)
return summary, err
}
const siteUsageQuery = `
SELECT
current_month,
current_week,
current_day,
COUNT(DISTINCT user_id) FILTER (WHERE month = current_month) AS uniques_month,
COUNT(DISTINCT user_id) FILTER (WHERE week = current_week) AS uniques_week,
COUNT(DISTINCT user_id) FILTER (WHERE day = current_day) AS uniques_day,
COUNT(DISTINCT user_id) FILTER (WHERE month = current_month AND registered) AS registered_uniques_month,
COUNT(DISTINCT user_id) FILTER (WHERE week = current_week AND registered) AS registered_uniques_week,
COUNT(DISTINCT user_id) FILTER (WHERE day = current_day AND registered) AS registered_uniques_day,
COUNT(DISTINCT user_id) FILTER (WHERE month = current_month AND source = 'CODEHOSTINTEGRATION')
AS integration_uniques_month,
COUNT(DISTINCT user_id) FILTER (WHERE week = current_week AND source = 'CODEHOSTINTEGRATION')
AS integration_uniques_week,
COUNT(DISTINCT user_id) FILTER (WHERE day = current_day AND source = 'CODEHOSTINTEGRATION')
AS integration_uniques_day,
COUNT(DISTINCT user_id) FILTER (
WHERE month = current_month AND name LIKE 'ViewSiteAdmin%%%%'
) AS manage_uniques_month,
COUNT(DISTINCT user_id) FILTER (
WHERE month = current_month AND name IN (
'ViewRepository',
'ViewBlob',
'ViewTree',
'SearchResultsQueried'
)
) AS code_uniques_month,
COUNT(DISTINCT user_id) FILTER (
WHERE month = current_month AND name IN (
'SavedSearchEmailClicked',
'SavedSearchSlackClicked',
'SavedSearchEmailNotificationSent'
)
) AS verify_uniques_month,
COUNT(DISTINCT user_id) FILTER (
WHERE month = current_month AND name IN (
'DiffSearchResultsQueried'
)
) AS monitor_uniques_month,
COUNT(DISTINCT user_id) FILTER (
WHERE week = current_week AND name LIKE 'ViewSiteAdmin%%%%'
) AS manage_uniques_week,
COUNT(DISTINCT user_id) FILTER (
WHERE week = current_week AND name IN (
'ViewRepository',
'ViewBlob',
'ViewTree',
'SearchResultsQueried'
)
) AS code_uniques_week,
COUNT(DISTINCT user_id) FILTER (
WHERE week = current_week AND name IN (
'SavedSearchEmailClicked',
'SavedSearchSlackClicked',
'SavedSearchEmailNotificationSent'
)
) AS verify_uniques_week,
COUNT(DISTINCT user_id) FILTER (
WHERE week = current_week AND name IN (
'DiffSearchResultsQueried'
)
) AS monitor_uniques_week
FROM (
-- This sub-query is here to avoid re-doing this work above on each aggregation.
SELECT
name,
user_id != 0 as registered,
CASE WHEN user_id = 0
-- It's faster to group by an int rather than text, so we convert
-- the anonymous_user_id to an int, rather than the user_id to text.
THEN ('x'||substr(md5(anonymous_user_id), 1, 8))::bit(32)::int
ELSE user_id
END AS user_id,
source,
DATE_TRUNC('month', TIMEZONE('UTC', timestamp)) as month,
DATE_TRUNC('week', TIMEZONE('UTC', timestamp) + '1 day'::interval) - '1 day'::interval as week,
DATE_TRUNC('day', TIMEZONE('UTC', timestamp)) as day,
DATE_TRUNC('month', TIMEZONE('UTC', %s::timestamp)) as current_month,
DATE_TRUNC('week', TIMEZONE('UTC', %s::timestamp) + '1 day'::interval) - '1 day'::interval as current_week,
DATE_TRUNC('day', TIMEZONE('UTC', %s::timestamp)) as current_day
FROM event_logs
WHERE timestamp >= DATE_TRUNC('month', TIMEZONE('UTC', %s::timestamp))
) events
GROUP BY current_month, current_week, current_day
`
// AggregatedEvents calculates AggregatedEvent for each every unique event type.
func (l *eventLogs) AggregatedEvents(ctx context.Context) ([]types.AggregatedEvent, error) {
return l.aggregatedEvents(ctx, time.Now().UTC())
}
func (l *eventLogs) aggregatedEvents(ctx context.Context, now time.Time) (events []types.AggregatedEvent, err error) {
query := sqlf.Sprintf(aggregatedEventsQuery, now, now, now, now)
rows, err := dbconn.Global.QueryContext(ctx, query.Query(sqlf.PostgresBindVar), query.Args()...)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var event types.AggregatedEvent
err := rows.Scan(
&event.Name,
&event.Month,
&event.Week,
&event.Day,
&event.TotalMonth,
&event.TotalWeek,
&event.TotalDay,
&event.UniquesMonth,
&event.UniquesWeek,
&event.UniquesDay,
pq.Array(&event.LatenciesMonth),
pq.Array(&event.LatenciesWeek),
pq.Array(&event.LatenciesDay),
)
if err != nil {
return nil, err
}
events = append(events, event)
}
if err = rows.Err(); err != nil {
return nil, err
}
return events, nil
}
const aggregatedEventsQuery = `
-- This query does multiple aggregations over the current day, week and month in one
-- pass over the event_logs table. These are: unique number of users, total
-- number of events and 50th, 90th and 99th percentile latency (when there's latency captured).
SELECT
name,
current_month,
current_week,
current_day,
COUNT(*) FILTER (WHERE month = current_month) AS total_month,
COUNT(*) FILTER (WHERE week = current_week) AS total_week,
COUNT(*) FILTER (WHERE day = current_day) AS total_day,
COUNT(DISTINCT user_id) FILTER (WHERE month = current_month) AS uniques_month,
COUNT(DISTINCT user_id) FILTER (WHERE week = current_week) AS uniques_week,
COUNT(DISTINCT user_id) FILTER (WHERE day = current_day) AS uniques_day,
PERCENTILE_CONT(ARRAY[0.50, 0.90, 0.99])
WITHIN GROUP (ORDER BY latency) FILTER (WHERE month = current_month)
AS latencies_month,
PERCENTILE_CONT(ARRAY[0.50, 0.90, 0.99])
WITHIN GROUP (ORDER BY latency) FILTER (WHERE week = current_week)
AS latencies_week,
PERCENTILE_CONT(ARRAY[0.50, 0.90, 0.99])
WITHIN GROUP (ORDER BY latency) FILTER (WHERE day = current_day)
AS latencies_day
FROM (
-- This sub-query is here to avoid re-doing this work above on each aggregation.
SELECT
name,
-- Postgres 9.6 needs to go from text to integer (i.e. can't go directly to integer)
(argument->'durationMs')::text::integer as latency,
CASE WHEN user_id = 0
-- It's faster to group by an int rather than text, so we convert
-- the anonymous_user_id to an int, rather than the user_id to text.
THEN ('x'||substr(md5(anonymous_user_id), 1, 8))::bit(32)::int
ELSE user_id
END AS user_id,
DATE_TRUNC('month', TIMEZONE('UTC', timestamp)) as month,
DATE_TRUNC('week', TIMEZONE('UTC', timestamp) + '1 day'::interval) - '1 day'::interval as week,
DATE_TRUNC('day', TIMEZONE('UTC', timestamp)) as day,
DATE_TRUNC('month', TIMEZONE('UTC', %s::timestamp)) as current_month,
DATE_TRUNC('week', TIMEZONE('UTC', %s::timestamp) + '1 day'::interval) - '1 day'::interval as current_week,
DATE_TRUNC('day', TIMEZONE('UTC', %s::timestamp)) as current_day
FROM event_logs
WHERE timestamp >= DATE_TRUNC('month', TIMEZONE('UTC', %s::timestamp)) AND name IN (
'codeintel.lsifHover',
'codeintel.searchHover',
'codeintel.lsifDefinitions',
'codeintel.searchDefinitions',
'codeintel.lsifReferences',
'codeintel.searchReferences',
'search.latencies.literal',
'search.latencies.regexp',
'search.latencies.structural',
'search.latencies.file',
'search.latencies.repo',
'search.latencies.diff',
'search.latencies.commit',
'search.latencies.symbol'
)
) events
GROUP BY name, current_month, current_week, current_day
`