...
|
...
|
@@ -50,6 +50,9 @@ type RankResult struct{} |
|
|
|
|
|
//计算排行分
|
|
|
func ComputeRankScore() (err error) {
|
|
|
var (
|
|
|
periods []*models.RankPeriod
|
|
|
)
|
|
|
defer func() {
|
|
|
if p := recover(); p != nil {
|
|
|
log.Error(p)
|
...
|
...
|
@@ -64,12 +67,16 @@ func ComputeRankScore() (err error) { |
|
|
defer func() {
|
|
|
atomic.CompareAndSwapInt32(&ComputeRankScoreFlag, 1, 0)
|
|
|
}()
|
|
|
//1.l 查询进行中的赛季
|
|
|
if periods, err = models.GetRankPeriods(0, 0, []int{protocol.RankPeriodBegin}); err != nil {
|
|
|
return
|
|
|
}
|
|
|
//启用多个协程执行
|
|
|
var wg sync.WaitGroup
|
|
|
for i := 0; i < RankGoroutineNum; i++ {
|
|
|
index := i
|
|
|
work := func() {
|
|
|
if err = computeRankScore(index, wg); err != nil {
|
|
|
if err = computeRankScore(index, wg, RankGoroutineNum, periods); err != nil {
|
|
|
log.Error(err)
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -92,15 +99,14 @@ func updateRankPeriodStatus(fromStatus int, toStatus int) { |
|
|
sql = `update rank_period set status=?,update_at=NOW() where status=? and UNIX_TIMESTAMP(begin_time)>? `
|
|
|
}
|
|
|
orm := orm.NewOrm()
|
|
|
if err := utils.ExecuteSQLWithOrmer(orm, sql, toStatus, fromStatus, time.Now().Unix()); err != nil {
|
|
|
if _, err := orm.Raw(sql, toStatus, fromStatus, time.Now().Unix()).Exec(); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func computeRankScore(index int, wg sync.WaitGroup) (err error) {
|
|
|
func computeRankScore(index int, wg sync.WaitGroup, gNum int, periods []*models.RankPeriod) (err error) {
|
|
|
var (
|
|
|
periods []*models.RankPeriod
|
|
|
rankRanges []*models.RankRange
|
|
|
rankRangeDatas []*models.RankRangeData
|
|
|
)
|
...
|
...
|
@@ -112,14 +118,10 @@ func computeRankScore(index int, wg sync.WaitGroup) (err error) { |
|
|
log.Error(p)
|
|
|
}
|
|
|
}()
|
|
|
//1.l 查询进行中的赛季
|
|
|
if periods, err = models.GetRankPeriods(0, 0, []int{protocol.RankPeriodBegin}); err != nil {
|
|
|
return
|
|
|
}
|
|
|
//2.查询对应 rank_type_id 的rank_data
|
|
|
for i := range periods {
|
|
|
period := periods[i]
|
|
|
if (period.Id % RankGoroutineNum) != index {
|
|
|
if (period.Id % gNum) != index {
|
|
|
return
|
|
|
}
|
|
|
if rankRanges, err = models.GetRankRanges(period.CompanyId, period.RankTypeId); err == orm.ErrNoRows {
|
...
|
...
|
@@ -139,6 +141,7 @@ func computeRankScore(index int, wg sync.WaitGroup) (err error) { |
|
|
}
|
|
|
//按类型
|
|
|
//3.查询用户列表/部门列表
|
|
|
logProcessInfo(period)
|
|
|
for j := range rankRanges {
|
|
|
var relationIds []int64
|
|
|
rankRange := rankRanges[j]
|
...
|
...
|
@@ -156,7 +159,6 @@ func computeRankScore(index int, wg sync.WaitGroup) (err error) { |
|
|
}
|
|
|
updateRankByRelationIds(relationIds, period, rankRange)
|
|
|
}
|
|
|
logProcessInfo(period)
|
|
|
}
|
|
|
|
|
|
//4.调用统计接口列表
|
...
|
...
|
@@ -227,6 +229,7 @@ func updateRank(o RankOption) { |
|
|
if _, e = models.AddRank(rank); e != nil {
|
|
|
log.Error(e)
|
|
|
}
|
|
|
log.Debug(fmt.Sprintf("【排行榜统计】 新增加 周期编号:%v 公司:%v 评分者编号:%v (榜单类型:%v 参与类型:%v) ", rank.RankPeriodId, rank.CompanyId, rank.RelationId, rank.RankTypeId, rank.RankRangeId))
|
|
|
} else {
|
|
|
//数据有变化的时候才更新到表
|
|
|
if !reflect.DeepEqual(rankTmp, *rank) {
|
...
|
...
|
@@ -234,6 +237,7 @@ func updateRank(o RankOption) { |
|
|
if e = models.UpdateRankById(rank); e != nil {
|
|
|
log.Error(e)
|
|
|
}
|
|
|
log.Debug(fmt.Sprintf("【排行榜统计】 更新 周期编号:%v 公司:%v 评分者编号:%v (榜单类型:%v 参与类型:%v)", rank.RankPeriodId, rank.CompanyId, rank.RelationId, rank.RankTypeId, rank.RankRangeId))
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -266,12 +270,29 @@ company_id=%v and rank_type_id=%v and rank_period_id=%v` |
|
|
scoreConfig.SumScore.DiscoveryFactor,
|
|
|
scoreConfig.SumScore.CatchFactor, o.CompanyId, o.RankTypeId, o.RankPeriodId)
|
|
|
orm := orm.NewOrm()
|
|
|
if err := utils.ExecuteSQLWithOrmer(orm, sql); err != nil {
|
|
|
if _, err := orm.Raw(sql).Exec(); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//手动单赛季统计
|
|
|
func ComputeRankScoreByPeriod(periodId int) (err error) {
|
|
|
//启用多个协程执行
|
|
|
var (
|
|
|
wg sync.WaitGroup
|
|
|
period *models.RankPeriod
|
|
|
)
|
|
|
if period, err = models.GetRankPeriodById(periodId); err != nil {
|
|
|
log.Error(err)
|
|
|
err = protocol.NewCustomMessage(1, "赛季不存在")
|
|
|
return
|
|
|
}
|
|
|
computeRankScore(0, wg, 1, []*models.RankPeriod{period})
|
|
|
wg.Wait()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//发现计算规则
|
|
|
type RankDiscovery struct{}
|
|
|
|
...
|
...
|
|