Skip to content

Commit

Permalink
fix: cache profile cmd - grouped by platform
Browse files Browse the repository at this point in the history
  • Loading branch information
anhnh12 committed Sep 20, 2023
1 parent 95b2109 commit 323ffff
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
39 changes: 22 additions & 17 deletions pkg/job/cache_profile_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,31 @@ func (j *cacheProfileCommands) Run() error {
}

var wg sync.WaitGroup
wg.Add(7 * len(list))
wg.Add(6 * len(list))

for _, u := range list {
l := j.logger.Fields(logger.Fields{"profile_id": u.ProfileId})

go func(u model.CommandUsageCounter) {
_, err = j.entity.GetSvc().MochiProfile.GetByDiscordID(u.UserPlatformId, false)
if err != nil {
l.Error(err, "svc.MochiProfile.GetByDiscordID() failed")
}
l := j.logger.Fields(logger.Fields{"profile_id": u.ProfileId, "platform_id": u.UserPlatformId, "platform": u.Platform})
l.Info("start caching /profile data")

if u.Platform == "discord" {
go func(u model.CommandUsageCounter) {
_, err = j.entity.GetSvc().MochiProfile.GetByDiscordID(u.UserPlatformId, false)
if err != nil {
l.Error(err, "svc.MochiProfile.GetByDiscordID() failed")
}
wg.Done()
}(u)
} else if u.Platform == "telegram" {
go func(u model.CommandUsageCounter) {
_, err = j.entity.GetSvc().MochiProfile.GetByTelegramID(u.UserPlatformId, false)
if err != nil {
l.Error(err, "svc.MochiProfile.GetByTelegramID() failed")
}
wg.Done()
}(u)
} else {
wg.Done()
}(u)

go func(u model.CommandUsageCounter) {
_, err = j.entity.GetSvc().MochiProfile.GetByTelegramID(u.UserPlatformId, false)
if err != nil {
l.Error(err, "svc.MochiProfile.GetByTelegramID() failed")
}
wg.Done()
}(u)
}

go func(u model.CommandUsageCounter) {
_, err = j.entity.GetSvc().MochiProfile.GetProfileActivities(u.ProfileId)
Expand Down
1 change: 1 addition & 0 deletions pkg/model/profile_command_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ type CommandUsageCounter struct {
TotalUsage int64
ProfileId string
UserPlatformId string
Platform string
}
8 changes: 4 additions & 4 deletions pkg/repo/profile_command_usage/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ func NewPG(db *gorm.DB) Store {
}

func (pg *pg) GetTopProfileUsage(top int) ([]model.CommandUsageCounter, error) {
q1 := pg.db.Table("profile_command_usages").Select("user_platform_id, profile_id, count(*) as usage").Where("command = '/profile' AND params = ''").Group("user_platform_id, profile_id")
q2 := pg.db.Table("profile_command_usages").Select("replace(params, 'user:', '') as user_platform_id, count(*) as usage").Where("command = '/profile' AND params != ''").Group("params")
q1 := pg.db.Table("profile_command_usages").Select("user_platform_id, profile_id, platform, count(*) as usage").Where("command = '/profile' AND params = ''").Group("user_platform_id, profile_id, platform")
q2 := pg.db.Table("profile_command_usages").Select("replace(params, 'user:', '') as user_platform_id, platform, count(*) as usage").Where("command = '/profile' AND params != ''").Group("params, platform")

rows, err := pg.db.Raw("WITH t1 AS (?), t2 AS (?) SELECT t1.usage + coalesce(t2.usage, 0) as total_usage, t1.user_platform_id, t1.profile_id FROM t1 left join t2 on t1.user_platform_id = t2.user_platform_id ORDER BY total_usage DESC LIMIT ?", q1, q2, top).Rows()
rows, err := pg.db.Raw("WITH t1 AS (?), t2 AS (?) SELECT t1.usage + coalesce(t2.usage, 0) as total_usage, t1.user_platform_id, t1.profile_id, t1.platform FROM t1 left join t2 on t1.user_platform_id = t2.user_platform_id and t1.platform = t2.platform ORDER BY total_usage DESC LIMIT ?", q1, q2, top).Rows()
if err != nil {
return nil, err
}

var list []model.CommandUsageCounter
for rows.Next() {
var r model.CommandUsageCounter
if err := rows.Scan(&r.TotalUsage, &r.UserPlatformId, &r.ProfileId); err != nil {
if err := rows.Scan(&r.TotalUsage, &r.UserPlatformId, &r.ProfileId, &r.Platform); err != nil {
return nil, err
}
list = append(list, r)
Expand Down

0 comments on commit 323ffff

Please sign in to comment.