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,51 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"batch_param.go",
"model.go",
"xints.go",
],
importpath = "go-common/app/interface/main/push-archive/model",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/archive/api:go_default_library",
"//app/service/main/relation/model:go_default_library",
"//library/log:go_default_library",
"//library/xstr: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"],
)
go_test(
name = "go_default_test",
srcs = [
"batch_param_test.go",
"xints_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//vendor/github.com/smartystreets/goconvey/convey:go_default_library"],
)

View File

@@ -0,0 +1,61 @@
package model
import (
"crypto/md5"
"encoding/hex"
"fmt"
"time"
"go-common/library/log"
"go-common/library/xstr"
)
// ParamHandler fn
type ParamHandler func(target *map[string]interface{}, args ...interface{})
// BatchParam str
type BatchParam struct {
Params map[string]interface{}
Handler ParamHandler
}
// NewBatchParam func
func NewBatchParam(p map[string]interface{}, h ParamHandler) *BatchParam {
if h == nil {
h = BaseParamHandler
}
return &BatchParam{
Params: p,
Handler: h,
}
}
var (
// BaseParamHandler fn
BaseParamHandler = func(target *map[string]interface{}, args ...interface{}) {}
// PushParamHandler fn
PushParamHandler = func(target *map[string]interface{}, args ...interface{}) {
var (
ok bool
arc *Archive
fans []int64
)
if target == nil || (*target)["archive"] == nil {
log.Warn("PushParamHandler target(%+v)/target[archive] nil, args(%+v)", target, args)
} else if arc, ok = (*target)["archive"].(*Archive); !ok || arc == nil {
log.Warn("PushParamHandler target[archive]=%+v parse failed/nil, args(%+v), target(%+v)", (*target)["archive"], args, target)
}
if arc == nil {
arc = &Archive{}
}
if len(args) != 1 || args[0] == nil {
log.Warn("PushParamHandler args(%+v) less than 1 or nil, target(%+v) archive(%+v)", args, target, arc)
} else if fans, ok = args[0].([]int64); !ok {
log.Warn("PushParamHandler args(%+v) parse failed, target(%+v) archive(%+v)", args, target, arc)
}
b := md5.Sum([]byte(fmt.Sprintf("%d%s", arc.ID, xstr.JoinInts(fans))))
(*target)["uuid"] = fmt.Sprintf("%s%d", hex.EncodeToString(b[:]), time.Now().UnixNano())
}
)

View File

@@ -0,0 +1,45 @@
package model
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestBatchParam_Format(t *testing.T) {
Convey("BatchParam_Format", t, func() {
f := func(p *BatchParam, t *testing.T, args ...interface{}) {
t.Logf("args(%+v)", args...)
p.Handler(&p.Params, args...)
t.Logf("params(%+v)\r\n", p)
}
var uuid interface{}
m := map[string]interface{}{
"h1": 10,
"archive": &Archive{ID: int64(12)},
}
p1 := NewBatchParam(m, BaseParamHandler)
f(p1, t)
_, exist := p1.Params["uuid"]
So(exist, ShouldEqual, false)
p2 := NewBatchParam(m, PushParamHandler)
f(p2, t)
_, exist = p2.Params["uuid"]
So(exist, ShouldEqual, true)
uuid = p2.Params["uuid"]
f(p2, t, []int{1, 2})
_, exist = p2.Params["uuid"]
So(exist, ShouldEqual, true)
So(p2.Params["uuid"], ShouldNotEqual, uuid)
f(p2, t, []int64{1, 2})
_, exist = p2.Params["uuid"]
So(exist, ShouldEqual, true)
var h ParamHandler
p2.Params["h1"] = 1
t.Logf("params(%+v), %+v, %v", p2.Params, h, h == nil)
})
}

View File

@@ -0,0 +1,135 @@
package model
import (
"encoding/json"
"time"
"go-common/app/service/main/archive/api"
relmdl "go-common/app/service/main/relation/model"
)
const (
// PushTypeUnknown 用户未上报推送设置
PushTypeUnknown = iota
// PushTypeForbid 禁止推送稿件更新通知
PushTypeForbid
// PushTypeSpecial 推送特别关注的upper的更新
PushTypeSpecial
// PushTypeAttention 推送关注的upper的更新
PushTypeAttention
)
const (
// RelationAttention 关注
RelationAttention = iota + 1
// RelationSpecial 特别关注
RelationSpecial
)
const (
// StatisticsUnpush 命中分组但未推送
StatisticsUnpush = iota
// StatisticsPush 命中分组且推送
StatisticsPush = 1
)
const (
// GroupDataTypeDefault 默认
GroupDataTypeDefault = "default"
// GroupDataTypeHBase AI脚本提供的hbase数据
GroupDataTypeHBase = "hbase"
// GroupDataTypeAbtest ab实验数据
GroupDataTypeAbtest = "ab_test"
// GroupDataTypeAbComparison ab对照数据
GroupDataTypeAbComparison = "ab_comparison"
)
const (
// AttrBitIsPGC pgc稿件的属性位
AttrBitIsPGC = 9
)
// Setting user push setting.
type Setting struct {
Type int `json:"type"`
}
// Message canal databus message.
type Message struct {
Action string `json:"action"`
Table string `json:"table"`
New json.RawMessage `json:"new"`
Old json.RawMessage `json:"old"`
}
// Relation user relation.
type Relation struct {
Mid int64 `json:"mid,omitempty"`
Fid int64 `json:"fid,omitempty"`
Attribute uint32 `json:"attribute"`
Status int `json:"status"`
MTime string `json:"mtime"`
CTime string `json:"ctime"`
}
// Following judge that whether has following relation.
func (r *Relation) Following() bool {
attr := relmdl.Following{Attribute: r.Attribute}
return attr.Following()
}
// RelationTagUser user relatino tag.
type RelationTagUser struct {
Mid int64 `json:"mid,omitempty"`
Fid int64 `json:"fid,omitempty"`
Tag string `json:"tag"`
MTime string `json:"mtime"`
CTime string `json:"ctime"`
}
// HasTag judge that whether has specified tag.
func (r *RelationTagUser) HasTag(tag int64) bool {
i := new(Ints)
i.Scan([]byte(r.Tag))
return i.Exist(tag)
}
// Archive model
type Archive struct {
ID int64 `json:"aid"`
Mid int64 `json:"mid"`
TypeID int16 `json:"typeid"`
HumanRank int `json:"humanrank"`
Duration int `json:"duration"`
Title string `json:"title"`
Cover string `json:"cover"`
Content string `json:"content"`
Tag string `json:"tag"`
Attribute int32 `json:"attribute"`
Copyright int8 `json:"copyright"`
AreaLimit int8 `json:"arealimit"`
State int `json:"state"`
Author string `json:"author"`
Access int `json:"access"`
Forward int `json:"forward"`
PubTime string `json:"pubtime"`
Round int8 `json:"round"`
CTime string `json:"ctime"`
MTime string `json:"mtime"`
}
// IsNormal judge that whether archive's state is normally.
func (a *Archive) IsNormal() bool {
arc := api.Arc{State: int32(a.State)}
return arc.IsNormal()
}
// PushStatistic 推送统计数据对象
type PushStatistic struct {
Aid int64 `json:"aid"`
Group string `json:"group"`
Type int `json:"type"`
Mids string `json:"mids"`
MidsCounter int `json:"mids_counter"`
CTime time.Time `json:"ctime"`
}

View File

@@ -0,0 +1,91 @@
package model
import (
"database/sql/driver"
"encoding/binary"
)
// Ints be used to MySql\Protobuf varbinary converting.
type Ints []int64
// MarshalTo marshal int64 slice to bytes,each int64 will occupy Fixed 8 bytes.
// if the argument data not supplied with the full size,it will return the actual written size
func (is Ints) MarshalTo(data []byte) (int, error) {
for i, n := range is {
start := i * 8
end := (i + 1) * 8
if len(data) < end {
return start, nil
}
bs := data[start:end]
binary.BigEndian.PutUint64(bs, uint64(n))
}
return 8 * len(is), nil
}
// Size return the total size it will occupy in bytes
func (is Ints) Size() int {
return len(is) * 8
}
// Unmarshal parse the data into int64 slice
func (is *Ints) Unmarshal(data []byte) error {
return is.Scan(data)
}
// Scan parse the data into int64 slice
func (is *Ints) Scan(src interface{}) (err error) {
switch sc := src.(type) {
case []byte:
var res []int64
for i := 0; i < len(sc) && i+8 <= len(sc); i += 8 {
ui := binary.BigEndian.Uint64(sc[i : i+8])
res = append(res, int64(ui))
}
*is = res
}
return
}
// Value marshal int64 slice to driver.Value,each int64 will occupy Fixed 8 bytes
func (is Ints) Value() (driver.Value, error) {
return is.Bytes(), nil
}
// Bytes marshal int64 slice to bytes,each int64 will occupy Fixed 8 bytes
func (is Ints) Bytes() []byte {
res := make([]byte, 0, 8*len(is))
for _, i := range is {
bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, uint64(i))
res = append(res, bs...)
}
return res
}
// Evict get rid of the sepcified num from the slice
func (is *Ints) Evict(e int64) (ok bool) {
res := make([]int64, len(*is)-1)
for _, v := range *is {
if v != e {
res = append(res, v)
} else {
ok = true
}
}
*is = res
return
}
// Exist judge the sepcified num is in the slice or not
func (is Ints) Exist(i int64) (e bool) {
for _, v := range is {
if v == i {
e = true
return
}
}
return
}

View File

@@ -0,0 +1,74 @@
package model
import (
"testing"
)
func TestMarshalAndUnmarshal(t *testing.T) {
var a = Ints{1, 2, 3}
data := make([]byte, a.Size())
n, err := a.MarshalTo(data)
if n != 24 {
t.Logf("marshal size must be 24")
t.FailNow()
}
if err != nil {
t.Fatalf("err:%v", err)
}
var b Ints
err = b.Unmarshal(data)
if err != nil {
t.Fatalf("err:%v", err)
}
if b[0] != 1 || b[1] != 2 || b[2] != 3 {
t.Logf("unmarshal failed!b:%v", b)
t.FailNow()
}
}
func TestUncompleteMarshal(t *testing.T) {
var a = Ints{1, 2, 3}
data := make([]byte, a.Size()-7)
n, err := a.MarshalTo(data)
if n != 16 {
t.Logf("marshal size must be 16")
t.FailNow()
}
if err != nil {
t.Fatalf("err:%v", err)
}
var b Ints
err = b.Unmarshal(data)
if err != nil {
t.Fatalf("err:%v", err)
}
if b[0] != 1 || b[1] != 2 {
t.Logf("unmarshal failed!b:%v", b)
t.FailNow()
}
}
func TestNilMarshal(t *testing.T) {
var a = Ints{1, 2, 3}
var data []byte
n, err := a.MarshalTo(data)
if n != 0 {
t.Logf("marshal size must be 0")
t.FailNow()
}
if err != nil {
t.Fatalf("err:%v", err)
}
var b Ints
err = b.Unmarshal(data)
if err != nil {
t.Fatalf("err:%v", err)
}
if b != nil {
t.Logf("unmarshal failed!b:%v", b)
t.FailNow()
}
}