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,71 @@
load(
"@io_bazel_rules_go//proto:def.bzl",
"go_proto_library",
)
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"db.go",
"model.go",
],
embed = [":model_go_proto"],
importpath = "go-common/app/service/main/ugcpay-rank/internal/model",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/log:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"@com_github_gogo_protobuf//gogoproto:go_default_library",
"@com_github_gogo_protobuf//proto: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 = ["model_test.go"],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//library/log:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
proto_library(
name = "model_proto",
srcs = ["model.proto"],
tags = ["automanaged"],
deps = ["@gogo_special_proto//github.com/gogo/protobuf/gogoproto"],
)
go_proto_library(
name = "model_go_proto",
compilers = ["@io_bazel_rules_go//proto:gogofast_proto"],
importpath = "go-common/app/service/main/ugcpay-rank/internal/model",
proto = ":model_proto",
tags = ["automanaged"],
deps = ["@com_github_gogo_protobuf//gogoproto:go_default_library"],
)

View File

@@ -0,0 +1,53 @@
package model
import (
"time"
)
// DBElecUPRank .
type DBElecUPRank struct {
ID int64
UPMID int64
PayMID int64
PayAmount int64
Ver int64
Hidden bool
CTime time.Time
MTime time.Time
}
// DBElecAVRank .
type DBElecAVRank struct {
ID int64
AVID int64
UPMID int64
PayMID int64
PayAmount int64
Ver int64
Hidden bool
CTime time.Time
MTime time.Time
}
// DBElecMessage .
type DBElecMessage struct {
ID int64
Ver int64
AVID int64
UPMID int64
PayMID int64
Message string
Replied bool
Hidden bool
CTime time.Time
MTime time.Time
}
// DBElecUserSetting .
type DBElecUserSetting struct {
ID int64
MID int64
Value int
CTime time.Time
MTime time.Time
}

View File

@@ -0,0 +1,155 @@
package model
import (
"encoding/json"
// "fmt"
// "time"
"go-common/library/log"
"github.com/pkg/errors"
)
// Trend enum
const (
TrendHold = 0
TrendUp = 1
TrendDown = 2
)
// Charge 充电amount: 总充电数
func (e *RankElecPrepUPProto) Charge(payMID int64, amount int64, newElecer bool) {
// 检查是否在榜,如果在榜直接更新
ele := e.Find(payMID)
if ele != nil {
ele.Amount = amount
e.update(ele)
return
}
if newElecer {
e.Count++
e.CountUPTotalElec++
}
// 排行榜列表为最大长度 且 榜单末位的充电数 >= 新的充电数,返回
if len(e.List) >= e.Size_ && e.List[len(e.List)-1].Amount >= amount {
return
}
// 没有在榜且可以上榜则插入原榜
newEle := &RankElecPrepElementProto{
Rank: -1,
MID: payMID,
TrendType: TrendHold,
Amount: amount,
}
e.insert(newEle)
}
// UpdateMessage 更新留言
func (e *RankElecPrepUPProto) UpdateMessage(payMID int64, message string, hidden bool) {
ele := e.Find(payMID)
if ele != nil {
ele.Message = &ElecMessageProto{
Message: message,
Hidden: hidden,
}
}
}
// Find 获得榜单中payMID的排名信息如果不存在则返回nil
func (e *RankElecPrepUPProto) Find(payMID int64) (ele *RankElecPrepElementProto) {
for _, r := range e.List {
if r.MID == payMID {
ele = r
return
}
}
return nil
}
// 更新排名
func (e *RankElecPrepUPProto) update(ele *RankElecPrepElementProto) {
for i := range e.List {
if e.List[i] == nil {
log.Error("ElecPrepUPRank: %s, index: %d, ele: %+v", e, i, ele)
}
if e.List[i].MID != ele.MID && e.List[i].Amount >= ele.Amount {
continue
}
newRank := e.List[i].Rank
if err := e.shift(i, ele.Rank-1); err != nil {
log.Error("ElecPrepUPRank.update err: %+v, ele: %+v in rank: %s ", err, ele, e)
return
}
ele.Rank = newRank
e.List[i] = ele
break
}
}
// 插入新排名到榜单
func (e *RankElecPrepUPProto) insert(ele *RankElecPrepElementProto) {
for i := range e.List {
if e.List[i].Amount >= ele.Amount {
continue
}
// 找到新排名的位置,并插入
ele.Rank = e.List[i].Rank
if len(e.List) >= e.Size_ { // 榜单已满
if err := e.shift(i, len(e.List)-1); err != nil {
log.Error("ElecPrepUPRank.insert err: %+v, ele: %+v in rank: %s ", err, ele, e)
return
}
} else { // 榜单未满
tailEle := e.List[len(e.List)-1]
if err := e.shift(i, len(e.List)-1); err != nil {
log.Error("ElecPrepUPRank.insert err: %+v, ele: %+v in rank: %s ", err, ele, e)
return
}
tailEle.Rank++
e.List = append(e.List, tailEle)
}
e.List[i] = ele
break
}
// 插入到末位
if ele.Rank < 0 {
e.List = append(e.List, ele)
ele.Rank = len(e.List)
}
}
// shift 将排名从 fromRank 起后移一位到 toRank原 toRank 会被丢弃,原 fromRank 将会空出
func (e *RankElecPrepUPProto) shift(fromIndex, toIndex int) (err error) {
if fromIndex > toIndex {
err = errors.Errorf("shift from(%d) > to(%d)", fromIndex, toIndex)
return
}
if len(e.List)-1 < toIndex {
err = errors.Errorf("shift out of range [%d,%d], just have: %d", fromIndex, toIndex, len(e.List))
return
}
lastEle := e.List[fromIndex]
e.List[fromIndex] = nil
for i := fromIndex + 1; i <= toIndex; i++ {
lastEle.Rank++
e.List[i], lastEle = lastEle, e.List[i]
}
return
}
// Message binlog databus msg.
type Message struct {
Action string `json:"action"`
Table string `json:"table"`
New json.RawMessage `json:"new"`
Old json.RawMessage `json:"old"`
}
// ElecUserSetting .
type ElecUserSetting int32
// ShowMessage 充电榜单是否显示留言
func (e ElecUserSetting) ShowMessage() bool {
return (e & 0x1) > 0
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,64 @@
syntax = "proto3";
package main.account.ugcpay.service.model;
option go_package = "model";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.goproto_getters_all) = false;
message RankElecPrepUPProto {
int64 CountUPTotalElec = 1;
int64 Count = 2;
int64 UPMID = 3;
int64 Size = 4 [(gogoproto.casttype) = "int"];
repeated RankElecPrepElementProto List = 5;
}
message RankElecPrepAVProto {
RankElecPrepUPProto RankElecPrepUPProto = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
int64 AVID = 2;
}
message RankElecPrepElementProto {
int64 MID = 1;
int64 Rank = 2 [(gogoproto.casttype) = "int"];
uint32 TrendType = 3 [(gogoproto.casttype) = "uint8"];
int64 Amount = 4;
ElecMessageProto Message = 5;
}
message RankElecUPProto {
int64 CountUPTotalElec = 1 ;
int64 Count = 2 ;
int64 UPMID = 3 ;
int64 Size = 4 [(gogoproto.casttype) = "int"];
repeated RankElecElementProto List = 5;
}
message RankElecAVProto {
int64 CountUPTotalElec = 1 ;
int64 Count = 2 ;
int64 AVID = 3 ;
int64 UPMID = 4 ;
int64 Size = 5 [(gogoproto.casttype) = "int"];
repeated RankElecElementProto List = 6;
}
message RankElecElementProto {
RankElecPrepElementProto RankElecPrepElementProto = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
VIPInfoProto VIP = 2;
string Nickname = 3;
string Avatar = 4;
}
message ElecMessageProto {
string Message = 1;
bool Hidden = 2;
}
message VIPInfoProto {
int32 Type = 1;
int32 Status = 2;
int64 DueDate = 3;
}

View File

@@ -0,0 +1,248 @@
package model
import (
"math/rand"
"testing"
"github.com/smartystreets/goconvey/convey"
"go-common/library/log"
)
func TestElecPrepUPRankShift(t *testing.T) {
convey.Convey("shift\n", t, func() {
rank := &RankElecPrepUPProto{
Count: 2,
UPMID: 1,
Size_: 100,
}
rank.List = []*RankElecPrepElementProto{
&RankElecPrepElementProto{
MID: 46333,
Rank: 1,
Amount: 100,
}, &RankElecPrepElementProto{
MID: 35858,
Rank: 2,
Amount: 99,
}, &RankElecPrepElementProto{
MID: 233,
Rank: 3,
Amount: 98,
}, &RankElecPrepElementProto{
MID: 2,
Rank: 4,
Amount: 1,
},
}
rank.shift(0, 2)
log.Info("%s", rank)
convey.So(rank.List, convey.ShouldHaveLength, 4)
convey.So(rank.List[0], convey.ShouldBeNil)
convey.So(rank.List[1].Amount, convey.ShouldEqual, 100)
convey.So(rank.List[2].Amount, convey.ShouldEqual, 99)
})
}
func TestElecPrepUPRankUpdate(t *testing.T) {
convey.Convey("shuffle\n", t, func() {
rank := &RankElecPrepUPProto{
Count: 2,
UPMID: 1,
Size_: 100,
}
rank.List = []*RankElecPrepElementProto{
&RankElecPrepElementProto{
MID: 46333,
Rank: 1,
Amount: 100,
}, &RankElecPrepElementProto{
MID: 35858,
Rank: 2,
Amount: 99,
}, &RankElecPrepElementProto{
MID: 233,
Rank: 3,
Amount: 98,
}, &RankElecPrepElementProto{
MID: 2,
Rank: 4,
Amount: 1,
},
}
rank.update(&RankElecPrepElementProto{
MID: 2,
Rank: 4,
Amount: 2,
})
log.Info("%s", rank)
})
}
func TestElecPrepUPRankInsert(t *testing.T) {
convey.Convey("shuffle\n", t, func() {
rank := &RankElecPrepUPProto{
Count: 2,
UPMID: 1,
Size_: 100,
}
rank.List = []*RankElecPrepElementProto{
&RankElecPrepElementProto{
MID: 46333,
Rank: 1,
Amount: 100,
}, &RankElecPrepElementProto{
MID: 35858,
Rank: 2,
Amount: 99,
}, &RankElecPrepElementProto{
MID: 233,
Rank: 3,
Amount: 98,
}, &RankElecPrepElementProto{
MID: 2,
Rank: 4,
Amount: 1,
},
}
rank.insert(&RankElecPrepElementProto{
MID: 322,
Rank: -1,
Amount: 101,
})
log.Info("%s", rank)
})
}
func TestElecPrepUPRankCharge(t *testing.T) {
convey.Convey("charge\n", t, func() {
rank := &RankElecPrepUPProto{
Count: 0,
UPMID: 2,
Size_: 3,
}
rank.Charge(35858, 100, true)
convey.So(len(rank.List), convey.ShouldEqual, 1)
convey.So(rank.List[0], convey.ShouldResemble, &RankElecPrepElementProto{
MID: 35858,
Rank: 1,
Amount: 100,
})
rank.Charge(46333, 100, true)
convey.So(len(rank.List), convey.ShouldEqual, 2)
convey.So(rank.List[1], convey.ShouldResemble, &RankElecPrepElementProto{
MID: 46333,
Rank: 2,
Amount: 100,
})
rank.Charge(233, 100, true)
convey.So(len(rank.List), convey.ShouldEqual, 3)
convey.So(rank.List[2], convey.ShouldResemble, &RankElecPrepElementProto{
MID: 233,
Rank: 3,
Amount: 100,
})
log.Info("%s", rank)
rank.Charge(46333, 101, false)
log.Info("%s", rank)
convey.So(len(rank.List), convey.ShouldEqual, 3)
convey.So(rank.List[0], convey.ShouldResemble, &RankElecPrepElementProto{
MID: 46333,
Rank: 1,
Amount: 101,
})
convey.So(rank.List[1], convey.ShouldResemble, &RankElecPrepElementProto{
MID: 35858,
Rank: 2,
Amount: 100,
})
rank.Charge(233, 200, false)
convey.So(len(rank.List), convey.ShouldEqual, 3)
convey.So(rank.List[0], convey.ShouldResemble, &RankElecPrepElementProto{
MID: 233,
Rank: 1,
Amount: 200,
})
convey.So(rank.List[1], convey.ShouldResemble, &RankElecPrepElementProto{
MID: 46333,
Rank: 2,
Amount: 101,
})
log.Info("%s", rank)
})
}
func TestElecPrepUPRankUpdateMessage(t *testing.T) {
convey.Convey("charge\n", t, func() {
rank := &RankElecPrepUPProto{
Count: 0,
UPMID: 2,
Size_: 3,
}
rank.Charge(35858, 100, true)
rank.Charge(46333, 100, true)
rank.Charge(233, 100, true)
rank.UpdateMessage(35858, "ut", false)
rank.UpdateMessage(46333, "ut", true)
rank.UpdateMessage(233, "ut-hello", false)
convey.So(rank.Find(35858), convey.ShouldNotBeNil)
convey.So(rank.Find(35858).Message, convey.ShouldNotBeNil)
convey.So(rank.Find(35858).Message.Message, convey.ShouldEqual, "ut")
convey.So(rank.Find(35858).Message.Hidden, convey.ShouldEqual, false)
})
}
func TestElecPrepUPRankChargeRandom(t *testing.T) {
rank := &RankElecPrepUPProto{
Count: 0,
UPMID: 1,
Size_: 100,
}
for i := 0; i < 100; i++ {
rank.Charge(randomEle())
}
log.Info("%s", rank)
}
func BenchmarkElecPrepUPRankCharge(b *testing.B) {
rank := &RankElecPrepUPProto{
Count: 0,
UPMID: 1,
Size_: 100,
}
for i := 0; i < b.N; i++ {
rank.Charge(randomEle())
}
log.Info("%s", rank)
}
var (
mids = []int64{2, 46333, 35858, 233}
i = 0
)
func randomEle() (mid int64, amount int64, isNew bool) {
mid = mids[rand.Intn(len(mids))]
i++
amount = int64(i)
isNew = true
return
}
func TestElecUserSetting(t *testing.T) {
convey.Convey("TestElecUserSetting", t, func() {
set := ElecUserSetting(2147483646)
convey.So(set.ShowMessage(), convey.ShouldBeFalse)
set = ElecUserSetting(0x7ffffff)
convey.So(set.ShowMessage(), convey.ShouldBeTrue)
set = ElecUserSetting(0x1)
convey.So(set.ShowMessage(), convey.ShouldBeTrue)
})
}