Create & Init Project...
This commit is contained in:
61
app/interface/main/tv/dao/audit/BUILD
Normal file
61
app/interface/main/tv/dao/audit/BUILD
Normal file
@ -0,0 +1,61 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"audit.go",
|
||||
"dao.go",
|
||||
"transcode.go",
|
||||
],
|
||||
importpath = "go-common/app/interface/main/tv/dao/audit",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/interface/main/tv/conf:go_default_library",
|
||||
"//app/interface/main/tv/model:go_default_library",
|
||||
"//library/cache/memcache:go_default_library",
|
||||
"//library/database/sql:go_default_library",
|
||||
"//library/ecode:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//library/time: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"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"audit_test.go",
|
||||
"dao_test.go",
|
||||
"transcode_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
rundir = ".",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//app/interface/main/tv/conf:go_default_library",
|
||||
"//app/interface/main/tv/model:go_default_library",
|
||||
"//library/database/sql:go_default_library",
|
||||
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
|
||||
],
|
||||
)
|
50
app/interface/main/tv/dao/audit/audit.go
Normal file
50
app/interface/main/tv/dao/audit/audit.go
Normal file
@ -0,0 +1,50 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"context"
|
||||
xtime "time"
|
||||
|
||||
"go-common/app/interface/main/tv/model"
|
||||
xsql "go-common/library/database/sql"
|
||||
"go-common/library/time"
|
||||
)
|
||||
|
||||
const (
|
||||
_updateCont = "UPDATE `tv_content` SET `state` = ?, `valid` = ?, `reason` = ?, `inject_time` = ? WHERE `epid` = ? AND `is_deleted` = 0"
|
||||
_updateSea = "UPDATE `tv_ep_season` SET `check` = ?, `valid` = ?, `reason` = ?, `inject_time` = ? WHERE `id` = ? AND `is_deleted` = 0"
|
||||
_updateVideo = "UPDATE `ugc_video` SET `result` = ?, `valid` = ?, `reason` = ? , `inject_time` = ? WHERE `cid` = ? AND `deleted` = 0"
|
||||
_updateArc = "UPDATE `ugc_archive` SET `result` = ?, `valid` = ?, `reason` = ?, `inject_time` = ? WHERE `aid` = ? AND `deleted` = 0"
|
||||
)
|
||||
|
||||
// BeginTran def.
|
||||
func (d *Dao) BeginTran(c context.Context) (tx *xsql.Tx, err error) {
|
||||
return d.db.Begin(c)
|
||||
}
|
||||
|
||||
// UpdateVideo .
|
||||
func (d *Dao) UpdateVideo(c context.Context, v *model.AuditOp, tx *xsql.Tx) (err error) {
|
||||
now := time.Time(xtime.Now().Unix())
|
||||
_, err = tx.Exec(_updateVideo, v.Result, v.Valid, v.AuditMsg, now, v.KID)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateArc .
|
||||
func (d *Dao) UpdateArc(c context.Context, v *model.AuditOp, tx *xsql.Tx) (err error) {
|
||||
now := time.Time(xtime.Now().Unix())
|
||||
_, err = tx.Exec(_updateArc, v.Result, v.Valid, v.AuditMsg, now, v.KID)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateCont .
|
||||
func (d *Dao) UpdateCont(c context.Context, val *model.AuditOp, tx *xsql.Tx) (err error) {
|
||||
now := time.Time(xtime.Now().Unix())
|
||||
_, err = tx.Exec(_updateCont, val.Result, val.Valid, val.AuditMsg, now, val.KID)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateSea .
|
||||
func (d *Dao) UpdateSea(c context.Context, val *model.AuditOp, tx *xsql.Tx) (err error) {
|
||||
now := time.Time(xtime.Now().Unix())
|
||||
_, err = tx.Exec(_updateSea, val.Result, val.Valid, val.AuditMsg, now, val.KID)
|
||||
return
|
||||
}
|
83
app/interface/main/tv/dao/audit/audit_test.go
Normal file
83
app/interface/main/tv/dao/audit/audit_test.go
Normal file
@ -0,0 +1,83 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go-common/app/interface/main/tv/model"
|
||||
"testing"
|
||||
|
||||
"github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestAuditBeginTrans(t *testing.T) {
|
||||
var (
|
||||
c = context.Background()
|
||||
)
|
||||
convey.Convey("BeginTran", t, func(ctx convey.C) {
|
||||
tx, err := d.BeginTran(c)
|
||||
ctx.Convey("Then err should be nil.tx should not be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldBeNil)
|
||||
ctx.So(tx, convey.ShouldNotBeNil)
|
||||
})
|
||||
tx.Commit()
|
||||
})
|
||||
}
|
||||
|
||||
func TestAuditUpdateVideo(t *testing.T) {
|
||||
var (
|
||||
c = context.Background()
|
||||
v = &model.AuditOp{}
|
||||
tx, _ = d.BeginTran(c)
|
||||
)
|
||||
convey.Convey("UpdateVideo", t, func(ctx convey.C) {
|
||||
err := d.UpdateVideo(c, v, tx)
|
||||
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldBeNil)
|
||||
})
|
||||
tx.Commit()
|
||||
})
|
||||
}
|
||||
|
||||
func TestAuditUpdateArc(t *testing.T) {
|
||||
var (
|
||||
c = context.Background()
|
||||
v = &model.AuditOp{}
|
||||
tx, _ = d.BeginTran(c)
|
||||
)
|
||||
convey.Convey("UpdateArc", t, func(ctx convey.C) {
|
||||
err := d.UpdateArc(c, v, tx)
|
||||
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldBeNil)
|
||||
})
|
||||
tx.Commit()
|
||||
})
|
||||
}
|
||||
|
||||
func TestAuditUpdateCont(t *testing.T) {
|
||||
var (
|
||||
c = context.Background()
|
||||
val = &model.AuditOp{}
|
||||
tx, _ = d.BeginTran(c)
|
||||
)
|
||||
convey.Convey("UpdateCont", t, func(ctx convey.C) {
|
||||
err := d.UpdateCont(c, val, tx)
|
||||
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldBeNil)
|
||||
})
|
||||
tx.Commit()
|
||||
})
|
||||
}
|
||||
|
||||
func TestAuditUpdateSea(t *testing.T) {
|
||||
var (
|
||||
c = context.Background()
|
||||
val = &model.AuditOp{}
|
||||
tx, _ = d.BeginTran(c)
|
||||
)
|
||||
convey.Convey("UpdateSea", t, func(ctx convey.C) {
|
||||
err := d.UpdateSea(c, val, tx)
|
||||
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldBeNil)
|
||||
})
|
||||
tx.Commit()
|
||||
})
|
||||
}
|
24
app/interface/main/tv/dao/audit/dao.go
Normal file
24
app/interface/main/tv/dao/audit/dao.go
Normal file
@ -0,0 +1,24 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"go-common/app/interface/main/tv/conf"
|
||||
"go-common/library/cache/memcache"
|
||||
"go-common/library/database/sql"
|
||||
)
|
||||
|
||||
// Dao is account dao.
|
||||
type Dao struct {
|
||||
mc *memcache.Pool
|
||||
conf *conf.Config
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
// New account dao.
|
||||
func New(c *conf.Config) (d *Dao) {
|
||||
d = &Dao{
|
||||
conf: c,
|
||||
mc: memcache.NewPool(c.Memcache.Config),
|
||||
db: sql.NewMySQL(c.Mysql),
|
||||
}
|
||||
return
|
||||
}
|
44
app/interface/main/tv/dao/audit/dao_test.go
Normal file
44
app/interface/main/tv/dao/audit/dao_test.go
Normal file
@ -0,0 +1,44 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"os"
|
||||
|
||||
"go-common/app/interface/main/tv/conf"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
var (
|
||||
d *Dao
|
||||
ctx = context.Background()
|
||||
)
|
||||
|
||||
func init() {
|
||||
//dir, _ := filepath.Abs("../../cmd/tv-interface.toml")
|
||||
//flag.Set("conf", dir)
|
||||
if os.Getenv("DEPLOY_ENV") != "" {
|
||||
flag.Set("app_id", "main.web-svr.tv-interface")
|
||||
flag.Set("conf_token", "07c1826c1f39df02a1411cdd6f455879")
|
||||
flag.Set("tree_id", "15326")
|
||||
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)
|
||||
}
|
||||
|
||||
func WithDao(f func(d *Dao)) func() {
|
||||
return func() {
|
||||
Reset(func() {})
|
||||
f(d)
|
||||
}
|
||||
}
|
76
app/interface/main/tv/dao/audit/transcode.go
Normal file
76
app/interface/main/tv/dao/audit/transcode.go
Normal file
@ -0,0 +1,76 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go-common/library/ecode"
|
||||
"go-common/library/log"
|
||||
"go-common/library/time"
|
||||
"go-common/library/xstr"
|
||||
)
|
||||
|
||||
const (
|
||||
_PgcCID = "SELECT `id` FROM `tv_content` WHERE `cid` = ? AND `is_deleted` = 0"
|
||||
_UgcCID = "SELECT `id` FROM `ugc_video` WHERE `cid` = ? AND `deleted` = 0"
|
||||
_transcodePGC = "UPDATE `tv_content` SET `transcoded` = ?, mark_time = NOW() WHERE `id` IN (%s) "
|
||||
_transcodeUGC = "UPDATE `ugc_video` SET `transcoded` = ? WHERE `id` IN (%s)"
|
||||
_applyPGC = "UPDATE `tv_content` SET `apply_time` = ? WHERE `id` IN (%s)"
|
||||
)
|
||||
|
||||
// checkCID picks one ugc or pgc video data with its cid
|
||||
func (d *Dao) checkCID(c context.Context, cid int64, query string) (ids []int64, err error) {
|
||||
rows, err := d.db.Query(c, query, cid)
|
||||
if err != nil {
|
||||
log.Error("checkCID d.db.Query (%s) cid %d, error(%v)", query, cid, err)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var li int64
|
||||
if err = rows.Scan(&li); err != nil {
|
||||
log.Error("checkCID Query (%s) row.Scan error(%v)", query, err)
|
||||
return
|
||||
}
|
||||
ids = append(ids, li)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
log.Error("checkCID Query (%s) rows Err cid %d, err %v", query, cid, err)
|
||||
return
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
err = ecode.NothingFound
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UgcCID picks one ugc video data with its cid
|
||||
func (d *Dao) UgcCID(c context.Context, cid int64) (ids []int64, err error) {
|
||||
return d.checkCID(c, cid, _UgcCID)
|
||||
}
|
||||
|
||||
// PgcCID picks one ugc video data with its cid
|
||||
func (d *Dao) PgcCID(c context.Context, cid int64) (ids []int64, err error) {
|
||||
return d.checkCID(c, cid, _PgcCID)
|
||||
}
|
||||
|
||||
func (d *Dao) updateCIDs(c context.Context, query string, value interface{}) (err error) {
|
||||
if _, err = d.db.Exec(c, query, value); err != nil {
|
||||
log.Error("updateCIDs, Query %s, d.db.Exec.error(%v)", query, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// PgcTranscode updates the transcoded status of an ep data
|
||||
func (d *Dao) PgcTranscode(c context.Context, ids []int64, action int64) (err error) {
|
||||
return d.updateCIDs(c, fmt.Sprintf(_transcodePGC, xstr.JoinInts(ids)), action)
|
||||
}
|
||||
|
||||
// UgcTranscode updates the transcoded status of an ep data
|
||||
func (d *Dao) UgcTranscode(c context.Context, ids []int64, action int64) (err error) {
|
||||
return d.updateCIDs(c, fmt.Sprintf(_transcodeUGC, xstr.JoinInts(ids)), action)
|
||||
}
|
||||
|
||||
// ApplyPGC saves pgc apply_time; only PGC needs this
|
||||
func (d *Dao) ApplyPGC(c context.Context, ids []int64, aTime int64) (err error) {
|
||||
return d.updateCIDs(c, fmt.Sprintf(_applyPGC, xstr.JoinInts(ids)), time.Time(aTime))
|
||||
}
|
128
app/interface/main/tv/dao/audit/transcode_test.go
Normal file
128
app/interface/main/tv/dao/audit/transcode_test.go
Normal file
@ -0,0 +1,128 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go-common/library/database/sql"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func getCID(db *sql.DB, isPGC bool, double bool) (cid int64, err error) {
|
||||
var query string
|
||||
if isPGC {
|
||||
if double {
|
||||
query = "SELECT cid FROM tv_content WHERE is_deleted = 0 GROUP BY cid HAVING COUNT(1) >1 LIMIT 1"
|
||||
} else {
|
||||
query = "SELECT cid FROM tv_content WHERE is_deleted = 0 LIMIT 1"
|
||||
}
|
||||
} else {
|
||||
if double {
|
||||
query = "SELECT cid FROM ugc_video WHERE deleted = 0 GROUP BY cid HAVING COUNT(1) > 1 LIMIT 1"
|
||||
} else {
|
||||
query = "SELECT cid FROM ugc_video WHERE deleted = 0 LIMIT 1"
|
||||
}
|
||||
}
|
||||
if err = db.QueryRow(ctx, query).Scan(&cid); err != nil {
|
||||
fmt.Println("No Ready Video : ", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("pick CID ", cid)
|
||||
return
|
||||
}
|
||||
|
||||
func tryDouble(isPGC bool) (cid int64, err error) {
|
||||
var errDouble error
|
||||
if cid, errDouble = getCID(d.db, isPGC, true); errDouble != nil {
|
||||
fmt.Println("Double Err ", errDouble, " Will Use Single")
|
||||
if cid, err = getCID(d.db, isPGC, false); err != nil {
|
||||
fmt.Println("Single Err ", err, " Will Fail")
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func TestDao_UgcCID(t *testing.T) {
|
||||
Convey("TestDao_UgcCID", t, WithDao(func(d *Dao) {
|
||||
cid, errTry := tryDouble(false)
|
||||
if errTry != nil {
|
||||
fmt.Println("No Cid Ready")
|
||||
return
|
||||
}
|
||||
cont, err := d.UgcCID(ctx, cid)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(cont), ShouldBeGreaterThan, 0)
|
||||
fmt.Println(cont)
|
||||
}))
|
||||
}
|
||||
|
||||
func TestDao_PgcCID(t *testing.T) {
|
||||
Convey("TestDao_PgcCID", t, WithDao(func(d *Dao) {
|
||||
cid, errTry := tryDouble(true)
|
||||
if errTry != nil {
|
||||
fmt.Println("No Cid Ready")
|
||||
return
|
||||
}
|
||||
cont, err := d.PgcCID(ctx, cid)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(cont), ShouldBeGreaterThan, 0)
|
||||
fmt.Println(cont)
|
||||
}))
|
||||
}
|
||||
|
||||
func TestDao_UgcTranscode(t *testing.T) {
|
||||
Convey("TestDao_UgcTranscode", t, WithDao(func(d *Dao) {
|
||||
cid, errTry := tryDouble(false)
|
||||
if errTry != nil {
|
||||
fmt.Println("No Cid Ready")
|
||||
return
|
||||
}
|
||||
cont, _ := d.UgcCID(ctx, cid)
|
||||
if len(cont) == 0 {
|
||||
fmt.Println("UgcCid Error!")
|
||||
return
|
||||
}
|
||||
fmt.Println("To Update ", cont)
|
||||
err := d.UgcTranscode(ctx, cont, 1)
|
||||
So(err, ShouldBeNil)
|
||||
}))
|
||||
}
|
||||
|
||||
func TestDao_PgcTranscode(t *testing.T) {
|
||||
Convey("TestDao_PgcTranscode", t, WithDao(func(d *Dao) {
|
||||
cid, errTry := tryDouble(true)
|
||||
if errTry != nil {
|
||||
fmt.Println("No Cid Ready")
|
||||
return
|
||||
}
|
||||
cont, _ := d.PgcCID(ctx, cid)
|
||||
if len(cont) == 0 {
|
||||
fmt.Println("PgcCid Error!")
|
||||
return
|
||||
}
|
||||
fmt.Println("To Update ", cont)
|
||||
err := d.PgcTranscode(ctx, cont, 1)
|
||||
So(err, ShouldBeNil)
|
||||
}))
|
||||
}
|
||||
|
||||
func TestDao_ApplyPGC(t *testing.T) {
|
||||
Convey("TestDao_ApplyPGC", t, WithDao(func(d *Dao) {
|
||||
cid, errTry := tryDouble(true)
|
||||
if errTry != nil {
|
||||
fmt.Println("No Cid Ready")
|
||||
return
|
||||
}
|
||||
cont, _ := d.PgcCID(ctx, cid)
|
||||
if len(cont) == 0 {
|
||||
fmt.Println("PgcCid Error!")
|
||||
return
|
||||
}
|
||||
fmt.Println("To Update ", cont)
|
||||
err := d.ApplyPGC(ctx, cont, time.Now().Unix())
|
||||
So(err, ShouldBeNil)
|
||||
}))
|
||||
}
|
Reference in New Issue
Block a user