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,73 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"account_test.go",
"coin_test.go",
"reply_test.go",
"service_test.go",
"spy_test.go",
"vip_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/figure/conf:go_default_library",
"//app/job/main/figure/model:go_default_library",
"//app/job/main/reply/model/reply:go_default_library",
"//app/service/main/coin/model:go_default_library",
"//app/service/main/spy/model:go_default_library",
"//library/log:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"account.go",
"blocked.go",
"coin.go",
"danmaku.go",
"pay.go",
"reply.go",
"service.go",
"spy.go",
"sync.go",
"vip.go",
],
importpath = "go-common/app/job/main/figure/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/figure/conf:go_default_library",
"//app/job/main/figure/dao:go_default_library",
"//app/job/main/figure/model:go_default_library",
"//app/service/main/coin/model:go_default_library",
"//app/service/main/spy/model:go_default_library",
"//library/log:go_default_library",
"//library/queue/databus: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,46 @@
package service
import (
"context"
"time"
"go-common/app/job/main/figure/model"
"go-common/library/log"
)
// AccountExp handle user exp chenage message
func (s *Service) AccountExp(c context.Context, mid, exp int64) (err error) {
s.figureDao.UpdateAccountExp(c, mid, exp)
return
}
// AccountReg handle user register message, init user figure info
func (s *Service) AccountReg(c context.Context, mid int64) (err error) {
var id int64
if id, err = s.figureDao.ExistFigure(c, mid); err != nil || id != 0 {
log.Info("user(%d) already init", mid)
return
}
f := &model.Figure{
Mid: mid,
LawfulScore: s.c.Figure.Lawful,
WideScore: s.c.Figure.Wide,
FriendlyScore: s.c.Figure.Friendly,
BountyScore: s.c.Figure.Bounty,
CreativityScore: s.c.Figure.Creativity,
Ver: 1,
Ctime: time.Now(),
Mtime: time.Now(),
}
if _, err = s.figureDao.SaveFigure(c, f); err != nil {
return
}
s.figureDao.AddFigureInfoCache(c, f)
return
}
// AccountViewVideo handle user view video message
func (s *Service) AccountViewVideo(c context.Context, mid int64) (err error) {
s.figureDao.IncArchiveViews(c, mid)
return
}

View File

@@ -0,0 +1,31 @@
package service
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var (
testAccountMid int64 = 152
testExp int64 = 10
)
func TestSaveFigure(t *testing.T) {
once.Do(startService)
s.AccountReg(context.TODO(), 7593623)
}
// go test -test.v -test.run TestAccountExp
func TestAccountExp(t *testing.T) {
Convey("TestSaveFigure put account exp", t, WithService(func(s *Service) {
So(s.AccountReg(context.TODO(), 10), ShouldBeNil)
}))
Convey("TestAccountExp put account exp", t, WithService(func(s *Service) {
So(s.AccountExp(context.TODO(), testAccountMid, testExp), ShouldBeNil)
}))
Convey("TestAccountViewVideo put account exp", t, WithService(func(s *Service) {
So(s.AccountViewVideo(context.TODO(), testAccountMid), ShouldBeNil)
}))
}

View File

@@ -0,0 +1,24 @@
package service
import (
"context"
)
var (
rageMap = map[int16]string{
1: "S",
2: "A",
3: "B",
4: "C",
}
)
// BlockedKPIInfo .
func (s *Service) BlockedKPIInfo(c context.Context, mid int64, rage int16) (err error) {
if len(rageMap[rage]) != 0 {
s.figureDao.BlockedRage(c, mid, rage)
} else {
s.figureDao.BlockedRage(c, mid, int16(0))
}
return
}

View File

@@ -0,0 +1,13 @@
package service
import (
"context"
coinm "go-common/app/service/main/coin/model"
)
// PutCoinInfo handle user coin chenage message
func (s *Service) PutCoinInfo(c context.Context, msg *coinm.DataBus) (err error) {
s.figureDao.PutCoinCount(c, msg.Mid)
return
}

View File

@@ -0,0 +1,22 @@
package service
import (
"context"
coinm "go-common/app/service/main/coin/model"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var (
testCoinMid int64 = 130
)
// go test -test.v -test.run TestPutCoinInfo
func TestPutCoinInfo(t *testing.T) {
Convey("TestPutCoinInfo put coin info", t, WithService(func(s *Service) {
So(s.PutCoinInfo(context.TODO(), &coinm.DataBus{
Mid: testCoinMid,
}), ShouldBeNil)
}))
}

View File

@@ -0,0 +1,22 @@
package service
import (
"context"
"go-common/app/job/main/figure/model"
)
const (
_reportDel = "report_del"
)
// DanmakuReport .
func (s *Service) DanmakuReport(c context.Context, d *model.DMAction) (err error) {
if err = s.figureDao.DanmakuReport(c, d.Data.OwnerUID, model.ACColumnPublishDanmakuDeleted, -1); err != nil {
return
}
if err = s.figureDao.DanmakuReport(c, d.Data.ReportUID, model.ACColumnDanmakuReoprtPassed, 1); err != nil {
return
}
return
}

View File

@@ -0,0 +1,19 @@
package service
import (
"context"
"go-common/app/job/main/figure/model"
)
const (
_live = 11
)
// PayOrderInfo handle user coin chenage message
func (s *Service) PayOrderInfo(c context.Context, mid, money int64, merchant int8) (err error) {
column := model.ACColumnPayMoney
if merchant == _live {
column = model.ACColumnPayLiveMoney
}
return s.figureDao.PayOrderInfo(c, column, mid, money)
}

View File

@@ -0,0 +1,36 @@
package service
import (
"context"
"time"
"go-common/app/job/main/figure/model"
)
// PutReplyInfo handle user reply info chenage message
func (s *Service) PutReplyInfo(c context.Context, info *model.ReplyEvent) (err error) {
switch info.Action {
case model.EventAdd:
// only handle normal state reply
if info.Reply.State == 0 {
s.figureDao.PutReplyAct(c, info.Mid, model.ACColumnReplyAct, int64(1))
}
case model.EventLike:
s.figureDao.PutReplyAct(c, info.Reply.Mid, model.ACColumnReplyLiked, int64(1))
case model.EventLikeCancel:
s.figureDao.PutReplyAct(c, info.Reply.Mid, model.ACColumnReplyLiked, int64(-1))
case model.EventHate:
s.figureDao.PutReplyAct(c, info.Reply.Mid, model.ACColumnReplyHate, int64(1))
case model.EventHateCancel:
s.figureDao.PutReplyAct(c, info.Reply.Mid, model.ACColumnReplyHate, int64(-1))
case model.EventReportDel:
s.figureDao.PutReplyAct(c, info.Report.Mid, model.ACColumnReplyReoprtPassed, int64(1))
s.figureDao.PutReplyAct(c, info.Reply.Mid, model.ACColumnPublishReplyDeleted, int64(-1))
s.figureDao.SetWaiteUserCache(c, info.Report.Mid, s.figureDao.Version(time.Now()))
case model.EventReportRecover:
s.figureDao.PutReplyAct(c, info.Report.Mid, model.ACColumnReplyReoprtPassed, int64(-1))
s.figureDao.PutReplyAct(c, info.Reply.Mid, model.ACColumnPublishReplyDeleted, int64(1))
s.figureDao.SetWaiteUserCache(c, info.Report.Mid, s.figureDao.Version(time.Now()))
}
return
}

View File

@@ -0,0 +1,72 @@
package service
import (
"context"
"testing"
"go-common/app/job/main/figure/model"
repmol "go-common/app/job/main/reply/model/reply"
. "github.com/smartystreets/goconvey/convey"
)
var (
testReplyMid int64 = 120
testRefReplyMid int64 = 121
)
// go test -test.v -test.run TestPutReplyInfo
func TestPutReplyInfo(t *testing.T) {
Convey("TestPutReplyInfo put add reply", t, WithService(func(s *Service) {
So(s.PutReplyInfo(context.TODO(), &model.ReplyEvent{
Mid: testReplyMid,
Action: model.EventAdd,
Reply: &repmol.Reply{
Mid: testRefReplyMid,
},
}), ShouldBeNil)
}))
Convey("TestPutReplyInfo put add reply", t, WithService(func(s *Service) {
So(s.PutReplyInfo(context.TODO(), &model.ReplyEvent{
Mid: testReplyMid,
Action: model.EventLike,
Reply: &repmol.Reply{
Mid: testRefReplyMid,
},
}), ShouldBeNil)
So(s.PutReplyInfo(context.TODO(), &model.ReplyEvent{
Mid: testReplyMid,
Action: model.EventLike,
Reply: &repmol.Reply{
Mid: testRefReplyMid,
},
}), ShouldBeNil)
}))
Convey("TestPutReplyInfo put add reply", t, WithService(func(s *Service) {
So(s.PutReplyInfo(context.TODO(), &model.ReplyEvent{
Mid: testReplyMid,
Action: model.EventLikeCancel,
Reply: &repmol.Reply{
Mid: testRefReplyMid,
},
}), ShouldBeNil)
}))
Convey("TestPutReplyInfo put hate", t, WithService(func(s *Service) {
So(s.PutReplyInfo(context.TODO(), &model.ReplyEvent{
Mid: testReplyMid,
Action: model.EventHate,
Reply: &repmol.Reply{
Mid: testRefReplyMid,
},
}), ShouldBeNil)
}))
Convey("TestPutReplyInfo put hate cancel", t, WithService(func(s *Service) {
So(s.PutReplyInfo(context.TODO(), &model.ReplyEvent{
Mid: testReplyMid,
Action: model.EventHateCancel,
Reply: &repmol.Reply{
Mid: testRefReplyMid,
},
}), ShouldBeNil)
}))
}

View File

@@ -0,0 +1,533 @@
package service
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"runtime/debug"
"strings"
"time"
"go-common/app/job/main/figure/conf"
"go-common/app/job/main/figure/dao"
"go-common/app/job/main/figure/model"
coinm "go-common/app/service/main/coin/model"
spym "go-common/app/service/main/spy/model"
"go-common/library/log"
"go-common/library/queue/databus"
)
const (
_insertAction = "insert"
_vipTable = "vip_user_info"
_payTable = "pay_pay_order"
_blockedTable = "blocked_kpi"
// pay success status.
_paySuccess = 2
)
// Service biz service def.
type Service struct {
c *conf.Config
figureDao *dao.Dao
accExpDatabus *databus.Databus
accRegDatabus *databus.Databus
vipDatabus *databus.Databus
spyDatabus *databus.Databus
coinDatabus *databus.Databus
replyInfoDatabus *databus.Databus
payDatabus *databus.Databus
danmakuDatabus *databus.Databus
blockedDatabus *databus.Databus
}
// New new a Service and return.
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
figureDao: dao.New(c),
}
if c.DataSource.AccountExp != nil {
s.accExpDatabus = databus.New(c.DataSource.AccountExp)
go s.accexpproc()
}
if c.DataSource.AccountReg != nil {
s.accRegDatabus = databus.New(c.DataSource.AccountReg)
go s.accregproc()
}
if c.DataSource.Vip != nil {
s.vipDatabus = databus.New(c.DataSource.Vip)
go s.vipproc()
}
if c.DataSource.Spy != nil {
s.spyDatabus = databus.New(c.DataSource.Spy)
go s.spyproc()
}
if c.DataSource.Coin != nil {
s.coinDatabus = databus.New(c.DataSource.Coin)
go s.coinproc()
}
if c.DataSource.ReplyInfo != nil {
s.replyInfoDatabus = databus.New(c.DataSource.ReplyInfo)
go s.replyinfoproc()
}
if c.DataSource.Pay != nil {
s.payDatabus = databus.New(c.DataSource.Pay)
go s.payproc()
}
if c.DataSource.Danmaku != nil {
s.danmakuDatabus = databus.New(c.DataSource.Danmaku)
go s.danmakuproc()
}
if c.DataSource.Blocked != nil {
s.blockedDatabus = databus.New(c.DataSource.Blocked)
go s.blockedproc()
}
go s.syncproc()
return s
}
// Ping check dao health.
func (s *Service) Ping(c context.Context) (err error) {
s.figureDao.Ping(c)
return
}
// Close close all dao.
func (s *Service) Close() {
s.figureDao.Close()
}
func (s *Service) accexpproc() {
defer func() {
if x := recover(); x != nil {
log.Error("s.accexpproc panic(%v) %s", x, debug.Stack())
go s.accexpproc()
log.Info("s.accexpproc recover")
}
}()
var (
err error
msg *databus.Message
msgChan = s.accExpDatabus.Messages()
ok bool
exp *model.MsgAccountLog
)
for {
select {
case msg, ok = <-msgChan:
if !ok {
log.Info("accproc msgChan closed")
}
}
if err = msg.Commit(); err != nil {
log.Error("msg.Commit err(%v)", err)
}
exp = &model.MsgAccountLog{}
if err = json.Unmarshal([]byte(msg.Value), exp); err != nil {
log.Error("json.Unmarshal(%v) err(%v)", msg, err)
continue
}
log.Info("exp Info (%+v)", exp)
if err = s.AccountExp(context.Background(), exp.Mid, int64(exp.ExpTo())); err != nil {
log.Error("s.AccountExp(%v) err(%v)", exp, err)
continue
}
s.figureDao.SetWaiteUserCache(context.Background(), exp.Mid, s.figureDao.Version(time.Now()))
if exp.IsViewExp() {
if err = s.AccountViewVideo(context.Background(), exp.Mid); err != nil {
log.Error("s.AccountExp(%v) err(%v)", exp, err)
continue
}
}
}
}
func (s *Service) accregproc() {
defer func() {
if x := recover(); x != nil {
log.Error("s.accregproc panic(%v) %s", x, debug.Stack())
go s.accregproc()
log.Info("s.accregproc recover")
}
}()
var (
err error
msg *databus.Message
msgChan = s.accRegDatabus.Messages()
ok bool
)
for {
select {
case msg, ok = <-msgChan:
if !ok {
log.Info("accproc msgChan closed")
}
}
if err = msg.Commit(); err != nil {
log.Error("msg.Commit err(%v)", err)
}
reg := &model.MsgCanal{}
if err = json.Unmarshal([]byte(msg.Value), reg); err != nil {
log.Error("json.Unmarshal(%v) err(%v)", msg, err)
continue
}
log.Info("reg log %+v", reg)
if reg.Action == _insertAction {
var info struct {
Mid int64 `json:"mid"`
}
if err = json.Unmarshal(reg.New, &info); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(reg.New), err)
return
}
if err = s.AccountReg(context.Background(), info.Mid); err != nil {
log.Error("s.AccountReg(%v) err(%v)", reg, err)
continue
}
s.figureDao.SetWaiteUserCache(context.Background(), info.Mid, s.figureDao.Version(time.Now()))
}
}
}
func (s *Service) vipproc() {
defer func() {
if x := recover(); x != nil {
log.Error("s.vipproc panic(%v) %s", x, debug.Stack())
go s.vipproc()
log.Info("s.vipproc recover")
}
}()
var (
err error
msg *databus.Message
msgChan = s.vipDatabus.Messages()
ok bool
)
for {
select {
case msg, ok = <-msgChan:
if !ok {
log.Info("vipproc msgChan closed")
}
}
if err = msg.Commit(); err != nil {
log.Error("msg.Commit err(%v)", err)
}
v := &model.MsgCanal{}
if err = json.Unmarshal([]byte(msg.Value), v); err != nil {
log.Error("json.Unmarshal(%v) err(%v)", v, err)
continue
}
log.Info("vip log %+v", v)
if v.Table == _vipTable {
var vipInfo struct {
Mid int64 `json:"mid"`
VipStatus int32 `json:"vip_status"`
}
if err = json.Unmarshal(v.New, &vipInfo); err != nil {
log.Error("json.Unmarshal(%v) err(%v)", v.New, err)
continue
}
if err = s.UpdateVipStatus(context.Background(), vipInfo.Mid, vipInfo.VipStatus); err != nil {
log.Error("s.UpdateVipStatus(%v) err(%v)", v, err)
continue
}
s.figureDao.SetWaiteUserCache(context.Background(), vipInfo.Mid, s.figureDao.Version(time.Now()))
}
}
}
func (s *Service) spyproc() {
defer func() {
if x := recover(); x != nil {
log.Error("s.spyproc panic(%v) %s", x, debug.Stack())
go s.spyproc()
log.Info("s.spyproc recover")
}
}()
var (
err error
msg *databus.Message
msgChan = s.spyDatabus.Messages()
ok bool
)
for {
select {
case msg, ok = <-msgChan:
if !ok {
log.Info("spyproc msgChan closed")
}
}
if err = msg.Commit(); err != nil {
log.Error("msg.Commit err(%v)", err)
}
sc := &spym.ScoreChange{}
if err = json.Unmarshal([]byte(msg.Value), sc); err != nil {
log.Error("json.Unmarshal(%v) err(%v)", sc, err)
continue
}
log.Info("spy log %+v", sc)
if err = s.PutSpyScore(context.Background(), sc); err != nil {
log.Error("s.PutSpyScore(%v) err(%v)", sc, err)
continue
}
s.figureDao.SetWaiteUserCache(context.Background(), sc.Mid, s.figureDao.Version(time.Now()))
}
}
func (s *Service) coinproc() {
defer func() {
if x := recover(); x != nil {
log.Error("s.coinproc panic(%v) %s", x, debug.Stack())
go s.coinproc()
log.Info("s.coinproc recover")
}
}()
var (
err error
msg *databus.Message
msgChan = s.coinDatabus.Messages()
ok bool
)
for {
select {
case msg, ok = <-msgChan:
if !ok {
log.Info("coinproc msgChan closed")
}
}
if err = msg.Commit(); err != nil {
log.Error("msg.Commit err(%v)", err)
}
cd := &coinm.DataBus{}
if err = json.Unmarshal([]byte(msg.Value), cd); err != nil {
log.Error("json.Unmarshal(%v) err(%v)", cd, err)
continue
}
log.Info("coin log %+v", cd)
if err = s.PutCoinInfo(context.Background(), cd); err != nil {
log.Error("s.PutCoinInfo(%v) err(%v)", cd, err)
continue
}
s.figureDao.SetWaiteUserCache(context.Background(), cd.Mid, s.figureDao.Version(time.Now()))
}
}
func (s *Service) replyinfoproc() {
defer func() {
if x := recover(); x != nil {
log.Error("s.replyinfoproc panic(%v) %s", x, debug.Stack())
go s.replyinfoproc()
log.Info("s.replyinfoproc recover")
}
}()
var (
err error
msg *databus.Message
msgChan = s.replyInfoDatabus.Messages()
ok bool
)
for {
select {
case msg, ok = <-msgChan:
if !ok {
log.Info("replyinfoproc msgChan closed")
}
}
if err = msg.Commit(); err != nil {
log.Error("msg.Commit err(%v)", err)
}
m := &model.ReplyEvent{}
if err = json.Unmarshal([]byte(msg.Value), m); err != nil {
log.Error("json.Unmarshal(%v) err(%v)", m, err)
continue
}
log.Info("reply log %+v", m)
if err = s.PutReplyInfo(context.Background(), m); err != nil {
log.Error("s.PutCoinInfo(%v) err(%v)", m, err)
continue
}
if m.Action == model.EventAdd {
s.figureDao.SetWaiteUserCache(context.Background(), m.Mid, s.figureDao.Version(time.Now()))
} else {
s.figureDao.SetWaiteUserCache(context.Background(), m.Reply.Mid, s.figureDao.Version(time.Now()))
}
}
}
func (s *Service) syncproc() {
defer func() {
if x := recover(); x != nil {
log.Error("s.syncproc panic(%v) %s", x, debug.Stack())
go s.syncproc()
log.Info("s.syncproc recover")
}
}()
if s.c.Figure.Sync {
log.Info("start import data after half hour.")
time.Sleep(5 * time.Minute)
log.Info("start import data.")
var (
vipPath = s.c.Figure.VipPath
files []os.FileInfo
err error
)
if files, err = ioutil.ReadDir(vipPath); err != nil {
log.Error("ioutile.ReadDir(%s) err [%s]", vipPath, err)
}
for _, f := range files {
s.SyncUserVIP(context.TODO(), vipPath+"/"+f.Name())
}
log.Info("end import data.")
}
}
func (s *Service) payproc() {
defer func() {
if x := recover(); x != nil {
log.Error("s.payproc panic(%v) %s", x, debug.Stack())
go s.payproc()
log.Info("s.payproc recover")
}
}()
var (
err error
msg *databus.Message
msgChan = s.payDatabus.Messages()
ok bool
)
for {
select {
case msg, ok = <-msgChan:
if !ok {
log.Info("payproc msgChan closed")
}
}
if err = msg.Commit(); err != nil {
log.Error("msg.Commit err(%v)", err)
}
v := &model.MsgCanal{}
if err = json.Unmarshal([]byte(msg.Value), v); err != nil {
log.Error("json.Unmarshal(%v) err(%v)", v, err)
continue
}
log.Info("pay log %+v", v)
if strings.HasPrefix(v.Table, _payTable) {
var payOrder struct {
Mid int64 `json:"pay_mid"`
Money float64 `json:"bp"`
MerchantID int8 `json:"merchant_id"`
Status int32 `json:"status"`
}
if err = json.Unmarshal(v.New, &payOrder); err != nil {
log.Error("json.Unmarshal(%v) err(%v)", v.New, err)
continue
}
if payOrder.Status != _paySuccess {
continue
}
// update YUAN to Fen
money := int64(payOrder.Money * 100)
if err = s.PayOrderInfo(context.Background(), payOrder.Mid, money, payOrder.MerchantID); err != nil {
log.Error("s.PayOrderInfo(%v) err(%v)", payOrder, err)
continue
}
s.figureDao.SetWaiteUserCache(context.Background(), payOrder.Mid, s.figureDao.Version(time.Now()))
}
}
}
// 风纪委相关
func (s *Service) blockedproc() {
defer func() {
if x := recover(); x != nil {
log.Error("s.blockedproc panic(%v) %s", x, debug.Stack())
go s.blockedproc()
log.Info("s.blockedproc recover")
}
}()
var (
err error
msg *databus.Message
msgChan = s.blockedDatabus.Messages()
ok bool
)
for {
select {
case msg, ok = <-msgChan:
if !ok {
log.Info("blockedproc msgChan closed")
}
}
if err = msg.Commit(); err != nil {
log.Error("msg.Commit err(%v)", err)
}
v := &model.MsgCanal{}
if err = json.Unmarshal([]byte(msg.Value), v); err != nil {
log.Error("json.Unmarshal(%v) err(%v)", v, err)
continue
}
log.Info("blocked log %+v", v)
if v.Table == _blockedTable {
var KPIInfo struct {
Mid int64 `json:"mid"`
Rate int16 `json:"rate"`
}
if err = json.Unmarshal(v.New, &KPIInfo); err != nil {
log.Error("json.Unmarshal(%v) err(%v)", v.New, err)
continue
}
if err = s.BlockedKPIInfo(context.Background(), KPIInfo.Mid, KPIInfo.Rate); err != nil {
log.Error("s.BlockedKPIInfo(%v) err(%v)", KPIInfo, err)
continue
}
s.figureDao.SetWaiteUserCache(context.Background(), KPIInfo.Mid, s.figureDao.Version(time.Now()))
}
}
}
func (s *Service) danmakuproc() {
defer func() {
if x := recover(); x != nil {
log.Error("s.danmakuproc panic(%v) %s", x, debug.Stack())
go s.danmakuproc()
log.Info("s.danmakuproc recover")
}
}()
var (
err error
msg *databus.Message
msgChan = s.danmakuDatabus.Messages()
ok bool
)
for {
select {
case msg, ok = <-msgChan:
if !ok {
log.Info("danmakuproc msgChan closed")
}
}
if err = msg.Commit(); err != nil {
log.Error("msg.Commit err(%+v)", err)
}
m := &model.DMAction{}
if err = json.Unmarshal([]byte(msg.Value), m); err != nil {
log.Error("json.Unmarshal(%v) err(%+v)", m, err)
continue
}
log.Info("danmaku msg %+v", m)
if m.Action == _reportDel {
if err = s.DanmakuReport(context.Background(), m); err != nil {
log.Error("s.DanmakuReport(%v) err(%+v)", m, err)
continue
}
s.figureDao.SetWaiteUserCache(context.Background(), m.Data.OwnerUID, s.figureDao.Version(time.Now()))
s.figureDao.SetWaiteUserCache(context.Background(), m.Data.ReportUID, s.figureDao.Version(time.Now()))
}
}
}

View File

@@ -0,0 +1,58 @@
package service
import (
"flag"
"go-common/app/job/main/figure/conf"
"go-common/library/log"
"path/filepath"
"sync"
"time"
. "github.com/smartystreets/goconvey/convey"
)
var (
once sync.Once
s *Service
)
func initConf() {
if err := conf.Init(); err != nil {
panic(err)
}
log.Init(conf.Conf.Log)
defer log.Close()
}
func init() {
var (
err error
)
dir, _ := filepath.Abs("../cmd/figure-job-test.toml")
flag.Set("conf", dir)
if err = conf.Init(); err != nil {
panic(err)
}
if s == nil {
s = New(conf.Conf)
}
time.Sleep(time.Second)
}
func startService() {
initConf()
if s == nil {
s = New(conf.Conf)
}
time.Sleep(time.Second * 2)
}
func CleanCache() {
}
func WithService(f func(s *Service)) func() {
return func() {
Reset(func() { CleanCache() })
f(s)
}
}

View File

@@ -0,0 +1,21 @@
package service
import (
"context"
"go-common/app/job/main/figure/model"
spym "go-common/app/service/main/spy/model"
)
// PutSpyScore handle user spy score chenage message
func (s *Service) PutSpyScore(c context.Context, sc *spym.ScoreChange) (err error) {
s.figureDao.PutSpyScore(c, sc.Mid, sc.Score)
if sc.Reason == spym.CoinReason {
if sc.RiskLevel == spym.CoinHighRisk {
s.figureDao.PutCoinUnusual(c, sc.Mid, model.ACColumnHighRisk)
} else {
s.figureDao.PutCoinUnusual(c, sc.Mid, model.ACColumnLowRisk)
}
}
return
}

View File

@@ -0,0 +1,50 @@
package service
import (
"context"
"time"
spym "go-common/app/service/main/spy/model"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var (
testSpyMid int64 = 111
)
// go test -test.v -test.run TestPutSpyScore
func TestPutSpyScore(t *testing.T) {
Convey("Test_PutSpyScore put no reason msg", t, WithService(func(s *Service) {
So(s.PutSpyScore(context.TODO(), &spym.ScoreChange{
Mid: testSpyMid,
Score: 80,
EventScore: 80,
BaseScore: 80,
TS: time.Now().Unix(),
}), ShouldBeNil)
}))
Convey("Test_PutSpyScore put coin-service reason msg", t, WithService(func(s *Service) {
So(s.PutSpyScore(context.TODO(), &spym.ScoreChange{
Mid: testSpyMid,
Score: 70,
EventScore: 80,
BaseScore: 80,
TS: time.Now().Unix(),
Reason: "coin-service",
RiskLevel: 3,
}), ShouldBeNil)
}))
Convey("Test_PutSpyScore put coin-service high risk reason msg", t, WithService(func(s *Service) {
So(s.PutSpyScore(context.TODO(), &spym.ScoreChange{
Mid: testSpyMid,
Score: 8,
EventScore: 80,
BaseScore: 80,
TS: time.Now().Unix(),
Reason: "coin-service",
RiskLevel: 7,
}), ShouldBeNil)
}))
}

View File

@@ -0,0 +1,50 @@
package service
import (
"bufio"
"context"
"io"
"os"
"strconv"
"strings"
"time"
"go-common/library/log"
)
func (s *Service) SyncUserVIP(ctx context.Context, file string) {
log.Info("Start sync user VIP")
f, err := os.OpenFile(file, os.O_RDONLY, os.ModePerm)
if err != nil {
log.Error("os.OpenFile(%s) , err [%s]", file, err)
return
}
br := bufio.NewReader(f)
var (
str string
mid int64
count int
)
for {
if str, err = br.ReadString('\n'); err != nil {
if err == io.EOF {
break
}
log.Error("br.ReadString error [%s]", err)
return
}
if mid, err = strconv.ParseInt(strings.TrimSpace(str), 10, 64); err != nil {
log.Error("Parse midstr [%s] error [%s]", str, err)
continue
}
if err = s.figureDao.UpdateVipStatus(ctx, mid, 1); err != nil {
log.Error("s.figureDao.UpdateVipStatus(%d,1) err [%s]", mid, err)
return
}
count++
if count%10000 == 0 {
time.Sleep(time.Second)
}
}
log.Info("End sync user VIP count [%d]", count)
}

View File

@@ -0,0 +1,11 @@
package service
import (
"context"
)
// UpdateVipStatus handle user vip staus change
func (s *Service) UpdateVipStatus(c context.Context, mid int64, vs int32) (err error) {
s.figureDao.UpdateVipStatus(c, mid, vs)
return
}

View File

@@ -0,0 +1,20 @@
package service
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var (
testVipStatus int32 = 2
testVipMid int64 = 110
)
//go test -test.v -test.run TestUpdateVipStatus
func TestUpdateVipStatus(t *testing.T) {
Convey("TestUpdateVipStatus update vip status", t, WithService(func(s *Service) {
So(s.UpdateVipStatus(context.TODO(), testVipMid, testVipStatus), ShouldBeNil)
}))
}