Skip to content

Commit

Permalink
refactor(perf): user first heartbeats query
Browse files Browse the repository at this point in the history
  • Loading branch information
muety committed Nov 25, 2024
1 parent 24751ea commit f162acc
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 175 deletions.
18 changes: 10 additions & 8 deletions repositories/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,22 @@ func (r *HeartbeatRepository) GetLatestByFilters(user *models.User, filterMap ma

func (r *HeartbeatRepository) GetFirstByUsers() ([]*models.TimeByUser, error) {
var result []*models.TimeByUser
r.db.Model(&models.User{}).
Select(utils.QuoteSql(r.db, "users.id as %s, min(time) as %s", "user", "time")).
Joins("left join heartbeats on users.id = heartbeats.user_id").
Group("users.id").
r.db.Raw("with agg as (select " + utils.QuoteSql(r.db, "user_id, min(time) as %s", "time") + " from heartbeats group by user_id) " +
"select " + utils.QuoteSql(r.db, "id as %s, time ", "user") +
"from users " +
"left join agg on agg.user_id = id " +
"order by users.id").
Scan(&result)
return result, nil
}

func (r *HeartbeatRepository) GetLastByUsers() ([]*models.TimeByUser, error) {
var result []*models.TimeByUser
r.db.Model(&models.User{}).
Select(utils.QuoteSql(r.db, "users.id as %s, max(time) as %s", "user", "time")).
Joins("left join heartbeats on users.id = heartbeats.user_id").
Group("user").
r.db.Raw("with agg as (select " + utils.QuoteSql(r.db, "user_id, max(time) as %s", "time") + " from heartbeats group by user_id) " +
"select " + utils.QuoteSql(r.db, "id as %s, time ", "user") +
"from users " +
"left join agg on agg.user_id = id " +
"order by users.id").
Scan(&result)
return result, nil
}
Expand Down
4 changes: 2 additions & 2 deletions services/aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func (srv *AggregationService) AggregateSummaries(userIds datastructure.Set[stri
slog.Info("generating summaries")

// Get a map from user ids to the time of their latest summary or nil if none exists yet
lastUserSummaryTimes, err := srv.summaryService.GetLatestByUser()
lastUserSummaryTimes, err := srv.summaryService.GetLatestByUser() // TODO: build user-specific variant of this query for efficiency
if err != nil {
config.Log().Error("error occurred", "error", err)
return err
}

// Get a map from user ids to the time of their earliest heartbeats or nil if none exists yet
firstUserHeartbeatTimes, err := srv.heartbeatService.GetFirstByUsers()
firstUserHeartbeatTimes, err := srv.heartbeatService.GetFirstByUsers() // TODO: build user-specific variant of this query for efficiency
if err != nil {
config.Log().Error("error occurred", "error", err)
return err
Expand Down
Loading

0 comments on commit f162acc

Please sign in to comment.