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,56 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/interface/main/creative/dao/faq",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/model/faq:go_default_library",
"//library/cache/redis:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//library/time: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"],
)
go_test(
name = "go_default_test",
srcs = ["dao_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/model/faq:go_default_library",
"//library/cache/redis:go_default_library",
"//library/ecode:go_default_library",
"//vendor/github.com/bouk/monkey:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)

View File

@@ -0,0 +1,164 @@
package faq
import (
"context"
"encoding/json"
"fmt"
"go-common/app/interface/main/creative/conf"
"go-common/app/interface/main/creative/model/faq"
"go-common/library/cache/redis"
"go-common/library/ecode"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/metadata"
xtime "go-common/library/time"
"net/url"
"strconv"
)
const (
_notRobot = -1
_rsOk = "000000"
_searchURL = "/kb/searchInerDocListBilibili/4"
_hdKey = "faq_%s_%d_%d_%d"
)
// Dao define
type Dao struct {
c *conf.Config
client *bm.Client
searchURL string
redis *redis.Pool
redisExpire int32
}
// New init dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
client: bm.NewClient(c.HTTPClient.Normal),
searchURL: c.Host.HelpAPI + _searchURL,
redis: redis.NewPool(c.Redis.Cover.Config),
redisExpire: int32(120),
}
return
}
func keyHd(qTypeID string, keyFlag, pn, ps int) string {
return fmt.Sprintf(_hdKey, qTypeID, keyFlag, pn, ps)
}
// SetDetailCache set help detail to cache.
func (d *Dao) SetDetailCache(c context.Context, qTypeID string, keyFlag, pn, ps, total int, data []*faq.Detail) (err error) {
conn := d.redis.Get(c)
defer conn.Close()
count := 0
key := keyHd(qTypeID, keyFlag, pn, ps)
if err = conn.Send("DEL", key); err != nil {
log.Error("conn.Send(DEL, %s) error(%v)", key, err)
return
}
count++
var bs []byte
for _, detail := range data {
if bs, err = json.Marshal(detail); err != nil {
log.Error("json.Marshal(%v) error (%v)", detail, err)
return
}
if err = conn.Send("ZADD", key, combineHd(detail.UpdateTime, total), bs); err != nil {
log.Error("conn.Send(ZADD, %s, %s) error(%v)", key, string(bs), err)
return
}
count++
}
if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.redisExpire, err)
return
}
count++
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
for i := 0; i < count; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
// DetailCache get help detail to cache.
func (d *Dao) DetailCache(c context.Context, qTypeID string, keyFlag, pn, ps int) (res []*faq.Detail, count int, err error) {
conn := d.redis.Get(c)
defer conn.Close()
key := keyHd(qTypeID, keyFlag, pn, ps)
values, err := redis.Values(conn.Do("ZREVRANGE", key, 0, -1, "WITHSCORES"))
if err != nil {
log.Error("conn.Do(ZREVRANGE, %s) error(%v)", key, err)
return
}
if len(values) == 0 {
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush() err(%v)", err)
return
}
var num int64
for len(values) > 0 {
bs := []byte{}
if values, err = redis.Scan(values, &bs, &num); err != nil {
log.Error("redis.Scan(%v) error(%v)", values, err)
return
}
detail := &faq.Detail{}
if err = json.Unmarshal(bs, detail); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", bs, err)
return
}
res = append(res, detail)
}
count = fromHd(num)
return
}
func fromHd(i int64) int {
return int(i & 0xffff)
}
func combineHd(create xtime.Time, count int) int64 {
return create.Time().Unix()<<16 | int64(count)
}
// Detail fn
func (d *Dao) Detail(c context.Context, qTypeID string, keyFlag, pn, ps int) (data []*faq.Detail, total int, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("questionTypeId", qTypeID)
params.Set("keyFlag", strconv.Itoa(keyFlag))
params.Set("keyWords", "")
params.Set("pageNo", strconv.Itoa(pn))
params.Set("pageSize", strconv.Itoa(ps))
params.Set("robotFlag", strconv.Itoa(_notRobot))
var res struct {
Code string `json:"retCode"`
Data []*faq.Detail `json:"items"`
Total int `json:"totalCount"`
}
if err = d.client.Get(c, d.searchURL, ip, params, &res); err != nil {
log.Error("Detail d.searchURL url(%s)|(%+v)", d.searchURL+"?"+params.Encode(), res)
err = ecode.HelpDetailError
return
}
log.Info("Detail d.searchURL url(%s)|(%+v)", d.searchURL+"?"+params.Encode(), res.Code)
if res.Code != _rsOk {
log.Error("Detail d.searchURL url(%s)|(%+v)", d.searchURL+"?"+params.Encode(), res.Code)
err = ecode.HelpDetailError
return
}
total = res.Total
data = res.Data
return
}

View File

@@ -0,0 +1,193 @@
package faq
import (
"context"
"encoding/json"
"errors"
"flag"
"go-common/app/interface/main/creative/conf"
"go-common/app/interface/main/creative/model/faq"
"go-common/library/cache/redis"
"go-common/library/ecode"
"os"
"reflect"
"strings"
"testing"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
gock "gopkg.in/h2non/gock.v1"
)
var (
d *Dao
errConnClosed = errors.New("redigo: connection closed")
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.creative")
flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
flag.Set("tree_id", "2305")
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/creative.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
d.client.SetTransport(gock.DefaultTransport)
return r
}
func TestFaqDetail(t *testing.T) {
var (
qTypeID string
keyFlag, pn, ps int
c = context.TODO()
err error
res struct {
Code string `json:"retCode"`
Data []*faq.Detail `json:"items"`
Total int `json:"totalCount"`
}
data []*faq.Detail
total int
)
convey.Convey("1", t, func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.searchURL).Reply(-502)
data, total, err = d.Detail(c, qTypeID, keyFlag, pn, ps)
ctx.Convey("1", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(err, convey.ShouldEqual, ecode.HelpDetailError)
ctx.So(total, convey.ShouldBeZeroValue)
})
})
convey.Convey("2", t, func(ctx convey.C) {
res.Code = "000000"
res.Data = make([]*faq.Detail, 0)
res.Total = 100
js, _ := json.Marshal(res)
defer gock.OffAll()
httpMock("GET", d.searchURL).Reply(200).JSON(string(js))
data, total, err = d.Detail(c, qTypeID, keyFlag, pn, ps)
ctx.Convey("2", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
ctx.So(total, convey.ShouldEqual, 100)
})
})
convey.Convey("2", t, func(ctx convey.C) {
res.Code = "0000001"
res.Data = make([]*faq.Detail, 0)
res.Total = 100
js, _ := json.Marshal(res)
defer gock.OffAll()
httpMock("GET", d.searchURL).Reply(200).JSON(string(js))
data, total, err = d.Detail(c, qTypeID, keyFlag, pn, ps)
ctx.Convey("2", func(ctx convey.C) {
ctx.So(data, convey.ShouldBeNil)
ctx.So(err, convey.ShouldEqual, ecode.HelpDetailError)
ctx.So(total, convey.ShouldEqual, 0)
})
})
}
func TestFaqDetailCache(t *testing.T) {
var (
qTypeID = "faq_id"
keyFlag, pn, ps = int(1), int(1), int(10)
c = context.TODO()
err error
data []*faq.Detail
total int
)
convey.Convey("1", t, func(ctx convey.C) {
connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.redis), "Get", func(_ *redis.Pool, _ context.Context) redis.Conn {
return redis.MockWith(errConnClosed)
})
defer connGuard.Unpatch()
data, total, err = d.DetailCache(c, qTypeID, keyFlag, pn, ps)
ctx.Convey("1", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(total, convey.ShouldBeZeroValue)
ctx.So(data, convey.ShouldBeNil)
})
})
convey.Convey("2", t, func(ctx convey.C) {
connGuard := monkey.Patch(redis.Values, func(_ interface{}, _ error) ([]interface{}, error) {
detail := &faq.Detail{}
data, _ := json.Marshal(detail)
res := []interface{}{
data,
}
return res, nil
})
defer connGuard.Unpatch()
data, total, err = d.DetailCache(c, qTypeID, keyFlag, pn, ps)
ctx.Convey("2", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(total, convey.ShouldBeZeroValue)
ctx.So(data, convey.ShouldBeNil)
})
})
}
func TestFaqSetDetailCache(t *testing.T) {
var (
qTypeID = "faq_id"
keyFlag, pn, ps = int(1), int(1), int(10)
c = context.TODO()
err error
data = make([]*faq.Detail, 0)
total int
)
convey.Convey("1", t, func(ctx convey.C) {
connGuard := monkey.PatchInstanceMethod(reflect.TypeOf(d.redis), "Get", func(_ *redis.Pool, _ context.Context) redis.Conn {
return redis.MockWith(errConnClosed)
})
defer connGuard.Unpatch()
err = d.SetDetailCache(c, qTypeID, keyFlag, pn, ps, total, data)
ctx.Convey("1", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
convey.Convey("2", t, func(ctx convey.C) {
data = append(data, &faq.Detail{})
err = d.SetDetailCache(c, qTypeID, keyFlag, pn, ps, total, data)
ctx.Convey("2", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
// convey.Convey("2", t, func(ctx convey.C) {
// connGuard := monkey.Patch(redis.Values, func(_ interface{}, _ error) ([]interface{}, error) {
// detail := &faq.Detail{}
// data, _ := json.Marshal(detail)
// res := []interface{}{
// data,
// }
// return res, nil
// })
// defer connGuard.Unpatch()
// data, total, err = d.DetailCache(c, qTypeID, keyFlag, pn, ps)
// ctx.Convey("2", func(ctx convey.C) {
// ctx.So(err, convey.ShouldNotBeNil)
// ctx.So(total, convey.ShouldBeZeroValue)
// ctx.So(data, convey.ShouldBeNil)
// })
// })
}