Initial commit

This commit is contained in:
Donny
2019-04-22 20:46:32 +08:00
commit 49ab8aadd1
25441 changed files with 4055000 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"infoc_test.go",
"region_test.go",
"service_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-feed/conf:go_default_library",
"//app/interface/main/app-feed/model/tag:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"infoc.go",
"region.go",
"service.go",
],
importpath = "go-common/app/interface/main/app-feed/service/region",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-feed/conf:go_default_library",
"//app/interface/main/app-feed/dao/tag:go_default_library",
"//app/interface/main/app-feed/model:go_default_library",
"//app/interface/main/app-feed/model/tag:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/log/infoc: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,121 @@
package region
import (
"bytes"
"strconv"
"time"
"go-common/app/interface/main/app-feed/model/tag"
"go-common/library/log"
binfoc "go-common/library/log/infoc"
)
type tagsInfoc struct {
typ string
mid string
client string
build string
buvid string
disid string
ip string
api string
now string
tags []*tag.Tag
}
type tagInfoc struct {
typ string
mid string
client string
build string
buvid string
disid string
ip string
api string
now string
rid string
tid string
}
func (s *Service) TagsInfoc(mid int64, plat int8, build int, buvid, disid, ip, api string, tags []*tag.Tag, now time.Time) {
select {
case s.logCh <- tagsInfoc{"推荐页入口", strconv.FormatInt(mid, 10), strconv.Itoa(int(plat)), strconv.Itoa(build), buvid, disid, ip, api, strconv.FormatInt(now.Unix(), 10), tags}:
default:
log.Warn("infoc log buffer is full")
}
}
func (s *Service) ChangeTagsInfoc(mid int64, plat int8, build int, buvid, disid, ip, api string, tags []*tag.Tag, now time.Time) {
select {
case s.logCh <- tagsInfoc{"换一换", strconv.FormatInt(mid, 10), strconv.Itoa(int(plat)), strconv.Itoa(build), buvid, disid, ip, api, strconv.FormatInt(now.Unix(), 10), tags}:
default:
log.Warn("infoc log buffer is full")
}
}
func (s *Service) AddTagInfoc(mid int64, plat int8, build int, buvid, disid, ip, api string, rid int, tid int64, now time.Time) {
select {
case s.logCh <- tagInfoc{"订阅标签", strconv.FormatInt(mid, 10), strconv.Itoa(int(plat)), strconv.Itoa(build), buvid, disid, ip, api, strconv.FormatInt(now.Unix(), 10), strconv.Itoa(rid), strconv.FormatInt(tid, 10)}:
default:
log.Warn("infoc log buffer is full")
}
}
func (s *Service) CancelTagInfoc(mid int64, plat int8, build int, buvid, disid, ip, api string, rid int, tid int64, now time.Time) {
select {
case s.logCh <- tagInfoc{"取消标签", strconv.FormatInt(mid, 10), strconv.Itoa(int(plat)), strconv.Itoa(build), buvid, disid, ip, api, strconv.FormatInt(now.Unix(), 10), strconv.Itoa(rid), strconv.FormatInt(tid, 10)}:
default:
log.Warn("infoc log buffer is full")
}
}
// writeInfoc
func (s *Service) infocproc() {
const (
// infoc format {"section":{"id":"%s","pos":1,"items":[{"id":%s,"pos":%d,"url":""}]}}
noItem1 = `{"section":{"id":"`
noItem2 = `","pos":1,"items":[]}}`
)
var (
msg1 = []byte(`{"section":{"id":"`)
msg2 = []byte(`","pos":1,"items":[`)
msg3 = []byte(`{"id":`)
msg4 = []byte(`,"name":"`)
msg5 = []byte(`","url":""},`)
msg6 = []byte(`]}}`)
inf2 = binfoc.New(s.c.TagInfoc2)
buf bytes.Buffer
list string
)
for {
i, ok := <-s.logCh
if !ok {
log.Warn("infoc proc exit")
return
}
switch v := i.(type) {
case tagsInfoc:
if len(v.tags) > 0 {
buf.Write(msg1)
buf.WriteString(v.typ)
buf.Write(msg2)
for _, v := range v.tags {
buf.Write(msg3)
buf.WriteString(strconv.FormatInt(v.ID, 10))
buf.Write(msg4)
buf.WriteString(v.Name)
buf.Write(msg5)
}
buf.Truncate(buf.Len() - 1)
buf.Write(msg6)
list = buf.String()
buf.Reset()
} else {
list = noItem1 + v.typ + noItem2
}
inf2.Info(v.ip, v.now, v.api, v.buvid, v.mid, v.client, "1", list, v.disid, "1", v.build)
case tagInfoc:
inf2.Info(v.ip, v.now, v.api, v.buvid, v.mid, v.client, "1", list, v.disid, "1", v.build)
}
}
}

View File

@@ -0,0 +1,128 @@
package region
import (
"testing"
"time"
"go-common/app/interface/main/app-feed/model/tag"
)
func TestService_TagsInfoc(t *testing.T) {
type args struct {
mid int64
plat int8
build int
buvid string
disid string
ip string
api string
tags []*tag.Tag
now time.Time
}
tests := []struct {
name string
s *Service
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.s.TagsInfoc(tt.args.mid, tt.args.plat, tt.args.build, tt.args.buvid, tt.args.disid, tt.args.ip, tt.args.api, tt.args.tags, tt.args.now)
})
}
}
func TestService_ChangeTagsInfoc(t *testing.T) {
type args struct {
mid int64
plat int8
build int
buvid string
disid string
ip string
api string
tags []*tag.Tag
now time.Time
}
tests := []struct {
name string
s *Service
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.s.ChangeTagsInfoc(tt.args.mid, tt.args.plat, tt.args.build, tt.args.buvid, tt.args.disid, tt.args.ip, tt.args.api, tt.args.tags, tt.args.now)
})
}
}
func TestService_AddTagInfoc(t *testing.T) {
type args struct {
mid int64
plat int8
build int
buvid string
disid string
ip string
api string
rid int
tid int64
now time.Time
}
tests := []struct {
name string
s *Service
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.s.AddTagInfoc(tt.args.mid, tt.args.plat, tt.args.build, tt.args.buvid, tt.args.disid, tt.args.ip, tt.args.api, tt.args.rid, tt.args.tid, tt.args.now)
})
}
}
func TestService_CancelTagInfoc(t *testing.T) {
type args struct {
mid int64
plat int8
build int
buvid string
disid string
ip string
api string
rid int
tid int64
now time.Time
}
tests := []struct {
name string
s *Service
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.s.CancelTagInfoc(tt.args.mid, tt.args.plat, tt.args.build, tt.args.buvid, tt.args.disid, tt.args.ip, tt.args.api, tt.args.rid, tt.args.tid, tt.args.now)
})
}
}
func TestService_infocproc(t *testing.T) {
tests := []struct {
name string
s *Service
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.s.infocproc()
})
}
}

View File

@@ -0,0 +1,102 @@
package region
import (
"context"
"time"
"go-common/app/interface/main/app-feed/model"
"go-common/app/interface/main/app-feed/model/tag"
"go-common/library/ecode"
"go-common/library/log"
)
var (
_emptyHotTags = []*tag.Hot{}
_emptyTags = []*tag.Tag{}
_banRegion = map[int16]struct{}{
// bangumi
33: struct{}{},
32: struct{}{},
153: struct{}{},
51: struct{}{},
152: struct{}{},
// music
29: struct{}{},
54: struct{}{},
130: struct{}{},
// tech
37: struct{}{},
96: struct{}{},
// movie
145: struct{}{},
146: struct{}{},
147: struct{}{},
// TV series
15: struct{}{},
34: struct{}{},
86: struct{}{},
// entertainment
71: struct{}{},
137: struct{}{},
131: struct{}{},
}
)
// HotTags get hot tags of region id.
func (s *Service) HotTags(c context.Context, mid int64, rid int16, ver string, plat int8, now time.Time) (hs []*tag.Hot, version string, err error) {
if hs, err = s.tg.Hots(c, mid, rid, now); err != nil {
log.Error("tg.HotTags(%d) error(%v)", rid, err)
return
}
if model.IsOverseas(plat) {
for _, hot := range hs {
if _, ok := _banRegion[hot.Rid]; ok {
hot.Tags = _emptyTags
}
}
}
if len(hs) == 0 {
hs = _emptyHotTags
return
}
version = s.md5(hs)
if ver == version {
err = ecode.NotModified
}
return
}
func (s *Service) SubTags(c context.Context, mid int64, pn, ps int) (t *tag.SubTag) {
t = &tag.SubTag{}
sub, err := s.tg.SubTags(c, mid, 0, pn, ps)
if err != nil {
log.Error("s.tg.SubTags(%d,%d,%d) error(%v)", mid, pn, ps, err)
return
}
subTags := make([]*tag.Tag, 0, len(sub.Tags))
for _, st := range sub.Tags {
subTag := &tag.Tag{ID: st.ID, Name: st.Name, IsAtten: st.IsAtten}
subTags = append(subTags, subTag)
}
if len(subTags) != 0 {
t.SubTags = subTags
} else {
t.SubTags = _emptyTags
}
t.Count = sub.Total
return
}
func (s *Service) AddTag(c context.Context, mid, tid int64, now time.Time) (err error) {
if err = s.tg.Add(c, mid, tid, now); err != nil {
log.Error("s.tg.Add(%d, %d) error(%v)", mid, tid, err)
}
return
}
func (s *Service) CancelTag(c context.Context, mid, tid int64, now time.Time) (err error) {
if err = s.tg.Cancel(c, mid, tid, now); err != nil {
log.Error("s.tg.Cancel(%d, %d) error(%v)", mid, tid, err)
}
return
}

View File

@@ -0,0 +1,126 @@
package region
import (
"context"
"flag"
"path/filepath"
"reflect"
"testing"
"time"
"go-common/app/interface/main/app-feed/conf"
"go-common/app/interface/main/app-feed/model/tag"
. "github.com/smartystreets/goconvey/convey"
)
var (
s *Service
)
func init() {
dir, _ := filepath.Abs("../../cmd/app-feed-test.toml")
flag.Set("conf", dir)
conf.Init()
s = New(conf.Conf)
}
func TestService_HotTags(t *testing.T) {
type args struct {
c context.Context
mid int64
rid int16
ver string
plat int8
now time.Time
}
tests := []struct {
name string
args args
wantHs []*tag.Hot
wantVersion string
wantErr error
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotHs, gotVersion, err := s.HotTags(tt.args.c, tt.args.mid, tt.args.rid, tt.args.ver, tt.args.plat, tt.args.now)
So(gotHs, ShouldEqual, tt.wantHs)
So(gotVersion, ShouldEqual, tt.wantVersion)
So(err, ShouldEqual, tt.wantErr)
})
}
}
func TestService_SubTags(t *testing.T) {
type args struct {
c context.Context
mid int64
pn int
ps int
}
tests := []struct {
name string
s *Service
args args
wantT *tag.SubTag
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotT := tt.s.SubTags(tt.args.c, tt.args.mid, tt.args.pn, tt.args.ps); !reflect.DeepEqual(gotT, tt.wantT) {
t.Errorf("Service.SubTags() = %v, want %v", gotT, tt.wantT)
}
})
}
}
func TestService_AddTag(t *testing.T) {
type args struct {
c context.Context
mid int64
tid int64
now time.Time
}
tests := []struct {
name string
s *Service
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.s.AddTag(tt.args.c, tt.args.mid, tt.args.tid, tt.args.now); (err != nil) != tt.wantErr {
t.Errorf("Service.AddTag() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestService_CancelTag(t *testing.T) {
type args struct {
c context.Context
mid int64
tid int64
now time.Time
}
tests := []struct {
name string
s *Service
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.s.CancelTag(tt.args.c, tt.args.mid, tt.args.tid, tt.args.now); (err != nil) != tt.wantErr {
t.Errorf("Service.CancelTag() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@@ -0,0 +1,45 @@
package region
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"time"
"go-common/app/interface/main/app-feed/conf"
tagdao "go-common/app/interface/main/app-feed/dao/tag"
"go-common/library/log"
)
type Service struct {
c *conf.Config
// dao
tg *tagdao.Dao
// tick
tick time.Duration
// infoc
logCh chan interface{}
}
// New a region service.
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
tg: tagdao.New(c),
tick: time.Duration(c.Tick),
// infoc
logCh: make(chan interface{}, 1024),
}
go s.infocproc()
return
}
func (s *Service) md5(v interface{}) string {
bs, err := json.Marshal(v)
if err != nil {
log.Error("json.Marshal error(%v)", err)
return "region_version"
}
hs := md5.Sum(bs)
return hex.EncodeToString(hs[:])
}

View File

@@ -0,0 +1,49 @@
package region
import (
"reflect"
"testing"
"go-common/app/interface/main/app-feed/conf"
)
func TestNew(t *testing.T) {
type args struct {
c *conf.Config
}
tests := []struct {
name string
args args
wantS *Service
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotS := New(tt.args.c); !reflect.DeepEqual(gotS, tt.wantS) {
t.Errorf("New() = %v, want %v", gotS, tt.wantS)
}
})
}
}
func TestService_md5(t *testing.T) {
type args struct {
v interface{}
}
tests := []struct {
name string
s *Service
args args
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.s.md5(tt.args.v); got != tt.want {
t.Errorf("Service.md5() = %v, want %v", got, tt.want)
}
})
}
}