Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["rank_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-show/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"audit.go",
"rank.go",
],
importpath = "go-common/app/interface/main/app-show/service/rank",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-show/conf:go_default_library",
"//app/interface/main/app-show/dao/account:go_default_library",
"//app/interface/main/app-show/dao/archive:go_default_library",
"//app/interface/main/app-show/dao/audit:go_default_library",
"//app/interface/main/app-show/dao/recommend:go_default_library",
"//app/interface/main/app-show/dao/region:go_default_library",
"//app/interface/main/app-show/dao/relation:go_default_library",
"//app/interface/main/app-show/model:go_default_library",
"//app/interface/main/app-show/model/region:go_default_library",
"//app/service/main/account/model:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//app/service/main/archive/model/archive:go_default_library",
"//app/service/main/relation/model:go_default_library",
"//library/log:go_default_library",
"//library/sync/errgroup:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,85 @@
package rank
import (
"context"
"fmt"
"strconv"
"go-common/app/interface/main/app-show/model"
"go-common/app/interface/main/app-show/model/region"
"go-common/app/service/main/archive/api"
"go-common/library/log"
)
func (s *Service) loadAuditCache() {
as, err := s.adt.Audits(context.TODO())
if err != nil {
log.Error("s.adt.Audits error(%v)", err)
return
}
s.auditCache = as
}
// Audit check audit plat and ip, then return audit data.
func (s *Service) Audit(c context.Context, mobiApp, order string, plat int8, build, rid int) (res []*region.ShowItem, ok bool) {
if plats, ok := s.auditCache[mobiApp]; ok {
if _, ok = plats[build]; ok {
res = s.auditData(c, plat, rid, _auditRankAids, order)
return res, true
}
}
return nil, false
}
// auditData some data for audit.
func (s *Service) auditData(c context.Context, p int8, rid int, auditAids map[string][]int64, order string) (res []*region.ShowItem) {
key := fmt.Sprintf(_initRank, order, rid)
aids := auditAids[key]
// archive
as, err := s.arc.ArchivesPB(c, aids)
if err != nil {
log.Error("s.arc.ArchivesPB() error(%v)", err)
as = map[int64]*api.Arc{}
}
for _, aid := range aids {
if aid == 0 {
continue
}
item := &region.ShowItem{}
item.Goto = model.GotoAv
item.Param = strconv.FormatInt(aid, 10)
item.URI = model.FillURI(item.Goto, item.Param, nil)
if a, ok := as[aid]; ok {
item.Title = a.Title
item.Cover = a.Pic
item.Name = a.Author.Name
item.Play = int(a.Stat.View)
item.Danmaku = int(a.Stat.Danmaku)
item.Reply = int(a.Stat.Reply)
item.Fav = int(a.Stat.Fav)
}
res = append(res, item)
}
return
}
var (
_auditRankAids = map[string][]int64{
// rid
"rank_key_all_13": []int64{2434272, 7408756, 2426501, 2425990, 2387429, 2425770, 2419790, 2425440, 2414401, 2414476, 2402042, 7411278, 7418371},
"rank_key_all_1": []int64{104614, 190257, 199852, 233649, 258271, 275039, 274410, 286678, 291630, 292757, 299622, 306718, 430248, 432195, 462832, 476328, 508255, 524615, 539880, 559050, 560190, 572499, 576101, 575891, 582598, 588108, 591021, 602868, 620545, 627451, 627197, 621797, 630172, 663583, 666971, 747286, 744310, 789570, 824454, 832270, 851414, 853831, 874099, 884419, 880538, 2460323, 2459108, 2469560, 2471090},
"rank_key_all_3": []int64{8363, 209887, 221107, 322800, 333069, 360940, 361637, 400161, 402851, 465230, 454882, 462766, 482844, 539600, 595218, 644461, 644935, 756287, 785484, 801851, 850424, 853895, 875374, 881693, 1964781, 1966586, 1966643, 1969527, 1968681, 1986904, 1986802, 1984971, 1984330, 2294239, 2473751, 2473083},
"rank_key_all_129": []int64{886841, 936016, 966192, 1406019, 1773160, 1935680, 1958897, 1960912, 1964367, 1976153, 1985297, 1984555},
"rank_key_all_4": []int64{846437, 862006, 861854, 861796, 861711, 861409, 861209, 861124, 860863, 860273, 860187, 859930, 859374, 859264, 859132, 866115, 866091, 865870, 865843, 865610, 865511, 865442, 865388, 865318, 865259, 865076, 864958, 864845, 864737, 864732, 864700, 864528, 864390, 864360, 864344, 864264, 864121, 863929, 864969, 864762},
"rank_key_all_36": []int64{403120, 580862, 834839, 844027, 862316, 876708, 875624, 872364, 873174, 880857, 1971484, 1969254, 1968901, 1986932, 1985610, 2312249, 2310494, 2313588, 2316089, 2317272},
"rank_key_all_5": []int64{2318219},
"rank_key_all_119": []int64{78287, 638240, 913421, 1625784, 1636345, 1727650, 1743126, 1911041, 1959692, 1975358, 1976535, 1979757, 1982533, 1986533, 1985956},
"rank_key_all_23": []int64{1974120, 2038896, 2006455, 1957548, 377264, 1983046, 1955773, 1933830},
"rank_key_all_155": []int64{4991648},
"rank_key_all_160": []int64{8141, 7790, 7698, 6608, 6510, 6336, 6234, 5876, 5464, 5309, 5242, 5230, 5222, 4914, 10040, 9649, 8996, 8993, 11239, 11472, 11787, 12243, 12182, 12727, 15347, 16900, 16762, 17907, 18888, 200595, 288592, 286616, 339727, 393459, 517475, 530016, 551985, 555395, 581574, 588820, 592772, 592586, 633358, 648300, 659978, 666026, 658894, 658644, 658110, 667016, 668533, 668054, 673692, 673220, 672850, 670544, 677766, 680535, 680029, 670137, 679027, 695862, 686220, 685223, 689694, 690522, 702762, 698339, 696603, 701297, 707328, 700111, 708986, 708474, 699555, 699508},
"rank_key_all_11": []int64{1916941, 2031210, 2034983, 2015734, 2016150, 1964927, 1961205, 2039658},
"rank_key_all_33": []int64{2434272},
"rank_key_all_0": []int64{104614, 190257, 199852, 233649, 258271, 275039, 274410, 286678, 291630, 292757, 299622, 306718, 430248, 432195, 462832, 476328, 508255, 524615, 539880, 559050, 560190, 572499, 576101, 575891, 582598, 588108, 591021, 602868, 620545, 627451, 627197, 621797, 630172, 663583, 666971, 747286, 744310, 789570, 824454, 832270, 851414, 853831, 874099, 884419, 880538, 2460323, 2459108, 2469560, 2471090, 209887, 221107, 322800, 333069, 360940, 361637, 400161, 402851, 465230, 454882, 462766, 482844, 539600, 595218, 644461, 644935, 756287, 785484, 801851, 850424, 853895, 875374, 881693, 1964781, 1966586, 1966643, 1969527, 1968681},
"rank_key_origin_0": []int64{291630, 299622, 364103, 430248, 432195, 462832, 508255, 539880, 559050, 560190, 576101, 582598, 588108, 620545, 627451, 627197, 630172, 824454, 832270, 851414, 874099, 884419, 880538, 2460323, 2459108, 2471090, 322800, 333069, 360940, 361637, 400161, 402851, 465230, 454882, 462766, 482844, 595218, 644461, 644935, 756287, 785484, 836018, 850424, 853895, 875374, 881693, 1966643, 1969527, 1968681, 1986904, 1986802, 1984330, 2473751, 2473083, 861854, 866115, 866091, 865843, 865610, 865511, 865442, 865388, 865318, 864958, 864845, 864737, 864700, 864528, 864390, 864360, 864344, 864264, 864121, 863929, 864705, 1977493, 1982920, 2312184, 2316655, 2314523},
}
)

View File

@@ -0,0 +1,298 @@
package rank
import (
"context"
"fmt"
"time"
"go-common/app/interface/main/app-show/conf"
accdao "go-common/app/interface/main/app-show/dao/account"
arcdao "go-common/app/interface/main/app-show/dao/archive"
adtdao "go-common/app/interface/main/app-show/dao/audit"
rcmmndao "go-common/app/interface/main/app-show/dao/recommend"
rgdao "go-common/app/interface/main/app-show/dao/region"
reldao "go-common/app/interface/main/app-show/dao/relation"
"go-common/app/interface/main/app-show/model"
"go-common/app/interface/main/app-show/model/region"
account "go-common/app/service/main/account/model"
"go-common/app/service/main/archive/api"
"go-common/app/service/main/archive/model/archive"
relation "go-common/app/service/main/relation/model"
"go-common/library/log"
"go-common/library/sync/errgroup"
)
const (
_initRank = "rank_key_%s_%d"
)
var (
// 番剧 动画,音乐,舞蹈,游戏,科技,娱乐,鬼畜,电影,时尚, 生活,连载番剧(二级分区),国漫,影视,纪录片,国创相关,数码
_tids = []int{13, 1, 3, 129, 4, 36, 5, 119, 23, 155, 160, 11, 33, 167, 181, 177, 168, 188}
// region.ShowItem
_emptyShowItems = []*region.ShowItem{}
// _pgctids = map[int]struct{}{
// 177: struct{}{},
// }
)
type Service struct {
c *conf.Config
// region
rdao *rgdao.Dao
// rcmmnd
rcmmnd *rcmmndao.Dao
// archive
arc *arcdao.Dao
// audit
adt *adtdao.Dao
// account
accd *accdao.Dao
// relation
reldao *reldao.Dao
// tick
tick time.Duration
// ranking
rankCache map[string][]*region.ShowItem
rankOseaCache map[string][]*region.ShowItem
// audit cache
auditCache map[string]map[int]struct{} // audit mobi_app builds
}
// New new a region service.
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
rdao: rgdao.New(c),
rcmmnd: rcmmndao.New(c),
// archive
arc: arcdao.New(c),
// audit
adt: adtdao.New(c),
// account
accd: accdao.New(c),
// relation
reldao: reldao.New(c),
// tick
tick: time.Duration(c.Tick),
// ranking
rankCache: map[string][]*region.ShowItem{},
rankOseaCache: map[string][]*region.ShowItem{},
// audit cache
auditCache: map[string]map[int]struct{}{},
}
time.Sleep(time.Second * 2)
s.load()
s.loadAuditCache()
go s.loadproc()
return
}
// RankShow
func (s *Service) RankShow(c context.Context, plat int8, rid, pn, ps int, mid int64, order string) (res []*region.ShowItem) {
var (
key = fmt.Sprintf(_initRank, order, rid)
tmp []*region.ShowItem
authorMids []int64
authorMidExist = map[int64]struct{}{}
authorRelations map[int64]*account.Relation
authorStats map[int64]*relation.Stat
authorCards map[int64]*account.Card
err error
)
if model.IsOverseas(plat) {
tmp = s.rankOseaCache[key]
} else {
tmp = s.rankCache[key]
}
start := (pn - 1) * ps
end := start + ps
if len(tmp) == 0 || start > len(tmp) {
res = _emptyShowItems
return
}
if end < len(tmp) {
tmp = tmp[start:end]
} else {
tmp = tmp[start:]
}
for _, t := range tmp {
i := &region.ShowItem{}
*i = *t
res = append(res, i)
// up mid
if _, ok := authorMidExist[i.Mid]; !ok && i.Mid > 0 {
authorMids = append(authorMids, i.Mid)
authorMidExist[i.Mid] = struct{}{}
}
}
if len(authorMids) > 0 {
g, ctx := errgroup.WithContext(c)
g.Go(func() error {
if authorCards, err = s.accd.Cards3(ctx, authorMids); err != nil {
log.Error("s.accd.Cards3 error(%v)", err)
}
return nil
})
if mid > 0 {
g.Go(func() error {
if authorRelations, err = s.accd.Relations3(ctx, mid, authorMids); err != nil {
log.Error("s.accd.Relations2 error(%v)", err)
}
return nil
})
}
g.Go(func() error {
if authorStats, err = s.reldao.Stats(ctx, authorMids); err != nil {
log.Error("s.reldao.Stats error(%v)", err)
}
return nil
})
if err = g.Wait(); err != nil {
log.Error("RankUser errgroup.WithContext error(%v)", err)
}
}
for _, i := range res {
if len(authorRelations) > 0 {
if relations, ok := authorRelations[i.Mid]; ok {
if relations.Following {
i.Attribute = 1
}
}
}
if len(authorStats) > 0 {
if stats, ok := authorStats[i.Mid]; ok {
i.Follower = int(stats.Follower)
}
}
if len(authorCards) > 0 {
if info, ok := authorCards[i.Mid]; ok {
ov := &region.OfficialVerify{}
ov.FromOfficialVerify(info.Official)
i.OfficialVerify = ov
}
}
if !model.IsIPad(plat) {
if i.RedirectURL != "" {
i.URI = i.RedirectURL
i.Goto = model.GotoBangumi
}
}
}
return
}
// loadproc
func (s *Service) loadproc() {
for {
time.Sleep(s.tick)
s.load()
s.loadAuditCache()
}
}
// load load Rank all
func (s *Service) load() {
var (
tmp = map[string][]*region.ShowItem{}
tmpOsea = map[string][]*region.ShowItem{}
)
for _, rid := range _tids {
if rid == 33 {
aids, others, scores, err := s.rcmmnd.RankAppBangumi(context.TODO())
key := fmt.Sprintf(_initRank, "bangumi", 0)
if err != nil || len(aids) < 5 {
log.Error("s.rcmmnd.RankAppBangumi len lt 20 OR error(%v)", err)
tmp[key], tmpOsea[key] = s.rankCache[key], s.rankOseaCache[key]
continue
}
tmp[key], tmpOsea[key] = s.fromRankAids(context.TODO(), aids, others, scores)
log.Info("loadRankBangumi success")
} else {
aids, others, scores, err := s.rcmmnd.RankAppRegion(context.TODO(), rid)
key := fmt.Sprintf(_initRank, "all", rid)
if err != nil || len(aids) < 5 {
log.Error("s.rcmmnd.RankAppRegion rid (%v) len lt 20 OR error(%v)", rid, err)
tmp[key], tmpOsea[key] = s.rankCache[key], s.rankOseaCache[key]
continue
}
tmp[key], tmpOsea[key] = s.fromRankAids(context.TODO(), aids, others, scores)
log.Info("loadRankRegion(%s_%d) success", "all", rid)
}
}
aids, others, scores, err := s.rcmmnd.RankAppAll(context.TODO())
key := fmt.Sprintf(_initRank, "all", 0)
if err != nil || len(aids) < 5 {
log.Error("s.rcmmnd.RankAppAll(%s) len lt 20 OR error(%v)", "all", err)
return
}
tmp[key], tmpOsea[key] = s.fromRankAids(context.TODO(), aids, others, scores)
log.Info("loadRank(%s) success", "all")
aids, others, scores, err = s.rcmmnd.RankAppOrigin(context.TODO())
key = fmt.Sprintf(_initRank, "origin", 0)
if err != nil || len(aids) < 5 {
log.Error("s.rcmmnd.RankAppOrigin(%s) len lt 20 OR error(%v)", "all", err)
return
}
tmp[key], tmpOsea[key] = s.fromRankAids(context.TODO(), aids, others, scores)
log.Info("loadRank(%s) success", "origin")
if len(tmp) > 0 {
s.rankCache = tmp
}
if len(tmpOsea) > 0 {
s.rankOseaCache = tmpOsea
}
}
// fromRankAids
func (s *Service) fromRankAids(ctx context.Context, aids []int64, others, scores map[int64]int64) (sis, sisOsea []*region.ShowItem) {
var (
aid int64
as map[int64]*api.Arc
arc *api.Arc
ok bool
err error
paid int64
)
if as, err = s.arc.ArchivesPB(ctx, aids); err != nil {
log.Error("s.arc.ArchivesPB error(%v)", err)
return
}
if len(as) == 0 {
log.Warn("s.arc.ArchivesPB(%v) length is 0", aids)
return
}
child := map[int64][]*region.ShowItem{}
childOsea := map[int64][]*region.ShowItem{}
for _, aid = range aids {
if arc, ok = as[aid]; ok {
if paid, ok = others[arc.Aid]; ok {
i := &region.ShowItem{}
i.FromArchivePBRank(arc, scores)
child[paid] = append(child[paid], i)
if arc.AttrVal(archive.AttrBitOverseaLock) == 0 {
childOsea[paid] = append(childOsea[paid], i)
}
}
}
}
for _, aid = range aids {
if arc, ok = as[aid]; ok {
if _, ok = others[arc.Aid]; !ok {
i := &region.ShowItem{}
i.FromArchivePBRank(arc, scores)
if arc.AttrVal(archive.AttrBitOverseaLock) == 0 {
if tmpchild, ok := childOsea[arc.Aid]; ok {
i.Children = tmpchild
}
sisOsea = append(sisOsea, i)
}
if tmpchild, ok := child[arc.Aid]; ok {
i.Children = tmpchild
}
sis = append(sis, i)
}
}
}
return
}

View File

@@ -0,0 +1,37 @@
package rank
import (
"context"
"flag"
"go-common/app/interface/main/app-show/conf"
"path/filepath"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
var (
s *Service
)
func WithService(f func(s *Service)) func() {
return func() {
f(s)
}
}
func init() {
dir, _ := filepath.Abs("../../cmd/app-show-test.toml")
flag.Set("conf", dir)
conf.Init()
s = New(conf.Conf)
time.Sleep(time.Second)
}
func TestRankShow(t *testing.T) {
Convey("get RankShow data", t, WithService(func(s *Service) {
res := s.RankShow(context.TODO(), 0, 1, 1, 20, 0, "all")
So(res, ShouldNotBeEmpty)
}))
}