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,61 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"audit_test.go",
"card_test.go",
"dao_test.go",
"relate_test.go",
"sidebar_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/resource/conf: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 = [
"audit.go",
"card.go",
"dao.go",
"relate.go",
"sidebar.go",
],
importpath = "go-common/app/service/main/resource/dao/show",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/resource/conf:go_default_library",
"//app/service/main/resource/model:go_default_library",
"//library/database/sql:go_default_library",
"//library/log: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,35 @@
package show
import (
"context"
"go-common/library/log"
)
var (
_auditSQL = "SELECT mobi_app,build FROM audit"
)
// Audit get audit.
func (d *Dao) Audit(c context.Context) (res map[string][]int, err error) {
res = make(map[string][]int)
rows, err := d.db.Query(c, _auditSQL)
if err != nil {
log.Error("d.audit error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var (
mobiApp string
build int
)
if err = rows.Scan(&mobiApp, &build); err != nil {
log.Error("d.audit rows.Scan error(%v)", err)
res = nil
return
}
res[mobiApp] = append(res[mobiApp], build)
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,36 @@
package show
import (
"context"
"fmt"
"reflect"
"testing"
xsql "go-common/library/database/sql"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
// TestDaoAudit test audit.
func TestDaoAudit(t *testing.T) {
convey.Convey("Audit", t, func(ctx convey.C) {
ctx.Convey("When everything is correct", func(ctx convey.C) {
res, err := d.Audit(context.Background())
ctx.Convey("Error should be nil, res should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
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.Audit(context.Background())
ctx.Convey("Error should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,76 @@
package show
import (
"context"
"strconv"
"time"
"go-common/app/service/main/resource/model"
"go-common/library/log"
)
var (
_appPosRecSQL = "SELECT r.id,r.tab,r.resource_id,r.type,r.title,r.cover,r.re_type,r.re_value,r.plat_ver,r.desc,r.tag_id FROM app_pos_rec AS r WHERE r.stime<? AND r.etime>? AND r.state=1 AND r.resource_id=3 ORDER BY r.weight ASC"
_appContentRSQL = "SELECT c.id,c.module,c.rec_id,c.ctype,c.cvalue,c.ctitle,c.tag_id FROM app_content AS c, app_pos_rec AS r WHERE c.rec_id=r.id AND r.state=1 AND r.stime<? AND r.etime>? AND c.module=1"
)
// PosRecs get pos resrouce
func (d *Dao) PosRecs(c context.Context, now time.Time) (res map[int8][]*model.Card, err error) {
res = map[int8][]*model.Card{}
rows, err := d.db.Query(c, _appPosRecSQL, now, now)
if err != nil {
log.Error("d.PosRecs error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
card := &model.Card{}
if err = rows.Scan(&card.ID, &card.Tab, &card.RegionID, &card.Type, &card.Title, &card.Cover, &card.Rtype, &card.Rvalue, &card.PlatVer, &card.Desc, &card.TagID); err != nil {
log.Error("d.PosRecs rows.Scan error(%v)", err)
res = nil
return
}
for _, limit := range card.CardPlatChange() {
tmpc := &model.Card{}
*tmpc = *card
tmpc.Plat = limit.Plat
tmpc.Build = limit.Build
tmpc.Condition = limit.Condition
tmpc.PlatVer = ""
tmpc.TypeStr = model.GotoDaily
tmpc.Goto = model.GotoDaily
tmpc.Param = tmpc.Rvalue
tmpc.URI = model.FillURI(tmpc.Goto, tmpc.Param)
res[tmpc.Plat] = append(res[tmpc.Plat], tmpc)
}
}
err = rows.Err()
return
}
// RecContents get resource contents
func (d *Dao) RecContents(c context.Context, now time.Time) (res map[int][]*model.Content, aids map[int][]int64, err error) {
res = map[int][]*model.Content{}
aids = map[int][]int64{}
rows, err := d.db.Query(c, _appContentRSQL, now, now)
if err != nil {
log.Error("d.RecContents error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
card := &model.Content{}
if err = rows.Scan(&card.ID, &card.Module, &card.RecID, &card.Type, &card.Value, &card.Title, &card.TagID); err != nil {
log.Error("d.RecContents rows.Scan error(%v)", err)
res = nil
return
}
res[card.RecID] = append(res[card.RecID], card)
if card.Type == model.CardGotoAv {
aidInt, _ := strconv.ParseInt(card.Value, 10, 64)
aids[card.RecID] = append(aids[card.RecID], aidInt)
}
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,59 @@
package show
import (
"context"
"fmt"
"reflect"
"testing"
"time"
xsql "go-common/library/database/sql"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoPosRecs(t *testing.T) {
convey.Convey("PosRecs", t, func(ctx convey.C) {
ctx.Convey("When everything is correct", func(ctx convey.C) {
res, err := d.PosRecs(context.Background(), time.Now())
ctx.Convey("Error should be nil, res should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
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.PosRecs(context.Background(), time.Now())
ctx.Convey("Error should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRecContents(t *testing.T) {
convey.Convey("RecContents", t, func(ctx convey.C) {
ctx.Convey("When everything is correct", func() {
res, aids, err := d.RecContents(context.Background(), time.Now())
ctx.Convey("Error should be nil, res, aids should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
ctx.So(aids, convey.ShouldNotBeNil)
})
})
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.RecContents(context.Background(), time.Now())
ctx.Convey("Error should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,33 @@
package show
import (
"context"
"go-common/app/service/main/resource/conf"
xsql "go-common/library/database/sql"
)
// Dao is resource dao.
type Dao struct {
db *xsql.DB
c *conf.Config
}
// New init mysql db
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: xsql.NewMySQL(c.DB.Show),
}
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,48 @@
package show
import (
"context"
"flag"
"os"
"testing"
"go-common/app/service/main/resource/conf"
"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 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,47 @@
package show
import (
"context"
"encoding/json"
"time"
"go-common/app/service/main/resource/model"
"go-common/library/log"
)
const (
_relateSQL = "SELECT `id`,`param`,`title`,`rec_reason`,`position`,`plat_ver`,`stime`,`etime`,`pgc_ids` FROM app_rcmd_pos WHERE `state`=1 AND `goto`='special' AND `pgc_relation`=1 AND `stime`<? AND `etime`>?"
)
// Relate get all relate rec.
func (d *Dao) Relate(c context.Context, now time.Time) (relates []*model.Relate, err error) {
rows, err := d.db.Query(c, _relateSQL, now, now)
if err != nil {
log.Error("d.Relate.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
r := &model.Relate{}
var verStr string
if err = rows.Scan(&r.ID, &r.Param, &r.Title, &r.RecReason, &r.Position, &verStr, &r.STime, &r.ETime, &r.PgcIDs); err != nil {
log.Error("d.Relate.rows.Scan error(%v)", err)
return
}
if verStr != "" {
var verStruct []*model.Version
if err = json.Unmarshal([]byte(verStr), &verStruct); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", verStr, err)
return
}
vm := make(map[int8][]*model.Version, len(verStruct))
for _, v := range verStruct {
vm[v.Plat] = append(vm[v.Plat], v)
}
r.Versions = vm
}
relates = append(relates, r)
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,20 @@
package show
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoRelate(t *testing.T) {
convey.Convey("Relate", t, func(ctx convey.C) {
ctx.Convey("When everything is correct", func(ctx convey.C) {
_, err := d.Relate(context.Background(), time.Now())
ctx.Convey("Error should be nil, res should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,43 @@
package show
import (
"context"
"time"
"go-common/app/service/main/resource/model"
"go-common/library/log"
)
const (
_selSideSQL = `SELECT s.id,s.plat,s.module,s.name,s.logo,s.logo_white,s.param,s.rank,l.build,l.conditions,s.tip,s.need_login,s.white_url,s.logo_selected,s.tab_id,s.red_dot_url,lang.name FROM
sidebar AS s,sidebar_limit AS l,language AS lang WHERE s.state=1 AND s.id=l.s_id AND lang.id=s.lang_id AND s.online_time<? ORDER BY s.rank DESC,l.id ASC`
)
// SideBar get side bar.
func (d *Dao) SideBar(ctx context.Context, now time.Time) (ss []*model.SideBar, limits map[int64][]*model.SideBarLimit, err error) {
rows, err := d.db.Query(ctx, _selSideSQL, now)
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
limits = make(map[int64][]*model.SideBarLimit)
for rows.Next() {
s := &model.SideBar{}
if err = rows.Scan(&s.ID, &s.Plat, &s.Module, &s.Name, &s.Logo, &s.LogoWhite, &s.Param, &s.Rank, &s.Build, &s.Conditions, &s.Tip, &s.NeedLogin, &s.WhiteURL, &s.LogoSelected, &s.TabID, &s.Red, &s.Language); err != nil {
log.Error("row.Scan error(%v)", err)
return
}
if _, ok := limits[s.ID]; !ok {
ss = append(ss, s)
}
limit := &model.SideBarLimit{
ID: s.ID,
Build: s.Build,
Condition: s.Conditions,
}
limits[s.ID] = append(limits[s.ID], limit)
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,36 @@
package show
import (
"context"
"fmt"
"reflect"
"testing"
"time"
xsql "go-common/library/database/sql"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
// Test_SideBar test dao side bar
func TestDaoSideBar(t *testing.T) {
convey.Convey("SidebBar", t, func(ctx convey.C) {
ctx.Convey("When everyting is correct", func(ctx convey.C) {
_, _, err := d.SideBar(context.Background(), time.Now())
ctx.Convey("Error should be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
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.SideBar(context.Background(), time.Now())
ctx.Convey("Error should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}