Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"addit_test.go",
"archive_oper_test.go",
"archive_recheck_test.go",
"archive_test.go",
"audit_test.go",
"config_test.go",
"dao_test.go",
"flow_design_test.go",
"task_test.go",
"type_test.go",
"video_test.go",
"video_track_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/videoup-report/conf:go_default_library",
"//app/job/main/videoup-report/model/archive:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"addit.go",
"archive.go",
"archive_delay.go",
"archive_oper.go",
"archive_recheck.go",
"audit.go",
"config.go",
"dao.go",
"flow_design.go",
"report.go",
"task.go",
"task_consumer.go",
"task_dispatch.go",
"task_dispatch_done.go",
"task_dispatch_extend.go",
"task_json_config.go",
"task_oper_history.go",
"track.go",
"type.go",
"video.go",
"video_track.go",
],
importpath = "go-common/app/job/main/videoup-report/dao/archive",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/videoup-report/conf:go_default_library",
"//app/job/main/videoup-report/model/archive:go_default_library",
"//app/job/main/videoup-report/model/task:go_default_library",
"//app/job/main/videoup-report/model/utils:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/xstr:go_default_library",
"//vendor/github.com/dgryski/go-farm: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,54 @@
package archive
import (
"context"
"fmt"
"go-common/app/job/main/videoup-report/model/archive"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_additSQL = "SELECT id,aid,source,redirect_url,mission_id,up_from,order_id,dynamic FROM archive_addit WHERE aid=?"
_additsSQL = "SELECT id,aid,source,redirect_url,mission_id,up_from,order_id,dynamic FROM archive_addit WHERE aid IN (%s)"
)
// Addit get archive addit.
func (d *Dao) Addit(c context.Context, aid int64) (addit *archive.Addit, err error) {
row := d.db.QueryRow(c, _additSQL, aid)
addit = &archive.Addit{}
if err = row.Scan(&addit.ID, &addit.Aid, &addit.Source, &addit.RedirectURL, &addit.MissionID, &addit.UpFrom, &addit.OrderID, &addit.Dynamic); err != nil {
if err == sql.ErrNoRows {
addit = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// Addits batch get archive addit.
func (d *Dao) Addits(c context.Context, aids []int64) (addits map[int64]*archive.Addit, err error) {
addits = make(map[int64]*archive.Addit)
if len(aids) < 1 {
return
}
rows, err := d.db.Query(c, fmt.Sprintf(_additsSQL, xstr.JoinInts(aids)))
if err != nil {
log.Error("d.db.Query(%s) error(%v)", fmt.Sprintf(_additsSQL, xstr.JoinInts(aids)), err)
return
}
defer rows.Close()
for rows.Next() {
addit := &archive.Addit{}
if err = rows.Scan(&addit.ID, &addit.Aid, &addit.Source, &addit.RedirectURL, &addit.MissionID, &addit.UpFrom, &addit.OrderID, &addit.Dynamic); err != nil {
log.Error("row.Scan error(%v)", err)
return
}
addits[addit.Aid] = addit
}
return
}

View File

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

View File

@@ -0,0 +1,76 @@
package archive
import (
"context"
"fmt"
"time"
"go-common/app/job/main/videoup-report/model/archive"
"go-common/library/log"
)
const (
_archiveByAidSQL = "SELECT id,mid,typeid,copyright,author,title,cover,content,duration,round,attribute,access,state,tag,pubtime,ctime,mtime FROM archive WHERE id=? "
_arcNoteSQL = "SELECT coalesce(note,'') from archive where id=?"
_upOriginalAids = "SELECT " +
"a.id,a.mid,a.typeid,a.copyright,a.author,a.title,a.cover,a.content,a.duration,a.round,a.attribute,a.access,a.state,a.tag,a.pubtime,a.ctime,a.mtime " +
"FROM archive AS a LEFT JOIN archive_delay as delay ON delay.aid = a.id " +
"WHERE a.mid=? AND a.copyright=? AND a.ctime>=? AND a.ctime<? AND (a.state >= 0 OR delay.state >= 0)"
_upArcTagSQL = "UPDATE archive SET tag=? WHERE id=?"
)
// ArchiveByAid get archive by aid
func (d *Dao) ArchiveByAid(c context.Context, aid int64) (arc *archive.Archive, err error) {
row := d.db.QueryRow(c, _archiveByAidSQL, aid)
arc = &archive.Archive{}
if err = row.Scan(&arc.ID, &arc.Mid, &arc.TypeID, &arc.Copyright, &arc.Author, &arc.Title, &arc.Cover, &arc.Desc, &arc.Duration,
&arc.Round, &arc.Attribute, &arc.Access, &arc.State, &arc.Tag, &arc.PTime, &arc.CTime, &arc.MTime); err != nil {
log.Error("row.Scan error(%v)", err)
}
return
}
//ArchiveNote 稿件审核的备注字段可能为NIL
func (d *Dao) ArchiveNote(c context.Context, aid int64) (note string, err error) {
if err = d.db.QueryRow(c, _arcNoteSQL, aid).Scan(&note); err != nil {
log.Error("ArchiveNote db.row.Scan error(%v), aid(%d)", err, aid)
}
return
}
// ExcitationArchivesByTime 获取Up主过审的自制稿件
func (d *Dao) ExcitationArchivesByTime(c context.Context, mid int64, bt, et time.Time) (archives []*archive.Archive, err error) {
archives = []*archive.Archive{}
if mid < 1 {
err = fmt.Errorf("wrong mid(%d)", mid)
return
}
rows, err := d.db.Query(c, _upOriginalAids, mid, archive.CopyrightOriginal, bt, et)
if err != nil {
log.Error("d.db.Query(%s,%d,%d,%v,%v) error(%v)", _upOriginalAids, mid, archive.CopyrightOriginal, bt, et)
return
}
defer rows.Close()
for rows.Next() {
arc := &archive.Archive{}
if err = rows.Scan(&arc.ID, &arc.Mid, &arc.TypeID, &arc.Copyright, &arc.Author, &arc.Title, &arc.Cover, &arc.Desc, &arc.Duration,
&arc.Round, &arc.Attribute, &arc.Access, &arc.State, &arc.Tag, &arc.PTime, &arc.CTime, &arc.MTime); err != nil {
log.Error("rows.Scan() error(%v)", err)
return
}
archives = append(archives, arc)
}
return
}
//UpTag update archive tag
func (d *Dao) UpTag(c context.Context, aid int64, tags string) (rows int64, err error) {
res, err := d.db.Exec(c, _upArcTagSQL, tags, aid)
if err != nil {
log.Error("d.UpTag.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}

View File

@@ -0,0 +1,28 @@
package archive
import (
"context"
"go-common/app/job/main/videoup-report/model/archive"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_delaysSQL = "SELECT id,aid,dtime,type,state FROM archive_delay WHERE aid=? AND deleted_at = 0 ORDER BY dtime DESC LIMIT 1"
)
// Delay get delay by aid
func (d *Dao) Delay(c context.Context, aid int64) (delay *archive.Delay, err error) {
rows := d.db.QueryRow(c, _delaysSQL, aid)
delay = &archive.Delay{}
if err = rows.Scan(&delay.ID, &delay.Aid, &delay.DTime, &delay.Type, &delay.State); err != nil {
if err == sql.ErrNoRows {
delay = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}

View File

@@ -0,0 +1,81 @@
package archive
import (
"context"
"database/sql"
"go-common/app/job/main/videoup-report/model/archive"
"go-common/library/log"
)
const (
_operLastRoundSQL = "SELECT id,aid,uid,typeid,state,round,attribute,last_id,ctime,mtime FROM archive_oper WHERE aid = ? AND round = ? ORDER BY id DESC LIMIT 1"
_operNextRoundSQL = "SELECT id,aid,uid,typeid,state,round,attribute,last_id,ctime,mtime FROM archive_oper WHERE id > ? AND aid = ? AND round != ? ORDER BY id ASC LIMIT 1"
_operInsertSQL = "INSERT INTO archive_oper(aid,uid,typeid,state,content,round,attribute,last_id,remark) VALUES(?,399,?,?,?,?,?,?,?)"
_lastArcOperSQL = "SELECT aid,uid,typeid,content,round,attribute,last_id,remark FROM archive_oper WHERE aid=? AND uid!=399 ORDER BY mtime DESC LIMIT 1"
_lastVideoOperUIDSQL = "SELECT uid FROM archive_video_oper WHERE vid=? AND uid != 399 ORDER BY mtime DESC LIMIT 1;"
_lastVideoOperSQL = "SELECT aid, uid, vid, status, content, attribute, last_id, remark FROM archive_video_oper WHERE vid=? AND uid != 399 ORDER BY mtime DESC LIMIT 1;"
)
// LastRoundOper get last archive round record.
func (d *Dao) LastRoundOper(c context.Context, aid int64, round int8) (oper *archive.Oper, err error) {
row := d.db.QueryRow(c, _operLastRoundSQL, aid, round)
oper = &archive.Oper{}
if err = row.Scan(&oper.ID, &oper.AID, &oper.UID, &oper.TypeID, &oper.State, &oper.Round, &oper.Attribute, &oper.LastID, &oper.CTime, &oper.MTime); err != nil {
log.Error("row.Scan error(%v)", err)
return
}
return
}
// NextRoundOper get next archive round record.
func (d *Dao) NextRoundOper(c context.Context, id int64, aid int64, round int8) (oper *archive.Oper, err error) {
row := d.db.QueryRow(c, _operNextRoundSQL, id, aid, round)
oper = &archive.Oper{}
if err = row.Scan(&oper.ID, &oper.AID, &oper.UID, &oper.TypeID, &oper.State, &oper.Round, &oper.Attribute, &oper.LastID, &oper.CTime, &oper.MTime); err != nil {
log.Error("row.Scan error(%v)", err)
return
}
return
}
// AddArchiveOper add archive operate log
func (d *Dao) AddArchiveOper(c context.Context, aid int64, attribute int32, typeid int16, state int, round int8, lastID int64, content, remark string) (id int64, err error) {
var (
res sql.Result
)
if res, err = d.db.Exec(c, _operInsertSQL, aid, typeid, state, content, round, attribute, lastID, remark); err != nil {
log.Error("AddArchiveOper(%d,%d,%d,%d,%s,%d,%d,%d,%s) error(%v)", aid, typeid, state, content, round, attribute, lastID, remark, err)
return
}
id, err = res.LastInsertId()
return
}
//LastVideoOperUID get the last manual-operate operator id by vid
func (d *Dao) LastVideoOperUID(c context.Context, vid int64) (uid int64, err error) {
if err = d.db.QueryRow(c, _lastVideoOperUIDSQL, vid).Scan(&uid); err != nil {
log.Error("LastVideoOperUID db.row.Scan error(%v) vid(%d)", err, vid)
}
return
}
//LastVideoOper get the last manual-operate record by vid
func (d *Dao) LastVideoOper(c context.Context, vid int64) (oper *archive.VideoOper, err error) {
oper = &archive.VideoOper{}
if err = d.db.QueryRow(c, _lastVideoOperSQL, vid).Scan(&oper.AID, &oper.UID, &oper.VID, &oper.Status, &oper.Content, &oper.Attribute, &oper.LastID, &oper.Remark); err != nil {
log.Error("LastVideoOper db.row.Scan error(%v), vid(%d)", err, vid)
}
return
}
// LastArcOper get a archive last history.
func (d *Dao) LastArcOper(c context.Context, aid int64) (re *archive.Oper, err error) {
re = &archive.Oper{}
if err = d.db.QueryRow(c, _lastArcOperSQL, aid).Scan(&re.AID, &re.UID, &re.TypeID, &re.Content, &re.Round, &re.Attribute, &re.LastID, &re.Remark); err != nil {
log.Error(" LastArcOper db.row.Scan error(%v) aid(%d)", err, aid)
}
return
}

View File

@@ -0,0 +1,45 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
"go-common/app/job/main/videoup-report/model/archive"
)
func TestDao_AddArchiveOper(t *testing.T) {
Convey("AddArchiveOper", t, func() {
c := context.TODO()
a, _ := d.ArchiveByAid(c, 1)
id, err := d.AddArchiveOper(context.TODO(), a.ID, a.Attribute, a.TypeID, a.State, a.Round, 0, "随意一个变更", "测试啦")
So(err, ShouldBeNil)
Println(id)
})
}
func Test_LastVideoOperUID(t *testing.T) {
var (
c = context.TODO()
err error
sub int64
)
Convey("LastVideoOperUID", t, func() {
sub, err = d.LastVideoOperUID(c, 2333)
So(err, ShouldNotBeNil)
So(sub, ShouldBeZeroValue)
})
}
func Test_LastVideoOper(t *testing.T) {
var (
c = context.TODO()
err error
sub *archive.VideoOper
)
Convey("LastVideoOper", t, func() {
sub, err = d.LastVideoOper(c, 2333)
So(err, ShouldNotBeNil)
So(sub, ShouldNotBeNil)
})
}

View File

@@ -0,0 +1,111 @@
package archive
import (
"context"
sql2 "database/sql"
"go-common/app/job/main/videoup-report/model/archive"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_recheckByAid = "SELECT id,type,aid,uid,state,ctime,mtime FROM archive_recheck WHERE aid =? and type = ?"
_inRecheck = "INSERT INTO archive_recheck (type,aid) VALUES (?,?)"
_upRecheckState = "UPDATE archive_recheck SET state=? WHERE aid =? and type = ?"
_upMidRecheckState = "UPDATE archive_recheck as r LEFT JOIN archive AS a ON a.id = r.aid SET r.state=? WHERE a.mid =? and r.type = ? AND r.state = ?"
)
// AddRecheckAids add recheck aids
func (d *Dao) AddRecheckAids(c context.Context, tp int, aids []int64, ignRechecked bool) (err error) {
for _, aid := range aids {
recheck, _ := d.RecheckByAid(c, tp, aid)
if recheck != nil {
if ignRechecked && recheck.State != archive.RecheckStateIgnore {
continue
}
if recheck.State == archive.RecheckStateWait {
log.Info("d.AddRecheckAids(%d) already in recheck", aid)
continue
}
if err = d.UpdateRecheckState(c, tp, aid, archive.RecheckStateWait); err != nil {
log.Error("d.UpdateRecheckState error(%v)", err)
continue
}
} else if _, err = d.db.Exec(c, _inRecheck, tp, aid); err != nil {
log.Error("d.AddRecheckAids.Exec error(%v)", err)
continue
}
a, err := d.ArchiveByAid(c, aid)
if err != nil {
log.Error("d.ArchiveByAid error(%v)", err)
err = nil
continue
}
tpStr := archive.RecheckType(tp)
if tpStr != "" {
d.AddArchiveOper(c, aid, a.Attribute, a.TypeID, a.State, a.Round, 0, "", "待"+tpStr)
}
}
return
}
// UpdateMidRecheckState 设置某个UP主的未回查稿件回查状态
func (d *Dao) UpdateMidRecheckState(c context.Context, tp int, mid int64, state int8) (err error) {
if _, err = d.db.Exec(c, _upMidRecheckState, state, mid, tp, archive.RecheckStateWait); err != nil {
log.Error("d.updateRecheckState.Exec error(%v)", err)
return
}
return
}
// TxAddRecheckAID add recheck aid to db
func (d *Dao) TxAddRecheckAID(tx *sql.Tx, tp int, aid int64) (id int64, err error) {
var (
res sql2.Result
)
if res, err = tx.Exec(_inRecheck, tp, aid); err != nil {
log.Error("TxAddRecheckAID error(%v) type(%d) aid(%d)", err, tp, aid)
return
}
id, err = res.LastInsertId()
return
}
// UpdateRecheckState update recheck state
func (d *Dao) UpdateRecheckState(c context.Context, tp int, aid int64, state int8) (err error) {
if _, err = d.db.Exec(c, _upRecheckState, state, aid, tp); err != nil {
log.Error("d.updateRecheckState.Exec error(%v)", err)
return
}
return
}
//TxUpRecheckState update recheck state
func (d *Dao) TxUpRecheckState(tx *sql.Tx, tp int, aid int64, state int8) (row int64, err error) {
var res sql2.Result
if res, err = tx.Exec(_upRecheckState, state, aid, tp); err != nil {
log.Error("d.TxUpRecheckState.Exec error(%v)", err)
return
}
row, err = res.RowsAffected()
return
}
// RecheckByAid find archive recheck
func (d *Dao) RecheckByAid(c context.Context, tp int, aid int64) (recheck *archive.Recheck, err error) {
row := d.db.QueryRow(c, _recheckByAid, aid, tp)
recheck = &archive.Recheck{}
if err = row.Scan(&recheck.ID, &recheck.Type, &recheck.Aid, &recheck.UID, &recheck.State, &recheck.CTime, &recheck.MTime); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
recheck = nil
return
}
return
}

View File

@@ -0,0 +1,22 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
"go-common/app/job/main/videoup-report/model/archive"
)
func TestDao_TxAddRecheckAID(t *testing.T) {
Convey("TxAddRecheckAID", t, func() {
c := context.TODO()
a, _ := d.ArchiveByAid(c, 1)
tx, _ := d.BeginTran(c)
id, err := d.TxAddRecheckAID(tx, archive.TypeChannelRecheck, a.ID)
tx.Commit()
So(err, ShouldBeNil)
Println(id)
})
}

View File

@@ -0,0 +1,27 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
"time"
)
func Test_Archive(t *testing.T) {
Convey("ArchiveByAid", t, func() {
archive, err := d.ArchiveByAid(context.TODO(), 1)
So(err, ShouldBeNil)
Println(archive)
})
}
func Test_ExcitationArchivesByTime(t *testing.T) {
Convey("ExcitationArchivesByTime", t, func() {
now := time.Now()
st := now.Add(-1680000 * time.Hour)
archives, err := d.ExcitationArchivesByTime(context.TODO(), 27515256, st, now)
So(err, ShouldBeNil)
Println(archives)
})
}

View File

@@ -0,0 +1,19 @@
package archive
import (
"context"
"go-common/library/log"
)
const (
_seNoteSQL = "SELECT note FROM archive_video_audit WHERE vid=?;"
)
//VideoAuditNote get note by vid
func (d *Dao) VideoAuditNote(c context.Context, vid int64) (note string, err error) {
if err = d.db.QueryRow(c, _seNoteSQL, vid).Scan(&note); err != nil {
log.Error("VideoAuditNote db.row.scan error(%v) vid(%d)", err, vid)
}
return
}

View File

@@ -0,0 +1,21 @@
package archive
import (
"context"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func Test_VideoAuditNote(t *testing.T) {
var (
c = context.TODO()
err error
sub string
)
Convey("VideoAuditNote", t, func() {
sub, err = d.VideoAuditNote(c, 2333)
So(err, ShouldNotBeNil)
So(sub, ShouldBeEmpty)
})
}

View File

@@ -0,0 +1,72 @@
package archive
import (
"context"
"database/sql"
"encoding/json"
"go-common/app/job/main/videoup-report/model/task"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_confSQL = "SELECT value FROM archive_config WHERE state=0 AND name=?"
_confForAuditType = "wait_audit_arctype"
_confForWeightValue = "weight_conf_values"
)
// AuditTypesConf get audit conf
func (d *Dao) AuditTypesConf(c context.Context) (atps map[int16]struct{}, err error) {
row := d.db.QueryRow(c, _confSQL, _confForAuditType)
var (
value string
typeIDs []int64
)
if err = row.Scan(&value); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
return
}
typeIDs, err = xstr.SplitInts(value)
if err != nil {
log.Error("archive_config value(%s) xstr.SplitInts error(%v)", value, err)
return
}
atps = map[int16]struct{}{}
for _, typeid := range typeIDs {
atps[int16(typeid)] = struct{}{}
}
return
}
//WeightValueConf 权重数值配置
func (d *Dao) WeightValueConf(c context.Context) (wvconf *task.WeightValueConf, err error) {
var value []byte
if err = d.db.QueryRow(c, _confSQL, _confForWeightValue).Scan(&value); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan() error(%v)", err)
}
return
}
wvconf = new(task.WeightValueConf)
if err = json.Unmarshal(value, wvconf); err != nil {
log.Error("json.Unmarshal error(%v)", err)
wvconf = nil
return
}
wvconf.Nsum9 = wvconf.Nlv5 * 3
wvconf.Nsum15 = (wvconf.Nlv1 * 2) + wvconf.Nsum9
wvconf.Nsum27 = (wvconf.Nlv2 * 4) + wvconf.Nsum15
wvconf.Nsum45 = (wvconf.Nlv3 * 6) + wvconf.Nsum27
wvconf.Tsum2h = wvconf.Tlv1 * 40
wvconf.Tsum1h = (wvconf.Tlv2 * 20) + wvconf.Tsum2h
wvconf.MinWeight = -(wvconf.Tsum1h + wvconf.Tlv3*10)
return
}

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,80 @@
package archive
import (
"context"
"go-common/app/job/main/videoup-report/model/archive"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_oidFlowCount = "SELECT COUNT(*) as count FROM flow_design WHERE state = 0 AND pool = ? AND group_id = ? AND oid = ?"
_inFlowSQL = "INSERT into flow_design(pool,oid,group_id,uid,remark) VALUES (?,?,?,?,?)"
_inFlowLogSQL = "INSERT into flow_design_log(pool,oid,group_id,uid,action,remark) VALUES (?,?,?,?,?,?)"
_upFlowStateSQL = "UPDATE flow_design SET state=? WHERE id=?"
_flowUniqueSQL = "SELECT id,pool,oid,group_id,parent,state FROM flow_design WHERE oid=? AND pool=? AND group_id=? LIMIT 1"
)
// HasFlowGroup check if has flow group record
func (d *Dao) HasFlowGroup(c context.Context, pool int, gid, oid int64) (has bool, err error) {
var (
count int
)
row := d.db.QueryRow(c, _oidFlowCount, pool, gid, oid)
if err = row.Scan(&count); err != nil {
log.Error("d.hasFlowGroup err(%v)", err)
return
}
has = count > 0
return
}
// TxAddFlow tx add flow_design.
func (d *Dao) TxAddFlow(tx *sql.Tx, pool int8, oid, uid, groupID int64, remark string) (id int64, err error) {
res, err := tx.Exec(_inFlowSQL, pool, oid, groupID, uid, remark)
if err != nil {
log.Error("d.TxAddFlow.Exec() error(%v)", err)
return
}
id, err = res.LastInsertId()
return
}
// TxAddFlowLog tx add flow_design log.
func (d *Dao) TxAddFlowLog(tx *sql.Tx, pool, action int8, oid, uid, groupID int64, remark string) (id int64, err error) {
res, err := tx.Exec(_inFlowLogSQL, pool, oid, groupID, uid, action, remark)
if err != nil {
log.Error("d._inFlowLog.Exec() error(%v)", err)
return
}
id, err = res.LastInsertId()
return
}
// TxUpFlowState 更新pool!=1的流量套餐资源的状态
// return int64, error/nil
func (d *Dao) TxUpFlowState(tx *sql.Tx, id int64, state int8) (rows int64, err error) {
res, err := tx.Exec(_upFlowStateSQL, state, id)
if err != nil {
log.Error("TxUpFlowState.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}
// FlowUnique 获取命中 指定流量套餐的记录
// return *archive.FlowData/nil, error/nil
func (d *Dao) FlowUnique(c context.Context, oid, groupID int64, pool int8) (f *archive.FlowData, err error) {
f = &archive.FlowData{}
if err = d.db.QueryRow(context.TODO(), _flowUniqueSQL, oid, pool, groupID).Scan(&f.ID, &f.Pool, &f.OID, &f.GroupID, &f.Parent, &f.State); err != nil {
if err == sql.ErrNoRows {
err = nil
f = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}

View File

@@ -0,0 +1,51 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
"go-common/app/job/main/videoup-report/model/archive"
)
func TestDao_TxAddFlow(t *testing.T) {
Convey("TxAddFlow", t, func() {
c := context.TODO()
tx, _ := d.BeginTran(c)
fid, err := d.TxAddFlow(tx, archive.PoolArcForbid, 1, 0, archive.FLowGroupIDChannel, "测试添加")
tx.Commit()
So(err, ShouldBeNil)
Println(fid)
})
}
func TestDao_TxAddFlowLog(t *testing.T) {
Convey("TxAddFlowLog", t, func() {
c := context.TODO()
tx, _ := d.BeginTran(c)
id, err := d.TxAddFlowLog(tx, archive.PoolArcForbid, archive.FlowLogAdd, 1, 0, archive.FLowGroupIDChannel, "测试添加")
tx.Commit()
So(err, ShouldBeNil)
Println(id)
})
}
func TestDao_TxUpFlowState(t *testing.T) {
Convey("TxUpFlowState", t, func() {
c := context.TODO()
tx, _ := d.BeginTran(c)
id, err := d.TxUpFlowState(tx, 551, archive.FlowDelete)
tx.Commit()
So(err, ShouldBeNil)
Println(id)
})
}
func TestDao_FlowUnique(t *testing.T) {
Convey("FlowUnique", t, func() {
c := context.TODO()
f, err := d.FlowUnique(c, 1, archive.FLowGroupIDChannel, archive.PoolArcForbid)
So(err, ShouldBeNil)
Println(f)
})
}

View File

@@ -0,0 +1,62 @@
package archive
import (
"context"
"time"
"go-common/app/job/main/videoup-report/model/archive"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_reportAddByTypeIDSQL = "INSERT INTO archive_report_sum(content,ctime,mtime,type) VALUE(?,?,?,?)"
_reportGetByTypeIDSQL = "SELECT id,content,ctime,mtime,type FROM archive_report_sum WHERE type=? AND mtime>=? AND mtime<=?"
_reportLastByTypeIDSQL = "SELECT id,content,ctime,mtime,type FROM archive_report_sum WHERE type=? ORDER BY id DESC LIMIT 1"
)
// ReportLast get last inserted report
func (d *Dao) ReportLast(c context.Context, typeid int8) (report *archive.Report, err error) {
row := d.db.QueryRow(c, _reportLastByTypeIDSQL, typeid)
report = &archive.Report{}
if err = row.Scan(&report.ID, &report.Content, &report.CTime, &report.MTime, &report.TypeID); err != nil {
if err == sql.ErrNoRows {
report = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
return
}
return
}
// ReportAdd report add of typeid
func (d *Dao) ReportAdd(c context.Context, typeid int8, content string, ctime, mtime time.Time) (lastID int64, err error) {
res, err := d.db.Exec(c, _reportAddByTypeIDSQL, content, ctime, mtime, typeid)
if err != nil {
log.Error("d.TaskTookAddStmt.Exec error(%v)", err)
return
}
lastID, err = res.LastInsertId()
return
}
// Reports report get of typeid
func (d *Dao) Reports(c context.Context, typeid int8, stime, etime time.Time) (reports []*archive.Report, err error) {
rows, err := d.db.Query(c, _reportGetByTypeIDSQL, typeid, stime, etime)
if err != nil {
log.Error("d.Reports.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
report := &archive.Report{}
if err = rows.Scan(&report.ID, &report.Content, &report.CTime, &report.MTime, &report.TypeID); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
reports = append(reports, report)
}
return
}

View File

@@ -0,0 +1,143 @@
package archive
import (
"context"
"time"
"go-common/app/job/main/videoup-report/model/archive"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
// task
_taskByMtimeSQL = "SELECT id,state,ctime,mtime FROM task_dispatch WHERE mtime>? and ptime=0"
_taskDoneByMtimeSQL = "SELECT id,state,ctime,mtime FROM task_dispatch_done WHERE mtime>? and ptime=0"
_taskByUntreatedSQL = "SELECT id,state,ctime,mtime FROM task_dispatch WHERE (state=0 OR state=1) and ptime=0"
// task took in and sel
_addTaskTookSQL = "INSERT INTO task_dispatch_took(m50,m60,m80,m90,type,ctime,mtime) VALUE(?,?,?,?,?,?,?)"
_taskTooksSQL = "SELECT id,m50,m60,m80,m90,type,ctime,mtime FROM task_dispatch_took WHERE type=1 AND ctime>?"
_taskTookByHalfHourSQL = "SELECT id,m50,m60,m80,m90,type,ctime,mtime FROM task_dispatch_took WHERE type=2 ORDER BY ctime DESC LIMIT 1"
_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"
)
// TaskByMtime gets to took the task by mtime
func (d *Dao) TaskByMtime(c context.Context, stime time.Time) (tasks []*archive.Task, err error) {
rows, err := d.db.Query(c, _taskByMtimeSQL, stime)
if err != nil {
log.Error("d.taskStmt.Query(%v) error(%v)", stime, err)
return
}
defer rows.Close()
for rows.Next() {
task := &archive.Task{}
if err = rows.Scan(&task.ID, &task.State, &task.Ctime, &task.Mtime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
tasks = append(tasks, task)
}
return
}
// TaskDoneByMtime gets to took the task done by mtime
func (d *Dao) TaskDoneByMtime(c context.Context, stime time.Time) (tasks []*archive.Task, err error) {
rows, err := d.db.Query(c, _taskDoneByMtimeSQL, stime)
if err != nil {
log.Error("d.taskStmt.Query(%v) error(%v)", stime, err)
return
}
defer rows.Close()
for rows.Next() {
task := &archive.Task{}
if err = rows.Scan(&task.ID, &task.State, &task.Ctime, &task.Mtime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
tasks = append(tasks, task)
}
return
}
// TaskByUntreated gets to took the task by untreated
func (d *Dao) TaskByUntreated(c context.Context) (tasks []*archive.Task, err error) {
rows, err := d.db.Query(c, _taskByUntreatedSQL)
if err != nil {
log.Error("d.taskStmt.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
task := &archive.Task{}
if err = rows.Scan(&task.ID, &task.State, &task.Ctime, &task.Mtime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
tasks = append(tasks, task)
}
return
}
// AddTaskTook add TaskTook
func (d *Dao) AddTaskTook(c context.Context, took *archive.TaskTook) (lastID int64, err error) {
res, err := d.db.Exec(c, _addTaskTookSQL, took.M50, took.M60, took.M80, took.M90, took.TypeID, took.Ctime, took.Mtime)
if err != nil {
log.Error("d.TaskTookAddStmt.Exec error(%v)", err)
return
}
lastID, err = res.LastInsertId()
return
}
// TaskTooks gets TaskTook by ctime
func (d *Dao) TaskTooks(c context.Context, stime time.Time) (tooks []*archive.TaskTook, err error) {
rows, err := d.db.Query(c, _taskTooksSQL, stime)
if err != nil {
log.Error("d.TaskTookStmt.Query() error(%v)", 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
}
// TaskTookByHalfHour get TaskTook by half hour
func (d *Dao) TaskTookByHalfHour(c context.Context) (took *archive.TaskTook, err error) {
row := d.db.QueryRow(c, _taskTookByHalfHourSQL)
took = &archive.TaskTook{}
if err = row.Scan(&took.ID, &took.M50, &took.M60, &took.M80, &took.M90, &took.TypeID, &took.Ctime, &took.Mtime); err != nil {
if err == sql.ErrNoRows {
took = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// 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.db.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,32 @@
package archive
import (
"context"
"fmt"
"go-common/library/log"
)
const (
_consumerOnlineSQL = "SELECT uid FROM task_consumer WHERE uid IN (%s) AND state=1"
)
//ConsumerOnline get online task_consumer
func (d *Dao) ConsumerOnline(c context.Context, uids string) (ids []int64, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_consumerOnlineSQL, uids))
if err != nil {
log.Error("d.db.Query(%s, %v) error(%v)", _consumerOnlineSQL, uids, err)
return
}
defer rows.Close()
for rows.Next() {
var id int64
if err = rows.Scan(&id); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
ids = append(ids, id)
}
return
}

View File

@@ -0,0 +1,117 @@
package archive
import (
"context"
"time"
"go-common/app/job/main/videoup-report/model/task"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_dispatchSQL = "SELECT id,state FROM task_dispatch WHERE aid=? AND cid=? ORDER BY id DESC"
_inDispatchSQL = "INSERT INTO task_dispatch(pool,subject,adminid,aid,cid,uid,state,conf_id,conf_state,conf_weight,upspecial,cftime,ptime) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)"
_delDispatchSQL = "UPDATE task_dispatch SET state=? WHERE aid=? AND cid=? AND state!=?"
_delDispatchByAidSQL = "UPDATE task_dispatch SET state=? WHERE aid=? AND state IN (?,?,?)"
_delDispatchByTimeSQL = "DELETE FROM task_dispatch WHERE mtime>=? AND mtime<=? AND state in (2,6)"
_taskIDforWeightSQL = "SELECT id FROM task_dispatch WHERE state=0 AND id>? ORDER BY id ASC limit 1000"
_upTaskWeightSQL = "UPDATE task_dispatch set weight=?,uptime=now() where id=? and state=0"
_upSpecialSQL = "UPDATE task_dispatch SET upspecial=? WHERE id=?"
)
// DispatchState get dipatch state.
func (d *Dao) DispatchState(c context.Context, aid, cid int64) (id int64, state int8, err error) {
row := d.db.QueryRow(c, _dispatchSQL, aid, cid)
if err = row.Scan(&id, &state); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("row.Scan(%d) error(%v)", err)
return
}
return
}
// AddDispatch add task dispatch
func (d *Dao) AddDispatch(c context.Context, t *task.Task) (lastID int64, err error) {
res, err := d.db.Exec(c, _inDispatchSQL, t.Pool, t.Subject, t.AdminID, t.Aid, t.Cid, t.UID, t.State,
t.ConfigID, t.ConfigState, t.ConfigWeight, t.UPSpecial, t.CFtime, t.Ptime)
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", _inDispatchSQL, err)
return
}
return res.LastInsertId()
}
// DelDispatch del dispatch.
func (d *Dao) DelDispatch(c context.Context, aid, cid int64) (rows int64, err error) {
res, err := d.db.Exec(c, _delDispatchSQL, task.StateForTaskUserDeleted, aid, cid, task.StateForTaskUserDeleted)
if err != nil {
log.Error("d.db.Exec(%s, %d, %d) error(%v)", _delDispatchSQL, aid, cid, err)
return
}
return res.RowsAffected()
}
// DelDispatchByAid del dispatch by aid.
func (d *Dao) DelDispatchByAid(c context.Context, aid int64) (rows int64, err error) {
res, err := d.db.Exec(c, _delDispatchByAidSQL, task.StateForTaskUserDeleted, aid, task.StateForTaskDefault, task.StateForTaskWork, task.StateForTaskDelay)
if err != nil {
log.Error("d.db.Exec(%s, %d, %d) error(%v)", _delDispatchByAidSQL, aid, err)
return
}
return res.RowsAffected()
}
// TxDelDispatchByTime del dispatch by time segment
func (d *Dao) TxDelDispatchByTime(c context.Context, tx *sql.Tx, startTime, endTime time.Time) (rows int64, err error) {
res, err := tx.Exec(_delDispatchByTimeSQL, startTime.Format("2006-01-02 15:04:05"), endTime.Format("2006-01-02 15:04:05"))
if err != nil {
log.Error("tx.Exec(%s, %s, %s) error(%v)", _delDispatchByTimeSQL, startTime.Format("2006-01-02 15:04:05"), endTime.Format("2006-01-02 15:04:05"), err)
return
}
return res.RowsAffected()
}
// TaskIDforWeight 获取需要更新权重的任务id(用于给redis批量读取)
func (d *Dao) TaskIDforWeight(c context.Context, lastid int64) (ids []int64, last int64, err error) {
rows, err := d.db.Query(c, _taskIDforWeightSQL, lastid) //获取一批待审核任务
if err != nil {
log.Error("d.db.Query(%s, %d) error(%v)", _taskIDforWeightSQL, lastid, err)
return
}
defer rows.Close()
for rows.Next() {
var id int64
if err = rows.Scan(&id); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
ids = append(ids, id)
last = id
}
return
}
// UpTaskWeight 更新单条权重
func (d *Dao) UpTaskWeight(c context.Context, taskid int64, weight int64) (rows int64, err error) {
res, err := d.db.Exec(c, _upTaskWeightSQL, weight, taskid)
if err != nil {
log.Error("d.db.Exec(%s,%d,%d) error(%v)", _upTaskWeightSQL, weight, taskid, err)
return
}
return res.RowsAffected()
}
// SetUpSpecial 更新单条权重
func (d *Dao) SetUpSpecial(c context.Context, taskid int64, special int8) (rows int64, err error) {
res, err := d.db.Exec(c, _upSpecialSQL, special, taskid)
if err != nil {
log.Error("d.db.Exec(%s,%d,%d) error(%v)", _upSpecialSQL, special, taskid, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,46 @@
package archive
import (
"context"
"time"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_inDispatchDoneSQL = `INSERT IGNORE INTO task_dispatch_done(task_id,pool,subject,adminid,aid,cid,uid,state,utime,ctime,mtime,dtime,gtime,weight,conf_id,conf_state,conf_weight,upspecial,ptime,uptime,cftime)
SELECT id,pool,subject,adminid,aid,cid,uid,state,utime,ctime,mtime,dtime,gtime,weight,conf_id,conf_state,conf_weight,upspecial,ptime,uptime,cftime FROM task_dispatch WHERE mtime>=? AND mtime<=? AND state IN (2,6);`
_delTaskDoneBeforeSQL = "DELETE FROM task_dispatch_done WHERE mtime<=? LIMIT ?"
_delTaskBeforeSQL = "DELETE FROM task_dispatch WHERE mtime<=? LIMIT ?"
)
// TxAddDispatchDone add task dispatch done
func (d *Dao) TxAddDispatchDone(c context.Context, tx *sql.Tx, startTime, endTime time.Time) (rows int64, err error) {
res, err := tx.Exec(_inDispatchDoneSQL, startTime.Format("2006-01-02 15:04:05"), endTime.Format("2006-01-02 15:04:05"))
if err != nil {
log.Error("tx.Exec(%s, %s, %s) error(%v)", _inDispatchDoneSQL, startTime.Format("2006-01-02 15:04:05"), endTime.Format("2006-01-02 15:04:05"), err)
return
}
return res.RowsAffected()
}
// DelTaskDoneBefore del task_dispatch_done
func (d *Dao) DelTaskDoneBefore(c context.Context, before time.Time, limit int64) (rows int64, err error) {
res, err := d.db.Exec(c, _delTaskDoneBeforeSQL, before.Format("2006-01-02 15:04:05"), limit)
if err != nil {
log.Error("d.db.Exec(%s, %s, %d) error(%v)", _delTaskDoneBeforeSQL, before, limit, err)
return
}
return res.RowsAffected()
}
// DelTaskBefore del task_dispatch
func (d *Dao) DelTaskBefore(c context.Context, before time.Time, limit int64) (rows int64, err error) {
res, err := d.db.Exec(c, _delTaskBeforeSQL, before.Format("2006-01-02 15:04:05"), limit)
if err != nil {
log.Error("d.db.Exec(%s, %s, %d) error(%v)", _delTaskBeforeSQL, before, limit, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,71 @@
package archive
import (
"context"
"database/sql"
"encoding/json"
"time"
"go-common/app/job/main/videoup-report/model/task"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
const (
_getTaskWeight = "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.state=0 AND t.id>? LIMIT 1000"
_inDispatchExtendSQL = "INSERT INTO task_dispatch_extend(task_id,description) VALUE (?,?)"
_delTaskExtendSQL = "DELETE FROM task_dispatch_extend WHERE mtime < ? LIMIT ?"
)
// GetTaskWeight 从数据库读取权重配置
func (d *Dao) GetTaskWeight(c context.Context, lastid int64) (mcases map[int64]*task.WeightParams, err error) {
var (
rows *xsql.Rows
desc sql.NullString
)
if rows, err = d.db.Query(c, _getTaskWeight, lastid); err != nil {
log.Error("d.db.Query(%s, %d) error(%v)", _getTaskWeight, lastid, err)
return
}
defer rows.Close()
mcases = make(map[int64]*task.WeightParams)
for rows.Next() {
tp := new(task.WeightParams)
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 {
arr := []*task.ConfigItem{}
if err = json.Unmarshal([]byte(desc.String), &arr); err != nil {
arr = nil
log.Error("json.Unmarshal error(%v)", err)
continue
}
tp.CfItems = arr
}
mcases[tp.TaskID] = tp
}
return
}
// InDispatchExtend 扩展表,记录权重配置信息
func (d *Dao) InDispatchExtend(c context.Context, taskid int64, desc string) (lastid int64, err error) {
res, err := d.db.Exec(c, _inDispatchExtendSQL, taskid, desc)
if err != nil {
log.Error("tx.Exec(%s, %d, %v) error(%v)", _inDispatchExtendSQL, taskid, desc, err)
return
}
return res.LastInsertId()
}
// DelTaskExtend del task_dispatch_extend
func (d *Dao) DelTaskExtend(c context.Context, before time.Time, limit int64) (rows int64, err error) {
res, err := d.db.Exec(c, _delTaskExtendSQL, before.Format("2006-01-02 15:04:05"), limit)
if err != nil {
log.Error("d.db.Exec(%s, %s, %d) error(%v)", _delTaskExtendSQL, before, limit, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,142 @@
package archive
import (
"context"
"encoding/json"
"fmt"
tmod "go-common/app/job/main/videoup-report/model/task"
"go-common/app/job/main/videoup-report/model/utils"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_assignConfigsSQL = "SELECT id,uids,pool,config_mids,config_tids,config_time,adminid,state,stime,etime FROM task_config WHERE state=0"
_delAConfsSQL = "UPDATE task_config SET state=1 WHERE id IN (%s)"
_weightConfSQL = "SELECT id,description,mtime FROM task_weight_config WHERE state=0" // 查
_delWConfsSQL = "UPDATE task_weight_config SET state=1 WHERE id IN (%s)"
)
//AssignConfigs take config
func (d *Dao) AssignConfigs(c context.Context) (tasks map[int64]*tmod.AssignConfig, err error) {
rows, err := d.db.Query(c, _assignConfigsSQL)
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
tasks = make(map[int64]*tmod.AssignConfig)
defer rows.Close()
for rows.Next() {
var (
midStr string
uidsStr string
tidStr string
durationStr string
mids []int64
tids []int64
durations []int64
)
t := &tmod.AssignConfig{}
if err = rows.Scan(&t.ID, &uidsStr, &t.Pool, &midStr, &tidStr, &durationStr, &t.AdminID, &t.State, &t.STime, &t.ETime); err != nil {
log.Error("rows.Scan error(%v)", err)
continue
}
if uidsStr != "" {
if t.UIDs, err = xstr.SplitInts(uidsStr); err != nil {
log.Error("xstr.SplitInts(%s) errror(%v)", uidsStr, err)
err = nil
continue
}
}
if midStr != "" {
if mids, err = xstr.SplitInts(midStr); err != nil {
log.Error("xstr.SplitInts(%s) error(%v)", midStr, err)
err = nil
continue
}
t.MIDs = make(map[int64]struct{}, len(mids))
for _, mid := range mids {
t.MIDs[mid] = struct{}{}
}
}
if tidStr != "" {
if tids, err = xstr.SplitInts(tidStr); err != nil {
log.Error("xstr.SplitInts(%s) error(%v)", tidStr, err)
err = nil
continue
}
t.TIDs = make(map[int16]struct{}, len(tids))
for _, tid := range tids {
t.TIDs[int16(tid)] = struct{}{}
}
}
if durationStr != "" {
if durations, err = xstr.SplitInts(durationStr); err != nil || len(durations) != 2 {
log.Error("xstr.SplitInts(%s) error(%v)", durationStr, err)
err = nil
continue
}
t.MinDuration = durations[0]
t.MaxDuration = durations[1]
}
if len(t.UIDs) > 0 {
tasks[t.ID] = t
}
}
return
}
// DelAssignConfs 删除指派配置
func (d *Dao) DelAssignConfs(c context.Context, ids []int64) (rows int64, err error) {
sqlstring := fmt.Sprintf(_delAConfsSQL, xstr.JoinInts(ids))
res, err := d.db.Exec(c, sqlstring)
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", sqlstring, err)
return
}
return res.RowsAffected()
}
// WeightConf 所有有效的配置(用于检测是否和以及有的配置冲突)
func (d *Dao) WeightConf(c context.Context) (items []*tmod.ConfigItem, err error) {
var (
id int64
descb []byte
rows *sql.Rows
wci *tmod.ConfigItem
mtime utils.FormatTime
)
if rows, err = d.db.Query(c, _weightConfSQL); err != nil {
log.Error("d.db.Query(%s) error(%v)", _weightConfSQL, err)
return
}
defer rows.Close()
for rows.Next() {
wci = new(tmod.ConfigItem)
if err = rows.Scan(&id, &descb, &mtime); 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)
continue
}
wci.Mtime = mtime
wci.ID = id
items = append(items, wci)
}
return
}
// DelWeightConfs 删除权重配置
func (d *Dao) DelWeightConfs(c context.Context, ids []int64) (rows int64, err error) {
sqlstring := fmt.Sprintf(_delWConfsSQL, xstr.JoinInts(ids))
res, err := d.db.Exec(c, sqlstring)
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", sqlstring, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,56 @@
package archive
import (
"context"
"time"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_inTaskHisSQL = "INSERT INTO task_oper_history(pool,action,task_id,cid,uid,result,reason) VALUE (?,?,?,?,?,?,?);"
_mvTaskOperHisSQL = "INSERT IGNORE INTO task_oper_history_done(id,pool,action,task_id,cid,uid,result,reason,utime,ctime,mtime) SELECT id,pool,action,task_id,cid,uid,result,reason,utime,ctime,mtime FROM task_oper_history WHERE mtime < ? LIMIT ?"
_delTaskOperHisSQL = "DELETE FROM task_oper_history WHERE mtime < ? LIMIT ?"
_delTaskHistoryDoneSQL = "DELETE FROM task_oper_history_done WHERE mtime < ? LIMIT ?"
)
// AddTaskHis add task oper history
func (d *Dao) AddTaskHis(c context.Context, pool int8, action int8, taskID int64, cid int64, uid int64, result int16, reason string) (rows int64, err error) {
res, err := d.db.Exec(c, _inTaskHisSQL, pool, action, taskID, cid, uid, result, reason)
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", _inTaskHisSQL, err)
return
}
return res.RowsAffected()
}
// TxMoveTaskOperDone select into from task_oper_history to task_oper_history_done before t.
func (d *Dao) TxMoveTaskOperDone(tx *sql.Tx, t time.Time, limit int64) (rows int64, err error) {
res, err := tx.Exec(_mvTaskOperHisSQL, t, limit)
if err != nil {
log.Error("tx.Exec(%s, %s, %d) error(%v)", _mvTaskOperHisSQL, t, limit, err)
return
}
return res.RowsAffected()
}
// TxDelTaskOper delete from task_oper_history before t.
func (d *Dao) TxDelTaskOper(tx *sql.Tx, t time.Time, limit int64) (rows int64, err error) {
res, err := tx.Exec(_delTaskOperHisSQL, t, limit)
if err != nil {
log.Error("tx.Exec(%s, %s, %d) error(%v)", _delTaskOperHisSQL, t, limit, err)
return
}
return res.RowsAffected()
}
// DelTaskHistoryDone del oper done
func (d *Dao) DelTaskHistoryDone(c context.Context, before time.Time, limit int64) (rows int64, err error) {
res, err := d.db.Exec(c, _delTaskHistoryDoneSQL, before.Format("2006-01-02 15:04:05"), limit)
if err != nil {
log.Error("d.db.Exec(%s, %s, %d) error(%v)", _delTaskHistoryDoneSQL, before, limit, err)
return
}
return res.RowsAffected()
}

View File

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

View File

@@ -0,0 +1,22 @@
package archive
import (
"context"
"go-common/library/log"
)
const (
_inArcHis = "INSERT INTO archive_track(aid,state,round,attribute,remark,ctime,mtime) VALUES(?,?,?,?,?,?,?)"
)
// AddTrack insert archive track history
func (d *Dao) AddTrack(c context.Context, aid int64, state int, round int8, attr int32, remark string, ctime, mtime string) (rows int64, err error) {
rs, err := d.db.Exec(c, _inArcHis, aid, state, round, attr, remark, ctime, mtime)
if err != nil {
log.Error("d.inArcHisStmt.Exec(%d, %d, %d, %s, %s, %s) error(%v)", aid, state, round, attr, remark, ctime, mtime, err)
return
}
rows, err = rs.RowsAffected()
return
}

View File

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

View File

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

View File

@@ -0,0 +1,85 @@
package archive
import (
"context"
"go-common/library/log"
"time"
"database/sql"
"go-common/app/job/main/videoup-report/model/archive"
farm "github.com/dgryski/go-farm"
)
const (
_updatedFilenamesByTime = "SELECT filename FROM video WHERE mtime >= ? AND mtime < ?"
_videos2SQL = `SELECT vr.id,v.filename,vr.cid,vr.aid,vr.title,vr.description,v.src_type,v.duration,v.filesize,v.resolutions,
v.playurl,v.failcode,vr.index_order,v.attribute,v.xcode_state,v.status,vr.state,vr.ctime,vr.mtime FROM archive_video_relation AS vr JOIN video AS v ON vr.cid=v.id WHERE vr.aid=? ORDER BY vr.index_order`
_newVideoByFnSQL = `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,v.status,avr.ctime,avr.mtime FROM archive_video_relation avr JOIN video v on avr.cid = v.id
WHERE hash64=? AND filename=?`
)
// UpdatedFilenames Get updated video's filename between stime and etime.
func (d *Dao) UpdatedFilenames(c context.Context, stime, etime time.Time) (fns []string, err error) {
rows, err := d.db.Query(c, _updatedFilenamesByTime, stime, etime)
if err != nil {
log.Error("d.UpdatedFilenames.Query(%v,%v) error(%v)", stime, etime, err)
return
}
defer rows.Close()
for rows.Next() {
fn := ""
if err = rows.Scan(&fn); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
fns = append(fns, fn)
}
return
}
// Videos2 get videos by 2 table em.......
func (d *Dao) Videos2(c context.Context, aid int64) (vs []*archive.Video, err error) {
rows, err := d.db.Query(c, _videos2SQL, aid)
if err != nil {
log.Error("d.db.Query(%s, %d) error(%v)", _videos2SQL, aid, err)
return
}
defer rows.Close()
for rows.Next() {
v := &archive.Video{}
if err = rows.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
&v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &v.Status, &v.State, &v.CTime, &v.MTime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
vs = append(vs, v)
}
return
}
// NewVideo get video info by filename.
func (d *Dao) NewVideo(c context.Context, filename string) (v *archive.Video, err error) {
hash64 := int64(farm.Hash64([]byte(filename)))
row := d.db.QueryRow(c, _newVideoByFnSQL, hash64, filename)
v = &archive.Video{}
var avrState, vState int16
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, &avrState, &vState, &v.CTime, &v.MTime); err != nil {
if err == sql.ErrNoRows {
v = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
return
}
// 2 state map to 1
if avrState == archive.VideoStatusDelete {
v.Status = archive.VideoStatusDelete
} else {
v.Status = vState
}
return
}

View File

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

View File

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

View File

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