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,60 @@
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",
"redis_test.go",
"vdoad_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/resource/conf:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/sql:go_default_library",
"//vendor/github.com/bouk/monkey:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"redis.go",
"vdoad.go",
],
importpath = "go-common/app/service/main/resource/dao/ads",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/resource/conf:go_default_library",
"//app/service/main/resource/model:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//vendor/github.com/dgryski/go-farm: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,40 @@
package ads
import (
"context"
"time"
"go-common/app/service/main/resource/conf"
"go-common/library/cache/redis"
xsql "go-common/library/database/sql"
)
// Dao is resource dao.
type Dao struct {
db *xsql.DB
c *conf.Config
// redis
redis *redis.Pool
expire int32
}
// New init mysql db
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: xsql.NewMySQL(c.DB.Ads),
redis: redis.NewPool(c.Redis.Ads.Config),
expire: int32(time.Duration(c.Redis.Ads.Expire) / time.Second),
}
return
}
// Close close the resource.
func (d *Dao) Close() {
d.db.Close()
}
// Ping check dao health.
func (d *Dao) Ping(c context.Context) error {
return d.db.Ping(c)
}

View File

@@ -0,0 +1,64 @@
package ads
import (
"context"
"flag"
"os"
"testing"
"go-common/app/service/main/resource/conf"
"go-common/library/cache/redis"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.resource-service")
flag.Set("conf_token", "y79sErNhxggjvULS0O8Czas9PaxHBF5o")
flag.Set("tree_id", "3232")
flag.Set("conf_version", "docker-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/resource-service-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
// func CleanCache() {
// pool := redis.NewPool(conf.Conf.Redis.Ads.Config)
// pool.Get(context.TODO()).Do("FLUSHDB")
// }
func WithDao(f func(d *Dao)) func() {
return func() {
convey.Reset(func() {
pool := redis.NewPool(conf.Conf.Redis.Ads.Config)
pool.Get(context.TODO()).Do("FLUSHDB")
})
f(d)
}
}
func TestDaoPing(t *testing.T) {
convey.Convey("Ping", t, func(ctx convey.C) {
err := d.Ping(context.TODO())
ctx.Convey("Err should be nil", func() {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,94 @@
package ads
import (
"context"
"fmt"
"go-common/library/cache/redis"
"go-common/library/log"
"github.com/dgryski/go-farm"
)
const (
_prefixBuvid = "buvid:%d"
)
func (d *Dao) keyBuvid(buvid string) (key string) {
num := int64(farm.Hash32([]byte(buvid)))
key = fmt.Sprintf(_prefixBuvid, num%d.c.HashNum)
return
}
// ExistsAuth if existes buvid in redis.
func (d *Dao) ExistsAuth(c context.Context, key string) (ok bool, err error) {
var conn = d.redis.Get(c)
defer conn.Close()
if ok, err = redis.Bool(conn.Do("EXISTS", key)); err != nil {
log.Error("EXISTS key(%s), error(%v)", key, err)
}
return
}
// BuvidCount get buvid count info from redis.
func (d *Dao) BuvidCount(c context.Context, faid int64, buvid string) (res int64, err error) {
var (
key = d.keyBuvid(buvid)
conn = d.redis.Get(c)
field = faid
)
defer conn.Close()
if res, err = redis.Int64(conn.Do("HGET", key, field)); err != nil {
if err != redis.ErrNil {
log.Error("BuvidCount conn.Send HGET(%v, %v) error(%v)", key, field, err)
return
}
err = nil
}
return
}
// AddBuvidCount add buvid count info into redis.
func (d *Dao) AddBuvidCount(c context.Context, buvidCounts map[string]map[int64]int64) (err error) {
var (
key string
count int
conn = d.redis.Get(c)
faid int64
playCount int64
ok bool
)
defer conn.Close()
for buvid, buvidCount := range buvidCounts {
key = d.keyBuvid(buvid)
if ok, err = d.ExistsAuth(c, key); err != nil {
log.Error("EXISTS key(%s) error(%v)", key, err)
return
}
for faid, playCount = range buvidCount {
if err = conn.Send("HSET", key, faid, playCount); err != nil {
log.Error("HSET key(%s) field(%d) playCount(%d) error(%v)", key, faid, playCount, err)
return
}
count++
}
if !ok {
if err = conn.Send("EXPIRE", key, d.expire); err != nil {
log.Error("EXPIRE key(%s) expire(%v) error(%v)", key, d.expire, err)
return
}
count++
}
}
if err = conn.Flush(); err != nil {
log.Error("BuvidCount conn.Flush error(%v)", err)
return
}
for i := 0; i < count; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("BuvidCount conn.Receive error(%v)", err)
return
}
}
return
}

View File

@@ -0,0 +1,60 @@
package ads
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestAdsBuvidCache(t *testing.T) {
var (
buvidCache = map[string]map[int64]int64{
"123456": {
10097289: 1,
10097290: 5,
},
"234567": {
10097288: 3,
10098523: 1,
},
}
res int64
err error
)
convey.Convey("AddBuvidCount - add cache", t, WithDao(func(d *Dao) {
err = d.AddBuvidCount(context.Background(), buvidCache)
convey.Convey("Error should be nil", func() {
convey.So(err, convey.ShouldBeNil)
})
convey.Convey("BuvidCount - get cache", func() {
convey.Convey("Case 1: faid = 10097290, buvid = 123456", func() {
res, err = d.BuvidCount(context.Background(), 10097290, "123456")
convey.Convey("Error should be nil, res should be 5", func() {
convey.So(err, convey.ShouldBeNil)
convey.So(res, convey.ShouldEqual, 5)
})
})
convey.Convey("Case 2: faid = 10097288, buvid = 234567", func() {
res, err := d.BuvidCount(context.Background(), 10097288, "234567")
convey.Convey("Error should be nil, res should be 3", func() {
convey.So(err, convey.ShouldBeNil)
convey.So(res, convey.ShouldEqual, 3)
})
})
})
convey.Convey("keyBuvid", func() {
key := d.keyBuvid("234567")
convey.Convey("key should not be nil", func() {
convey.So(key, convey.ShouldNotBeNil)
})
convey.Convey("ExistsAuth - expire cache", func() {
res, err := d.ExistsAuth(context.Background(), key)
convey.Convey("Error should be nil, res should be true", func() {
convey.So(err, convey.ShouldBeNil)
convey.So(res, convey.ShouldEqual, true)
})
})
})
}))
}

View File

@@ -0,0 +1,49 @@
package ads
import (
"context"
"database/sql"
"time"
"go-common/app/service/main/resource/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
const (
_videoAdsSQL = `SELECT name,contract_id,aid,season_id,typeid,ad_cid,ad_strategy,ad_url,ad_order,skipable,note,agency_name,agency_country,
agency_area,price,verified,state,front_aid,target,platform,type,user_set,play_count,mtime FROM video_ads WHERE state=0 AND verified=1 AND starttime<? AND endtime>? ORDER BY mtime,ctime ASC`
)
// VideoAds get video_ads
func (dao *Dao) VideoAds(c context.Context) (ads []*model.VideoAD, err error) {
var (
rows *xsql.Rows
now = time.Now()
)
if rows, err = dao.db.Query(c, _videoAdsSQL, now, now); err != nil {
log.Error("dao.Exec(%v, %v), err (%v)", now, now, err)
return
}
defer rows.Close()
for rows.Next() {
ad := &model.VideoAD{}
var (
agencyCountry sql.NullInt64
agencyArea sql.NullInt64
price sql.NullFloat64
)
if err = rows.Scan(&ad.Name, &ad.ContractID, &ad.Aids, &ad.SeasonID, &ad.TypeID, &ad.AdCid, &ad.AdStrategy,
&ad.AdURL, &ad.AdOrder, &ad.Skipable, &ad.Note, &ad.AgencyName, &agencyCountry, &agencyArea,
&price, &ad.Verified, &ad.State, &ad.FrontAid, &ad.Target, &ad.Platform, &ad.Type, &ad.UserSet, &ad.PlayCount, &ad.MTime); err != nil {
log.Error("rows.Scan(), err (%v)", err)
return
}
ad.AgencyCountry = int(agencyCountry.Int64)
ad.AgencyArea = int(agencyArea.Int64)
ad.Price = float32(price.Float64)
ads = append(ads, ad)
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,35 @@
package ads
import (
"context"
"fmt"
"reflect"
"testing"
xsql "go-common/library/database/sql"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
func TestAdsVideoAds(t *testing.T) {
convey.Convey("VideoAds", t, func(ctx convey.C) {
ctx.Convey("When everything is correct", func(ctx convey.C) {
res, err := d.VideoAds(context.Background())
ctx.Convey("Error should be nil, res should not be empty(No Data)", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.SkipSo(res, convey.ShouldNotBeEmpty)
})
})
ctx.Convey("When db.Query gets error", func(ctx convey.C) {
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Query", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (*xsql.Rows, error) {
return nil, fmt.Errorf("db.Query error")
})
defer guard.Unpatch()
_, err := d.VideoAds(context.Background())
ctx.Convey("Error should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}