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,69 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"addit_test.go",
"archive_test.go",
"config_test.go",
"dao_test.go",
"first_pass_test.go",
"oper_test.go",
"staff_test.go",
"type_test.go",
"video_test.go",
"video_track_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/archive/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"addit.go",
"archive.go",
"config.go",
"dao.go",
"first_pass.go",
"oper.go",
"staff.go",
"type.go",
"video.go",
"video_track.go",
],
importpath = "go-common/app/job/main/archive/dao/archive",
tags = ["automanaged"],
deps = [
"//app/job/main/archive/conf:go_default_library",
"//app/job/main/archive/model/archive:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/xstr: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,28 @@
package archive
import (
"context"
"go-common/app/job/main/archive/model/archive"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_additSQL = "SELECT id,aid,source,redirect_url,mission_id,up_from,order_id,dynamic FROM archive_addit WHERE aid=?"
)
// Addit get archive addit.
func (d *Dao) Addit(c context.Context, aid int64) (addit *archive.Addit, err error) {
row := d.db.QueryRow(c, _additSQL, aid)
addit = &archive.Addit{}
if err = row.Scan(&addit.ID, &addit.Aid, &addit.Source, &addit.RedirectURL, &addit.MissionID, &addit.UpFrom, &addit.OrderID, &addit.Dynamic); err != nil {
if err == sql.ErrNoRows {
addit = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}

View File

@@ -0,0 +1,16 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Addit(t *testing.T) {
Convey("Addit", t, func() {
addit, err := d.Addit(context.TODO(), 1)
So(err, ShouldBeNil)
Println(addit)
})
}

View File

@@ -0,0 +1,32 @@
package archive
import (
"context"
"database/sql"
"go-common/app/job/main/archive/model/archive"
"go-common/library/log"
)
const (
_arcSQL = "SELECT id,mid,typeid,copyright,author,title,cover,content,tag,duration,round,attribute,access,state,reject_reason,pubtime,ctime,mtime,forward FROM archive WHERE id=?"
)
// Archive get a archive by avid.
func (d *Dao) Archive(c context.Context, aid int64) (a *archive.Archive, err error) {
var reason sql.NullString
row := d.db.QueryRow(c, _arcSQL, aid)
a = &archive.Archive{}
if err = row.Scan(&a.ID, &a.Mid, &a.TypeID, &a.Copyright, &a.Author, &a.Title, &a.Cover, &a.Content, &a.Tag, &a.Duration,
&a.Round, &a.Attribute, &a.Access, &a.State, &reason, &a.PubTime, &a.CTime, &a.MTime, &a.Forward); err != nil {
if err == sql.ErrNoRows {
a = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
return
}
a.Reason = reason.String
return
}

View File

@@ -0,0 +1,16 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Archive(t *testing.T) {
Convey("Archive", t, func() {
archive, err := d.Archive(context.TODO(), 1)
So(err, ShouldBeNil)
Println(archive)
})
}

View File

@@ -0,0 +1,41 @@
package archive
import (
"context"
"database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_confSQL = "SELECT value FROM archive_config WHERE state=0 AND name=?"
_confForAuditType = "wait_audit_arctype"
)
// AuditTypesConf get audit conf
func (d *Dao) AuditTypesConf(c context.Context) (atps map[int16]struct{}, err error) {
row := d.db.QueryRow(c, _confSQL, _confForAuditType)
var (
value string
typeIDs []int64
)
if err = row.Scan(&value); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
return
}
typeIDs, err = xstr.SplitInts(value)
if err != nil {
log.Error("archive_config value(%s) xstr.SplitInts error(%v)", value, err)
return
}
atps = map[int16]struct{}{}
for _, typeid := range typeIDs {
atps[int16(typeid)] = struct{}{}
}
return
}

View File

@@ -0,0 +1,16 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_AuditTypesConf(t *testing.T) {
Convey("AuditTypesConf", t, func() {
configs, err := d.AuditTypesConf(context.TODO())
So(err, ShouldBeNil)
Println(configs)
})
}

View File

@@ -0,0 +1,28 @@
package archive
import (
"context"
"go-common/app/job/main/archive/conf"
"go-common/library/database/sql"
)
// Dao is redis dao.
type Dao struct {
c *conf.Config
db *sql.DB
}
// New is new redis dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.DB.Archive),
}
return d
}
// BeginTran begin transcation.
func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
return d.db.Begin(c)
}

View File

@@ -0,0 +1,34 @@
package archive
import (
"flag"
"os"
"testing"
"go-common/app/job/main/archive/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.archive-job")
flag.Set("conf_token", "MmQwIqWAyIaIu8CKb7MKcNSYlGGhoudN")
flag.Set("tree_id", "2301")
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")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,25 @@
package archive
import (
"context"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_slFirstPassByAID = "SELECT `id` FROM `archive_first_pass` WHERE `aid`=? LIMIT 1"
)
// GetFirstPassByAID is
func (d *Dao) GetFirstPassByAID(c context.Context, aid int64) (id int64, err error) {
row := d.db.QueryRow(c, _slFirstPassByAID, aid)
if err = row.Scan(&id); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("GetFirstPassByAID error(%v) aid(%d)", err, aid)
}
}
return
}

View File

@@ -0,0 +1,21 @@
package archive
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestGetFirstPassByAID(t *testing.T) {
var (
c = context.TODO()
aid = int64(4052032)
)
convey.Convey("GetFirstPassByAID", t, func(ctx convey.C) {
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
_, err := d.GetFirstPassByAID(c, aid)
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,26 @@
package archive
import (
"context"
"database/sql"
"go-common/app/job/main/archive/model/archive"
"go-common/library/log"
)
const (
_arcPassedOperSQL = "SELECT id FROM archive_oper WHERE aid=? AND state>=? LIMIT 1"
)
// PassedOper check archive passed
func (d *Dao) PassedOper(c context.Context, aid int64) (id int64, err error) {
row := d.db.QueryRow(c, _arcPassedOperSQL, aid, archive.StateOpen)
if err = row.Scan(&id); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}

View File

@@ -0,0 +1,15 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_PassedOper(t *testing.T) {
Convey("PassedOper", t, func() {
_, err := d.PassedOper(context.TODO(), 1)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,32 @@
package archive
import (
"context"
"go-common/app/job/main/archive/model/archive"
"go-common/library/log"
)
const (
_staffSQL = "SELECT aid,staff_mid,staff_title,ctime,mtime FROM archive_staff WHERE aid=? AND state=1"
)
// Staff get Staff by aid.
func (d *Dao) Staff(c context.Context, aid int64) (res []*archive.Staff, err error) {
rows, err := d.db.Query(c, _staffSQL, aid)
if err != nil {
log.Error("d.db.Query(%d) error(%v)", aid, err)
return
}
defer rows.Close()
for rows.Next() {
s := &archive.Staff{}
if err = rows.Scan(&s.Aid, &s.Mid, &s.Title, &s.Ctime, &s.Mtime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
res = append(res, s)
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,21 @@
package archive
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestStaff(t *testing.T) {
var (
c = context.TODO()
aid = int64(4052032)
)
convey.Convey("Staff", t, func(ctx convey.C) {
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
_, err := d.Staff(c, aid)
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,31 @@
package archive
import (
"context"
"go-common/library/log"
)
const (
_tpsSQL = "SELECT id,pid FROM archive_type WHERE pid !=0"
)
// TypeMapping is second types opposite first types.
func (d *Dao) TypeMapping(c context.Context) (rmap map[int16]int16, err error) {
rows, err := d.db.Query(c, _tpsSQL)
if err != nil {
log.Error("d.tpsStmt.Query error(%v)", err)
return
}
defer rows.Close()
rmap = map[int16]int16{}
for rows.Next() {
var id, pid int16
if err = rows.Scan(&id, &pid); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
rmap[id] = pid
}
return
}

View File

@@ -0,0 +1,15 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_TypeMapping(t *testing.T) {
Convey("TypeMapping", t, func() {
_, err := d.TypeMapping(context.TODO())
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,74 @@
package archive
import (
"context"
"go-common/app/job/main/archive/model/archive"
"go-common/library/log"
)
const (
_videosSQL = `SELECT id,filename,cid,aid,eptitle,description,src_type,duration,filesize,resolutions,playurl,failinfo,
index_order,attribute,xcode_state,status,ctime,mtime FROM archive_video WHERE aid=? ORDER BY index_order`
_videos2SQL = `SELECT vr.id,v.filename,vr.cid,vr.aid,vr.title,vr.description,v.src_type,v.duration,v.filesize,v.resolutions,
v.playurl,v.failcode,vr.index_order,v.attribute,v.xcode_state,v.status,vr.state,vr.ctime,vr.mtime,v.dimensions FROM archive_video_relation AS vr JOIN video AS v ON vr.cid=v.id WHERE vr.aid=? ORDER BY vr.index_order`
_aidsSQL = "SELECT aid FROM archive_video_relation WHERE cid=? AND state=0"
)
// Videos get videos by aid
func (d *Dao) Videos(c context.Context, aid int64) (vs []*archive.Video, err error) {
rows, err := d.db.Query(c, _videosSQL, aid)
if err != nil {
log.Error("d.db.Query(%s, %d) error(%v)", _videosSQL, aid, err)
return
}
defer rows.Close()
for rows.Next() {
v := &archive.Video{}
if err = rows.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
&v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &v.Status, &v.CTime, &v.MTime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
vs = append(vs, v)
}
return
}
// Videos2 get videos by 2 table em.......
func (d *Dao) Videos2(c context.Context, aid int64) (vs []*archive.Video, err error) {
rows, err := d.db.Query(c, _videos2SQL, aid)
if err != nil {
log.Error("d.db.Query(%s, %d) error(%v)", _videos2SQL, aid, err)
return
}
defer rows.Close()
for rows.Next() {
v := &archive.Video{}
if err = rows.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
&v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &v.Status, &v.State, &v.CTime, &v.MTime, &v.Dimensions); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
vs = append(vs, v)
}
return
}
// Aids get aids by cid
func (d *Dao) Aids(c context.Context, cid int64) (aids []int64, err error) {
rows, err := d.db.Query(c, _aidsSQL, cid)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
var aid int64
if err = rows.Scan(&aid); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
aids = append(aids, aid)
}
return
}

View File

@@ -0,0 +1,25 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Videos(t *testing.T) {
Convey("Videos", t, func() {
_, err := d.Videos(context.TODO(), 1)
So(err, ShouldBeNil)
})
}
func Test_Videos2(t *testing.T) {
Convey("Videos2", t, func() {
vs, err := d.Videos2(context.TODO(), 10098500)
So(err, ShouldBeNil)
for _, v := range vs {
Printf("%+v", v)
}
})
}

View File

@@ -0,0 +1,22 @@
package archive
import (
"context"
"go-common/library/log"
)
const (
_inVideoHis = "INSERT INTO archive_video_track(aid,filename,status,xcode_state,remark,ctime,mtime) VALUES(?,?,?,?,?,?,?)"
)
// InVideoHis insert video track history
func (d *Dao) InVideoHis(c context.Context, aid int64, filename string, status int16, xcodeState int8, remark string, ctime, mtime string) (rows int64, err error) {
rs, err := d.db.Exec(c, _inVideoHis, aid, filename, status, xcodeState, remark, ctime, mtime)
if err != nil {
log.Error("d.inVideoHisStmt.Exec(%d, %s, %d, %d, %s, %s, %s) error(%v)", aid, filename, status, xcodeState, remark, ctime, mtime, err)
return
}
rows, err = rs.RowsAffected()
return
}

View File

@@ -0,0 +1,15 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_InVideoHis(t *testing.T) {
Convey("InVideoHis", t, func() {
_, err := d.InVideoHis(context.TODO(), 1, "Test_InVideoHis", 0, 0, "", "", "")
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,56 @@
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",
"email_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/archive/conf:go_default_library",
"//app/job/main/archive/model/result:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"email.go",
],
importpath = "go-common/app/job/main/archive/dao/email",
tags = ["automanaged"],
deps = [
"//app/job/main/archive/conf:go_default_library",
"//app/job/main/archive/model/result:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//app/service/main/archive/model/archive:go_default_library",
"//library/log:go_default_library",
"//vendor/gopkg.in/gomail.v2: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,22 @@
package email
import (
gomail "gopkg.in/gomail.v2"
"go-common/app/job/main/archive/conf"
)
// Dao is redis dao.
type Dao struct {
c *conf.Config
email *gomail.Dialer
}
// New is new redis dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
email: gomail.NewDialer(c.Mail.Host, c.Mail.Port, c.Mail.Username, c.Mail.Password),
}
return d
}

View File

@@ -0,0 +1,19 @@
package email
import (
"flag"
"path/filepath"
"go-common/app/job/main/archive/conf"
)
var (
d *Dao
)
func init() {
dir, _ := filepath.Abs("../../cmd/archive-job-test.toml")
flag.Set("conf", dir)
conf.Init()
d = New(conf.Conf)
}

View File

@@ -0,0 +1,87 @@
package email
import (
"encoding/json"
"fmt"
"strconv"
"go-common/app/job/main/archive/model/result"
"go-common/app/service/main/archive/api"
"go-common/app/service/main/archive/model/archive"
"go-common/library/log"
gomail "gopkg.in/gomail.v2"
)
var (
_states = map[int]string{
0: "开放浏览",
-1: "待审",
-2: "打回稿件回收站",
-3: "网警锁定删除",
-4: "锁定稿件",
// -5: "锁定稿件开放浏览",
-6: "修复待审",
-7: "暂缓审核",
// -8: "补档待审",
-9: "等待转码",
-10: "延迟发布",
-11: "视频源待修",
// -12: "上传失败",
-13: "允许评论待审",
// -14: "临时回收站",
-15: "分发中",
-16: "转码失败",
-30: "创建已提交",
-40: "UP主定时发布",
-100: "UP主删除",
}
)
func stateDescribe(state int) string {
des, ok := _states[state]
if ok {
return des
}
return strconv.Itoa(state)
}
const (
_bangumiMailSub = "番剧稿件《%s》状态变更"
_movieMailSub = "电影稿件《%s》状态变更"
_mailBody = `
稿件标题:%s
稿件状态:%s => %s
其他变更:%s => %s
稿件地址http://www.bilibili.com/video/av%d
审核后台http://manager.bilibili.co/#!/archive/modify/%d`
)
// PGCNotifyMail notify pgc mail
func (d *Dao) PGCNotifyMail(a *api.Arc, nw *result.Archive, old *result.Archive) {
msg := gomail.NewMessage()
msg.SetHeader("From", d.c.Mail.Username)
switch {
case a.AttrVal(archive.AttrBitIsBangumi) == archive.AttrYes:
msg.SetHeader("To", d.c.Mail.Bangumi...)
msg.SetHeader("Subject", fmt.Sprintf(_bangumiMailSub, a.Title))
case a.AttrVal(archive.AttrBitIsMovie) == archive.AttrYes:
msg.SetHeader("To", d.c.Mail.Movie...)
msg.SetHeader("Subject", fmt.Sprintf(_movieMailSub, a.Title))
default:
return
}
obs, _ := json.Marshal(old)
nbs, _ := json.Marshal(nw)
if old.State != nw.State {
oldState, newState := stateDescribe(old.State), stateDescribe(nw.State)
msg.SetBody("text/plain", fmt.Sprintf(_mailBody, a.Title, oldState, newState, obs, nbs, a.Aid, a.Aid))
} else {
state := stateDescribe(int(a.State))
msg.SetBody("text/plain", fmt.Sprintf(_mailBody, a.Title, state, state, obs, nbs, a.Aid, a.Aid))
}
if err := d.email.DialAndSend(msg); err != nil {
log.Error("s.email.DialAndSend(aid: %d) error(%v)", a.Aid, err)
}
}

View File

@@ -0,0 +1,15 @@
package email
import (
"go-common/app/job/main/archive/model/result"
"go-common/app/service/main/archive/api"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_PGCNotifyMail(t *testing.T) {
Convey("PGCNotifyMail", t, func() {
d.PGCNotifyMail(&api.Arc{}, &result.Archive{}, &result.Archive{})
})
}

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",
"monitor_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/archive/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"monitor.go",
],
importpath = "go-common/app/job/main/archive/dao/monitor",
tags = ["automanaged"],
deps = [
"//app/job/main/archive/conf:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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,21 @@
package monitor
import (
"go-common/app/job/main/archive/conf"
bm "go-common/library/net/http/blademaster"
)
// Dao is redis dao.
type Dao struct {
c *conf.Config
client *bm.Client
}
// New is new redis dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
client: bm.NewClient(c.HTTPClient),
}
return d
}

View File

@@ -0,0 +1,19 @@
package monitor
import (
"flag"
"path/filepath"
"go-common/app/job/main/archive/conf"
)
var (
d *Dao
)
func init() {
dir, _ := filepath.Abs("../../cmd/archive-job-test.toml")
flag.Set("conf", dir)
conf.Init()
d = New(conf.Conf)
}

View File

@@ -0,0 +1,55 @@
package monitor
import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"go-common/library/log"
)
type wxParams struct {
Username string `json:"username"`
Content string `json:"content"`
Token string `json:"token"`
Timestamp int64 `json:"timestamp"`
Sign string `json:"signature"`
}
type resp struct {
Status int64 `json:"status"`
Msg string `json:"msg"`
}
// Send send message to phone
func (d *Dao) Send(c context.Context, users, msg, token, secret string) (err error) {
params := url.Values{}
params.Set("username", users)
params.Set("content", msg)
params.Set("token", token)
params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
mh := md5.Sum([]byte(params.Encode() + secret))
params.Set("signature", hex.EncodeToString(mh[:]))
p := &wxParams{
Username: params.Get("username"),
Content: params.Get("content"),
Token: params.Get("token"),
Sign: params.Get("signature"),
}
p.Timestamp, _ = strconv.ParseInt(params.Get("timestamp"), 10, 64)
bs, _ := json.Marshal(p)
payload := strings.NewReader(string(bs))
req, _ := http.NewRequest("POST", "http://bap.bilibili.co/api/v1/message/add", payload)
req.Header.Add("content-type", "application/json; charset=utf-8")
v := &resp{}
if err = d.client.Do(context.TODO(), req, v); err != nil {
log.Error("s.client.Do error(%v)", err)
return
}
return
}

View File

@@ -0,0 +1,17 @@
package monitor
import (
"context"
"testing"
"go-common/app/job/main/archive/conf"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Send(t *testing.T) {
Convey("Send", t, func() {
err := d.Send(context.TODO(), conf.Conf.WeChantUsers, "报警短信test", conf.Conf.WeChatToken, conf.Conf.WeChatSecret)
So(err, ShouldBeNil)
})
}

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",
"reply_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/archive/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"reply.go",
],
importpath = "go-common/app/job/main/archive/dao/reply",
tags = ["automanaged"],
deps = [
"//app/job/main/archive/conf:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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,22 @@
package reply
import (
"go-common/app/job/main/archive/conf"
bm "go-common/library/net/http/blademaster"
)
// Dao is redis dao.
type Dao struct {
client *bm.Client
changeSubMid string
}
// New is new redis dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
client: bm.NewClient(c.HTTPClient),
// path
changeSubMid: c.Host.APICo + _changeSubjectMid,
}
return d
}

View File

@@ -0,0 +1,19 @@
package reply
import (
"flag"
"path/filepath"
"go-common/app/job/main/archive/conf"
)
var (
d *Dao
)
func init() {
dir, _ := filepath.Abs("../../cmd/archive-job-test.toml")
flag.Set("conf", dir)
conf.Init()
d = New(conf.Conf)
}

View File

@@ -0,0 +1,36 @@
package reply
import (
"context"
"errors"
"net/url"
"strconv"
"go-common/library/log"
)
const (
_changeSubjectMid = "/x/internal/v2/reply/subject/mid"
)
// ChangeSubjectMid change av's owner
func (d *Dao) ChangeSubjectMid(oid, mid int64) (err error) {
params := url.Values{}
params.Set("adid", "0")
params.Set("oid", strconv.FormatInt(oid, 10))
params.Set("type", "1")
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int64 `json:"code"`
}
if err = d.client.Post(context.TODO(), d.changeSubMid, "", params, &res); err != nil {
log.Error("d.client.Post(%s) error(%v)", d.changeSubMid+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
err = errors.New(strconv.FormatInt(res.Code, 10))
log.Error("d.client.Post(%s) code(%v)", d.changeSubMid+"?"+params.Encode(), res.Code)
return
}
return
}

View File

@@ -0,0 +1,13 @@
package reply
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_ChangeSubjectMid(t *testing.T) {
Convey("ChangeSubjectMid", t, func() {
d.ChangeSubjectMid(0, 1684013)
})
}

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 = [
"archive_test.go",
"dao_test.go",
"staff_test.go",
"video_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/archive/conf:go_default_library",
"//app/job/main/archive/model/archive:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"archive.go",
"dao.go",
"staff.go",
"video.go",
],
importpath = "go-common/app/job/main/archive/dao/result",
tags = ["automanaged"],
deps = [
"//app/job/main/archive/conf:go_default_library",
"//app/job/main/archive/model/archive:go_default_library",
"//app/job/main/archive/model/result: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,86 @@
package result
import (
"context"
"time"
"go-common/app/job/main/archive/model/archive"
"go-common/app/job/main/archive/model/result"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_arcSQL = "SELECT aid,mid,typeid,videos,copyright,title,cover,content,duration,attribute,state,access,pubtime,ctime,mission_id,order_id,redirect_url,forward,dynamic FROM archive WHERE aid=?"
_inArchiveSQL = `INSERT IGNORE INTO archive (aid,mid,typeid,videos,title,cover,content,duration,attribute,copyright,access,pubtime,state,mission_id,order_id,redirect_url,forward,dynamic,cid,dimensions)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
_upArchiveSQL = "UPDATE archive SET mid=?,typeid=?,videos=?,title=?,cover=?,content=?,duration=?,attribute=?,copyright=?,access=?,pubtime=?,state=?,mission_id=?,order_id=?,redirect_url=?,mtime=?,forward=?,dynamic=?,cid=?,dimensions=? WHERE aid=?"
_delArchiveSQL = "UPDATE archive SET state=? WHERE aid=?"
_upPassedSQL = "SELECT aid FROM archive WHERE mid=? AND state>=0 ORDER BY pubtime DESC"
)
// UpPassed is
func (d *Dao) UpPassed(c context.Context, mid int64) (aids []int64, err error) {
rows, err := d.db.Query(c, _upPassedSQL, mid)
if err != nil {
log.Error("d.db.Query(%s, %d) error(%v)", _upPassedSQL, mid, err)
return
}
defer rows.Close()
for rows.Next() {
var aid int64
if err = rows.Scan(&aid); err != nil {
log.Error("rows.Scan(%d) error(%v)", aid, err)
return
}
aids = append(aids, aid)
}
err = rows.Err()
return
}
// Archive get a archive by aid.
func (d *Dao) Archive(c context.Context, aid int64) (a *result.Archive, err error) {
row := d.db.QueryRow(c, _arcSQL, aid)
a = &result.Archive{}
if err = row.Scan(&a.AID, &a.Mid, &a.TypeID, &a.Videos, &a.Copyright, &a.Title, &a.Cover, &a.Content, &a.Duration,
&a.Attribute, &a.State, &a.Access, &a.PubTime, &a.CTime, &a.MissionID, &a.OrderID, &a.RedirectURL, &a.Forward, &a.Dynamic); err != nil {
if err == sql.ErrNoRows {
a = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// TxAddArchive add archive result
func (d *Dao) TxAddArchive(c context.Context, tx *sql.Tx, a *archive.Archive, ad *archive.Addit, videoCnt int, firstCid int64, dimensions string) (rows int64, err error) {
res, err := tx.Exec(_inArchiveSQL, a.ID, a.Mid, a.TypeID, videoCnt, a.Title, a.Cover, a.Content, a.Duration, a.Attribute, a.Copyright, a.Access, a.PubTime, a.State, ad.MissionID, ad.OrderID, ad.RedirectURL, a.Forward, ad.Dynamic, firstCid, dimensions)
if err != nil {
log.Error("tx.Exec(%s) error(%v)", _inArchiveSQL, err)
return
}
return res.RowsAffected()
}
// TxUpArchive update archive result
func (d *Dao) TxUpArchive(c context.Context, tx *sql.Tx, a *archive.Archive, ad *archive.Addit, videoCnt int, firstCid int64, dimensions string) (rows int64, err error) {
res, err := tx.Exec(_upArchiveSQL, a.Mid, a.TypeID, videoCnt, a.Title, a.Cover, a.Content, a.Duration, a.Attribute, a.Copyright, a.Access, a.PubTime, a.State, ad.MissionID, ad.OrderID, ad.RedirectURL, time.Now(), a.Forward, ad.Dynamic, firstCid, dimensions, a.ID)
if err != nil {
log.Error("tx.Exec(%s) error(%v)", _upArchiveSQL, err)
return
}
return res.RowsAffected()
}
// TxDelArchive delete archive
func (d *Dao) TxDelArchive(c context.Context, tx *sql.Tx, aid int64) (rows int64, err error) {
res, err := tx.Exec(_delArchiveSQL, archive.StateForbidUpDelete, aid)
if err != nil {
log.Error("tx.Execerror(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,59 @@
package result
import (
"context"
"testing"
"go-common/app/job/main/archive/model/archive"
. "github.com/smartystreets/goconvey/convey"
)
func Test_UpPassed(t *testing.T) {
Convey("UpPassed", t, func() {
a, err := d.UpPassed(context.TODO(), 1684013)
So(err, ShouldBeNil)
Println(a)
})
}
func Test_Archive(t *testing.T) {
Convey("Archive", t, func() {
a, err := d.Archive(context.TODO(), 1684013)
So(err, ShouldBeNil)
Println(a)
})
}
func Test_TxAddArchive(t *testing.T) {
Convey("TxAddArchive", t, func() {
tx, err := d.BeginTran(context.TODO())
So(err, ShouldBeNil)
_, err = d.TxAddArchive(context.TODO(), tx, &archive.Archive{}, &archive.Addit{}, 0, 0, "")
So(err, ShouldBeNil)
err = tx.Commit()
So(err, ShouldBeNil)
})
}
func Test_TxUpArchive(t *testing.T) {
Convey("TxUpArchive", t, func() {
tx, err := d.BeginTran(context.TODO())
So(err, ShouldBeNil)
_, err = d.TxUpArchive(context.TODO(), tx, &archive.Archive{ID: 0}, &archive.Addit{}, 0, 0, "")
So(err, ShouldBeNil)
err = tx.Commit()
So(err, ShouldBeNil)
})
}
func Test_TxDelArchive(t *testing.T) {
Convey("TxDelArchive", t, func() {
tx, err := d.BeginTran(context.TODO())
So(err, ShouldBeNil)
_, err = d.TxDelArchive(context.TODO(), tx, 0)
So(err, ShouldBeNil)
err = tx.Commit()
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,28 @@
package result
import (
"context"
"go-common/app/job/main/archive/conf"
"go-common/library/database/sql"
)
// Dao is redis dao.
type Dao struct {
c *conf.Config
db *sql.DB
}
// New is new redis dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.DB.Result),
}
return d
}
// BeginTran begin transcation.
func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
return d.db.Begin(c)
}

View File

@@ -0,0 +1,34 @@
package result
import (
"flag"
"os"
"testing"
"go-common/app/job/main/archive/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.archive-job")
flag.Set("conf_token", "MmQwIqWAyIaIu8CKb7MKcNSYlGGhoudN")
flag.Set("tree_id", "2301")
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")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,41 @@
package result
import (
"context"
"fmt"
"strings"
"go-common/app/job/main/archive/model/archive"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_addStaffSQL = "INSERT INTO archive_staff (aid,mid,title,ctime,mtime) VALUES "
_delStaffSQL = "DELETE FROM archive_staff WHERE aid=?"
)
// TxDelStaff del archive staff
func (d *Dao) TxDelStaff(c context.Context, tx *sql.Tx, aid int64) (err error) {
_, err = tx.Exec(_delStaffSQL, aid)
if err != nil {
log.Error("tx.Exec error(%v)", err)
return
}
return
}
// TxAddStaff add archive staff
func (d *Dao) TxAddStaff(c context.Context, tx *sql.Tx, aid int64, staff []*archive.Staff) (err error) {
var valSQL []string
for _, s := range staff {
valSQL = append(valSQL, fmt.Sprintf("(%d,%d,'%s','%s','%s')", s.Aid, s.Mid, s.Title, s.Ctime, s.Mtime))
}
valSQLStr := strings.Join(valSQL, ",")
_, err = tx.Exec(_addStaffSQL + valSQLStr)
if err != nil {
log.Error("tx.Exec error(%v)", err)
return
}
return
}

View File

@@ -0,0 +1,47 @@
package result
import (
"context"
"testing"
"go-common/app/job/main/archive/model/archive"
"github.com/smartystreets/goconvey/convey"
)
func TestTxDelStaff(t *testing.T) {
var (
c = context.TODO()
aid = int64(4052032)
)
convey.Convey("TxDelStaff", t, func(ctx convey.C) {
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
tx, err := d.BeginTran(c)
ctx.So(err, convey.ShouldBeNil)
err = d.TxDelStaff(c, tx, aid)
ctx.So(err, convey.ShouldBeNil)
err = tx.Commit()
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestTxAddStaff(t *testing.T) {
var (
c = context.TODO()
aid = int64(4052032)
staff []*archive.Staff
)
convey.Convey("TxAddStaff", t, func(ctx convey.C) {
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
staff = append(staff, &archive.Staff{Aid: aid, Mid: 3333, Title: "哈哈", Ctime: "2018-11-28T16:50:14+08:00", Mtime: "2018-12-21T11:41:37+08:00"})
staff = append(staff, &archive.Staff{Aid: aid, Mid: 4444, Title: "2223", Ctime: "2018-11-28T16:50:14+08:00", Mtime: "2018-12-21T11:41:38+08:00"})
tx, err := d.BeginTran(c)
ctx.So(err, convey.ShouldBeNil)
err = d.TxAddStaff(c, tx, aid, staff)
ctx.So(err, convey.ShouldBeNil)
err = tx.Commit()
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,47 @@
package result
import (
"context"
"go-common/app/job/main/archive/model/archive"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_inVideoSQL = `INSERT INTO archive_video (aid,eptitle,description,filename,src_type,cid,duration,index_order,attribute,weblink,dimensions) VALUES(?,?,?,?,?,?,?,?,?,?,?)
ON DUPLICATE KEY UPDATE eptitle=?,description=?,filename=?,src_type=?,duration=?,index_order=?,attribute=?,weblink=?,dimensions=?`
_delVideoByCidSQL = "DELETE FROM archive_video where aid=? and cid=?"
_delVideosSQL = "DELETE FROM archive_video WHERE aid=?"
)
// TxAddVideo add videos result
func (d *Dao) TxAddVideo(c context.Context, tx *sql.Tx, v *archive.Video) (rows int64, err error) {
res, err := tx.Exec(_inVideoSQL, v.Aid, v.Title, v.Desc, v.Filename, v.SrcType, v.Cid, v.Duration, v.Index, v.Attribute, v.WebLink, v.Dimensions,
v.Title, v.Desc, v.Filename, v.SrcType, v.Duration, v.Index, v.Attribute, v.WebLink, v.Dimensions)
if err != nil {
log.Error("tx.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// TxDelVideoByCid del videos by aid and cid
func (d *Dao) TxDelVideoByCid(c context.Context, tx *sql.Tx, aid, cid int64) (rows int64, err error) {
res, err := tx.Exec(_delVideoByCidSQL, aid, cid)
if err != nil {
log.Error("tx.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// TxDelVideos del videos
func (d *Dao) TxDelVideos(c context.Context, tx *sql.Tx, aid int64) (rows int64, err error) {
res, err := tx.Exec(_delVideosSQL, aid)
if err != nil {
log.Error("tx.Exec(%s, %d) error(%v)", _delVideosSQL, aid, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,43 @@
package result
import (
"context"
"testing"
"go-common/app/job/main/archive/model/archive"
. "github.com/smartystreets/goconvey/convey"
)
func Test_TxAddVideo(t *testing.T) {
Convey("Archive", t, func() {
tx, err := d.BeginTran(context.TODO())
So(err, ShouldBeNil)
_, err = d.TxAddVideo(context.TODO(), tx, &archive.Video{Aid: 1, Cid: 1})
So(err, ShouldBeNil)
err = tx.Commit()
So(err, ShouldBeNil)
})
}
func Test_TxDelVideoByCid(t *testing.T) {
Convey("TxDelVideoByCid", t, func() {
tx, err := d.BeginTran(context.TODO())
So(err, ShouldBeNil)
_, err = d.TxDelVideoByCid(context.TODO(), tx, 1, 1)
So(err, ShouldBeNil)
err = tx.Commit()
So(err, ShouldBeNil)
})
}
func Test_TxDelVideos(t *testing.T) {
Convey("TxDelVideos", t, func() {
tx, err := d.BeginTran(context.TODO())
So(err, ShouldBeNil)
_, err = d.TxDelVideos(context.TODO(), tx, 0)
So(err, ShouldBeNil)
err = tx.Commit()
So(err, ShouldBeNil)
})
}