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,57 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"rating_test.go",
"redis_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/up-rating/conf:go_default_library",
"//app/interface/main/up-rating/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"rating.go",
"redis.go",
],
importpath = "go-common/app/interface/main/up-rating/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/up-rating/conf:go_default_library",
"//app/interface/main/up-rating/model:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//vendor/github.com/pkg/errors: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,56 @@
package dao
import (
"context"
"time"
"go-common/app/interface/main/up-rating/conf"
"go-common/library/cache/redis"
"go-common/library/database/sql"
"go-common/library/log"
)
// Dao def dao struct
type Dao struct {
c *conf.Config
db *sql.DB
rddb *sql.DB
redis *redis.Pool
upRatingExpire int64
}
// New fn
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.DB.Main),
rddb: sql.NewMySQL(c.DB.Slave),
redis: redis.NewPool(c.Redis.Config),
upRatingExpire: int64(time.Duration(c.Redis.UpRatingExpire) / time.Second),
}
return d
}
// Ping ping db
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.db.Ping(c); err != nil {
log.Error("d.db.Ping error(%v)", err)
return
}
return
}
// Close close db conn
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
if d.redis != nil {
d.redis.Close()
}
}
// BeginTran begin transcation
func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
return d.db.Begin(c)
}

View File

@@ -0,0 +1,36 @@
package dao
import (
"flag"
"os"
"testing"
"go-common/app/interface/main/up-rating/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.up-rating-interface")
flag.Set("conf_appid", "main.archive.up-rating-interface")
flag.Set("conf_token", "db506d78d859ee901ac000bac393484c")
flag.Set("tree_id", "65467")
flag.Set("conf_version", "0.0.1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../cmd/up-rating-interface.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,58 @@
package dao
import (
"context"
"fmt"
"go-common/app/interface/main/up-rating/model"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_upScoreSQL = "SELECT mid, creativity_score, influence_score, credit_score, cdate FROM up_rating_%02d WHERE mid=? AND cdate=? AND is_deleted=0"
_taskStatusSQL = "SELECT status FROM task_status WHERE date=? "
_whitelistSQL = "SELECT count(*) FROM up_white_list WHERE mid=? AND is_deleted=0"
)
// White will del later
func (d *Dao) White(c context.Context, mid int64) (count int64, err error) {
err = d.rddb.QueryRow(c, _whitelistSQL, mid).Scan(&count)
if err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("UpScore row scan error(%v)", err)
}
}
return
}
// TaskStatus ...
func (d *Dao) TaskStatus(c context.Context, date string) (status int, err error) {
err = d.rddb.QueryRow(c, _taskStatusSQL, date).Scan(&status)
if err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("UpScore row scan error(%v)", err)
}
}
return
}
// UpScore gets score data of UP
func (d *Dao) UpScore(c context.Context, mon int, mid int64, date string) (score *model.Score, err error) {
score = new(model.Score)
row := d.rddb.QueryRow(c, fmt.Sprintf(_upScoreSQL, mon), mid, date)
err = row.Scan(&score.MID, &score.Creative, &score.Influence, &score.Credit, &score.CDate)
if err != nil {
if err == sql.ErrNoRows {
score, err = nil, nil
} else {
log.Error("UpScore row scan error(%v)", err)
}
}
return
}

View File

@@ -0,0 +1,41 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoTaskStatus(t *testing.T) {
convey.Convey("TaskStatus", t, func(ctx convey.C) {
var (
c = context.Background()
date = "2018-11-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
status, err := d.TaskStatus(c, date)
ctx.Convey("Then err should be nil.status should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(status, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpScore(t *testing.T) {
convey.Convey("UpScore", t, func(ctx convey.C) {
var (
c = context.Background()
mon = int(11)
mid = int64(1)
date = "2018-11-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.UpScore(c, mon, mid, date)
ctx.Convey("Then err should be nil.score should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,77 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"go-common/app/interface/main/up-rating/model"
"go-common/library/cache/redis"
"github.com/pkg/errors"
)
// GetUpRatingCache ...
func (d *Dao) GetUpRatingCache(c context.Context, mid int64) (rating *model.Rating, err error) {
var (
key = upRatingKey(mid)
conn = d.redis.Get(c)
data []byte
)
defer conn.Close()
if data, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
}
return
}
rating = new(model.Rating)
if err = json.Unmarshal(data, &rating); err != nil {
err = errors.WithStack(err)
return nil, err
}
return
}
// ExpireUpRatingCache ...
func (d *Dao) ExpireUpRatingCache(c context.Context, mid int64) (err error) {
var (
key = upRatingKey(mid)
conn = d.redis.Get(c)
)
defer conn.Close()
if err = conn.Send("EXPIRE", key, 0); err != nil {
return
}
return
}
// SetUpRatingCache ...
func (d *Dao) SetUpRatingCache(c context.Context, mid int64, rating *model.Rating) (err error) {
var (
key = upRatingKey(mid)
conn = d.redis.Get(c)
bs []byte
)
defer conn.Close()
if bs, err = json.Marshal(rating); err != nil {
return errors.WithStack(err)
}
if err = conn.Send("SET", key, bs); err != nil {
return
}
if err = conn.Send("EXPIRE", key, d.upRatingExpire); err != nil {
return
}
if err = conn.Flush(); err != nil {
return
}
if _, err = conn.Receive(); err != nil {
return
}
return
}
func upRatingKey(mid int64) string {
return fmt.Sprintf("up_rating_detail_mid_%d", mid)
}

View File

@@ -0,0 +1,55 @@
package dao
import (
"context"
"testing"
"go-common/app/interface/main/up-rating/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetUpRatingCache(t *testing.T) {
convey.Convey("GetUpRatingCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.GetUpRatingCache(c, mid)
ctx.Convey("Then err should be nil.rating should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetUpRatingCache(t *testing.T) {
convey.Convey("SetUpRatingCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
rating = &model.Rating{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetUpRatingCache(c, mid, rating)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoupRatingKey(t *testing.T) {
convey.Convey("upRatingKey", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := upRatingKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}