Skip to content

Commit

Permalink
feat(job): calc reviewer adoption
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Aug 22, 2023
1 parent 098e53d commit 3facca8
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 3 deletions.
4 changes: 2 additions & 2 deletions internal/dao/internal/poll_users.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions internal/logic/job/poll/calc_reviewer_adoption_rate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package poll

import (
"context"
"fmt"

"github.com/gogf/gf/v2/util/gconv"
"github.com/hitokoto-osc/reviewer/internal/consts"

"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/hitokoto-osc/reviewer/internal/dao"
)

func CalcReviewerAdoptionRate(ctx context.Context) error {
g.Log().Debug(ctx, "开始计算审核员采纳率……")
// 暴力获取存在记录的审核员和管理员(且当前存在权限的)
records, err := g.DB().Ctx(ctx).Raw(
fmt.Sprintf("SELECT `%s` FROM `%s` WHERE `%s` in ("+
"SELECT `%s` FROM `%s` WHERE `%s` = 1 OR `%s` = 1"+
")",
dao.PollUsers.Columns().UserId,
dao.PollUsers.Table(),
dao.PollUsers.Columns().UserId,
dao.Users.Columns().IsReviewer,
dao.Users.Table(),
dao.Users.Columns().IsAdmin,
dao.Users.Columns().IsReviewer,
),
).Array()
if err != nil {
return gerror.Wrap(err, "获取需要计算的用户失败")
}
userIDs := gconv.Ints(records)
for _, userID := range userIDs {
g.Log().Debugf(ctx, "开始计算用户 %d 的采纳率……", userID)
adoptions, e := getUserAdoptions(ctx, gconv.Uint(userID))
if e != nil {
e = gerror.Wrapf(e, "获取用户 %d 采纳率失败:", userID)
g.Log().Error(ctx, e)
continue
}
adoptionsRate := float64(adoptions.Adoptions) / float64(adoptions.Total)
// 更新用户采纳率
_, e = dao.PollUsers.Ctx(ctx).
Where(dao.PollUsers.Columns().UserId, userID).
Update(dao.PollUsers.Columns().AdoptionRate, adoptionsRate)
if e != nil {
e = gerror.Wrapf(e, "更新用户 %d 采纳率失败:", userID)
g.Log().Error(ctx, e)
}
}
g.Log().Debug(ctx, "计算审核员采纳率完成!")
return nil
}

type UserAdoptions struct {
Total int `json:"total"` // 总数
Adoptions int `json:"adoptions"` // 采纳数
}

func getUserAdoptions(ctx context.Context, userID uint) (*UserAdoptions, error) {
sql := fmt.Sprintf(`SELECT
COUNT( 1 ) AS total,
SUM(
CASE
WHEN `+"`%s`"+` = "超时自动处理" THEN 1 # 就当他通过了吧
WHEN ( pipeline.`+"`%s`"+` = %d AND log.`+"`%s`"+` = %d ) THEN 1
WHEN ( pipeline.`+"`%s`"+` = %d AND log.`+"`%s`"+` = %d ) THEN 1
WHEN ( pipeline.`+"`%s`"+` = %d AND log.`+"`%s`"+` = %d ) THEN 1 ELSE 0
END
) AS adoption
FROM
`+"`%s`"+` log
JOIN `+"`%s`"+` pipeline ON log.%s = pipeline.%s
WHERE
log.user_id = %d`,
dao.PollPipeline.Columns().Mark,
// WHEN 批准
dao.PollPipeline.Columns().Operate,
int(consts.PollStatusApproved),
dao.PollLog.Columns().Type,
int(consts.PollMethodApprove),
// WHEN 驳回
dao.PollPipeline.Columns().Operate,
int(consts.PollStatusRejected),
dao.PollLog.Columns().Type,
int(consts.PollMethodReject),
// WHEN 需要修改
dao.PollPipeline.Columns().Operate,
int(consts.PollStatusNeedModify),
dao.PollLog.Columns().Type,
int(consts.PollMethodNeedModify),
// 表
dao.PollLog.Table(),
dao.PollPipeline.Table(),
dao.PollLog.Columns().PollId,
dao.PollPipeline.Columns().PollId,
userID,
)
var result UserAdoptions
err := g.DB().Ctx(ctx).Raw(sql).Scan(&result)
if err != nil {
return nil, gerror.Wrap(err, "获取用户采纳率失败")
}
return &result, nil
}
4 changes: 4 additions & 0 deletions internal/logic/job/poll_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func DoPollDailyTask(ctx context.Context) {
if err != nil {
g.Log().Error(ctx, err)
}
err = poll.CalcReviewerAdoptionRate(ctx)
if err != nil {
g.Log().Error(ctx, err)
}
}

func RegisterPollTask(ctx context.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/logic/user/reviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func (s *sUser) GetReviewersAndAdmins(ctx context.Context) (users []entity.Users, err error) {
err = dao.Users.Ctx(ctx).Cache(gdb.CacheOption{
Duration: time.Minute * 10, // 缓存十分钟
Name: "users:should_do_notifications",
Name: "users:reviewersAndAdmins",
Force: false,
}).
WhereOr(dao.Users.Columns().IsReviewer, 1). // 审核员
Expand Down

0 comments on commit 3facca8

Please sign in to comment.