package vidmod import ( "time" "91porn-server/common/stderr" "91porn-server/models/commod" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo/options" ) const rankingQueryMaxTime = 3 * time.Second // FindCumulativeRanking keeps the ranking usable when the Redis period rank is // empty. It reuses persisted popularity counters and the existing compound // index for a bounded fallback query. func FindCumulativeRanking(newsType string, page commod.Page) ([]*VideoModel, bool, error) { if newsType == "" || page.PageNumber == 0 || page.PageSize == 0 || page.PageSize > 100 { return nil, false, stderr.ErrParamError } filter, sorts := cumulativeRankingQuery(newsType) opts := options.Find(). SetSkip(page.Skip64()). SetLimit(page.Limit64()). SetSort(sorts). SetMaxTime(rankingQueryMaxTime) return FindList(filter, opts) } func cumulativeRankingQuery(newsType string) (bson.M, bson.D) { return bson.M{ "newsType": newsType, "status": bson.M{"$in": []int{CheckPass, Free}}, }, bson.D{{Key: "likeCount", Value: -1}, {Key: "reviewAt", Value: -1}} }