fix: 汇总后批量校验游戏权限以优化分级统计
This commit is contained in:
@@ -84,9 +84,6 @@ type analyticsAggPO struct {
|
||||
|
||||
func analyticsRoot(db *gorm.DB, kind business.AnalyticsKind, scope business.AnalyticsScope, f business.AnalyticsFilter, table string, mode int, pay bool) (*gorm.DB, string) {
|
||||
q := analyticsQuery(db, scope, f, table, mode)
|
||||
if kind == business.GameAnalytics {
|
||||
q = q.Where("(r.source_db,r.game_id) IN (?)", approvedGamesQuery(db, scope.Viewer, f.Sources).Select("g.source_db,g.id"))
|
||||
}
|
||||
id := "r.game_id"
|
||||
if kind == business.ChannelAnalytics {
|
||||
id = "r.promote_id"
|
||||
@@ -216,6 +213,11 @@ func (r *analyticsRepo) Aggregate(ctx context.Context, kind business.AnalyticsKi
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if kind == business.GameAnalytics {
|
||||
if err := filterAnalyticsGames(db, scope, f, rowMap); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if withGlobal {
|
||||
totals := toPortraitCounts(global)
|
||||
result.GlobalCounts, result.GlobalPay = &totals, globalPay
|
||||
@@ -280,6 +282,35 @@ func analyticsPairs(keys []business.AnalyticsKey) [][]any {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Aggregate only visible-channel facts, then check game authorization once per
|
||||
// resulting game. Checking approved applications inside each report scan can
|
||||
// force MySQL 5.7 to repeatedly expand the application table before grouping.
|
||||
func filterAnalyticsGames(db *gorm.DB, scope business.AnalyticsScope, f business.AnalyticsFilter, rows map[business.AnalyticsKey]*business.AnalyticsRow) error {
|
||||
keys := make([]business.AnalyticsKey, 0, len(rows))
|
||||
for key := range rows {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
allowed := map[business.AnalyticsKey]bool{}
|
||||
for start := 0; start < len(keys); start += 300 {
|
||||
var games []analyticsAggPO
|
||||
q := approvedGamesQuery(db, scope.Viewer, f.Sources).Where("(g.source_db,g.id) IN ?", analyticsPairs(keys[start:min(start+300, len(keys))]))
|
||||
if err := q.Select("g.source_db,g.id AS entity_id").Scan(&games).Error; err != nil {
|
||||
return business.ErrStorage
|
||||
}
|
||||
for _, game := range games {
|
||||
if game.ID != nil {
|
||||
allowed[business.AnalyticsKey{SourceDB: game.SourceDB, ID: *game.ID}] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
for key := range rows {
|
||||
if !allowed[key] {
|
||||
delete(rows, key)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func filterAnalyticsRows(db *gorm.DB, scope business.AnalyticsScope, f business.AnalyticsFilter, rows map[business.AnalyticsKey]*business.AnalyticsRow) error {
|
||||
keys := []business.AnalyticsKey{}
|
||||
for key := range rows {
|
||||
|
||||
Reference in New Issue
Block a user