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,55 @@
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",
"databus_test.go",
"redis_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/archive/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"databus.go",
"redis.go",
],
importpath = "go-common/app/service/main/archive/dao/share",
tags = ["automanaged"],
deps = [
"//app/service/main/archive/conf:go_default_library",
"//library/cache/redis:go_default_library",
"//library/log:go_default_library",
"//library/net/ip:go_default_library",
"//library/queue/databus: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,48 @@
package share
import (
"context"
"go-common/library/cache/redis"
"go-common/library/queue/databus"
"go-common/app/service/main/archive/conf"
)
// Dao is share dao.
type Dao struct {
c *conf.Config
// redis
rds *redis.Pool
expire int32
// databus
statDbus *databus.Databus
shareDbus *databus.Databus
}
// New new a share dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
rds: redis.NewPool(c.Redis.Archive.Config),
expire: 168 * 60 * 60,
statDbus: databus.New(c.StatDatabus),
shareDbus: databus.New(c.ShareDatabus),
}
return d
}
// Close close resource.
func (d *Dao) Close() {
if d.rds != nil {
d.rds.Close()
}
}
// Ping ping success.
func (d *Dao) Ping(c context.Context) (err error) {
conn := d.rds.Get(c)
_, err = conn.Do("SET", "PING", "PONG")
conn.Close()
return
}

View File

@@ -0,0 +1,33 @@
package share
import (
"flag"
"go-common/app/service/main/archive/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.archive-service")
flag.Set("conf_token", "Y2LJhIsHx87nJaOBSxuG5TeZoLdBFlrE")
flag.Set("tree_id", "2302")
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")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,37 @@
package share
import (
"context"
"strconv"
"time"
"go-common/library/log"
)
// PubStatDatabus pub share count into databus.
func (d *Dao) PubStatDatabus(c context.Context, aid int64, share int) (err error) {
type stat struct {
Type string `json:"type"`
ID int64 `json:"id"`
Count int `json:"count"`
Ts int64 `json:"timestamp"`
}
if err = d.statDbus.Send(c, strconv.FormatInt(aid, 10), &stat{Type: "archive", ID: aid, Count: share, Ts: time.Now().Unix()}); err != nil {
log.Error("d.databus.Send error(%v)", err)
}
return
}
// PubShare pub first share to databus
func (d *Dao) PubShare(c context.Context, aid int64, mid int64, ip string) (err error) {
type share struct {
Event string `json:"event"`
Mid int64 `json:"mid"`
IP string `json:"ip"`
Ts int64 `json:"ts"`
}
if err = d.shareDbus.Send(c, strconv.FormatInt(mid, 10), &share{Event: "share", Mid: mid, IP: ip, Ts: time.Now().Unix()}); err != nil {
log.Error("d.shareDbus.Send error(%v)", err)
}
return
}

View File

@@ -0,0 +1,37 @@
package share
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestSharePubStatDatabus(t *testing.T) {
var (
c = context.TODO()
aid = int64(1)
share = int(1)
)
convey.Convey("PubStatDatabus", t, func(ctx convey.C) {
err := d.PubStatDatabus(c, aid, share)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestSharePubShare(t *testing.T) {
var (
c = context.TODO()
aid = int64(1)
mid = int64(1)
ip = "127.0.0.1"
)
convey.Convey("PubShare", t, func(ctx convey.C) {
err := d.PubShare(c, aid, mid, ip)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,75 @@
package share
import (
"context"
"fmt"
"time"
"go-common/library/cache/redis"
"go-common/library/log"
xip "go-common/library/net/ip"
)
const (
_prefixShare = "as"
// the first shared every day
_prefixFirst = "afs"
)
func redisKey(aid int64) string {
return fmt.Sprintf("%s_%d", _prefixShare, aid)
}
func redisFKey(mid int64, date string) string {
return fmt.Sprintf("%s_%s_%d", _prefixFirst, date, mid%10000)
}
// AddShare add a share with mid and ip to redis.
func (d *Dao) AddShare(c context.Context, mid, aid int64, ip string) (ok bool, err error) {
var (
key = redisKey(aid)
value = (mid << 32) | int64(xip.InetAtoN(ip))
conn = d.rds.Get(c)
)
defer conn.Close()
if err = conn.Send("SADD", key, value); err != nil {
log.Error("conn.Send(SADD, %s, %d) error(%v)", key, value, err)
return
}
if err = conn.Send("EXPIRE", key, d.expire); err != nil {
log.Error("conn.Send(EXPIRE, %s, %d) error(%v)", key, value, err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
}
if ok, err = redis.Bool(conn.Receive()); err != nil {
log.Error("conn.Receive error(%v)", err)
}
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive error(%v)", err)
}
return
}
// HadFirstShare if key not exist need set expire to one day.
func (d *Dao) HadFirstShare(c context.Context, mid, aid int64, ip string) (had bool, err error) {
var (
date = time.Now().Format("0102")
key = redisFKey(mid, date)
addOk bool
conn = d.rds.Get(c)
)
defer conn.Close()
if addOk, err = redis.Bool(conn.Do("SADD", key, mid)); err != nil {
log.Error("conn.Send(SADD, %s, %d) error(%v)", key, mid, err)
return
}
if addOk {
if _, err = conn.Do("EXPIRE", key, 24*60*60); err != nil {
log.Error("conn.Send(EXPIRE, %s, 24*60*60) error(%v)", key, err)
}
}
had = !addOk
return
}

View File

@@ -0,0 +1,65 @@
package share
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestShareredisKey(t *testing.T) {
var (
aid = int64(1)
)
convey.Convey("redisKey", t, func(ctx convey.C) {
p1 := redisKey(aid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestShareredisFKey(t *testing.T) {
var (
mid = int64(1)
date = ""
)
convey.Convey("redisFKey", t, func(ctx convey.C) {
p1 := redisFKey(mid, date)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestShareAddShare(t *testing.T) {
var (
c = context.TODO()
mid = int64(1)
aid = int64(1)
ip = ""
)
convey.Convey("AddShare", t, func(ctx convey.C) {
ok, err := d.AddShare(c, mid, aid, ip)
ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ok, convey.ShouldNotBeNil)
})
})
}
func TestShareHadFirstShare(t *testing.T) {
var (
c = context.TODO()
mid = int64(1)
aid = int64(1)
ip = ""
)
convey.Convey("HadFirstShare", t, func(ctx convey.C) {
had, err := d.HadFirstShare(c, mid, aid, ip)
ctx.Convey("Then err should be nil.had should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(had, convey.ShouldNotBeNil)
})
})
}