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,56 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["promote_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/web-show/conf:go_default_library",
"//app/interface/main/web-show/model/operation:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"notice.go",
"promote.go",
"service.go",
],
importpath = "go-common/app/interface/main/web-show/service/operation",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/web-show/conf:go_default_library",
"//app/interface/main/web-show/dao/operation:go_default_library",
"//app/interface/main/web-show/model/operation: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/ecode:go_default_library",
"//library/log: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,13 @@
package operation
import (
"context"
opmdl "go-common/app/interface/main/web-show/model/operation"
)
// Notice Service
func (s *Service) Notice(c context.Context, arg *opmdl.ArgOp) (res map[string][]*opmdl.Operation) {
res = s.operation(arg.Tp, arg.Rank, arg.Count)
return
}

View File

@@ -0,0 +1,79 @@
package operation
import (
"context"
"regexp"
"strconv"
opmdl "go-common/app/interface/main/web-show/model/operation"
"go-common/app/service/main/archive/api"
comarcmdl "go-common/app/service/main/archive/model/archive"
"go-common/library/ecode"
"go-common/library/log"
)
var (
_emptyPromoteMap = make(map[string][]*opmdl.Promote)
_avReg = regexp.MustCompile(`video\/av[0-9]+`)
)
// Promote Service
func (s *Service) Promote(c context.Context, arg *opmdl.ArgPromote) (res map[string][]*opmdl.Promote, err error) {
var (
ok bool
arcs map[int64]*api.Arc
arc *api.Arc
aid int64
aids []int64
)
opMap := s.operation(arg.Tp, arg.Rank, arg.Count)
for _, ops := range opMap {
for _, op := range ops {
if aid, err = s.regAid(op.Link); err != nil {
log.Error("service.regAid error(%v)", err)
continue
}
op.Aid = aid
aids = append(aids, aid)
}
}
argAids := &comarcmdl.ArgAids2{
Aids: aids,
}
if arcs, err = s.arcRPC.Archives3(c, argAids); err != nil {
log.Error("s.arcRPC.Archives2(arcAids:(%v), arcs), err(%v)", aids, err)
res = _emptyPromoteMap
return
}
res = make(map[string][]*opmdl.Promote)
for rk, ops := range opMap {
promotes := make([]*opmdl.Promote, 0, len(ops))
for _, op := range ops {
if arc, ok = arcs[op.Aid]; !ok {
continue
}
promote := &opmdl.Promote{
IsAd: int8(op.Ads),
Archive: arc,
}
promotes = append(promotes, promote)
}
res[rk] = promotes
}
return
}
// regAid Service
func (s *Service) regAid(link string) (aid int64, err error) {
avStr := _avReg.FindString(link)
if avStr != "" {
aidStr := avStr[8:]
if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil {
log.Error("strconv.ParseInt error(%v)", err)
return
}
} else {
err = ecode.ArchiveNotExist
}
return
}

View File

@@ -0,0 +1,46 @@
package operation
import (
"context"
"flag"
"path/filepath"
"testing"
"time"
"go-common/app/interface/main/web-show/conf"
rsmdl "go-common/app/interface/main/web-show/model/operation"
. "github.com/smartystreets/goconvey/convey"
)
var svf *Service
func init() {
dir, _ := filepath.Abs("../../cmd/web-show-test.toml")
flag.Set("conf", dir)
if err := conf.Init(); err != nil {
panic(err)
}
if svf == nil {
svf = New(conf.Conf)
}
time.Sleep(time.Second)
}
func WithService(f func(s *Service)) func() {
return func() {
f(svf)
}
}
func TestService_Resource(t *testing.T) {
Convey("should return without err", t, WithService(func(svf *Service) {
arg := &rsmdl.ArgPromote{
Tp: "test",
Count: 1,
Rank: 1,
}
res, err := svf.Promote(context.TODO(), arg)
So(err, ShouldBeNil)
So(len(res), ShouldBeGreaterThan, 0)
}))
}

View File

@@ -0,0 +1,109 @@
package operation
import (
"context"
"strconv"
"time"
"go-common/app/interface/main/web-show/conf"
"go-common/app/interface/main/web-show/dao/operation"
opdml "go-common/app/interface/main/web-show/model/operation"
arcrpc "go-common/app/service/main/archive/api/gorpc"
"go-common/library/log"
)
const (
_rankCacheLen = 20
)
// Service struct
type Service struct {
dao *operation.Dao
arcRPC *arcrpc.Service2
cache map[string]map[int][]*opdml.Operation
}
// New init
func New(c *conf.Config) (s *Service) {
s = &Service{
cache: make(map[string]map[int][]*opdml.Operation, len(opdml.Types)),
}
s.arcRPC = arcrpc.New2(c.RPCClient2.Archive)
s.dao = operation.New(c)
s.reload()
go s.loadproc()
return
}
// Notice return notice info
func (s *Service) operation(tp string, rank, num int) (res map[string][]*opdml.Operation) {
res = make(map[string][]*opdml.Operation)
tmp, ok := s.cache[tp]
if ok {
if rank != 0 {
if ns := tmp[rank]; ns != nil {
if len(ns) < num || num < 0 {
num = len(ns)
}
ns = ns[:num]
res[strconv.FormatInt(int64(rank), 10)] = ns
}
} else {
for rk, ns := range tmp {
if ns != nil {
if len(ns) < num || num < 0 {
num = len(ns)
}
ns = ns[:num]
res[strconv.FormatInt(int64(rk), 10)] = ns
}
}
}
}
return
}
// reload Service
func (s *Service) reload() {
var (
tmpT = make(map[string]map[int][]*opdml.Operation)
)
ops, err := s.dao.Operation(context.Background())
if err != nil {
log.Error("s.reloadNotice error(%v)", err)
return
}
for _, op := range ops {
tmp, ok := tmpT[op.Type]
if !ok {
tmp = make(map[int][]*opdml.Operation)
}
if len(tmp) > _rankCacheLen {
continue
}
tmp[op.Rank] = append(tmp[op.Rank], op)
tmpT[op.Type] = tmp
}
s.cache = tmpT
}
// loadproc Service
func (s *Service) loadproc() {
for {
s.reload()
time.Sleep(time.Duration(conf.Conf.Reload.Notice))
}
}
// Close Service
func (s *Service) Close() {
s.dao.Close()
}
// Ping Service
func (s *Service) Ping(c context.Context) (err error) {
err = s.dao.Ping(c)
return
}