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,51 @@
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",
"mysql_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/infra/notify/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"mysql.go",
],
importpath = "go-common/app/infra/notify/dao",
tags = ["automanaged"],
deps = [
"//app/infra/notify/conf:go_default_library",
"//app/infra/notify/model:go_default_library",
"//library/database/sql: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,33 @@
package dao
import (
"context"
"go-common/app/infra/notify/conf"
xsql "go-common/library/database/sql"
)
// Dao dao
type Dao struct {
c *conf.Config
db *xsql.DB
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
db: xsql.NewMySQL(c.MySQL),
}
return
}
// Close close the resource.
func (dao *Dao) Close() {
dao.db.Close()
}
// Ping dao ping
func (dao *Dao) Ping(c context.Context) error {
return dao.db.Ping(c)
}

View File

@@ -0,0 +1,33 @@
package dao
import (
"flag"
"go-common/app/infra/notify/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.common-arch.notify")
flag.Set("conf_token", "9919040d8742a2124abbed8368626075")
flag.Set("tree_id", "22513")
flag.Set("conf_version", "v1.0.0")
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,100 @@
package dao
import (
"context"
"encoding/json"
"go-common/app/infra/notify/model"
)
const (
_loadNotify = "SELECT n.id,topic.cluster,topic.topic,a.group,n.callback,n.concurrent,n.filter,n.mtime FROM notify AS n LEFT JOIN auth2 as a ON a.id=n.gid LEFT JOIN topic ON a.topic_id=topic.id WHERE n.state=1 AND n.zone=?"
_loadPub = `SELECT a.group,topic.cluster,topic.topic,a.operation,app2.app_secret FROM auth2 AS a LEFT JOIN app2 ON app2.id=a.app_id LEFT JOIN topic ON topic.id=a.topic_id`
_selFilter = "SELECT `filters` FROM filters WHERE nid=?"
_addFailBk = "INSERT INTO fail_backup(topic,`group`,`cluster`,msg,`index`) VALUE (?,?,?,?,?)"
_delFailBk = "DELETE FROM fail_backup where id=?"
_loadFailBk = "SELECT id,topic,`group`,`cluster`,`msg`,`offset`,`index` FROM fail_backup"
)
// LoadNotify load all notify config.
func (d *Dao) LoadNotify(c context.Context, zone string) (ns []*model.Watcher, err error) {
rows, err := d.db.Query(c, _loadNotify, zone)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
n := new(model.Watcher)
if err = rows.Scan(&n.ID, &n.Cluster, &n.Topic, &n.Group, &n.Callback, &n.Concurrent, &n.Filter, &n.Mtime); err != nil {
return
}
ns = append(ns, n)
}
return
}
// LoadPub load all pub config.
func (d *Dao) LoadPub(c context.Context) (ps []*model.Pub, err error) {
rows, err := d.db.Query(c, _loadPub)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
n := new(model.Pub)
if err = rows.Scan(&n.Group, &n.Cluster, &n.Topic, &n.Operation, &n.AppSecret); err != nil {
return
}
ps = append(ps, n)
}
return
}
// Filters get filter condition.
func (d *Dao) Filters(c context.Context, id int64) (fs []*model.Filter, err error) {
rows := d.db.QueryRow(c, _selFilter, id)
if err != nil {
return
}
var filters string
if err = rows.Scan(&filters); err != nil {
return
}
err = json.Unmarshal([]byte(filters), &fs)
return
}
// AddFailBk add fail msg to fail backup.
func (d *Dao) AddFailBk(c context.Context, topic, group, cluster, msg string, index int64) (id int64, err error) {
res, err := d.db.Exec(c, _addFailBk, topic, group, cluster, msg, index)
if err != nil {
return
}
return res.LastInsertId()
}
// DelFailBk del msg from fail backup.
func (d *Dao) DelFailBk(c context.Context, id int64) (affected int64, err error) {
res, err := d.db.Exec(c, _delFailBk, id)
if err != nil {
return
}
return res.RowsAffected()
}
// LoadFailBk load all fail backup msg.
func (d *Dao) LoadFailBk(c context.Context) (fbs []*model.FailBackup, err error) {
rows, err := d.db.Query(c, _loadFailBk)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
fb := new(model.FailBackup)
if err = rows.Scan(&fb.ID, &fb.Topic, &fb.Group, &fb.Cluster, &fb.Msg, &fb.Offset, &fb.Index); err != nil {
return
}
fbs = append(fbs, fb)
}
return
}

View File

@@ -0,0 +1,93 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoLoadNotify(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("LoadNotify", t, func(ctx convey.C) {
_, err := d.LoadNotify(c, "sh001")
ctx.Convey("Then err should be nil.ns should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
//ctx.So(ns, convey.ShouldNotBeNil)
})
})
}
func TestDaoLoadPub(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("LoadPub", t, func(ctx convey.C) {
_, err := d.LoadPub(c)
ctx.Convey("Then err should be nil.ps should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
// ctx.So(ps, convey.ShouldNotBeNil)
})
})
}
func TestDaoFilters(t *testing.T) {
var (
c = context.TODO()
id = int64(7)
)
convey.Convey("Filters", t, func(ctx convey.C) {
fs, err := d.Filters(c, id)
ctx.Convey("Then err should be nil.fs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(fs, convey.ShouldNotBeNil)
})
})
}
func TestDaoAddFailBk(t *testing.T) {
var (
c = context.TODO()
topic = ""
group = ""
cluster = ""
msg = ""
index = int64(0)
)
convey.Convey("AddFailBk", t, func(ctx convey.C) {
id, err := d.AddFailBk(c, topic, group, cluster, msg, index)
ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(id, convey.ShouldNotBeNil)
})
})
}
func TestDaoDelFailBk(t *testing.T) {
var (
c = context.TODO()
id = int64(0)
)
convey.Convey("DelFailBk", t, func(ctx convey.C) {
affected, err := d.DelFailBk(c, id)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
}
func TestDaoLoadFailBk(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("LoadFailBk", t, func(ctx convey.C) {
fbs, err := d.LoadFailBk(c)
ctx.Convey("Then err should be nil.fbs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(fbs, convey.ShouldNotBeNil)
})
})
}