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,70 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"api_test.go",
"dao_test.go",
"memcache_test.go",
"stat_test.go",
"view_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-intl/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"api.go",
"dao.go",
"memcache.go",
"stat.go",
"view.go",
],
importpath = "go-common/app/interface/main/app-intl/dao/archive",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-intl/conf:go_default_library",
"//app/interface/main/app-intl/model/player/archive:go_default_library",
"//app/interface/main/app-intl/model/view:go_default_library",
"//app/interface/main/history/model:go_default_library",
"//app/interface/main/history/rpc/client:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//app/service/main/archive/api/gorpc:go_default_library",
"//app/service/main/archive/model/archive:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//library/sync/errgroup:go_default_library",
"//library/xstr:go_default_library",
"//vendor/github.com/pkg/errors: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,133 @@
package archive
import (
"context"
"net/http"
"net/url"
"strconv"
"go-common/app/interface/main/app-intl/model/view"
"go-common/app/service/main/archive/model/archive"
"go-common/library/ecode"
"go-common/library/net/metadata"
"go-common/library/xstr"
"github.com/pkg/errors"
)
const (
_realteURL = "/recsys/related"
_commercialURL = "/x/internal/creative/arc/commercial"
_relateRecURL = "/recommand"
_playURL = "/playurl/batch"
)
// RelateAids get relate by aid
func (d *Dao) RelateAids(c context.Context, aid int64) (aids []int64, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("key", strconv.FormatInt(aid, 10))
var res struct {
Code int `json:"code"`
Data []*struct {
Value string `json:"value"`
} `json:"data"`
}
if err = d.client.Get(c, d.realteURL, ip, params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.realteURL+"?"+params.Encode())
return
}
if len(res.Data) != 0 {
if aids, err = xstr.SplitInts(res.Data[0].Value); err != nil {
err = errors.Wrap(err, res.Data[0].Value)
}
}
return
}
// Commercial is
func (d *Dao) Commercial(c context.Context, aid int64) (gameID int64, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("aid", strconv.FormatInt(aid, 10))
var res struct {
Code int `json:"code"`
Data *struct {
GameID int64 `json:"game_id"`
} `json:"data"`
}
if err = d.client.Get(c, d.commercialURL, ip, params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.commercialURL+"?"+params.Encode())
return
}
if res.Data != nil {
gameID = res.Data.GameID
}
return
}
// NewRelateAids relate online recommend 在线实时推荐
func (d *Dao) NewRelateAids(c context.Context, aid, mid int64, build int, buvid, from string, plat int8) (rec []*view.NewRelateRec, userFeature, returnCode string, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("from", "2")
params.Set("cmd", "related")
params.Set("timeout", "100")
params.Set("plat", strconv.Itoa(int(plat)))
params.Set("build", strconv.Itoa(build))
params.Set("buvid", buvid)
params.Set("from_av", strconv.FormatInt(aid, 10))
params.Set("request_cnt", "40")
params.Set("source_page", from)
var res struct {
Code int `json:"code"`
Data []*view.NewRelateRec `json:"data"`
UserFeature string `json:"user_feature"`
}
if err = d.client.Get(c, d.relateRecURL, ip, params, &res); err != nil {
returnCode = "500"
return
}
returnCode = strconv.Itoa(res.Code)
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.relateRecURL+"?"+params.Encode())
return
}
userFeature = res.UserFeature
rec = res.Data
return
}
// PlayerInfos cid with player info
func (d *Dao) PlayerInfos(c context.Context, cids []int64, qn int, platform string, fnver, fnval int) (pm map[uint32]*archive.BvcVideoItem, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("cid", xstr.JoinInts(cids))
params.Set("qn", strconv.Itoa(qn))
params.Set("platform", platform)
params.Set("uip", ip)
params.Set("layout", "pb")
params.Set("fnver", strconv.Itoa(fnver))
params.Set("fnval", strconv.Itoa(fnval))
var req *http.Request
if req, err = d.client.NewRequest("GET", d.playURL, ip, params); err != nil {
return
}
res := new(archive.BvcResponseMsg)
if err = d.client.PB(c, req, res); err != nil {
return
}
if int(res.Code) != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(int(res.Code)), d.playURL+params.Encode())
return
}
pm = res.Data
return
}

View File

@@ -0,0 +1,52 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestRelateAids(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.RelateAids(context.Background(), 1)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestCommercial(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.Commercial(context.Background(), 12)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestNewRelateAids(t *testing.T) {
Convey(t.Name(), t, func() {
_, _, _, err := d.NewRelateAids(context.Background(), 12, 12, 0, "", "", 1)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestPlayerInfos(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.PlayerInfos(context.Background(), []int64{12}, 1, "", 0, 2)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,254 @@
package archive
import (
"context"
"runtime"
"time"
"go-common/app/interface/main/app-intl/conf"
arcmdl "go-common/app/interface/main/app-intl/model/player/archive"
"go-common/app/interface/main/app-intl/model/view"
history "go-common/app/interface/main/history/model"
hisrpc "go-common/app/interface/main/history/rpc/client"
"go-common/app/service/main/archive/api"
arcrpc "go-common/app/service/main/archive/api/gorpc"
"go-common/app/service/main/archive/model/archive"
"go-common/library/cache/memcache"
"go-common/library/ecode"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/metadata"
"go-common/library/sync/errgroup"
"github.com/pkg/errors"
)
// Dao is archive dao.
type Dao struct {
// http client
client *bm.Client
realteURL string
commercialURL string
relateRecURL string
playURL string
// rpc
arcRPC *arcrpc.Service2
arcRPC2 *arcrpc.Service2
hisRPC *hisrpc.Service
// mc
mc *memcache.Pool
expireMc int32
expireRlt int32
// chan
mCh chan func()
}
// New new a archive dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// http client
client: bm.NewClient(c.HTTPWrite),
realteURL: c.Host.Data + _realteURL,
commercialURL: c.Host.APICo + _commercialURL,
relateRecURL: c.Host.Data + _relateRecURL,
playURL: c.Host.Bvcvod + _playURL,
// rpc
arcRPC: arcrpc.New2(c.ArchiveRPC),
arcRPC2: arcrpc.New2(c.ArchiveRPC2),
hisRPC: hisrpc.New(c.HisRPC),
// mc
mc: memcache.NewPool(c.Memcache.Feed.Config),
expireMc: int32(time.Duration(c.Memcache.Feed.Expire) / time.Second),
expireRlt: int32(time.Duration(c.Memcache.Archive.RelateExpire) / time.Second),
// mc proc
mCh: make(chan func(), 10240),
}
for i := 0; i < runtime.NumCPU()*2; i++ {
go d.cacheproc()
}
return
}
// Ping ping check memcache connection
func (d *Dao) Ping(c context.Context) (err error) {
return d.pingMC(c)
}
// Archives multi get archives.
func (d *Dao) Archives(c context.Context, aids []int64) (am map[int64]*api.Arc, err error) {
if len(aids) == 0 {
return
}
g, ctx := errgroup.WithContext(c)
g.Go(func() (err error) {
var missed []int64
if am, missed, err = d.arcsCache(ctx, aids); err != nil {
missed = aids
log.Error("%+v", err)
err = nil
}
if len(missed) == 0 {
return
}
var tmp map[int64]*api.Arc
arg := &archive.ArgAids2{Aids: missed}
if tmp, err = d.arcRPC.Archives3(ctx, arg); err != nil {
err = errors.Wrapf(err, "%v", arg)
return
}
for aid, a := range tmp {
am[aid] = a
}
return
})
var stm map[int64]*api.Stat
g.Go(func() (err error) {
var missed []int64
if stm, missed, err = d.statsCache(ctx, aids); err != nil {
missed = aids
log.Error("%+v", err)
err = nil
}
if len(missed) == 0 {
return
}
tmp, err := d.arcRPC.Stats3(ctx, &archive.ArgAids2{Aids: missed})
if err != nil {
log.Error("%+v", err)
err = nil
return
}
for _, st := range tmp {
stm[st.Aid] = st
}
return
})
if err = g.Wait(); err != nil {
return
}
for aid, arc := range am {
if st, ok := stm[aid]; ok {
arc.Stat = *st
}
}
return
}
// ArchivesWithPlayer archives witch player
func (d *Dao) ArchivesWithPlayer(c context.Context, aids []int64, qn int, platform string, fnver, fnval int) (res map[int64]*archive.ArchiveWithPlayer, err error) {
if len(aids) == 0 {
return
}
// 国际版暂时不秒开
// ip := metadata.String(c, metadata.RemoteIP)
ip := ""
arg := &archive.ArgPlayer{Aids: aids, Qn: qn, Platform: platform, Fnval: fnval, Fnver: fnver, RealIP: ip}
if res, err = d.arcRPC.ArchivesWithPlayer(c, arg); err != nil {
err = errors.Wrapf(err, "%v", arg)
}
return
}
// Archive get archive mc->rpc.
func (d *Dao) Archive(c context.Context, aid int64) (a *api.Arc, err error) {
if a, err = d.arcCache(c, aid); err != nil {
log.Error("%+v", err)
} else if a != nil {
return
}
arg := &archive.ArgAid2{Aid: aid}
if a, err = d.arcRPC.Archive3(c, arg); err != nil {
log.Error("d.arcRPC.Archive3(%v) error(%v)", arg, err)
if a, err = d.arcRPC2.Archive3(c, arg); err != nil {
err = errors.Wrapf(err, "%v", arg)
}
}
return
}
// Archive3 get archive.
func (d *Dao) Archive3(c context.Context, aid int64) (a *api.Arc, err error) {
arg := &archive.ArgAid2{Aid: aid}
if a, err = d.arcRPC.Archive3(c, arg); err != nil {
log.Error("d.arcRPC.Archive3(%v) error(%+v)", arg, err)
if a, err = d.arcRPC2.Archive3(c, arg); err != nil {
err = errors.Wrapf(err, "d.arcRPC2.Archive3(%v)", arg)
return
}
}
return
}
// Progress is archive plays progress .
func (d *Dao) Progress(c context.Context, aid, mid int64) (h *view.History, err error) {
ip := metadata.String(c, metadata.RemoteIP)
arg := &history.ArgPro{Mid: mid, Aids: []int64{aid}, RealIP: ip}
his, err := d.hisRPC.Progress(c, arg)
if err != nil {
log.Error("d.hisRPC.Progress(%v) error(%v)", arg, err)
return
}
if his[aid] != nil {
h = &view.History{Cid: his[aid].Cid, Progress: his[aid].Pro}
}
return
}
// UpCount2 get upper count.
func (d *Dao) UpCount2(c context.Context, mid int64) (cnt int, err error) {
arg := &archive.ArgUpCount2{Mid: mid}
if cnt, err = d.arcRPC.UpCount2(c, arg); err != nil {
err = errors.Wrapf(err, "%v", arg)
}
return
}
// ArchiveCache is
func (d *Dao) ArchiveCache(c context.Context, aid int64) (arc *arcmdl.Info, err error) {
var (
vp *archive.View3
cids []int64
)
if vp, err = d.ViewCache(c, aid); err != nil {
log.Error("%+v", err)
}
if vp == nil || vp.Archive3 == nil || len(vp.Pages) == 0 || vp.AttrVal(archive.AttrBitIsMovie) == archive.AttrYes {
if vp, err = d.View3(c, aid); err != nil {
log.Error("%+v", err)
err = ecode.NothingFound
return
}
}
if vp == nil || vp.Archive3 == nil || len(vp.Pages) == 0 {
err = ecode.NothingFound
return
}
for _, p := range vp.Pages {
cids = append(cids, p.Cid)
}
arc = &arcmdl.Info{
Aid: vp.Aid,
State: vp.State,
Mid: vp.Author.Mid,
Cids: cids,
Attribute: vp.Attribute,
}
return
}
// addCache add archive to mc or redis
func (d *Dao) addCache(f func()) {
select {
case d.mCh <- f:
default:
log.Warn("cacheproc chan full")
}
}
// cacheproc write memcache and stat redis use goroutine
func (d *Dao) cacheproc() {
for {
f := <-d.mCh
f()
}
}

View File

@@ -0,0 +1,104 @@
package archive
import (
"context"
"flag"
"os"
"testing"
"go-common/app/interface/main/app-intl/conf"
. "github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.app-intl")
flag.Set("conf_token", "02007e8d0f77d31baee89acb5ce6d3ac")
flag.Set("tree_id", "64518")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../../cmd/app-intl-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}
func TestArchives(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.Archives(context.Background(), []int64{1, 2})
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestArchivesWithPlayer(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.ArchivesWithPlayer(context.Background(), []int64{1, 2}, 1, "", 1, 2)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestArchive(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.Archive(context.Background(), 122)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestArchive3(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.Archive3(context.Background(), 122)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestProgress(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.Progress(context.Background(), 12, 133)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestUpCount2(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.UpCount2(context.Background(), 122)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,258 @@
package archive
import (
"context"
"strconv"
"go-common/app/interface/main/app-intl/model/view"
"go-common/app/service/main/archive/api"
"go-common/app/service/main/archive/model/archive"
"go-common/library/cache/memcache"
"go-common/library/log"
"github.com/pkg/errors"
)
const (
_prefixRelate = "al_"
_prefix = "a3p_"
_prxfixSt = "stp_"
_prefixViewStatic = "avp_"
_prefixViewContribute = "avpc_"
)
// keyArc is.
func keyArc(aid int64) string {
return _prefix + strconv.FormatInt(aid, 10)
}
// keySt is.
func keySt(aid int64) string {
return _prxfixSt + strconv.FormatInt(aid, 10)
}
// keyView is.
func keyView(aid int64) string {
return _prefixViewStatic + strconv.FormatInt(aid, 10)
}
// keyRl is.
func keyRl(aid int64) string {
return _prefixRelate + strconv.FormatInt(aid, 10)
}
// keyViewContribute is.
func keyViewContribute(mid int64) string {
return _prefixViewContribute + strconv.FormatInt(mid, 10)
}
// pingMC is.
func (d *Dao) pingMC(c context.Context) (err error) {
conn := d.mc.Get(c)
err = conn.Set(&memcache.Item{Key: "ping", Object: []byte{1}, Flags: memcache.FlagJSON, Expiration: 0})
conn.Close()
return
}
// statsCache get stat cache by aids
func (d *Dao) statsCache(c context.Context, aids []int64) (cached map[int64]*api.Stat, missed []int64, err error) {
cached = make(map[int64]*api.Stat, len(aids))
var (
conn = d.mc.Get(c)
keys = make([]string, 0, len(aids))
rs map[string]*memcache.Item
)
defer conn.Close()
for _, aid := range aids {
keys = append(keys, keySt(aid))
}
if rs, err = conn.GetMulti(keys); err != nil {
err = errors.Wrapf(err, "%v", keys)
return
}
for _, item := range rs {
var st = &api.Stat{}
if err = conn.Scan(item, st); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
err = nil
continue
}
cached[st.Aid] = st
}
if len(cached) == len(aids) {
return
}
for _, aid := range aids {
if _, ok := cached[aid]; !ok {
missed = append(missed, aid)
}
}
return
}
// arcsCache get archives cache.
func (d *Dao) arcsCache(c context.Context, aids []int64) (cached map[int64]*api.Arc, missed []int64, err error) {
var (
keys = make([]string, 0, len(aids))
conn = d.mc.Get(c)
aidmap = make(map[string]int64, len(aids))
rs map[string]*memcache.Item
a *api.Arc
)
cached = make(map[int64]*api.Arc, len(aids))
defer conn.Close()
for _, aid := range aids {
k := keyArc(aid)
if _, ok := aidmap[k]; !ok {
keys = append(keys, k)
aidmap[k] = aid
}
}
if rs, err = conn.GetMulti(keys); err != nil {
err = errors.Wrapf(err, "%v", keys)
return
}
for k, r := range rs {
a = &api.Arc{}
if err = conn.Scan(r, a); err != nil {
log.Error("conn.Scan(%s) error(%v)", r.Value, err)
err = nil
continue
}
cached[aidmap[k]] = a
// delete hit key
delete(aidmap, k)
}
// missed key
missed = make([]int64, 0, len(aidmap))
for _, aid := range aidmap {
missed = append(missed, aid)
}
return
}
// arcCache get archive cache.
func (d *Dao) arcCache(c context.Context, aid int64) (a *api.Arc, err error) {
conn := d.mc.Get(c)
key := keyArc(aid)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
a = &api.Arc{}
if err = conn.Scan(r, a); err != nil {
a = nil
err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
}
return
}
// viewCache get view cache from remote memecache .
func (d *Dao) viewCache(c context.Context, aid int64) (vs *archive.View3, err error) {
conn := d.mc.Get(c)
key := keyView(aid)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
vs = &archive.View3{Archive3: &archive.Archive3{}}
if err = conn.Scan(r, vs); err != nil {
vs = nil
err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
}
return
}
// statCache get a archive stat from cache.
func (d *Dao) statCache(c context.Context, aid int64) (st *api.Stat, err error) {
conn := d.mc.Get(c)
key := keySt(aid)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
st = &api.Stat{}
if err = conn.Scan(r, st); err != nil {
st = nil
err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
}
return
}
// RelatesCache get relates.
func (d *Dao) RelatesCache(c context.Context, aid int64) (rls []*view.Relate, err error) {
conn := d.mc.Get(c)
key := keyRl(aid)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
if err = conn.Scan(r, &rls); err != nil {
err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
}
return
}
// AddRelatesCache add relates
func (d *Dao) AddRelatesCache(aid int64, rls []*view.Relate) {
d.addCache(func() {
d.addRelatesCache(context.Background(), aid, rls)
})
}
// addRelatesCache add relates cache.
func (d *Dao) addRelatesCache(c context.Context, aid int64, rls []*view.Relate) (err error) {
conn := d.mc.Get(c)
key := keyRl(aid)
item := &memcache.Item{Key: key, Object: rls, Flags: memcache.FlagJSON, Expiration: d.expireRlt}
if err = conn.Set(item); err != nil {
err = errors.Wrapf(err, "conn.Set(%s,%v,%d)", key, rls, d.expireRlt)
}
conn.Close()
return
}
// ViewContributeCache get archive cache.
func (d *Dao) ViewContributeCache(c context.Context, mid int64) (aids []int64, err error) {
conn := d.mc.Get(c)
key := keyViewContribute(mid)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
if err = conn.Scan(r, &aids); err != nil {
err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
}
return
}

View File

@@ -0,0 +1,75 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestStatsCache(t *testing.T) {
Convey(t.Name(), t, func() {
_, _, err := d.statsCache(context.Background(), []int64{1, 2, 3, 4})
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestArcsCache(t *testing.T) {
Convey(t.Name(), t, func() {
_, _, err := d.arcsCache(context.Background(), []int64{1, 2, 3, 4})
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestArcCache(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.arcCache(context.Background(), 123)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestStatCache(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.statCache(context.Background(), 123)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestRelatesCache(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.RelatesCache(context.Background(), 123)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestViewContributeCache(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.ViewContributeCache(context.Background(), 12)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,28 @@
package archive
import (
"context"
"go-common/app/service/main/archive/api"
"go-common/app/service/main/archive/model/archive"
"go-common/library/log"
"github.com/pkg/errors"
)
// Stat get a archive stat.
func (d *Dao) Stat(c context.Context, aid int64) (st *api.Stat, err error) {
if st, err = d.statCache(c, aid); err != nil {
log.Error("%+v", err)
} else if st != nil {
return
}
arg := &archive.ArgAid2{Aid: aid}
if st, err = d.arcRPC.Stat3(c, arg); err != nil {
log.Error("d.arcRPC.Stat3(%v) error(%v)", arg, err)
if st, err = d.arcRPC2.Stat3(c, arg); err != nil {
err = errors.Wrapf(err, "%v", arg)
}
}
return
}

View File

@@ -0,0 +1,19 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestStat(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.Stat(context.Background(), 2)
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,71 @@
package archive
import (
"context"
"go-common/app/service/main/archive/api"
"go-common/app/service/main/archive/model/archive"
"go-common/library/ecode"
"go-common/library/log"
"github.com/pkg/errors"
)
// View3 view archive with pages pb.
func (d *Dao) View3(c context.Context, aid int64) (v *archive.View3, err error) {
arg := &archive.ArgAid2{Aid: aid}
if v, err = d.arcRPC.View3(c, arg); err != nil {
log.Error("d.arcRPC.View3(%v) error(%+v)", arg, err)
if ecode.Cause(err) == ecode.NothingFound {
err = nil
return
}
if v, err = d.arcRPC2.View3(c, arg); err != nil {
err = errors.Wrapf(err, "%v", arg)
}
}
return
}
// ViewCache get view static data from cache if cache missed from rpc.
func (d *Dao) ViewCache(c context.Context, aid int64) (vs *archive.View3, err error) {
if aid == 0 {
return
}
if vs, err = d.viewCache(c, aid); err != nil {
return
}
if vs != nil && vs.Archive3 != nil && len(vs.Pages) != 0 {
var st *api.Stat
if st, err = d.Stat(c, aid); err != nil {
log.Error("%+v", err)
err = nil
return
}
if st != nil {
vs.Archive3.Stat = archive.Stat3{
Aid: st.Aid,
View: st.View,
Danmaku: st.Danmaku,
Reply: st.Reply,
Fav: st.Fav,
Coin: st.Coin,
Share: st.Share,
NowRank: st.NowRank,
HisRank: st.HisRank,
Like: st.Like,
DisLike: st.DisLike,
}
}
}
return
}
// Description get archive description by aid.
func (d *Dao) Description(c context.Context, aid int64) (desc string, err error) {
arg := &archive.ArgAid{Aid: aid}
if desc, err = d.arcRPC.Description2(c, arg); err != nil {
err = errors.Wrapf(err, "%v", arg)
}
return
}

View File

@@ -0,0 +1,26 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestView3(t *testing.T) {
Convey(t.Name(), t, func() {
d.View3(context.Background(), 1)
})
}
func TestViewCache(t *testing.T) {
Convey(t.Name(), t, func() {
d.ViewCache(context.Background(), 1)
})
}
func TestDescription(t *testing.T) {
Convey(t.Name(), t, func() {
d.Description(context.Background(), 2)
})
}