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,49 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["rpc_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/history/model:go_default_library",
"//app/interface/main/history/rpc/client:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["rpc.go"],
importpath = "go-common/app/interface/main/history/server/gorpc",
tags = ["automanaged"],
deps = [
"//app/interface/main/history/conf:go_default_library",
"//app/interface/main/history/model:go_default_library",
"//app/interface/main/history/service:go_default_library",
"//library/ecode:go_default_library",
"//library/net/rpc:go_default_library",
"//library/net/rpc/context: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,136 @@
package gorpc
import (
"go-common/app/interface/main/history/conf"
"go-common/app/interface/main/history/model"
"go-common/app/interface/main/history/service"
"go-common/library/ecode"
"go-common/library/net/rpc"
"go-common/library/net/rpc/context"
)
// RPC represent rpc server
type RPC struct {
svc *service.Service
}
// New init rpc.
func New(c *conf.Config, s *service.Service) (svr *rpc.Server) {
r := &RPC{svc: s}
svr = rpc.NewServer(c.RPCServer)
if err := svr.Register(r); err != nil {
panic(err)
}
return
}
// Ping check connection success.
func (r *RPC) Ping(c context.Context, arg *struct{}, res *struct{}) (err error) {
return
}
// Progress report a user hisotry.
func (r *RPC) Progress(c context.Context, arg *model.ArgPro, res *map[int64]*model.History) (err error) {
*res, err = r.svc.Progress(c, arg.Mid, arg.Aids)
return
}
// Position report a user hisotry.
func (r *RPC) Position(c context.Context, arg *model.ArgPos, res *model.History) (err error) {
var v *model.History
tp, err := model.CheckBusiness(arg.Business)
if err != nil {
return
}
if tp > 0 {
arg.TP = tp
}
v, err = r.svc.Position(c, arg.Mid, arg.Aid, arg.TP)
if err == nil {
*res = *v
}
return
}
// Add (c context.Context, mid, src, rtime int64, ip string, h *model.History) (err error) .
func (r *RPC) Add(c context.Context, arg *model.ArgHistory, res *struct{}) (err error) {
if arg.History != nil {
var tp int8
if tp, err = model.CheckBusiness(arg.History.Business); err != nil {
return
} else if tp > 0 {
arg.History.TP = tp
}
}
err = r.svc.AddHistory(c, arg.Mid, arg.Realtime, arg.History)
return
}
// Delete delete histories
func (r *RPC) Delete(c context.Context, arg *model.ArgDelete, res *struct{}) (err error) {
if len(arg.Resources) == 0 {
err = ecode.RequestErr
return
}
histories := make([]*model.History, 0, len(arg.Resources))
for _, history := range arg.Resources {
var tp int8
if tp, err = model.MustCheckBusiness(history.Business); err != nil {
return
}
histories = append(histories, &model.History{
Aid: history.Oid,
TP: tp,
})
}
err = r.svc.Delete(c, arg.Mid, histories)
return
}
// History return all history .
func (r *RPC) History(c context.Context, arg *model.ArgHistories, res *[]*model.Resource) (err error) {
tp, err := model.CheckBusiness(arg.Business)
if err != nil {
return
}
if tp > 0 {
arg.TP = tp
}
*res, err = r.svc.Histories(c, arg.Mid, arg.TP, arg.Pn, arg.Ps)
return
}
// HistoryCursor return all history .
func (r *RPC) HistoryCursor(c context.Context, arg *model.ArgCursor, res *[]*model.Resource) (err error) {
tp, err := model.CheckBusiness(arg.Business)
if err != nil {
return
}
if tp > 0 {
arg.TP = tp
}
var tps []int8
for _, b := range arg.Businesses {
tp, err = model.CheckBusiness(b)
if err != nil {
return
}
tps = append(tps, tp)
}
*res, err = r.svc.HistoryCursor(c, arg.Mid, arg.Max, arg.ViewAt, arg.Ps, arg.TP, tps, arg.RealIP)
return
}
// Clear clear history
func (r *RPC) Clear(c context.Context, arg *model.ArgClear, res *struct{}) (err error) {
var tps []int8
for _, b := range arg.Businesses {
var tp int8
if tp, err = model.MustCheckBusiness(b); err != nil {
return
}
tps = append(tps, tp)
}
err = r.svc.ClearHistory(c, arg.Mid, tps)
return
}

View File

@@ -0,0 +1,55 @@
package gorpc
import (
"context"
"testing"
"go-common/app/interface/main/history/model"
rpcClient "go-common/app/interface/main/history/rpc/client"
. "github.com/smartystreets/goconvey/convey"
)
var (
ctx = context.TODO()
client *rpcClient.Service
)
func WithRPC(f func(client *rpcClient.Service)) func() {
return func() {
client = rpcClient.New(nil)
f(client)
}
}
func Test_Histroy_rpc(t *testing.T) {
Convey("rpc client Add", t, WithRPC(func(client *rpcClient.Service) {
arg := &model.ArgHistory{
Mid: 14771787,
History: &model.History{
Aid: 17406762,
TP: 1,
Pro: 122,
},
}
client.Add(ctx, arg)
}))
Convey("rpc client preogress", t, WithRPC(func(client *rpcClient.Service) {
arg := &model.ArgPro{
Mid: 14771787,
Aids: []int64{17406762},
}
client.Progress(ctx, arg)
}))
Convey("rpc client delete", t, WithRPC(func(client *rpcClient.Service) {
r := &model.Resource{Oid: 100, Business: "archive"}
arg := &model.ArgDelete{
Mid: 14771787,
Resources: []*model.Resource{r},
}
client.Delete(ctx, arg)
}))
Convey("rpc client history", t, WithRPC(func(client *rpcClient.Service) {
arg := &model.ArgHistories{Mid: 14771787}
client.History(ctx, arg)
}))
}

View File

@@ -0,0 +1,34 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["server.go"],
importpath = "go-common/app/interface/main/history/server/grpc",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/history/api/grpc:go_default_library",
"//app/interface/main/history/model:go_default_library",
"//app/interface/main/history/service:go_default_library",
"//library/net/rpc/warden: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,211 @@
// Package grpc generate by warden_gen
package grpc
import (
"context"
pb "go-common/app/interface/main/history/api/grpc"
"go-common/app/interface/main/history/model"
service "go-common/app/interface/main/history/service"
"go-common/library/net/rpc/warden"
)
// New History warden rpc server
func New(c *warden.ServerConfig, svr *service.Service) *warden.Server {
ws := warden.NewServer(c)
pb.RegisterHistoryServer(ws.Server(), &server{svr})
ws, err := ws.Start()
if err != nil {
panic(err)
}
return ws
}
type server struct {
svr *service.Service
}
var _ pb.HistoryServer = &server{}
// AddHistory add hisotry progress into hbase.
func (s *server) AddHistory(ctx context.Context, req *pb.AddHistoryReq) (*pb.AddHistoryReply, error) {
tp, err := model.MustCheckBusiness(req.H.Business)
if err != nil {
return nil, err
}
h := &model.History{
Mid: req.H.Mid,
Aid: req.H.Aid,
Sid: req.H.Sid,
Epid: req.H.Epid,
TP: tp,
Business: req.H.Business,
STP: int8(req.H.Stp),
Cid: req.H.Cid,
DT: int8(req.H.Dt),
Pro: req.H.Pro,
Unix: req.H.Unix,
}
return nil, s.svr.AddHistory(ctx, req.Mid, req.Rtime, h)
}
// Progress get view progress from cache/hbase.
func (s *server) Progress(ctx context.Context, req *pb.ProgressReq) (*pb.ProgressReply, error) {
histories, err := s.svr.Progress(ctx, req.Mid, req.Aids)
if err != nil {
return nil, err
}
reply := &pb.ProgressReply{Res: make(map[int64]*pb.ModelHistory)}
for k, v := range histories {
reply.Res[k] = &pb.ModelHistory{
Mid: v.Mid,
Aid: v.Aid,
Sid: v.Sid,
Epid: v.Epid,
Business: v.Business,
Stp: int32(v.STP),
Cid: v.Cid,
Dt: int32(v.DT),
Pro: v.Pro,
Unix: v.Unix,
}
}
return reply, nil
}
// Position get view progress from cache/hbase.
func (s *server) Position(ctx context.Context, req *pb.PositionReq) (*pb.PositionReply, error) {
tp, err := model.MustCheckBusiness(req.Business)
if err != nil {
return nil, err
}
h, err := s.svr.Position(ctx, req.Mid, req.Aid, tp)
if err != nil {
return nil, err
}
reply := &pb.PositionReply{
Res: &pb.ModelHistory{
Mid: h.Mid,
Aid: h.Aid,
Sid: h.Sid,
Epid: h.Epid,
Business: h.Business,
Stp: int32(h.STP),
Cid: h.Cid,
Dt: int32(h.DT),
Pro: h.Pro,
Unix: h.Unix,
},
}
return reply, err
}
// ClearHistory clear user's historys.
func (s *server) ClearHistory(ctx context.Context, req *pb.ClearHistoryReq) (*pb.ClearHistoryReply, error) {
var tps []int8
for _, b := range req.Businesses {
tp, err := model.MustCheckBusiness(b)
if err != nil {
return nil, err
}
tps = append(tps, tp)
}
return nil, s.svr.ClearHistory(ctx, req.Mid, tps)
}
// Histories return the user all av history.
func (s *server) Histories(ctx context.Context, req *pb.HistoriesReq) (*pb.HistoriesReply, error) {
tp, err := model.MustCheckBusiness(req.Business)
if err != nil {
return nil, err
}
resources, err := s.svr.Histories(ctx, req.Mid, tp, int(req.Pn), int(req.Ps))
if err != nil {
return nil, err
}
reply := &pb.HistoriesReply{}
for _, v := range resources {
reply.Res = append(reply.Res,
&pb.ModelResource{
Mid: v.Mid,
Oid: v.Oid,
Sid: v.Sid,
Epid: v.Epid,
Business: v.Business,
Stp: int32(v.STP),
Cid: v.Cid,
Dt: int32(v.DT),
Pro: v.Pro,
Unix: v.Unix,
})
}
return reply, nil
}
// HistoryCursor return the user all av history.
func (s *server) HistoryCursor(ctx context.Context, req *pb.HistoryCursorReq) (*pb.HistoryCursorReply, error) {
tp, err := model.MustCheckBusiness(req.Business)
if err != nil {
return nil, err
}
var tps []int8
for _, b := range req.Businesses {
t, er := model.MustCheckBusiness(b)
if er != nil {
return nil, er
}
tps = append(tps, t)
}
resources, err := s.svr.HistoryCursor(ctx, req.Mid, req.Max, req.ViewAt, int(req.Ps), tp, tps, req.Ip)
if err != nil {
return nil, err
}
reply := &pb.HistoryCursorReply{}
for _, v := range resources {
reply.Res = append(reply.Res,
&pb.ModelResource{
Mid: v.Mid,
Oid: v.Oid,
Sid: v.Sid,
Epid: v.Epid,
Business: v.Business,
Stp: int32(v.STP),
Cid: v.Cid,
Dt: int32(v.DT),
Pro: v.Pro,
Unix: v.Unix,
})
}
return reply, nil
}
// Delete .
func (s *server) Delete(ctx context.Context, req *pb.DeleteReq) (*pb.DeleteReply, error) {
var his []*model.History
for _, b := range req.His {
tp, err := model.MustCheckBusiness(b.Business)
if err != nil {
return nil, err
}
h := &model.History{
Mid: b.Mid,
Aid: b.Aid,
Sid: b.Sid,
Epid: b.Epid,
TP: tp,
Business: b.Business,
STP: int8(b.Stp),
Cid: b.Cid,
DT: int8(b.Dt),
Pro: b.Pro,
Unix: b.Unix,
}
his = append(his, h)
}
return nil, s.svr.Delete(ctx, req.Mid, his)
}
// FlushHistory flush to hbase from cache.
func (s *server) FlushHistory(ctx context.Context, req *pb.FlushHistoryReq) (*pb.FlushHistoryReply, error) {
return nil, s.svr.FlushHistory(ctx, req.Mids, req.Stime)
}