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,58 @@
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",
"merak_test.go",
"shelve_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/job/main/tv/conf:go_default_library",
"//library/database/sql:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"merak.go",
"shelve.go",
],
importpath = "go-common/app/job/main/tv/dao/cms",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/tv/conf:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/xstr:go_default_library",
"//vendor/github.com/pkg/errors: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,24 @@
package cms
import (
"go-common/app/job/main/tv/conf"
"go-common/library/database/sql"
httpx "go-common/library/net/http/blademaster"
)
// Dao dao.
type Dao struct {
conf *conf.Config
DB *sql.DB
client *httpx.Client
}
// New create a instance of Dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
conf: c,
DB: sql.NewMySQL(c.Mysql),
client: httpx.NewClient(conf.Conf.HTTPClient),
}
return
}

View File

@@ -0,0 +1,37 @@
package cms
import (
"flag"
"os"
"testing"
"go-common/app/job/main/tv/conf"
"gopkg.in/h2non/gock.v1"
)
var d *Dao
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.web-svr.tv-job")
flag.Set("conf_token", "ab3e9801a77c076b997de0ac5cb21775")
flag.Set("tree_id", "15260")
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/tv-job-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
d.client.SetTransport(gock.DefaultTransport)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,84 @@
package cms
import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"net/http"
"sort"
"strings"
"go-common/library/log"
"github.com/pkg/errors"
)
// MerakNotify send notify
func (d *Dao) MerakNotify(ctx context.Context, title, content string) (err error) {
var (
cfg = d.conf.Cfg.Merak
sign string
req *http.Request
body []byte
)
params := map[string]string{
"Action": "CreateWechatMessage",
"PublicKey": cfg.Key,
"UserName": strings.Join(cfg.Names, ","),
"Title": title,
"Content": content,
"TreeId": "",
}
if sign, err = MerakSign(params, cfg.Secret); err != nil {
log.Error("MerakNotify Failed to sign params: %+v: %+v", params, err)
return err
}
params["Signature"] = sign
if body, err = json.Marshal(params); err != nil {
log.Error("MerakNotify Json %v, Err %v", params, err)
return
}
if req, err = http.NewRequest(http.MethodPost, cfg.Host, bytes.NewReader(body)); err != nil {
log.Error("MerakNotify NewRequest Err %v, Host %v", err, cfg.Host)
return
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := struct {
Action string `json:"Action"`
RetCode int `json:"RetCode"`
Data []string `json:"Data"`
}{}
if err = d.client.Do(ctx, req, &res); err != nil {
return
}
if res.RetCode != 0 {
err = errors.Errorf("Merak error: %d", res.RetCode)
log.Error("Failed to send notify by merak with params: %+v: %+v", string(body), err)
return
}
return
}
// MerakSign is used to sign for merak wechat msg
func MerakSign(params map[string]string, secret string) (string, error) {
keys := make([]string, 0, len(params))
for k := range params {
keys = append(keys, k)
}
sort.Strings(keys)
buf := bytes.Buffer{}
for _, k := range keys {
buf.WriteString(k + params[k])
}
h := sha1.New()
if _, err := h.Write(buf.Bytes()); err != nil {
return "", errors.WithStack(err)
}
if _, err := h.Write([]byte(secret)); err != nil {
return "", errors.WithStack(err)
}
sum := h.Sum(nil)
return hex.EncodeToString(sum), nil
}

View File

@@ -0,0 +1,50 @@
package cms
import (
"context"
"fmt"
"strings"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoSendWechat(t *testing.T) {
convey.Convey("SendWechat", t, func(ctx convey.C) {
var (
content = "测试内容"
title = "测试标题"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.MerakNotify(context.Background(), content, title)
fmt.Println(err)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoGetMerakSign(t *testing.T) {
convey.Convey("sign", t, func(ctx convey.C) {
var (
params = map[string]string{
"Action": "CreateWechatMessage",
"PublicKey": "1",
"UserName": strings.Join([]string{"user1", "user2"}, ","),
"Title": "测试标题",
"Content": "测试内容",
"TreeId": "",
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1, err := MerakSign(params, "secret")
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldEqual, "59cd4e74b225a7d326ee7d6c89bf27cf2f6015dc")
})
})
})
}

View File

@@ -0,0 +1,117 @@
package cms
import (
"context"
"fmt"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_validSns = "SELECT DISTINCT a.id FROM tv_ep_season a LEFT JOIN tv_content b ON a.id = b.season_id " +
"WHERE a.is_deleted = 0 AND a.`check` = ? AND b.is_deleted = 0 AND b.valid = ? AND b.state = ?"
_allPassedSns = "SELECT id, valid FROM tv_ep_season WHERE is_deleted = 0 AND `check` = 1"
_actSns = "UPDATE tv_ep_season SET valid = ? WHERE id IN (%s)"
_offArcs = "SELECT aid FROM ugc_archive WHERE aid IN (%s) AND valid = 0 AND deleted = 0 AND result = 1 "
_reshelfArcs = "UPDATE ugc_archive SET valid = 1 WHERE aid IN (%s)"
_cmsOnline = 1
_cmsOffline = 0
_epPassed = 3
)
// ValidSns gets all the seasons that should be on the shelves, which includes free and audited episodes.
func (d *Dao) ValidSns(ctx context.Context, onlyfree bool) (res map[int64]int, err error) {
var (
rows *sql.Rows
validSql = _validSns
)
res = make(map[int64]int)
if onlyfree {
validSql = validSql + " AND b.pay_status = 2" // free episode
}
if rows, err = d.DB.Query(ctx, validSql, _cmsOnline, _cmsOnline, _epPassed); err != nil {
log.Error("d.ValidSns.Query: %s error(%v)", validSql, err)
return
}
defer rows.Close()
for rows.Next() {
var sid int64
if err = rows.Scan(&sid); err != nil {
log.Error("ValidSns row.Scan() error(%v)", err)
return
}
res[sid] = 1
}
if err = rows.Err(); err != nil {
log.Error("d.PgcCont.Query error(%v)", err)
}
return
}
// ShelveOp gets the status of all audited seasons on and off shelves, and compare the results with the "ValidSns" method above to determine which episodes need to be on or off shelves.
func (d *Dao) ShelveOp(ctx context.Context, validSns map[int64]int) (onIDs, offIDs []int64, err error) {
var rows *sql.Rows
if rows, err = d.DB.Query(ctx, _allPassedSns); err != nil {
log.Error("d.ShelveOp.Query: %s error(%v)", _allPassedSns, err)
return
}
defer rows.Close()
for rows.Next() {
var sid, valid int64
if err = rows.Scan(&sid, &valid); err != nil {
log.Error("ValidSns row.Scan() error(%v)", err)
return
}
_, ok := validSns[sid]
if ok && valid == 0 {
onIDs = append(onIDs, sid)
}
if !ok && valid == 1 {
offIDs = append(offIDs, sid)
}
}
if err = rows.Err(); err != nil {
log.Error("d.PgcCont.Query error(%v)", err)
}
return
}
// ActOps carries out the action on the season which need to be on/off shelves
func (d *Dao) ActOps(ctx context.Context, ids []int64, on bool) (err error) {
var action int
if on {
action = _cmsOnline
} else {
action = _cmsOffline
}
if _, err = d.DB.Exec(ctx, fmt.Sprintf(_actSns, xstr.JoinInts(ids)), action); err != nil {
log.Error("ActOps, Ids %v, Err %v", ids, err)
}
return
}
// OffArcs takes the archives that passed but cms invalid archives
func (d *Dao) OffArcs(ctx context.Context, aids []int64) (offAids []int64, err error) {
var rows *sql.Rows
if rows, err = d.DB.Query(ctx, fmt.Sprintf(_offArcs, xstr.JoinInts(aids))); err != nil {
return
}
defer rows.Close()
for rows.Next() {
var aid int64
if err = rows.Scan(&aid); err != nil {
return
}
offAids = append(offAids, aid)
}
err = rows.Err()
return
}
// ReshelfArcs re-put the arcs onshelf ( CMS valid )
func (d *Dao) ReshelfArcs(ctx context.Context, aids []int64) (err error) {
_, err = d.DB.Exec(ctx, fmt.Sprintf(_reshelfArcs, xstr.JoinInts(aids)))
return
}

View File

@@ -0,0 +1,117 @@
package cms
import (
"context"
"fmt"
"testing"
"go-common/library/database/sql"
"github.com/smartystreets/goconvey/convey"
)
func TestCmsValidSns(t *testing.T) {
var (
ctx = context.Background()
)
convey.Convey("ValidSns", t, func(cx convey.C) {
res, err := d.ValidSns(ctx, false)
cx.Convey("Consider Audited, Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
fmt.Println(len(res))
})
res, err = d.ValidSns(ctx, true)
cx.Convey("Consider Audited and Free, Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
fmt.Println(len(res))
})
})
}
func TestCmsShelveOp(t *testing.T) {
var (
ctx = context.Background()
validSns, _ = d.ValidSns(ctx, true)
)
convey.Convey("ShelveOp", t, func(cx convey.C) {
onIDs, offIDs, err := d.ShelveOp(ctx, validSns)
cx.Convey("Then err should be nil.onIDs,offIDs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(offIDs, convey.ShouldNotBeNil)
ctx.So(onIDs, convey.ShouldNotBeNil)
fmt.Println(offIDs)
fmt.Println(onIDs)
})
})
}
func TestCmsActOps(t *testing.T) {
var (
ctx = context.Background()
sid int64
)
d.DB.QueryRow(ctx, "SELECT id FROM tv_ep_season WHERE valid = 1 and is_deleted = 0 AND `check` = 1 LIMIT 1").Scan(&sid)
convey.Convey("ActOps", t, func(cx convey.C) {
err := d.ActOps(ctx, []int64{sid}, false)
cx.Convey("Action 0 Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
err = d.ActOps(ctx, []int64{sid}, true)
cx.Convey("Action 1 Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
fmt.Println(sid)
})
}
func TestCmsOffArcs(t *testing.T) {
var (
ctx = context.Background()
aid int64
)
convey.Convey("OffArcs", t, func(cx convey.C) {
cx.Convey("Then err should be nil.offAids should not be nil.", func(cx convey.C) {
if err := d.DB.QueryRow(ctx, "select aid from ugc_archive where deleted = 0 and valid = 0 and result = 1 limit 1").Scan(&aid); err != nil {
offAids, err := d.OffArcs(context.Background(), []int64{1, 2, 3})
cx.So(err, convey.ShouldBeNil)
cx.So(offAids, convey.ShouldBeNil)
} else {
fmt.Println("Have Aid ", aid)
offAids, err := d.OffArcs(context.Background(), []int64{1, 2, 3, aid})
cx.So(err, convey.ShouldBeNil)
cx.So(offAids, convey.ShouldNotBeNil)
}
})
cx.Convey("Arg Error", func(cx convey.C) {
_, err := d.OffArcs(context.Background(), []int64{})
cx.So(err, convey.ShouldNotBeNil)
})
cx.Convey("DB Error", func(cx convey.C) {
d.DB.Close()
_, err := d.OffArcs(context.Background(), []int64{1, 2, 3})
cx.So(err, convey.ShouldNotBeNil)
d.DB = sql.NewMySQL(d.conf.Mysql)
})
})
}
func TestCmsReshelfArcs(t *testing.T) {
convey.Convey("OffArcs", t, func(cx convey.C) {
cx.Convey("Then err should be nil.offAids should not be nil.", func(cx convey.C) {
err := d.ReshelfArcs(context.Background(), []int64{1, 2, 3})
cx.So(err, convey.ShouldBeNil)
})
cx.Convey("Arg Error", func(cx convey.C) {
err := d.ReshelfArcs(context.Background(), []int64{})
cx.So(err, convey.ShouldNotBeNil)
})
cx.Convey("DB Error", func(cx convey.C) {
d.DB.Close()
err := d.ReshelfArcs(context.Background(), []int64{1, 2, 3})
cx.So(err, convey.ShouldNotBeNil)
d.DB = sql.NewMySQL(d.conf.Mysql)
})
})
}