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,46 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["databus_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/reply/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["databus.go"],
importpath = "go-common/app/job/main/reply/dao/stat",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/reply/conf:go_default_library",
"//library/log: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,54 @@
package stat
import (
"context"
"strconv"
"time"
"go-common/app/job/main/reply/conf"
"go-common/library/log"
"go-common/library/queue/databus"
)
type statMsg struct {
ID int64 `json:"id"`
Timestamp int64 `json:"timestamp"`
Count int `json:"count"`
Type string `json:"type"`
}
// Dao stat dao.
type Dao struct {
// new databus stats
types map[int8]string
databus *databus.Databus
}
// New new a stat dao and return.
func New(c *conf.Config) *Dao {
d := new(Dao)
// new databus stats
d.types = make(map[int8]string)
for name, typ := range c.StatTypes {
d.types[typ] = name
}
d.databus = databus.New(c.Databus.Stats)
return d
}
// Send update stat.
func (d *Dao) Send(c context.Context, typ int8, oid int64, cnt int) (err error) {
// new databus stats
if name, ok := d.types[typ]; ok {
m := &statMsg{
ID: oid,
Type: name,
Count: cnt,
Timestamp: time.Now().Unix(),
}
if err = d.databus.Send(c, strconv.FormatInt(oid, 10), m); err != nil {
log.Error("d.databus.Send(%d,%d,%d) error(%v)", typ, oid, cnt, err)
}
}
return
}

View File

@@ -0,0 +1,54 @@
package stat
import (
"context"
"flag"
"go-common/app/job/main/reply/conf"
"os"
"testing"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.community.reply-job")
flag.Set("conf_token", "5deea0665f8a7670b22a719337a39c7d")
flag.Set("tree_id", "2123")
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/reply-job-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}
func TestStatSend(t *testing.T) {
convey.Convey("Send", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
oid = int64(0)
cnt = int(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.Send(c, typ, oid, cnt)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}