Initial commit

This commit is contained in:
Donny
2019-04-22 20:46:32 +08:00
commit 49ab8aadd1
25441 changed files with 4055000 additions and 0 deletions

View File

@@ -0,0 +1,101 @@
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",
"auth_role_test.go",
"consumer_test.go",
"dao_test.go",
"manager_test.go",
"oper_test.go",
"report_test.go",
"search_test.go",
"task_dispatch_test.go",
"task_history_test.go",
"task_oper_history_test.go",
"task_qa_test.go",
"task_qa_video_test.go",
"task_review_test.go",
"task_weight_test.go",
"type_test.go",
"up_group_test.go",
"user_test.go",
"video_test.go",
"weight_log_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/videoup-task/conf:go_default_library",
"//app/admin/main/videoup-task/model: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 = [
"archive.go",
"auth_role.go",
"consumer.go",
"dao.go",
"manager.go",
"oper.go",
"report.go",
"search.go",
"task_dispatch.go",
"task_history.go",
"task_oper_history.go",
"task_qa.go",
"task_qa_video.go",
"task_review.go",
"task_weight.go",
"type.go",
"up_group.go",
"user.go",
"video.go",
"weight_log.go",
],
importpath = "go-common/app/admin/main/videoup-task/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/videoup-task/conf:go_default_library",
"//app/admin/main/videoup-task/model:go_default_library",
"//app/admin/main/videoup/model/archive:go_default_library",
"//app/service/main/account/api:go_default_library",
"//app/service/main/account/model:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/hbase.v2:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/stat/prom:go_default_library",
"//library/xstr:go_default_library",
"//vendor/github.com/tsuna/gohbase/hrpc: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,45 @@
package dao
import (
"context"
"database/sql"
"go-common/app/admin/main/videoup-task/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
const (
_arcSQL = "SELECT id,mid,title,access,attribute,reject_reason,tag,forward,round,state,copyright,cover,content,typeid,pubtime,ctime,mtime FROM archive WHERE id=?"
_archiveParamSQL = `SELECT a.typeid,addit.up_from FROM archive AS a LEFT JOIN archive_addit AS addit ON a.id=addit.aid WHERE a.id=?`
)
// Archive get archive by aid
func (d *Dao) Archive(c context.Context, aid int64) (a *model.Archive, err error) {
var (
row = d.arcDB.QueryRow(c, _arcSQL, aid)
reason, tag sql.NullString
)
a = &model.Archive{}
if err = row.Scan(&a.Aid, &a.Mid, &a.Title, &a.Access, &a.Attribute, &reason, &tag, &a.Forward, &a.Round, &a.State,
&a.Copyright, &a.Cover, &a.Desc, &a.TypeID, &a.PTime, &a.CTime, &a.MTime); err != nil {
if err == xsql.ErrNoRows {
a = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
return
}
a.RejectReason = reason.String
a.Tag = tag.String
return
}
// ArchiveParam .
func (d *Dao) ArchiveParam(c context.Context, aid int64) (typeid int16, upfrom int8, err error) {
if err = d.arcDB.QueryRow(c, _archiveParamSQL, aid).Scan(&typeid, &upfrom); err != nil {
log.Error("ArchiveParam error(%v)", err)
}
return
}

View File

@@ -0,0 +1,37 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoArchive(t *testing.T) {
convey.Convey("Archive", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.Archive(c, aid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoArchiveParam(t *testing.T) {
convey.Convey("ArchiveParam", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.ArchiveParam(c, aid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
})
})
})
}

View File

@@ -0,0 +1,22 @@
package dao
import (
"context"
"database/sql"
"go-common/library/log"
)
const (
_getUserRoleSQL = "SELECT `role` FROM `auth_role` WHERE uid = ?"
)
// GetUserRole 用户角色
func (d *Dao) GetUserRole(c context.Context, uid int64) (role int8, err error) {
err = d.mngDB.QueryRow(c, _getUserRoleSQL, uid).Scan(&role)
if err != nil && err != sql.ErrNoRows {
log.Error("d.managerDB.Query error(%v)", err)
return
}
return role, nil
}

View File

@@ -0,0 +1,24 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetUserRole(t *testing.T) {
convey.Convey("GetUserRole", t, func(ctx convey.C) {
var (
c = context.Background()
uid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
role, err := d.GetUserRole(c, uid)
ctx.Convey("Then err should be nil.role should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(role, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,67 @@
package dao
import (
"context"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_taskUserCheckInSQL = "INSERT INTO task_consumer (uid,state) VALUES (?,1) ON DUPLICATE KEY UPDATE state = 1"
_taskUserCheckOffSQL = "UPDATE task_consumer SET state = 0 WHERE uid=?"
_consumersSQL = "SELECT id,uid,state,ctime,mtime FROM task_consumer where state=1"
_isConsumerOnSQL = "SELECT state FROM task_consumer WHERE uid=?"
)
// TaskUserCheckIn insert or update task consumer check state
func (d *Dao) TaskUserCheckIn(c context.Context, uid int64) (rows int64, err error) {
res, err := d.arcDB.Exec(c, _taskUserCheckInSQL, uid)
if err != nil {
log.Error("tx.Exec(%s, %d) error(%v)", _taskUserCheckInSQL, uid, err)
return
}
return res.RowsAffected()
}
// TaskUserCheckOff update task consumer check state
func (d *Dao) TaskUserCheckOff(c context.Context, uid int64) (rows int64, err error) {
res, err := d.arcDB.Exec(c, _taskUserCheckOffSQL, uid)
if err != nil {
log.Error("tx.Exec(%s, %d) error(%v)", _taskUserCheckOffSQL, uid, err)
return
}
return res.RowsAffected()
}
// Consumers 用户列表
func (d *Dao) Consumers(c context.Context) (cms []*model.Consumers, err error) {
rows, err := d.arcDB.Query(c, _consumersSQL)
if err != nil {
log.Error("d.arcDB.Query(%s) error(%v)", _consumersSQL, err)
return
}
defer rows.Close()
for rows.Next() {
cm := new(model.Consumers)
err = rows.Scan(&cm.ID, &cm.UID, &cm.State, &cm.Ctime, &cm.Mtime)
if err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
cms = append(cms, cm)
}
return
}
// IsConsumerOn 判断是否登入
func (d *Dao) IsConsumerOn(c context.Context, uid int64) (state int8) {
err := d.arcDB.QueryRow(c, _isConsumerOnSQL, uid).Scan(&state)
if err != nil {
if err != sql.ErrNoRows {
log.Error("d.arcDB.QueryRow error(%v)", err)
}
}
return
}

View File

@@ -0,0 +1,70 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoTaskUserCheckIn(t *testing.T) {
convey.Convey("TaskUserCheckIn", t, func(ctx convey.C) {
var (
c = context.Background()
uid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.TaskUserCheckIn(c, uid)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTaskUserCheckOff(t *testing.T) {
convey.Convey("TaskUserCheckOff", t, func(ctx convey.C) {
var (
c = context.Background()
uid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.TaskUserCheckOff(c, uid)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoConsumers(t *testing.T) {
convey.Convey("Consumers", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
cms, err := d.Consumers(c)
ctx.Convey("Then err should be nil.cms should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(cms, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoIsConsumerOn(t *testing.T) {
convey.Convey("IsConsumerOn", t, func(ctx convey.C) {
var (
c = context.Background()
uid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
state := d.IsConsumerOn(c, uid)
ctx.Convey("Then state should not be nil.", func(ctx convey.C) {
ctx.So(state, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,117 @@
package dao
import (
"context"
"time"
"go-common/app/admin/main/videoup-task/conf"
account "go-common/app/service/main/account/api"
"go-common/app/service/main/account/model"
"go-common/library/cache/redis"
"go-common/library/database/hbase.v2"
"go-common/library/database/sql"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/stat/prom"
)
//Dao dao.
type Dao struct {
c *conf.Config
arcDB *sql.DB
arcReadDB *sql.DB
mngDB *sql.DB
upGroupURL string
hbase *hbase.Client
redis *redis.Pool
hclient *bm.Client
//grpc
acc account.AccountClient
}
var errCount = prom.BusinessErrCount
//New new dao
func New(config *conf.Config) (d *Dao) {
d = &Dao{
c: config,
arcDB: sql.NewMySQL(config.DB.Archive),
arcReadDB: sql.NewMySQL(config.DB.ArchiveRead),
mngDB: sql.NewMySQL(config.DB.Manager),
upGroupURL: config.Host.API + "/x/internal/uper/special/get",
hbase: hbase.NewClient(config.HBase.Config),
redis: redis.NewPool(config.Redis.Weight.Config),
hclient: bm.NewClient(config.HTTPClient),
}
var err error
if d.acc, err = account.NewClient(config.GRPC.AccRPC); err != nil {
panic(err)
}
return
}
//BeginTran begin transaction
func (d *Dao) BeginTran(ctx context.Context) (tx *sql.Tx, err error) {
if tx, err = d.arcDB.Begin(ctx); err != nil {
PromeErr("arcdb: begintran", "BeginTran d.arcDB.Begin error(%v)", err)
}
return
}
//Close close
func (d *Dao) Close() {
if d.arcDB != nil {
d.arcDB.Close()
}
if d.arcReadDB != nil {
d.arcReadDB.Close()
}
if d.mngDB != nil {
d.mngDB.Close()
}
}
//Ping ping
func (d *Dao) Ping(ctx context.Context) (err error) {
if d.arcDB != nil {
if err = d.arcDB.Ping(ctx); err != nil {
PromeErr("arcdb: ping", "d.arcDB.Ping error(%v)", err)
return
}
}
if d.arcReadDB != nil {
if err = d.arcReadDB.Ping(ctx); err != nil {
PromeErr("arcReaddb: ping", "d.arcReadDB.Ping error(%v)", err)
return
}
}
if d.mngDB != nil {
if err = d.mngDB.Ping(ctx); err != nil {
PromeErr("mngdb: ping", "d.mngDB.Ping error(%v)", err)
return
}
}
return
}
//AccountInfos get multi mids' accountinfo
func (d *Dao) AccountInfos(ctx context.Context, mids []int64) (info map[int64]*model.Info, err error) {
tctx, cancel := context.WithTimeout(ctx, time.Millisecond*500)
defer cancel()
var infosreply *account.InfosReply
if infosreply, err = d.acc.Infos3(tctx, &account.MidsReq{Mids: mids}); err != nil {
PromeErr("account_infos", "AccountInfos d.acc.Infos3 error(%v) mid(%d)", err, mids)
}
if infosreply != nil {
info = infosreply.Infos
}
return
}
//PromeErr prome & log err
func PromeErr(name string, format string, args ...interface{}) {
errCount.Incr(name)
log.Error(format, args...)
}

View File

@@ -0,0 +1,47 @@
package dao
import (
"flag"
"os"
"strings"
"testing"
"go-common/app/admin/main/videoup-task/conf"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.videoup-task-admin")
flag.Set("conf_token", "7e2256b4bc8e4084cf848fe27a474a20")
flag.Set("tree_id", "25095")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "dev")
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/videoup-task-admin.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
d.hclient.SetTransport(gock.DefaultTransport)
m.Run()
os.Exit(0)
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}

View File

@@ -0,0 +1,95 @@
package dao
import (
"context"
"net/url"
"strings"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_uidsURL = "/x/admin/manager/users/uids"
_unamesURL = "/x/admin/manager/users/unames"
)
// Unames get unames by uid
func (d *Dao) Unames(c context.Context, uids []int64) (res map[int64]string, err error) {
var (
param = url.Values{}
uidStr = xstr.JoinInts(uids)
unameURI = d.c.Host.Manager + _unamesURL
)
param.Set("uids", uidStr)
var httpRes struct {
Code int `json:"code"`
Data map[int64]string `json:"data"`
Message string `json:"message"`
}
err = d.hclient.Get(c, unameURI, "", param, &httpRes)
if err != nil {
log.Error("d.client.Get(%s) error(%v)", unameURI+"?"+param.Encode(), err)
return
}
if httpRes.Code != 0 {
log.Error("url(%s) error(%v), code(%d), message(%s)", unameURI+"?"+param.Encode(), err, httpRes.Code, httpRes.Message)
}
res = httpRes.Data
return
}
// Uids get uids by unames
func (d *Dao) Uids(c context.Context, names []string) (res map[string]int64, err error) {
var (
param = url.Values{}
namesStr = strings.Join(names, ",")
uidURI = d.c.Host.Manager + _uidsURL
)
param.Set("unames", namesStr)
var httpRes struct {
Code int `json:"code"`
Data map[string]int64 `json:"data"`
Message string `json:"message"`
}
err = d.hclient.Get(c, uidURI, "", param, &httpRes)
if err != nil {
log.Error("d.client.Get(%s) error(%v)", uidURI+"?"+param.Encode(), err)
return
}
if httpRes.Code != 0 {
log.Error("url(%s) error(%v), code(%d), message(%s)", uidURI+"?"+param.Encode(), err, httpRes.Code, httpRes.Message)
}
res = httpRes.Data
return
}
// GetUIDByName 获取uid
func (d *Dao) GetUIDByName(c context.Context, name string) (uid int64, err error) {
var res map[string]int64
if res, err = d.Uids(c, []string{name}); err != nil {
return
}
if uid, ok := res[name]; ok {
return uid, nil
}
return
}
// GetNameByUID 获取用户名
func (d *Dao) GetNameByUID(c context.Context, uids []int64) (mcases map[int64][]interface{}, err error) {
var res map[int64]string
if res, err = d.Unames(c, uids); err != nil {
return
}
mcases = make(map[int64][]interface{})
for uid, uname := range res {
mcases[uid] = []interface{}{uname}
}
return
}

View File

@@ -0,0 +1,69 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoUids(t *testing.T) {
convey.Convey("Uids", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
httpMock("GET", d.c.Host.Manager+_uidsURL).Reply(200).JSON(`{"code":0}`)
_, err := d.Uids(c, []string{})
ctx.Convey("Then nil should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoUnames(t *testing.T) {
convey.Convey("Unames", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
httpMock("GET", d.c.Host.Manager+_unamesURL).Reply(200).JSON(`{"code":0,"message":"0","data":{"10086":"cxf"}}`)
_, err := d.Unames(c, []int64{})
ctx.Convey("Then nil should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoGetUIDByName(t *testing.T) {
convey.Convey("GetUIDByName", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
httpMock("GET", d.c.Host.Manager+_uidsURL).Reply(200).JSON(`{"code":0,"message":"0","data":{"cxf":10086}}`)
uid, err := d.GetUIDByName(c, "cxf")
ctx.Convey("Then nil should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(uid, convey.ShouldEqual, 10086)
})
})
})
}
func TestDaoGetNameByUID(t *testing.T) {
convey.Convey("GetNameByUID", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
httpMock("GET", d.c.Host.Manager+_unamesURL).Reply(200).JSON(`{"code":0,"message":"0","data":{"10086":"cxf"}}`)
_, err := d.GetNameByUID(c, []int64{10086})
ctx.Convey("Then nil should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,65 @@
package dao
import (
"context"
"time"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_inVideoOperSQL = "INSERT INTO archive_video_oper (aid,uid,vid,status,content,attribute,last_id,remark) VALUES (?,?,?,?,?,?,?,?)"
_videoOperSQL = "SELECT id,aid,uid,vid,status,content,attribute,last_id,remark,ctime FROM archive_video_oper WHERE vid = ? ORDER BY ctime DESC"
_upVideoOperSQL = "UPDATE archive_video_oper SET last_id=? WHERE id=?"
)
// AddVideoOper insert archive_video_oper.
func (d *Dao) AddVideoOper(c context.Context, aid, adminID, vid int64, attribute int32, status int16, lastID int64, content, remark string) (id int64, err error) {
res, err := d.arcDB.Exec(c, _inVideoOperSQL, aid, adminID, vid, status, content, attribute, lastID, remark)
if err != nil {
log.Error("d.inVideoOper.Exec error(%v)", err)
return
}
id, err = res.LastInsertId()
return
}
//VideoOpers get video oper history list
func (d *Dao) VideoOpers(c context.Context, vid int64) (op []*model.VOper, uids []int64, err error) {
var (
rows *sql.Rows
ctime time.Time
)
op = []*model.VOper{}
uids = []int64{}
if rows, err = d.arcReadDB.Query(c, _videoOperSQL, vid); err != nil {
log.Error("d.arcReadDB.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
p := &model.VOper{}
if err = rows.Scan(&p.ID, &p.AID, &p.UID, &p.VID, &p.Status, &p.Content, &p.Attribute, &p.LastID, &p.Remark, &ctime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
p.CTime = ctime.Format("2006-01-02 15:04:05")
op = append(op, p)
uids = append(uids, p.UID)
}
return
}
// UpVideoOper update archive_video_oper last_id by id.
func (d *Dao) UpVideoOper(c context.Context, lastID, id int64) (rows int64, err error) {
res, err := d.arcDB.Exec(c, _upVideoOperSQL, lastID, id)
if err != nil {
log.Error("d.upVideoOper.Exec error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}

View File

@@ -0,0 +1,44 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAddVideoOper(t *testing.T) {
var (
c = context.TODO()
aid = int64(1)
adminID = int64(421)
vid = int64(1)
attribute = int32(0)
status = int16(0)
lastID = int64(0)
content = "测试"
remark = "测试"
)
convey.Convey("AddVideoOper", t, func(ctx convey.C) {
id, err := d.AddVideoOper(c, aid, adminID, vid, attribute, status, lastID, content, remark)
ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(id, convey.ShouldNotBeNil)
})
})
}
func TestDaoVideoOpers(t *testing.T) {
var (
c = context.TODO()
vid = int64(1)
)
convey.Convey("VideoOpers", t, func(ctx convey.C) {
op, uids, err := d.VideoOpers(c, vid)
ctx.Convey("Then err should be nil.op,uids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(uids, convey.ShouldNotBeNil)
ctx.So(op, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,32 @@
package dao
import (
"context"
"time"
"go-common/app/admin/main/videoup/model/archive"
"go-common/library/log"
)
const (
_taskTooksByHalfHourSQL = "SELECT id,m50,m60,m80,m90,type,ctime,mtime FROM task_dispatch_took WHERE type=2 AND ctime>=? AND ctime<=? ORDER BY ctime ASC"
)
// TaskTooksByHalfHour get TaskTooks by half hour
func (d *Dao) TaskTooksByHalfHour(c context.Context, stime time.Time, etime time.Time) (tooks []*archive.TaskTook, err error) {
rows, err := d.arcDB.Query(c, _taskTooksByHalfHourSQL, stime, etime)
if err != nil {
log.Error("d.TaskTooksByHalfHour.Query(%v,%v) error(%v)", stime, etime, err)
return
}
defer rows.Close()
for rows.Next() {
took := &archive.TaskTook{}
if err = rows.Scan(&took.ID, &took.M50, &took.M60, &took.M80, &took.M90, &took.TypeID, &took.Ctime, &took.Mtime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
tooks = append(tooks, took)
}
return
}

View File

@@ -0,0 +1,25 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoTaskTooksByHalfHour(t *testing.T) {
convey.Convey("TaskTooksByHalfHour", t, func(ctx convey.C) {
var (
c = context.Background()
stime = time.Now()
etime = time.Now()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.TaskTooksByHalfHour(c, stime, etime)
ctx.Convey("Then err should be nil.tooks should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,98 @@
package dao
import (
"context"
"net/url"
"strconv"
"time"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_searchURL = "/x/admin/search/log"
)
// OutTime 退出时间,es的group by查询,最大1000条
func (d *Dao) OutTime(c context.Context, ids []int64) (mcases map[int64][]interface{}, err error) {
mcases = make(map[int64][]interface{})
params := url.Values{}
params.Set("appid", "log_audit_group")
params.Set("group", "uid")
params.Set("uid", xstr.JoinInts(ids))
params.Set("business", strconv.Itoa(model.LogClientConsumer))
params.Set("action", strconv.Itoa(int(model.ActionHandsOFF)))
params.Set("ps", strconv.Itoa(len(ids)))
res := &model.SearchLogResult{}
if err = d.hclient.Get(c, d.c.Host.Search+_searchURL, "", params, &res); err != nil {
log.Error("log_audit_group d.hclient.Get error(%v)", err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("log_audit_group ecode:%v", res.Code)
return
}
for _, item := range res.Data.Result {
mcases[item.UID] = []interface{}{item.Ctime}
}
log.Info("log_audit_group get: %s params:%s ret:%v", _searchURL, params.Encode(), res)
return
}
// InQuitList 登入登出日志
func (d *Dao) InQuitList(c context.Context, uids []int64, bt, et string) (l []*model.InQuit, err error) {
params := url.Values{}
params.Set("appid", "log_audit")
params.Set("business", strconv.Itoa(model.LogClientConsumer))
if len(uids) > 0 {
params.Set("uid", xstr.JoinInts(uids))
}
if len(bt) > 0 && len(et) > 0 {
params.Set("ctime_from", bt)
params.Set("ctime_to", et)
}
params.Set("order", "ctime")
params.Set("sort", "desc")
params.Set("ps", "10000")
res := &model.SearchLogResult{}
if err = d.hclient.Get(c, d.c.Host.Search+_searchURL, "", params, res); err != nil {
log.Error("InQuitList d.hclient.Get error(%v)", err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("InQuitList ecode:%v", res.Code)
return
}
mapHelp := make(map[int64]*model.InQuit)
for i := len(res.Data.Result) - 1; i >= 0; i-- {
item := res.Data.Result[i]
if item.Action == "0" {
ctime, _ := time.Parse(model.TimeFormatSec, item.Ctime)
iqlog := &model.InQuit{
Date: ctime.Format("2006-01-02"),
UID: item.UID,
Uname: item.Uname,
InTime: ctime.Format("15:04:05"),
}
mapHelp[item.UID] = iqlog
l = append([]*model.InQuit{iqlog}, l[:]...)
}
if item.Action == "1" {
if iqlog, ok := mapHelp[item.UID]; ok {
ctime, _ := time.Parse(model.TimeFormatSec, item.Ctime)
if date := ctime.Format("2006-01-02"); date == iqlog.Date {
iqlog.OutTime = ctime.Format("15:04:05")
} else {
iqlog.OutTime = ctime.Format(model.TimeFormatSec)
}
}
}
}
return
}

View File

@@ -0,0 +1,45 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoOutTime(t *testing.T) {
convey.Convey("OutTime", t, func(ctx convey.C) {
var (
c = context.Background()
ids = []int64{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
httpMock("GET", d.c.Host.Search+_searchURL).Reply(200).JSON(`{"code":0,"data":{"result":[{"uid":0}]}}`)
mcases, err := d.OutTime(c, ids)
ctx.Convey("Then err should be nil.mcases should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(mcases, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInQuitList(t *testing.T) {
convey.Convey("InQuitList", t, func(ctx convey.C) {
var (
c = context.Background()
uids = []int64{}
bt = ""
et = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
httpMock("GET", d.c.Host.Search+_searchURL).Reply(200).JSON(`{"code":0,"data":{"result":[{"uid":0,"action":"0"}]}}`)
l, err := d.InQuitList(c, uids, bt, et)
ctx.Convey("Then err should be nil.l should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(l, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,346 @@
package dao
import (
"context"
xsql "database/sql"
"encoding/json"
"fmt"
"strings"
"time"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_upTaskByIDSQL = "UPDATE task_dispatch SET %s WHERE id=?"
_upGtimeByIDSQL = "UPDATE task_dispatch SET gtime=? WHERE id=?"
_releaseByIDSQL = "UPDATE task_dispatch SET subject=0,state=0,uid=0,gtime='0000-00-00 00:00:00' WHERE id=?"
_releaseMtimeSQL = "UPDATE task_dispatch SET subject=0,state=0,uid=0,gtime='0000-00-00 00:00:00' WHERE id IN (%s) AND mtime<=?"
_timeOutTaskSQL = "SELECT id,cid,subject,mtime FROM task_dispatch WHERE (state=1 AND mtime<?) OR (state=0 AND uid<>0 AND ctime<?)"
_getRelTaskSQL = "SELECT id,cid,subject,mtime,gtime FROM task_dispatch WHERE state IN (0,1) AND uid=?"
_releaseSpecialSQL = "UPDATE task_dispatch SET subject=0,state=0,uid=0 WHERE id=? AND gtime='0000-00-00 00:00:00' AND mtime<=? AND state=? AND uid=?"
)
// UpGtimeByID update gtime
func (d *Dao) UpGtimeByID(c context.Context, id int64, gtime string) (rows int64, err error) {
var res xsql.Result
if res, err = d.arcDB.Exec(c, _upGtimeByIDSQL, gtime, id); err != nil {
log.Error("d.arcDB.Exec(%s, %v, %d) error(%v)", _upGtimeByIDSQL, gtime, id)
return
}
return res.RowsAffected()
}
// TxUpTaskByID 更新任务状态
func (d *Dao) TxUpTaskByID(tx *sql.Tx, id int64, paras map[string]interface{}) (rows int64, err error) {
arrSet := []string{}
arrParas := []interface{}{}
for k, v := range paras {
arrSet = append(arrSet, k+"=?")
arrParas = append(arrParas, v)
}
arrParas = append(arrParas, id)
sqlstring := fmt.Sprintf(_upTaskByIDSQL, strings.Join(arrSet, ","))
res, err := tx.Exec(sqlstring, arrParas...)
if err != nil {
log.Error("tx.Exec(%v %v) error(%v)", sqlstring, arrParas, err)
return
}
return res.RowsAffected()
}
// TxReleaseByID 释放指定任务
func (d *Dao) TxReleaseByID(tx *sql.Tx, id int64) (rows int64, err error) {
res, err := tx.Exec(_releaseByIDSQL, id)
if err != nil {
log.Error("tx.Exec(%s, %d) error(%v)", _releaseByIDSQL, id, err)
return
}
return res.RowsAffected()
}
// MulReleaseMtime 批量释放任务,加时间防止释放错误
func (d *Dao) MulReleaseMtime(c context.Context, ids []int64, mtime time.Time) (rows int64, err error) {
sqlstring := fmt.Sprintf(_releaseMtimeSQL, xstr.JoinInts(ids))
res, err := d.arcDB.Exec(c, sqlstring, mtime)
if err != nil {
log.Error("tx.Exec(%s, %v) error(%v)", sqlstring, mtime, err)
return
}
return res.RowsAffected()
}
// GetTimeOutTask 释放正在处理且超时的,释放指派后但长时间未审核的
func (d *Dao) GetTimeOutTask(c context.Context) (rts []*model.TaskForLog, err error) {
var (
rows *sql.Rows
)
if rows, err = d.arcDB.Query(c, _timeOutTaskSQL, time.Now().Add(-10*time.Minute), time.Now().Add(-80*time.Minute)); err != nil {
log.Error("d.arcDB.Query(%s) error(%v)", _timeOutTaskSQL, err)
return
}
defer rows.Close()
for rows.Next() {
rt := &model.TaskForLog{}
if err = rows.Scan(&rt.ID, &rt.Cid, &rt.Subject, &rt.Mtime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
rts = append(rts, rt)
}
return
}
// GetRelTask 用户登出或者主动释放(分配给该用户的都释放)
func (d *Dao) GetRelTask(c context.Context, uid int64) (rts []*model.TaskForLog, lastid int64, err error) {
var (
gtime time.Time
rows *sql.Rows
)
if rows, err = d.arcDB.Query(c, _getRelTaskSQL, uid); err != nil {
log.Error("d.arcDB.Query(%s, %d) error(%v)", _getRelTaskSQL, uid, err)
return
}
defer rows.Close()
for rows.Next() {
rt := &model.TaskForLog{}
if err = rows.Scan(&rt.ID, &rt.Cid, &rt.Subject, &rt.Mtime, &gtime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
if gtime.IsZero() {
rts = append(rts, rt)
} else {
lastid = rt.ID
}
}
return
}
// TxReleaseSpecial 延时固定时间释放的任务,需要校验释放时的状态,时间,认领人等
func (d *Dao) TxReleaseSpecial(tx *sql.Tx, mtime time.Time, state int8, taskid, uid int64) (rows int64, err error) {
res, err := tx.Exec(_releaseSpecialSQL, taskid, mtime, state, uid)
if err != nil {
log.Error("tx.Exec(%s, %d, %v, %d, %d) error(%v)", _releaseSpecialSQL, taskid, mtime, state, uid, err)
return
}
return res.RowsAffected()
}
const (
_userUndoneSpecifiedSQL = "SELECT id,pool,subject,adminid,aid,cid,uid,state,ctime,mtime FROM task_dispatch WHERE uid = ? AND state !=2 AND subject = 1"
_dispatchTaskSQL = "SELECT id,cid,mtime FROM task_dispatch WHERE uid in (0,?) AND state = 0 ORDER BY `weight` DESC,`subject` DESC,`id` ASC limit 8"
_upDispatchTaskSQL = "UPDATE task_dispatch SET state=1,uid=?,gtime='0000-00-00 00:00:00' WHERE id IN (%s) AND state=0"
_getNextTaskSQL = "SELECT id,pool,subject,adminid,aid,cid,uid,state,utime,ctime,mtime,dtime,gtime,weight FROM task_dispatch WHERE uid=? AND state = 1 ORDER BY `weight` DESC,`subject` DESC,`id` ASC limit 1"
_upTaskGtimeSQL = "UPDATE task_dispatch SET gtime=? WHERE id=?"
_listByConditionSQL = "SELECT id,pool,subject,adminid,aid,cid,uid,state,utime,ctime,mtime,dtime,gtime,weight FROM task_dispatch where %s order by %s %s"
_taskByIDSQL = "SELECT id,pool,subject,adminid,aid,cid,uid,state,utime,ctime,mtime,dtime,gtime,ptime,weight FROM task_dispatch WHERE id =? union " +
"SELECT task_id as id,pool,subject,adminid,aid,cid,uid,state,utime,ctime,mtime,dtime,gtime,ptime,weight FROM task_dispatch_done WHERE task_id=?"
_getWeightDBSQL = "SELECT t.id,t.state,a.mid,t.ctime,t.upspecial,t.ptime,e.description FROM `task_dispatch` AS t " +
"LEFT JOIN `task_dispatch_extend` AS e ON t.id=e.task_id INNER JOIN archive as a ON a.id=t.aid WHERE t.id IN (%s)"
)
// UserUndoneSpecTask get undone dispatch which belongs to someone.
func (d *Dao) UserUndoneSpecTask(c context.Context, uid int64) (tasks []*model.Task, err error) {
rows, err := d.arcDB.Query(c, _userUndoneSpecifiedSQL, uid)
if err != nil {
log.Error("d.arcDB.Query() error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
t := &model.Task{}
if err = rows.Scan(&t.ID, &t.Pool, &t.Subject, &t.AdminID, &t.Aid, &t.Cid, &t.UID, &t.State, &t.CTime, &t.MTime); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("row.Scan(%d) error(%v)", err)
return
}
tasks = append(tasks, t)
}
return
}
// GetDispatchTask 获取抢占到的任务(用于记录日志)
func (d *Dao) GetDispatchTask(c context.Context, uid int64) (tls []*model.TaskForLog, err error) {
rows, err := d.arcDB.Query(c, _dispatchTaskSQL, uid)
if err != nil {
log.Error("d.arcDB.Query(%s, %d) error(%v)", _dispatchTaskSQL, uid, err)
return
}
defer rows.Close()
for rows.Next() {
taskLog := &model.TaskForLog{}
if err = rows.Scan(&taskLog.ID, &taskLog.Cid, &taskLog.Mtime); err != nil {
log.Error("rows.Scan(%s, %d) error(%v)", _dispatchTaskSQL, uid, err)
return
}
tls = append(tls, taskLog)
}
return
}
// UpDispatchTask 抢占任务
func (d *Dao) UpDispatchTask(c context.Context, uid int64, ids []int64) (rows int64, err error) {
var (
res xsql.Result
sqlstring = fmt.Sprintf(_upDispatchTaskSQL, xstr.JoinInts(ids))
)
res, err = d.arcDB.Exec(c, sqlstring, uid)
if err != nil {
log.Error("d.arcDB.Exec(%s %d %v) error(%v)", sqlstring, uid, err)
return
}
return res.RowsAffected()
}
// GetNextTask 获取一条任务
func (d *Dao) GetNextTask(c context.Context, uid int64) (task *model.Task, err error) {
task = new(model.Task)
err = d.arcDB.QueryRow(c, _getNextTaskSQL, uid).Scan(&task.ID, &task.Pool, &task.Subject, &task.AdminID,
&task.Aid, &task.Cid, &task.UID, &task.State, &task.UTime, &task.CTime, &task.MTime, &task.DTime, &task.GTime, &task.Weight)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
log.Error("db.QueryRow(%d) error(%v)", err)
return nil, err
}
if task.GTime.TimeValue().IsZero() {
timeNow := time.Now()
_, err = d.arcDB.Exec(c, _upTaskGtimeSQL, timeNow, task.ID)
if err != nil {
log.Error("d.arcDB.Exec(%v,%d) error(%v)", timeNow, task.ID, err)
return nil, err
}
task.GTime = model.NewFormatTime(timeNow)
}
return
}
// TaskByID get task
func (d *Dao) TaskByID(c context.Context, id int64) (task *model.Task, err error) {
task = new(model.Task)
err = d.arcDB.QueryRow(c, _taskByIDSQL, id, id).Scan(&task.ID, &task.Pool, &task.Subject, &task.AdminID,
&task.Aid, &task.Cid, &task.UID, &task.State, &task.UTime, &task.CTime, &task.MTime, &task.DTime, &task.GTime, &task.PTime, &task.Weight)
if err != nil {
if err == sql.ErrNoRows {
err = nil
task = nil
return
}
log.Error("db.QueryRow(%d) error(%v)", id, err)
return nil, err
}
return
}
// ListByCondition 从数据库获取读取任务列表
func (d *Dao) ListByCondition(c context.Context, uid int64, pn, ps int, ltype, leader int8) (tasks []*model.Task, err error) {
var task *model.Task
tasks = []*model.Task{}
if !model.IsDispatch(ltype) {
log.Error("ListByCondition listtype(%d) error", ltype)
return
}
listSQL := d.sqlHelper(uid, pn, ps, ltype, leader)
rows, err := d.arcDB.Query(c, listSQL)
if err != nil {
log.Error("rddb.Query(%s) error(%v)", listSQL, err)
return
}
defer rows.Close()
for rows.Next() {
task = &model.Task{}
err = rows.Scan(&task.ID, &task.Pool, &task.Subject, &task.AdminID,
&task.Aid, &task.Cid, &task.UID, &task.State, &task.UTime, &task.CTime, &task.MTime, &task.DTime, &task.GTime, &task.Weight)
if err != nil {
log.Error("rows.Scan(%s) error(%v)", listSQL, err)
return nil, nil
}
tasks = append(tasks, task)
}
return
}
func (d *Dao) sqlHelper(uid int64, pn, ps int, ltype int8, leader int8) string {
var (
wherecase []string
ordercase []string
limitStr string
whereStr string
orderStr string
)
limitStr = fmt.Sprintf("LIMIT %d,%d", (pn-1)*ps, ps)
if uid != 0 && (ltype != model.TypeRealTime && leader != 1) { //实时任务或者组长不区分uid
wherecase = append(wherecase, fmt.Sprintf("uid=%d", uid))
}
ordercase = append(ordercase, "weight desc,ctime asc")
switch ltype {
case model.TypeRealTime:
wherecase = append(wherecase, "state=0")
case model.TypeDispatched:
wherecase = append(wherecase, "state=1 AND subject=0")
ordercase = append(ordercase, "utime desc")
case model.TypeDelay:
wherecase = append(wherecase, "state=3")
ordercase = append(ordercase, "dtime asc")
case model.TypeReview:
wherecase = append(wherecase, "state=5")
ordercase = append(ordercase, "mtime asc")
case model.TypeSpecialWait:
wherecase = append(wherecase, "state=1 AND subject=1")
ordercase = append(ordercase, "utime desc")
default:
wherecase = append(wherecase, "state=0")
}
whereStr = strings.Join(wherecase, " AND ")
orderStr = strings.Join(ordercase, ",")
return fmt.Sprintf(_listByConditionSQL, whereStr, orderStr, limitStr)
}
// GetWeightDB 从数据库读取权重配置
func (d *Dao) GetWeightDB(c context.Context, ids []int64) (mcases map[int64]*model.TaskPriority, err error) {
var (
rows *sql.Rows
desc xsql.NullString
)
sqlstring := fmt.Sprintf(_getWeightDBSQL, xstr.JoinInts(ids))
if rows, err = d.arcDB.Query(c, sqlstring); err != nil {
log.Error("d.arcDB.Query(%s) error(%v)", sqlstring, err)
return
}
defer rows.Close()
mcases = make(map[int64]*model.TaskPriority)
for rows.Next() {
tp := new(model.TaskPriority)
if err = rows.Scan(&tp.TaskID, &tp.State, &tp.Mid, &tp.Ctime, &tp.Special, &tp.Ptime, &desc); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
if desc.Valid && len(desc.String) > 0 {
if err = json.Unmarshal([]byte(desc.String), &(tp.CfItems)); err != nil {
log.Error("json.Unmarshal error(%v)", err)
return
}
}
mcases[tp.TaskID] = tp
}
return
}

View File

@@ -0,0 +1,184 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoUpGtimeByID(t *testing.T) {
convey.Convey("UpGtimeByID", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
gtime = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.UpGtimeByID(c, id, gtime)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUserUndoneSpecTask(t *testing.T) {
convey.Convey("UserUndoneSpecTask", t, func(ctx convey.C) {
var (
c = context.Background()
uid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.UserUndoneSpecTask(c, uid)
ctx.Convey("Then err should be nil.tasks should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoListByCondition(t *testing.T) {
convey.Convey("ListByCondition", t, func(ctx convey.C) {
var (
c = context.Background()
uid = int64(0)
pn = int(0)
ps = int(0)
ltype = int8(0)
leader = int8(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
tasks, err := d.ListByCondition(c, uid, pn, ps, ltype, leader)
ctx.Convey("Then err should be nil.tasks should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(tasks, convey.ShouldNotBeNil)
})
})
})
}
func TestDaosqlHelper(t *testing.T) {
convey.Convey("sqlHelper", t, func(ctx convey.C) {
var (
uid = int64(0)
pn = int(0)
ps = int(0)
ltype = int8(0)
leader = int8(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := d.sqlHelper(uid, pn, ps, ltype, leader)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetWeightDB(t *testing.T) {
convey.Convey("GetWeightDB", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.GetWeightDB(c, []int64{0, 1, 2})
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoTaskByID(t *testing.T) {
convey.Convey("TaskByID", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.TaskByID(c, 0)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoTxUpTaskByID(t *testing.T) {
convey.Convey("TxUpTaskByID", t, func(ctx convey.C) {
var (
c = context.Background()
)
tx, _ := d.BeginTran(c)
defer tx.Commit()
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.TxUpTaskByID(tx, 0, map[string]interface{}{"uid": 0})
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoTxReleaseByID(t *testing.T) {
convey.Convey("TxReleaseByID", t, func(ctx convey.C) {
var (
c = context.Background()
)
tx, _ := d.BeginTran(c)
defer tx.Commit()
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.TxReleaseByID(tx, 0)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoMulReleaseMtime(t *testing.T) {
convey.Convey("MulReleaseMtime", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.MulReleaseMtime(c, []int64{1}, time.Now())
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoGetTimeOutTask(t *testing.T) {
convey.Convey("GetTimeOutTask", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.GetTimeOutTask(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoGetRelTask(t *testing.T) {
convey.Convey("GetRelTask", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, _, err := d.GetRelTask(c, 0)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,61 @@
package dao
import (
"context"
"fmt"
"strings"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_inTaskHisSQL = "INSERT INTO task_oper_history(pool,action,task_id,cid,uid,result,reason,utime) VALUE (?,?,?,?,?,?,?,?)"
_mulinTaskHisSQL = "INSERT INTO task_oper_history(action,task_id,cid,uid) VALUES "
)
// TxAddTaskHis add task oper history
func (d *Dao) TxAddTaskHis(tx *sql.Tx, pool, action int8, taskID, cid, uid, utime int64, result int16, reason string) (rows int64, err error) {
res, err := tx.Exec(_inTaskHisSQL, pool, action, taskID, cid, uid, result, reason, utime)
if err != nil {
log.Error("tx.Exec(%s) error(%v)", _inTaskHisSQL, err)
return
}
return res.RowsAffected()
}
// AddTaskHis 非事务
func (d *Dao) AddTaskHis(c context.Context, pool, action int8, taskID, cid, uid, utime int64, result int16, reason string) (rows int64, err error) {
res, err := d.arcDB.Exec(c, _inTaskHisSQL, pool, action, taskID, cid, uid, result, reason, utime)
if err != nil {
log.Error("d.arcDB.Exec(%s) error(%v)", _inTaskHisSQL, err)
return
}
return res.RowsAffected()
}
// MulAddTaskHis 批量插入日志
func (d *Dao) MulAddTaskHis(c context.Context, tls []*model.TaskForLog, action int8, uid int64) (rows int64, err error) {
params := []string{}
for _, item := range tls {
var itemstr string
itemstr += fmt.Sprintf("(%d,", action)
itemstr += fmt.Sprintf("%d,", item.ID)
itemstr += fmt.Sprintf("%d,", item.Cid)
itemstr += fmt.Sprintf("%d)", uid)
params = append(params, itemstr)
}
if len(params) == 0 {
log.Warn("MulAddTaskHis empty params")
return
}
sqlsring := strings.Join(params, ",")
res, err := d.arcDB.Exec(c, _mulinTaskHisSQL+sqlsring)
if err != nil {
log.Error("d.arcDB.Exec(%s, %s) error(%v)", _mulinTaskHisSQL, sqlsring, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,50 @@
package dao
import (
"context"
"go-common/app/admin/main/videoup-task/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAddTaskHis(t *testing.T) {
convey.Convey("AddTaskHis", t, func(ctx convey.C) {
var (
c = context.Background()
pool = int8(0)
action = int8(0)
taskID = int64(0)
cid = int64(0)
uid = int64(0)
utime = int64(0)
result = int16(0)
reason = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.AddTaskHis(c, pool, action, taskID, cid, uid, utime, result, reason)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoMulAddTaskHis(t *testing.T) {
convey.Convey("MulAddTaskHis", t, func(ctx convey.C) {
var (
c = context.Background()
tls = []*model.TaskForLog{}
action = int8(0)
uid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.MulAddTaskHis(c, tls, action, uid)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,114 @@
package dao
import (
"context"
"time"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_avgUtimeSQL = "SELECT IFNULL(avg(utime),0.0) FROM task_oper_history WHERE action=2 AND uid=? AND ctime>=? AND ctime<?"
_sumDurationSQL = "SELECT IFNULL(sum(v.duration),0) FROM task_oper_history as t LEFT JOIN video as v ON t.cid=v.id WHERE action IN (2,5) AND t.uid=? AND t.ctime>=? AND t.ctime<?"
_actionCountSQL = "SELECT action,count(*) FROM task_oper_history WHERE uid=? AND ctime>=? AND ctime<? AND action IN (2,5,6,7) GROUP BY action"
_passCountSQL = "SELECT count(*) FROM task_oper_history WHERE action IN (2,5) AND uid=? AND ctime>=? AND ctime<? AND result=0"
_subjectCountSQL = `SELECT count(*) FROM (
SELECT id FROM task_dispatch WHERE uid=? AND state=? AND mtime>=? AND ctime<? AND subject=1
UNION ALL SELECT task_id as id FROM task_dispatch_done WHERE uid=? AND state=? AND mtime>=? AND ctime<? AND subject=1) as t
`
_activeUidsSQL = "SELECT DISTINCT uid from task_oper_history WHERE ctime>=? AND ctime<? AND uid!=0 AND action IN (2,5)"
)
// AvgUtimeByUID 平均处理耗时, 只统计action=2的
func (d *Dao) AvgUtimeByUID(c context.Context, uid int64, stime, etime time.Time) (utime float64, err error) {
if err = d.arcReadDB.QueryRow(c, _avgUtimeSQL, uid, stime, etime).Scan(&utime); err != nil {
if err == sql.ErrNoRows {
err = nil
}
log.Error("d.arcReadDB.QueryRow error(%v)", err)
}
return
}
// SumDurationByUID 视频总时长统计action=2,5的
func (d *Dao) SumDurationByUID(c context.Context, uid int64, stime, etime time.Time) (duration int64, err error) {
if err = d.arcReadDB.QueryRow(c, _sumDurationSQL, uid, stime, etime).Scan(&duration); err != nil {
if err == sql.ErrNoRows {
err = nil
}
log.Error("d.arcReadDB.QueryRow error(%v)", err)
}
return
}
// ActionCountByUID 操作个数统计
func (d *Dao) ActionCountByUID(c context.Context, uid int64, stime, etime time.Time) (mapAction map[int8]int64, err error) {
mapAction = make(map[int8]int64)
rows, err := d.arcReadDB.Query(c, _actionCountSQL, uid, stime, etime)
if err != nil {
log.Error("d.arcReadDB.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var (
action int8
count int64
)
if err = rows.Scan(&action, &count); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
mapAction[action] = count
}
return
}
// PassCountByUID 总过审个数
func (d *Dao) PassCountByUID(c context.Context, uid int64, stime, etime time.Time) (count int64, err error) {
if err = d.arcReadDB.QueryRow(c, _passCountSQL, uid, stime, etime).Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
}
log.Error("d.arcReadDB.QueryRow error(%v)", err)
}
return
}
// SubjectCountByUID 总指派个数
func (d *Dao) SubjectCountByUID(c context.Context, uid int64, stime, etime time.Time) (count int64, err error) {
if err = d.arcReadDB.QueryRow(c, _subjectCountSQL, uid, model.TaskStateCompleted, stime, etime, uid, model.TaskStateCompleted, stime, etime).Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
}
log.Error("d.arcReadDB.QueryRow error(%v)", err)
}
return
}
// ActiveUids 统计24小时内有提交的
func (d *Dao) ActiveUids(c context.Context, stime, etime time.Time) (uids []int64, err error) {
st := time.Now()
defer func() {
log.Info("ActiveUids du(%.2fm) wait(%.2fs)", etime.Sub(stime).Minutes(), time.Since(st).Seconds())
}()
rows, err := d.arcReadDB.Query(c, _activeUidsSQL, stime, etime)
if err != nil {
log.Error("d.arcReadDB.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var uid int64
if err = rows.Scan(&uid); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
uids = append(uids, uid)
}
return
}

View File

@@ -0,0 +1,103 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAvgUtimeByUID(t *testing.T) {
var (
c = context.TODO()
uid = int64(421)
stime = time.Now()
etime = time.Now()
)
convey.Convey("AvgUtimeByUID", t, func(ctx convey.C) {
utime, err := d.AvgUtimeByUID(c, uid, stime, etime)
ctx.Convey("Then err should be nil.utime should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(utime, convey.ShouldNotBeNil)
})
})
}
func TestDaoSumDurationByUID(t *testing.T) {
var (
c = context.TODO()
uid = int64(421)
stime = time.Now()
etime = time.Now()
)
convey.Convey("SumDurationByUID", t, func(ctx convey.C) {
duration, err := d.SumDurationByUID(c, uid, stime, etime)
ctx.Convey("Then err should be nil.duration should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(duration, convey.ShouldNotBeNil)
})
})
}
func TestDaoActionCountByUID(t *testing.T) {
var (
c = context.TODO()
uid = int64(421)
stime = time.Now()
etime = time.Now()
)
convey.Convey("ActionCountByUID", t, func(ctx convey.C) {
mapAction, err := d.ActionCountByUID(c, uid, stime, etime)
ctx.Convey("Then err should be nil.mapAction should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(mapAction, convey.ShouldNotBeNil)
})
})
}
func TestDaoPassCountByUID(t *testing.T) {
var (
c = context.TODO()
uid = int64(421)
stime = time.Now()
etime = time.Now()
)
convey.Convey("PassCountByUID", t, func(ctx convey.C) {
count, err := d.PassCountByUID(c, uid, stime, etime)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
}
func TestDaoSubjectCountByUID(t *testing.T) {
var (
c = context.TODO()
uid = int64(421)
stime = time.Now()
etime = time.Now()
)
convey.Convey("SubjectCountByUID", t, func(ctx convey.C) {
count, err := d.SubjectCountByUID(c, uid, stime, etime)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
}
func TestDaoActiveUids(t *testing.T) {
var (
c = context.TODO()
stime = time.Now().AddDate(-1, 0, 0)
etime = time.Now()
)
convey.Convey("ActiveUids", t, func(ctx convey.C) {
_, err := d.ActiveUids(c, stime, etime)
ctx.Convey("Then err should be nil.uids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,39 @@
package dao
import (
"context"
"time"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/database/sql"
)
const (
_inTaskQA = "INSERT INTO task_qa(state,type,detail_id,uid,ctime,mtime) VALUES(?,?,?,?,?,?)"
_upTaskQA = "UPDATE task_qa SET state=?, ftime=?, mtime=? WHERE id=?"
)
//InTaskQA insert a qa task
func (d *Dao) InTaskQA(tx *sql.Tx, uid int64, detailID int64, taskType int8) (id int64, err error) {
now := time.Now()
res, err := tx.Exec(_inTaskQA, model.QAStateWait, taskType, detailID, uid, now, now)
if err != nil {
PromeErr("arcdb: insert", "InTaskQA tx.Exe error(%v) uid(%d) detailid(%d)", err, uid, detailID)
return
}
id, err = res.LastInsertId()
return
}
//UpTask update qa task
func (d *Dao) UpTask(ctx context.Context, id int64, state int16, ftime time.Time) (rows int64, err error) {
res, err := d.arcDB.Exec(ctx, _upTaskQA, state, ftime, ftime, id)
if err != nil {
PromeErr("arcdb: update", "UpTask d.arcDB.Exec error(%v) id(%d) state(%d)", err, id, state)
return
}
rows, err = res.RowsAffected()
return
}

View File

@@ -0,0 +1,45 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoInTaskQA(t *testing.T) {
var (
tx, _ = d.BeginTran(context.TODO())
uid = int64(421)
detailID = int64(1)
taskType = int8(1)
)
convey.Convey("InTaskQA", t, func(ctx convey.C) {
id, err := d.InTaskQA(tx, uid, detailID, taskType)
if err == nil {
tx.Commit()
} else {
tx.Rollback()
}
ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(id, convey.ShouldNotBeNil)
})
})
}
func TestDaoUpTask(t *testing.T) {
var (
c = context.TODO()
id = int64(41)
state = int16(2)
ftime = time.Now()
)
convey.Convey("UpTask", t, func(ctx convey.C) {
_, err := d.UpTask(c, id, state, ftime)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,198 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"time"
xsql "database/sql"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_inQAVideo = "INSERT INTO task_qa_video(cid,aid,task_id,task_utime,attribute,mid,fans,up_groups,arc_title,arc_typeid,audit_status,audit_tagid,audit_submit,audit_details,ctime,mtime) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
_QATaskVideo = `SELECT qa.id, qa.state, qa.type, qa.detail_id, qa.uid, qa.ftime, qa.ctime,
qav.cid, qav.aid, coalesce(qav.task_id, 0) task_id, qav.task_utime, qav.attribute, qav.audit_tagid, qav.arc_title, qav.arc_typeid, qav.audit_status, qav.audit_submit, qav.audit_details, qav.mid, qav.fans, qav.up_groups
FROM task_qa qa LEFT JOIN task_qa_video qav ON qa.detail_id = qav.id WHERE qa.id = ? LIMIT 1`
_QATaskVideoSimple = `SELECT qa.id, qa.state, qa.type, qa.detail_id, qa.uid, qa.ftime, qa.ctime,
qav.cid, qav.aid, coalesce(qav.task_id, 0) task_id,qav.task_utime, qav.attribute, qav.audit_tagid, qav.arc_title, qav.arc_typeid, qav.audit_status, qav.audit_submit, qav.mid, qav.fans, qav.up_groups
FROM task_qa qa LEFT JOIN task_qa_video qav ON qa.detail_id = qav.id WHERE qa.id = ? LIMIT 1`
_QAVideoDetail = "SELECT qa.id, qav.id, qav.mid, qav.task_utime FROM task_qa qa LEFT JOIN task_qa_video qav ON qa.detail_id = qav.id WHERE qa.id IN (%s)"
_QAVideoByTASKID = `SELECT id FROM task_qa_video WHERE aid=? AND cid=? AND task_id=?`
_upQAVideoUTime = "UPDATE task_qa_video SET task_utime=? WHERE aid=? AND cid=? AND task_id=?"
_delQAVideo = "DELETE FROM task_qa_video WHERE mtime<? LIMIT ?"
_delQATask = "DELETE FROM task_qa WHERE mtime<? LIMIT ?"
)
//InsertQAVideo insert qa video detail
func (d *Dao) InsertQAVideo(tx *sql.Tx, dt *model.VideoDetail) (id int64, err error) {
var (
groups string
)
if len(dt.UPGroups) > 0 {
var b []byte
b, err = json.Marshal(dt.UPGroups)
if err != nil {
log.Error("InsertQAVideo json.Marshal(%v) error(%v) aid(%d) cid(%d)", dt.UPGroups, err, dt.AID, dt.CID)
return
}
groups = string(b)
}
now := time.Now()
res, err := tx.Exec(_inQAVideo, dt.CID, dt.AID, dt.TaskID, dt.TaskUTime, dt.Attribute, dt.MID, dt.Fans, groups,
dt.ArcTitle, dt.ArcTypeID, dt.AuditStatus, dt.TagID, dt.AuditSubmit, dt.AuditDetails, now, now)
if err != nil {
PromeErr("arcdb: exec", "InsertQAVideo tx.Exec error(%v) aid(%d) cid(%d)", err, dt.AID, dt.CID)
return
}
id, err = res.LastInsertId()
return
}
//QATaskVideoByID get by id
func (d *Dao) QATaskVideoByID(ctx context.Context, id int64) (q *model.QATaskVideo, err error) {
var (
groups string
)
q = new(model.QATaskVideo)
if err = d.arcReadDB.QueryRow(ctx, _QATaskVideo, id).Scan(&q.ID, &q.State, &q.Type, &q.DetailID, &q.UID, &q.FTime, &q.CTime,
&q.CID, &q.AID, &q.TaskID, &q.TaskUTime, &q.Attribute, &q.TagID, &q.ArcTitle, &q.ArcTypeID, &q.AuditStatus, &q.AuditSubmit, &q.AuditDetails,
&q.MID, &q.Fans, &groups); err != nil {
if err == sql.ErrNoRows {
err = nil
q = nil
} else {
PromeErr("arcReaddb: scan", "QATaskVideoByID row.Scan error(%v) id(%d)", err, id)
}
return
}
q.UPGroups = []int64{}
if groups != "" {
if err = json.Unmarshal([]byte(groups), &q.UPGroups); err != nil {
log.Error("QATaskVideoByID json.Unmarshal(%s) error(%v) id(%d)", groups, err, id)
return
}
}
return
}
// QATaskVideoSimpleByID get without audit_details by id
func (d *Dao) QATaskVideoSimpleByID(ctx context.Context, id int64) (q *model.QATaskVideo, err error) {
var (
groups string
)
q = new(model.QATaskVideo)
if err = d.arcReadDB.QueryRow(ctx, _QATaskVideoSimple, id).Scan(&q.ID, &q.State, &q.Type, &q.DetailID, &q.UID, &q.FTime, &q.CTime,
&q.CID, &q.AID, &q.TaskID, &q.TaskUTime, &q.Attribute, &q.TagID, &q.ArcTitle, &q.ArcTypeID, &q.AuditStatus, &q.AuditSubmit,
&q.MID, &q.Fans, &groups); err != nil {
if err == sql.ErrNoRows {
err = nil
q = nil
} else {
PromeErr("arcReaddb: scan", "QATaskVideoSimpleByID row.Scan error(%v) id(%d)", err, id)
}
return
}
q.UPGroups = []int64{}
if groups != "" {
if err = json.Unmarshal([]byte(groups), &q.UPGroups); err != nil {
log.Error("QATaskVideoByID json.Unmarshal(%s) error(%v) id(%d)", groups, err, id)
return
}
}
return
}
//QAVideoDetail get detail id & task_utime
func (d *Dao) QAVideoDetail(ctx context.Context, ids []int64) (list map[int64]map[string]int64, arr []int64, err error) {
var (
idStr string
rows *sql.Rows
)
list = map[int64]map[string]int64{}
arr = make([]int64, 0)
idStr = xstr.JoinInts(ids)
if rows, err = d.arcReadDB.Query(ctx, fmt.Sprintf(_QAVideoDetail, idStr)); err != nil {
PromeErr("arcReaddb: query", "QAVideoDetail d.arcReadDB.Query error(%v) ids(%s)", err, idStr)
return
}
defer rows.Close()
for rows.Next() {
var (
id, detailID, mid, taskUTime int64
)
if err = rows.Scan(&id, &detailID, &mid, &taskUTime); err != nil {
PromeErr("arcReaddb: scan", "QAVideoDetail rows.Scan error(%v) ids(%s)", err, idStr)
return
}
list[id] = map[string]int64{
"detail_id": detailID,
"mid": mid,
"task_utime": taskUTime,
}
arr = append(arr, mid)
}
return
}
//GetQAVideoID get id by aid & cid & taskid
func (d *Dao) GetQAVideoID(ctx context.Context, aid int64, cid int64, taskID int64) (id int64, err error) {
if err = d.arcDB.QueryRow(ctx, _QAVideoByTASKID, aid, cid, taskID).Scan(&id); err != nil {
if err == sql.ErrNoRows {
id = 0
err = nil
} else {
log.Error("GetQAVideoID scan error(%v) aid(%d) cid(%d) taskid(%d)", err, aid, cid, taskID)
}
}
return
}
//UpdateQAVideoUTime update task_utime
func (d *Dao) UpdateQAVideoUTime(ctx context.Context, aid int64, cid int64, taskID, utime int64) (err error) {
if _, err = d.arcDB.Exec(ctx, _upQAVideoUTime, utime, aid, cid, taskID); err != nil {
PromeErr("arcdb: exec", "UpdateQAVideoUTime error(%v) aid(%d) cid(%d) taskid(%d)", err, aid, cid, taskID)
}
return
}
//DelQAVideo 删除数据
func (d *Dao) DelQAVideo(ctx context.Context, mtime time.Time, limit int) (rows int64, err error) {
var (
result xsql.Result
)
if result, err = d.arcDB.Exec(ctx, _delQAVideo, mtime, limit); err != nil {
PromeErr("arcdb: exec", "DelQAVideo error(%v) mtime(%v)", err, mtime)
return
}
rows, err = result.RowsAffected()
return
}
//DelQATask 删除数据
func (d *Dao) DelQATask(ctx context.Context, mtime time.Time, limit int) (rows int64, err error) {
var (
result xsql.Result
)
if result, err = d.arcDB.Exec(ctx, _delQATask, mtime, limit); err != nil {
PromeErr("arcdb: exec", "DelQATask error(%v) mtime(%v)", err, mtime)
return
}
rows, err = result.RowsAffected()
return
}

View File

@@ -0,0 +1,130 @@
package dao
import (
"context"
"go-common/app/admin/main/videoup-task/model"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoInsertQAVideo(t *testing.T) {
var (
tx, _ = d.BeginTran(context.TODO())
dt = &model.VideoDetail{
UPGroups: []int64{0},
}
)
convey.Convey("InsertQAVideo", t, func(ctx convey.C) {
id, err := d.InsertQAVideo(tx, dt)
if err == nil {
tx.Commit()
} else {
tx.Rollback()
}
ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(id, convey.ShouldNotBeNil)
})
})
}
func TestDaoQAVideoDetail(t *testing.T) {
var (
c = context.TODO()
ids = []int64{437}
)
convey.Convey("QAVideoDetail", t, func(ctx convey.C) {
list, arr, err := d.QAVideoDetail(c, ids)
ctx.Convey("Then err should be nil.list,arr should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(arr, convey.ShouldNotBeNil)
ctx.So(list, convey.ShouldNotBeNil)
})
})
}
func TestDaoGetQAVideoID(t *testing.T) {
var (
c = context.TODO()
aid = int64(10110610)
cid = int64(10134188)
taskID = int64(8725)
)
convey.Convey("GetQAVideoID", t, func(ctx convey.C) {
id, err := d.GetQAVideoID(c, aid, cid, taskID)
ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(id, convey.ShouldNotBeNil)
})
})
}
func TestDaoUpdateQAVideoUTime(t *testing.T) {
var (
c = context.TODO()
aid = int64(10110610)
cid = int64(10134188)
taskID = int64(8725)
utime = int64(10)
)
convey.Convey("UpdateQAVideoUTime", t, func(ctx convey.C) {
err := d.UpdateQAVideoUTime(c, aid, cid, taskID, utime)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoDelQAVideo(t *testing.T) {
var (
c = context.TODO()
mtime = time.Now().AddDate(-1, -1, 0)
limit = int(1)
)
convey.Convey("DelQAVideo", t, func(ctx convey.C) {
_, err := d.DelQAVideo(c, mtime, limit)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoDelQATask(t *testing.T) {
var (
c = context.TODO()
mtime = time.Now().AddDate(-1, -1, 0)
limit = int(1)
)
convey.Convey("DelQATask", t, func(ctx convey.C) {
_, err := d.DelQATask(c, mtime, limit)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoQATaskVideoByID(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("QATaskVideoByID", t, func(ctx convey.C) {
_, err := d.QATaskVideoByID(c, 0)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoQATaskVideoSimpleByID(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("QATaskVideoSimpleByID", t, func(ctx convey.C) {
_, err := d.QATaskVideoSimpleByID(c, 0)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,240 @@
package dao
import (
"context"
xsql "database/sql"
"encoding/json"
"fmt"
"strings"
"time"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_reviewCfg = 3
_countSQL = "SELECT COUNT(*) FROM task_json_config"
_listConfsSQL = "SELECT id,conf_json,conf_type,btime,etime,state,uid,uname,description,mtime FROM task_json_config WHERE conf_type=3"
_reConfsSQL = "SELECT id,conf_json,conf_type,btime,etime,state,uid,uname,description,mtime FROM task_json_config WHERE conf_type=3"
_inConfSQL = "INSERT INTO task_json_config(conf_json,conf_type,btime,etime,state,uid,uname,description) VALUE (?,?,?,?,?,?,?,?)"
_upConfSQL = "UPDATE task_json_config SET conf_json=?,conf_type=?,btime=?,etime=?,state=?,uid=?,uname=?,description=? WHERE id=?"
_delConfSQL = "DELETE FROM task_json_config WHERE id=?"
_reviewSQL = "SELECT review_form FROM task_review WHERE task_id=?"
_inReviewSQL = "INSERT INTO task_review(task_id,review_form,uid,uname) VALUE (?,?,?,?)"
)
// ListConfs 配置列表
func (d *Dao) ListConfs(c context.Context, uids []int64, bt, et, sort string, pn, ps int64) (rcs []*model.ReviewConf, count int64, err error) {
var (
rows *sql.Rows
countstring, sqlstring, params string
wherecases []string
)
if len(uids) > 0 {
wherecases = append(wherecases, fmt.Sprintf("uid IN (%s)", xstr.JoinInts(uids)))
}
if len(bt) > 0 && len(et) > 0 {
wherecases = append(wherecases, fmt.Sprintf("mtime>='%s' AND mtime<='%s'", bt, et))
}
if len(wherecases) > 0 {
params = " AND " + strings.Join(wherecases, " AND ")
}
countstring = _countSQL + " WHERE conf_type=3" + params
sqlstring = _listConfsSQL + params + fmt.Sprintf(" ORDER BY mtime %s LIMIT %d,%d", sort, (pn-1)*ps, pn*ps)
if err = d.arcDB.QueryRow(c, countstring).Scan(&count); err != nil {
log.Error("d.arcDB.QueryRow(%s) error(%v)", countstring, err)
return
}
if count == 0 {
return
}
if rows, err = d.arcDB.Query(c, sqlstring); err != nil {
log.Error("d.arcDB.Query(%s) error(%v)", sqlstring, err)
return
}
defer rows.Close()
for rows.Next() {
var (
jsonCfg []byte
cfgType int8
)
trc := &model.ReviewConf{}
if err = rows.Scan(&trc.ID, &jsonCfg, &cfgType, &trc.Bt, &trc.Et, &trc.State, &trc.UID, &trc.Uname, &trc.Desc, &trc.Mt); err != nil {
log.Error("rows.Scan error(%v)", err)
continue
}
if err = json.Unmarshal(jsonCfg, trc); err != nil {
log.Error("json.Unmarshal error(%v)", err)
continue
}
trc.Refresh()
rcs = append(rcs, trc)
}
return
}
// ReviewConfs 复审配置
func (d *Dao) ReviewConfs(c context.Context) (rcs []*model.ReviewConf, err error) {
var rows *sql.Rows
if rows, err = d.arcDB.Query(c, _reConfsSQL); err != nil {
log.Error("d.arcDB.Query(%s, %d) error(%v)", _reConfsSQL, err)
return
}
defer rows.Close()
for rows.Next() {
var (
jsonCfg []byte
cfgType int8
)
trc := &model.ReviewConf{}
if err = rows.Scan(&trc.ID, &jsonCfg, &cfgType, &trc.Bt, &trc.Et, &trc.State, &trc.UID, &trc.Uname, &trc.Desc, &trc.Mt); err != nil {
log.Error("rows.Scan error(%v)", err)
continue
}
if err = json.Unmarshal(jsonCfg, trc); err != nil {
log.Error("json.Unmarshal error(%v)", err)
continue
}
trc.Refresh()
rcs = append(rcs, trc)
}
return
}
// InReviewConf 插入配置
func (d *Dao) InReviewConf(c context.Context, rc *model.ReviewConf) (lastid int64, err error) {
var (
res xsql.Result
jsonCfg []byte
)
v := new(struct {
Types []int64 `json:"types" params:"types"` // 分区
UpFroms []int64 `json:"upfroms" params:"upfroms"` // 投稿来源
UpGroups []int64 `json:"upgroups" params:"upgroups"` // 用户组
Uids []int64 `json:"uids" params:"uids"` // 指定uid
FansLow int64 `json:"fanslow" params:"fanslow"` // 粉丝数最低值
FansHigh int64 `json:"fanshigh" params:"fanshigh"` // 粉丝数最高
})
v.Types = rc.Types
v.UpFroms = rc.UpFroms
v.UpGroups = rc.UpGroups
v.Uids = rc.Uids
v.FansLow = rc.FansLow
v.FansHigh = rc.FansHigh
if rc.Bt.TimeValue().IsZero() {
rc.Bt = model.NewFormatTime(time.Now())
}
if jsonCfg, err = json.Marshal(v); err != nil {
log.Error("json.Marshal(%+v) error(%v)", rc, err)
return
}
if res, err = d.arcDB.Exec(c, _inConfSQL, jsonCfg, _reviewCfg, rc.Bt, rc.Et, 0, rc.UID, rc.Uname, rc.Desc); err != nil {
log.Error("d.arcDB.Exec(%+v) error(%s, %v)", _inConfSQL, rc, err)
return
}
return res.LastInsertId()
}
// UpReviewConf 更新指定配置
func (d *Dao) UpReviewConf(c context.Context, rc *model.ReviewConf) (lastid int64, err error) {
var (
res xsql.Result
jsonCfg []byte
)
v := new(struct {
Types []int64 `json:"types" params:"types"` // 分区
UpFroms []int64 `json:"upfroms" params:"upfroms"` // 投稿来源
UpGroups []int64 `json:"upgroups" params:"upgroups"` // 用户组
Uids []int64 `json:"uids" params:"uids"` // 指定uid
FansLow int64 `json:"fanslow" params:"fanslow"` // 粉丝数最低值
FansHigh int64 `json:"fanshigh" params:"fanshigh"` // 粉丝数最高
})
v.Types = rc.Types
v.UpFroms = rc.UpFroms
v.UpGroups = rc.UpGroups
v.Uids = rc.Uids
v.FansLow = rc.FansLow
v.FansHigh = rc.FansHigh
if rc.Bt.TimeValue().IsZero() {
rc.Bt = model.NewFormatTime(time.Now())
}
if jsonCfg, err = json.Marshal(v); err != nil {
log.Error("json.Marshal(%+v) error(%v)", rc, err)
return
}
if res, err = d.arcDB.Exec(c, _upConfSQL, jsonCfg, _reviewCfg, rc.Bt, rc.Et, rc.State, rc.UID, rc.Uname, rc.Desc, rc.ID); err != nil {
log.Error("d.arcDB.Exec(%s %+v) error(%v)", _upConfSQL, rc, err)
return
}
return res.RowsAffected()
}
// DelReviewConf 删除指定配置
func (d *Dao) DelReviewConf(c context.Context, id int) (lastid int64, err error) {
var res xsql.Result
if res, err = d.arcDB.Exec(c, _delConfSQL, id); err != nil {
log.Error("d.arcDB.Exec(%s %d) error(%v)", _delConfSQL, id, err)
return
}
return res.RowsAffected()
}
// ReviewForm 复审表单
func (d *Dao) ReviewForm(c context.Context, tid int64) (tsf *model.SubmitForm, err error) {
var form []byte
if err = d.arcDB.QueryRow(c, _reviewSQL, tid).Scan(&form); err != nil {
if err == sql.ErrNoRows {
log.Info("ReviewForm QueryRow empty(%d)", tid)
err = nil
return
}
log.Error("d.arcDB.QueryRow(%s, %d) error(%v)", _reviewSQL, tid, err)
return
}
tsf = &model.SubmitForm{}
if err = json.Unmarshal(form, tsf); err != nil {
log.Error("json.Unmarshal error(%v)", err)
tsf = nil
}
return
}
// InReviewForm insert submit form
func (d *Dao) InReviewForm(c context.Context, sf *model.SubmitForm, uid int64, uname string) (lastid int64, err error) {
var (
res xsql.Result
bsf []byte
)
if bsf, err = json.Marshal(sf); err != nil {
log.Error("json.Marshal error(%v)", err)
return
}
if res, err = d.arcDB.Exec(c, _inReviewSQL, sf.TaskID, bsf, uid, uname); err != nil {
log.Error("d.arcDB.Exec(%s,%d,%v,%d,%s) error(%v)", _inReviewSQL, sf.TaskID, bsf, uid, uname, err)
return
}
return res.LastInsertId()
}

View File

@@ -0,0 +1,80 @@
package dao
import (
"context"
"testing"
"go-common/app/admin/main/videoup-task/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoListConfs(t *testing.T) {
convey.Convey("ListConfs", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, _, err := d.ListConfs(c, []int64{}, "", "", "", 0, 0)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoReviewConfs(t *testing.T) {
convey.Convey("ReviewConfs", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.ReviewConfs(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoUpReviewConf(t *testing.T) {
convey.Convey("UpReviewConf", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.UpReviewConf(c, &model.ReviewConf{})
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoDelReviewConf(t *testing.T) {
convey.Convey("DelReviewConf", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.DelReviewConf(c, 0)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoReviewForm(t *testing.T) {
convey.Convey("ReviewForm", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.ReviewForm(c, 0)
ctx.Convey("Then err should be nil.max should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,337 @@
package dao
import (
"context"
xsql "database/sql"
"encoding/json"
"fmt"
"go-common/library/xstr"
"strings"
"time"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/cache/redis"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_getMaxWeightSQL = "SELECT MAX(weight) FROM task_dispatch WHERE state in (0,1)"
_upCwAfterAddSQL = "INSERT INTO `task_dispatch_extend` (`task_id`,`description`) VALUES(?,?) ON DUPLICATE KEY UPDATE description=?"
_inWeightConfSQL = "INSERT INTO task_weight_config(mid,rule,weight,uid,uname,radio,description) VALUES (?,?,?,?,?,?,?)" // 增
_delWeightConfSQL = "UPDATE task_weight_config SET state=1 WHERE id=?" // 软删
_listWeightConfSQL = "SELECT id,uname,state,rule,weight,mtime,description FROM task_weight_config" // 查
_WeightConfSQL = "SELECT id,description FROM task_weight_config WHERE state=0" // 查
_lwconfigHelpSQL = "SELECT t.id,t.cid,a.title,v.filename FROM task_dispatch t INNER JOIN archive a ON t.aid=a.id INNER JOIN archive_video v ON t.cid=v.cid WHERE t.id IN (%s)"
// archive_config
_confSQL = "SELECT value FROM archive_config WHERE state=0 AND name=?"
_upconfSQL = "UPDATE archive_config SET value=?,remark=? WHERE name=?"
_inconfSQL = "INSERT archive_config(value,remark,name,state) VALUE (?,?,?,0)"
_twexpire = 24 * 60 * 60 // 1 day
)
// GetMaxWeight 获取当前最大权重数值
func (d *Dao) GetMaxWeight(c context.Context) (max int64, err error) {
if err = d.arcDB.QueryRow(c, _getMaxWeightSQL).Scan(&max); err != nil {
log.Error("d.arcDB.QueryRow error(%v)", err)
err = nil
}
return
}
// UpCwAfterAdd update config weight after add config
func (d *Dao) UpCwAfterAdd(c context.Context, id int64, desc string) (rows int64, err error) {
row, err := d.arcDB.Exec(c, _upCwAfterAddSQL, id, desc, desc)
if err != nil {
log.Error("arcDB.Exec(%s,%d,%s,%s) error(%v)", _upCwAfterAddSQL, id, desc, desc, err)
return
}
return row.RowsAffected()
}
// InWeightConf 写入权重配置表
func (d *Dao) InWeightConf(c context.Context, mcases map[int64]*model.WCItem) (err error) {
tx, err := d.arcDB.Begin(c)
if err != nil {
log.Error("db.Begin() error(%v)", err)
return
}
for _, item := range mcases {
var descb []byte
if descb, err = json.Marshal(item); err != nil {
log.Error("json.Marshal(%+v) error(%v)", item, err)
tx.Rollback()
return
}
if _, err = tx.Exec(_inWeightConfSQL, item.CID, item.Rule, item.Weight, item.UID, item.Uname, item.Radio, string(descb)); err != nil {
log.Error("db.Exec(%s) error(%v)", _inWeightConfSQL, err)
tx.Rollback()
return
}
}
if err = tx.Commit(); err != nil {
log.Error("tx.Commit() error(%v)", err)
return
}
return
}
// DelWeightConf 删除权重配置
func (d *Dao) DelWeightConf(c context.Context, id int64) (rows int64, err error) {
res, err := d.arcDB.Exec(c, _delWeightConfSQL, id)
if err != nil {
log.Error("tx.Exec(%s %d) error(%v)", _delWeightConfSQL, id, err)
return
}
return res.RowsAffected()
}
// ListWeightConf 查看权重配置表列表
func (d *Dao) ListWeightConf(c context.Context, cf *model.Confs) (citems []*model.WCItem, err error) {
var (
count int64
rows *sql.Rows
where string
wherecase []string
descb []byte
bt = cf.Bt.TimeValue()
et = cf.Et.TimeValue()
)
if cid := cf.Cid; cid != -1 {
wherecase = append(wherecase, fmt.Sprintf("mid=%d", cid))
}
if operator := cf.Operator; len(operator) > 0 {
wherecase = append(wherecase, fmt.Sprintf("uname='%s'", operator))
}
if rule := cf.Rule; rule != -1 {
wherecase = append(wherecase, fmt.Sprintf("rule=%d", rule))
}
wherecase = append(wherecase, fmt.Sprintf("radio=%d AND state=%d", cf.Radio, cf.State))
where = "WHERE " + strings.Join(wherecase, " AND ")
sqlstring := fmt.Sprintf("%s %s LIMIT %d,%d", _listWeightConfSQL, where, (cf.Pn-1)*cf.Ps, cf.Pn*cf.Ps)
rows, err = d.arcDB.Query(c, sqlstring)
if err != nil {
log.Error("d.arcDB.Query(%s) error(%v)", sqlstring, err)
return
}
defer rows.Close()
for rows.Next() {
wci := &model.WCItem{}
if err = rows.Scan(&wci.ID, &wci.Uname, &wci.State, &wci.Rule, &wci.Weight, &wci.Mtime, &descb); err != nil {
log.Error("rows.Scan(%s) error(%v)", sqlstring, err)
return
}
if len(descb) > 0 {
if err = json.Unmarshal(descb, wci); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(descb), err)
err = nil
continue
}
eti := wci.Et.TimeValue()
// filter time
if !et.IsZero() && !bt.IsZero() && (bt.After(wci.Mtime.TimeValue()) || et.Before(wci.Mtime.TimeValue())) {
log.Info("config expired (%+v) parse et(%v)", wci, et)
continue
}
// filter state
if cf.State == 0 && !eti.IsZero() && eti.Before(time.Now()) {
log.Info("config expired (%+v) parse et(%v)", wci, eti)
continue
}
}
if count > 50 {
break
}
count++
citems = append(citems, wci)
}
return
}
// WeightConf 所有有效的配置(用于检测是否和已有的配置冲突)
func (d *Dao) WeightConf(c context.Context) (items []*model.WCItem, err error) {
var (
id int64
descb []byte
rows *sql.Rows
wci *model.WCItem
)
if rows, err = d.arcDB.Query(c, _WeightConfSQL); err != nil {
log.Error("d.arcDB.Query(%s) error(%v)", _WeightConfSQL, err)
return
}
defer rows.Close()
for rows.Next() {
wci = new(model.WCItem)
if err = rows.Scan(&id, &descb); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
if err = json.Unmarshal(descb, wci); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(descb), err)
err = nil
continue
}
wci.ID = id
items = append(items, wci)
}
return
}
// LWConfigHelp 补充任务对应稿件的title和filename
func (d *Dao) LWConfigHelp(c context.Context, ids []int64) (res map[int64][]interface{}, err error) {
var (
taskid, vid int64
filename, title string
rows *sql.Rows
)
rows, err = d.arcDB.Query(c, fmt.Sprintf(_lwconfigHelpSQL, xstr.JoinInts(ids)))
if err != nil {
log.Error("d.arcDB.Query(%v) error(%v)", ids, err)
return
}
defer rows.Close()
res = make(map[int64][]interface{})
for rows.Next() {
err = rows.Scan(&taskid, &vid, &title, &filename)
if err != nil {
log.Error("rows.Scan error(%v)", err)
continue
}
res[taskid] = []interface{}{filename, title, vid}
}
return
}
func key(id int64) string {
return fmt.Sprintf("tw_%d", id)
}
//SetWeightRedis 设置权重配置
func (d *Dao) SetWeightRedis(c context.Context, mcases map[int64]*model.TaskPriority) (err error) {
conn := d.redis.Get(c)
defer conn.Close()
for tid, mcase := range mcases {
var bs []byte
key := key(tid)
if bs, err = json.Marshal(mcase); err != nil {
log.Error("json.Marshal(%+v) error(%v)", mcase, err)
continue
}
if err = conn.Send("SET", key, bs); err != nil {
log.Error("SET error(%v)", err)
continue
}
if err = conn.Send("EXPIRE", key, _twexpire); err != nil {
log.Error("EXPIRE error(%v)", err)
continue
}
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
for i := 0; i < 2*len(mcases); i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
//GetWeightRedis 获取实时任务的权重配置
func (d *Dao) GetWeightRedis(c context.Context, ids []int64) (mcases map[int64]*model.TaskPriority, err error) {
conn := d.redis.Get(c)
defer conn.Close()
mcases = make(map[int64]*model.TaskPriority)
for _, id := range ids {
var bs []byte
key := key(int64(id))
if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %v) error(%v)", key, err)
}
continue
}
p := &model.TaskPriority{}
if err = json.Unmarshal(bs, p); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(bs), err)
err = nil
continue
}
mcases[int64(id)] = p
}
return
}
// WeightVC 获取权重分值
func (d *Dao) WeightVC(c context.Context) (wvc *model.WeightVC, err error) {
var value []byte
row := d.arcDB.QueryRow(c, _confSQL, model.ConfForWeightVC)
if err = row.Scan(&value); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
return
}
wvc = new(model.WeightVC)
if err = json.Unmarshal(value, wvc); err != nil {
log.Error("json.Unmarshal error(%v)", err)
wvc = nil
}
return
}
// SetWeightVC 设置权重分值
func (d *Dao) SetWeightVC(c context.Context, wvc *model.WeightVC, desc string) (rows int64, err error) {
var (
valueb []byte
res xsql.Result
)
if valueb, err = json.Marshal(wvc); err != nil {
log.Error("json.Marshal(%+v) error(%v)", wvc, err)
return
}
if res, err = d.arcDB.Exec(c, _upconfSQL, string(valueb), desc, model.ConfForWeightVC); err != nil {
log.Error("d.arcDB.Exec(%s, %s, %s, %s) error(%v)", _upconfSQL, string(valueb), desc, model.ConfForWeightVC, err)
return
}
return res.RowsAffected()
}
// InWeightVC 插入
func (d *Dao) InWeightVC(c context.Context, wvc *model.WeightVC, desc string) (rows int64, err error) {
var (
valueb []byte
res xsql.Result
)
if valueb, err = json.Marshal(wvc); err != nil {
log.Error("json.Marshal(%+v) error(%v)", wvc, err)
return
}
if res, err = d.arcDB.Exec(c, _inconfSQL, string(valueb), desc, model.ConfForWeightVC); err != nil {
log.Error("d.arcDB.Exec(%s, %s, %s, %s) error(%v)", _inconfSQL, string(valueb), desc, model.ConfForWeightVC, err)
return
}
return res.LastInsertId()
}

View File

@@ -0,0 +1,212 @@
package dao
import (
"context"
"go-common/app/admin/main/videoup-task/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetMaxWeight(t *testing.T) {
convey.Convey("GetMaxWeight", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
max, err := d.GetMaxWeight(c)
ctx.Convey("Then err should be nil.max should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(max, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpCwAfterAdd(t *testing.T) {
convey.Convey("UpCwAfterAdd", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
desc = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.UpCwAfterAdd(c, id, desc)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInWeightConf(t *testing.T) {
convey.Convey("InWeightConf", t, func(ctx convey.C) {
var (
c = context.Background()
mcases map[int64]*model.WCItem
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.InWeightConf(c, mcases)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoDelWeightConf(t *testing.T) {
convey.Convey("DelWeightConf", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.DelWeightConf(c, id)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoListWeightConf(t *testing.T) {
convey.Convey("ListWeightConf", t, func(ctx convey.C) {
var (
c = context.Background()
cf = &model.Confs{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.ListWeightConf(c, cf)
ctx.Convey("Then err should be nil.citems should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoWeightConf(t *testing.T) {
convey.Convey("WeightConf", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
items, err := d.WeightConf(c)
ctx.Convey("Then err should be nil.items should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(items, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokey(t *testing.T) {
convey.Convey("key", t, func(ctx convey.C) {
var (
id = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := key(id)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetWeightRedis(t *testing.T) {
convey.Convey("GetWeightRedis", t, func(ctx convey.C) {
var (
c = context.Background()
ids = []int64{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
mcases, err := d.GetWeightRedis(c, ids)
ctx.Convey("Then err should be nil.mcases should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(mcases, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoWeightVC(t *testing.T) {
convey.Convey("WeightVC", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
wvc, err := d.WeightVC(c)
ctx.Convey("Then err should be nil.wvc should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(wvc, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetWeightVC(t *testing.T) {
convey.Convey("SetWeightVC", t, func(ctx convey.C) {
var (
c = context.Background()
wvc = &model.WeightVC{}
desc = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.SetWeightVC(c, wvc, desc)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInWeightVC(t *testing.T) {
convey.Convey("InWeightVC", t, func(ctx convey.C) {
var (
c = context.Background()
wvc = &model.WeightVC{}
desc = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InWeightVC(c, wvc, desc)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoLWConfigHelp(t *testing.T) {
convey.Convey("LWConfigHelp", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.LWConfigHelp(c, []int64{1})
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetWeightRedis(t *testing.T) {
convey.Convey("SetWeightRedis", t, func(ctx convey.C) {
var (
c = context.Background()
mcases = map[int64]*model.TaskPriority{
0: &model.TaskPriority{},
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetWeightRedis(c, mcases)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,32 @@
package dao
import (
"context"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/log"
)
const (
_tpsSQL = "SELECT id,pid,name,description FROM archive_type"
)
// TypeMapping is second types opposite first types.
func (d *Dao) TypeMapping(c context.Context) (tmap map[int16]*model.Type, err error) {
rows, err := d.arcDB.Query(c, _tpsSQL)
if err != nil {
log.Error("d.arcDB.Query error(%v)", err)
return
}
defer rows.Close()
tmap = make(map[int16]*model.Type)
for rows.Next() {
t := &model.Type{}
if err = rows.Scan(&t.ID, &t.PID, &t.Name, &t.Desc); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
tmap[t.ID] = t
}
return
}

View File

@@ -0,0 +1,23 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoTypeMapping(t *testing.T) {
convey.Convey("TypeMapping", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
tmap, err := d.TypeMapping(c)
ctx.Convey("Then err should be nil.tmap should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(tmap, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,51 @@
package dao
import (
"context"
"net/url"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/log"
"go-common/library/xstr"
)
//UPGroups get all the up groups
func (d *Dao) UPGroups(c context.Context, mids []int64) (groups map[int64][]*model.UPGroup, err error) {
val := url.Values{}
mid := xstr.JoinInts(mids)
val.Set("mids", mid)
val.Set("group_id", "0")
groups = map[int64][]*model.UPGroup{}
for _, mid := range mids {
groups[mid] = []*model.UPGroup{}
}
var res struct {
Code int `json:"code"`
Data struct {
Items []map[string]interface{} `json:"items"`
}
}
if err = d.hclient.Get(c, d.upGroupURL, "", val, &res); err != nil {
log.Error("UPGroups url(%s) error(%v)", d.upGroupURL+"?"+val.Encode(), err)
return
}
if res.Code != 0 || res.Data.Items == nil {
log.Warn("UPGroups code(%d) !=0 or empty url(%s) error(%v)", res.Code, d.upGroupURL+"?"+val.Encode(), res.Code)
return
}
for _, item := range res.Data.Items {
g := &model.UPGroup{
ID: int64(item["group_id"].(float64)),
Tag: item["group_name"].(string),
ShortTag: item["group_tag"].(string),
FontColor: item["font_color"].(string),
BgColor: item["bg_color"].(string),
Note: item["note"].(string),
}
mid := int64(item["mid"].(float64))
groups[mid] = append(groups[mid], g)
}
return
}

View File

@@ -0,0 +1,22 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoUPGroups(t *testing.T) {
var (
c = context.TODO()
mids = []int64{27515256, 27515304}
)
convey.Convey("UPGroups", t, func(ctx convey.C) {
httpMock("GET", d.upGroupURL).Reply(200).JSON(`{"code":0,"data":{"items":[{"group_id":1,"group_name":"name","group_tag":"tag","font_color":"font","bg_color":"bg","note":"note","mid":11}]}}`)
_, err := d.UPGroups(c, mids)
ctx.Convey("Then err should be nil.groups should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,95 @@
package dao
import (
"context"
"fmt"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/database/sql"
"go-common/library/xstr"
)
const (
_usernameRoleSQL = `SELECT u.id, u.username, coalesce(r.role,0) role FROM user u LEFT JOIN auth_role r ON u.id = r.uid WHERE u.id IN (%s)`
_usernameDepartmentSQL = `SELECT u.id, u.username, coalesce(d.name,'') department FROM user u LEFT JOIN user_department d ON u.department_id = d.id WHERE u.id IN (%s)`
_usernameSQL = `SELECT id,username FROM user WHERE id IN (%s)`
)
//GetUsernameAndRole batch get username & role
func (d *Dao) GetUsernameAndRole(ctx context.Context, uids []int64) (list map[int64]*model.UserRole, err error) {
var (
rows *sql.Rows
)
list = map[int64]*model.UserRole{}
uidStr := xstr.JoinInts(uids)
if rows, err = d.mngDB.Query(ctx, fmt.Sprintf(_usernameRoleSQL, uidStr)); err != nil {
PromeErr("mngdb: query", "GetUsernameAndRole d.mngDB.Query error(%v) uids(%s)", err, uidStr)
return
}
defer rows.Close()
for rows.Next() {
u := new(model.UserRole)
if err = rows.Scan(&u.UID, &u.Name, &u.Role); err != nil {
PromeErr("mngdb: scan", "GetUsernameAndRole rows.Scan error(%v) uids(%s)", err, uidStr)
return
}
list[u.UID] = u
}
return
}
//GetUsernameAndDepartment batch get username & department
func (d *Dao) GetUsernameAndDepartment(ctx context.Context, uids []int64) (list map[int64]*model.UserDepart, err error) {
var (
rows *sql.Rows
)
list = map[int64]*model.UserDepart{}
uidStr := xstr.JoinInts(uids)
if rows, err = d.mngDB.Query(ctx, fmt.Sprintf(_usernameDepartmentSQL, uidStr)); err != nil {
PromeErr("mngdb: query", "GetUsernameAndDepartment d.mngDB.Query error(%v) uids(%s)", err, uidStr)
return
}
defer rows.Close()
for rows.Next() {
u := new(model.UserDepart)
if err = rows.Scan(&u.UID, &u.Name, &u.Department); err != nil {
PromeErr("mngdb: scan", "GetUsernameAndDepartment rows.Scan error(%v) uids(%s)", err, uidStr)
return
}
list[u.UID] = u
}
return
}
//GetUsername get username
func (d *Dao) GetUsername(ctx context.Context, uids []int64) (list map[int64]string, err error) {
var (
rows *sql.Rows
uid int64
name string
)
list = map[int64]string{}
uidStr := xstr.JoinInts(uids)
if rows, err = d.mngDB.Query(ctx, fmt.Sprintf(_usernameSQL, uidStr)); err != nil {
PromeErr("mngdb: query", "GetUsername d.mngDB.Query error(%v) uids(%s)", err, uidStr)
return
}
defer rows.Close()
for rows.Next() {
if err = rows.Scan(&uid, &name); err != nil {
PromeErr("mngdb: scan", "GetUsername rows.Scan error(%v) uids(%s)", err, uidStr)
return
}
list[uid] = name
}
return
}

View File

@@ -0,0 +1,50 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetUsernameAndRole(t *testing.T) {
var (
c = context.TODO()
uids = []int64{1, 74, 241}
)
convey.Convey("GetUsernameAndRole", t, func(ctx convey.C) {
list, err := d.GetUsernameAndRole(c, uids)
ctx.Convey("Then err should be nil.list should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(list, convey.ShouldNotBeNil)
})
})
}
func TestDaoGetUsernameAndDepartment(t *testing.T) {
var (
c = context.TODO()
uids = []int64{1, 74, 241}
)
convey.Convey("GetUsernameAndDepartment", t, func(ctx convey.C) {
list, err := d.GetUsernameAndDepartment(c, uids)
ctx.Convey("Then err should be nil.list should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(list, convey.ShouldNotBeNil)
})
})
}
func TestDaoGetUsername(t *testing.T) {
var (
c = context.TODO()
uids = []int64{1, 74, 241}
)
convey.Convey("GetUsername", t, func(ctx convey.C) {
list, err := d.GetUsername(c, uids)
ctx.Convey("Then err should be nil.list should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(list, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,103 @@
package dao
import (
"context"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_videoVID = `SELECT vr.id FROM archive_video_relation vr LEFT JOIN video v ON vr.cid=v.id
LEFT JOIN archive a ON vr.aid=a.id
WHERE vr.aid=? AND vr.cid=? AND vr.state != -100 AND v.status != -100 AND a.state != -100`
_video = `SELECT vr.id, vr.aid, vr.cid, ar.mid, ar.copyright, ar.typeid, v.status, v.attribute, v.xcode_state, vr.title, vr.description, v.filename,
coalesce(ad.tid, 0) tid, coalesce(ad.reason, '') reason, coalesce(ao.remark, '') note
FROM archive_video_relation vr LEFT JOIN video v ON vr.cid=v.id
LEFT JOIN archive ar ON vr.aid=ar.id
LEFT JOIN archive_video_audit ad ON vr.id = ad.vid
LEFT JOIN archive_video_oper ao ON vr.id = ao.vid
WHERE vr.aid=? AND vr.cid=? AND ao.content NOT LIKE '%一审任务质检TAG: [%'
ORDER BY ao.id DESC LIMIT 0,1`
_videoByCid = `SELECT vr.id,vr.aid,vr.title AS eptitle,vr.description,v.filename,v.src_type,vr.cid,v.duration,v.filesize,
v.resolutions,vr.index_order,vr.ctime,vr.mtime,v.status,v.playurl,v.attribute,v.failcode AS failinfo,v.xcode_state,v.weblink
FROM archive_video_relation AS vr LEFT JOIN video AS v ON vr.cid = v.id WHERE vr.cid = ?`
_newVideoIDSQL = `SELECT avr.id,v.filename,avr.cid,avr.aid,avr.title,avr.description,v.src_type,v.duration,v.filesize,v.resolutions,v.playurl,v.failcode,
avr.index_order,v.attribute,v.xcode_state,avr.state,avr.ctime,avr.mtime FROM archive_video_relation avr JOIN video v on avr.cid = v.id
WHERE avr.id=? LIMIT 1`
_videoAttributeSQL = `SELECT attribute FROM video WHERE id = ?`
)
// VideoAttribute get attr
func (d *Dao) VideoAttribute(ctx context.Context, cid int64) (attr int32, err error) {
if err = d.arcDB.QueryRow(ctx, _videoAttributeSQL, cid).Scan(&attr); err != nil {
if err == sql.ErrNoRows {
attr = 0
err = nil
} else {
PromeErr("arcdb: scan", "GetVVideoAttributeID row.Scan error(%v), cid(%d)", err, cid)
}
}
return
}
//GetVID get vid
func (d *Dao) GetVID(ctx context.Context, aid int64, cid int64) (vid int64, err error) {
if err = d.arcReadDB.QueryRow(ctx, _videoVID, aid, cid).Scan(&vid); err != nil {
if err == sql.ErrNoRows {
vid = 0
err = nil
} else {
PromeErr("arcReaddb: scan", "GetVID row.Scan error(%v) aid(%d), cid(%d)", err, aid, cid)
}
}
return
}
//Video get video by aid & cid
func (d *Dao) Video(ctx context.Context, aid int64, cid int64) (v *model.Video, err error) {
v = &model.Video{}
if err = d.arcReadDB.QueryRow(ctx, _video, aid, cid).Scan(&v.ID, &v.AID, &v.CID, &v.MID, &v.Copyright, &v.TypeID, &v.Status,
&v.Attribute, &v.XcodeState, &v.Title, &v.Description, &v.Filename,
&v.TagID, &v.Reason, &v.Note); err != nil {
if err == sql.ErrNoRows {
err = nil
v = nil
} else {
PromeErr("arcReaddb: scan", "Video row.Scan error(%v) aid(%d), cid(%d)", err, aid, cid)
}
}
return
}
// ArcVideoByCID get video by cid
func (d *Dao) ArcVideoByCID(c context.Context, cid int64) (v *model.ArcVideo, err error) {
row := d.arcDB.QueryRow(c, _videoByCid, cid)
v = &model.ArcVideo{}
if err = row.Scan(&v.ID, &v.Aid, &v.Title, &v.Desc, &v.Filename, &v.SrcType, &v.Cid, &v.Duration, &v.Filesize, &v.Resolutions, &v.Index, &v.CTime, &v.MTime, &v.Status, &v.Playurl, &v.Attribute, &v.FailCode, &v.XcodeState, &v.WebLink); err != nil {
if err == sql.ErrNoRows {
v = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// NewVideoByID .
func (d *Dao) NewVideoByID(c context.Context, id int64) (v *model.ArcVideo, err error) {
row := d.arcDB.QueryRow(c, _newVideoIDSQL, id)
v = &model.ArcVideo{}
if err = row.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 {
if err == sql.ErrNoRows {
v = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}

View File

@@ -0,0 +1,73 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetVID(t *testing.T) {
var (
c = context.TODO()
aid = int64(10098208)
cid = int64(10108827)
)
convey.Convey("GetVID", t, func(ctx convey.C) {
vid, err := d.GetVID(c, aid, cid)
ctx.Convey("Then err should be nil.vid should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(vid, convey.ShouldNotBeNil)
})
})
}
func TestDaoVideo(t *testing.T) {
var (
c = context.TODO()
aid = int64(10110750)
cid = int64(10134516)
)
convey.Convey("Video", t, func(ctx convey.C) {
_, err := d.Video(c, aid, cid)
ctx.Convey("Then err should be nil.v should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoVideoAttribute(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("VideoAttribute", t, func(ctx convey.C) {
_, err := d.VideoAttribute(c, 0)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoArcVideoByCID(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("ArcVideoByCID", t, func(ctx convey.C) {
_, err := d.ArcVideoByCID(c, 0)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoNewVideoByID(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("NewVideoByID", t, func(ctx convey.C) {
_, err := d.NewVideoByID(c, 0)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,57 @@
package dao
import (
"bytes"
"context"
"crypto/md5"
"encoding/binary"
"encoding/json"
"fmt"
"time"
"go-common/app/admin/main/videoup-task/model"
"go-common/library/log"
"github.com/tsuna/gohbase/hrpc"
)
var (
tableInfo = "ugc:ArchiveTaskWeight"
family = "weightlog"
familyB = []byte(family)
)
// hashRowKey create rowkey(md5(tid)[:2]+tid) for track by tid.
func hashRowKey(tid int64) string {
var bs = make([]byte, 8)
binary.LittleEndian.PutUint64(bs, uint64(tid))
rk := md5.Sum(bs)
return fmt.Sprintf("%x%d", rk[:2], tid)
}
// WeightLog get weight log.
func (d *Dao) WeightLog(c context.Context, taskid int64) (ls []*model.TaskWeightLog, err error) {
var (
result *hrpc.Result
key = hashRowKey(taskid)
ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
)
defer cancel()
if result, err = d.hbase.GetStr(ctx, tableInfo, key); err != nil {
log.Error("hrpc.NewGetStr(%s,%s) error(%v)", tableInfo, key, err)
return
}
for _, c := range result.Cells {
if c == nil || !bytes.Equal(c.Family, familyB) {
return
}
aLog := &model.TaskWeightLog{}
if err = json.Unmarshal(c.Value, aLog); err != nil {
log.Warn("json.Unmarshal(%s) error(%v)", string(c.Value), err)
err = nil
continue
}
ls = append(ls, aLog)
}
return
}

View File

@@ -0,0 +1,38 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaohashRowKey(t *testing.T) {
convey.Convey("hashRowKey", t, func(ctx convey.C) {
var (
tid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := hashRowKey(tid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoWeightLog(t *testing.T) {
convey.Convey("WeightLog", t, func(ctx convey.C) {
var (
c = context.Background()
taskid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
ls, err := d.WeightLog(c, taskid)
ctx.Convey("Then err should be nil.ls should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ls, convey.ShouldNotBeNil)
})
})
})
}