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,62 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"base.go",
"dao.go",
"paramter.go",
"past.go",
"rating.go",
"statistics.go",
"task_status.go",
],
importpath = "go-common/app/job/main/up-rating/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/up-rating/conf:go_default_library",
"//app/job/main/up-rating/model:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = [
"base_test.go",
"dao_test.go",
"paramter_test.go",
"past_test.go",
"rating_test.go",
"statistics_test.go",
"task_status_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/job/main/up-rating/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,85 @@
package dao
import (
"context"
"database/sql"
"fmt"
"time"
"go-common/app/job/main/up-rating/model"
"go-common/library/log"
)
const (
// get base info from up_level_info
_baseInfoSQL = "SELECT id,mid,tag_id,inc_play,inc_coin,avs,maafans,mahfans,open_avs,lock_avs,cdate FROM up_level_info_%02d WHERE id > ? AND id <= ? LIMIT ?"
_baseTotalSQL = "SELECT id,mid,total_fans,total_avs,total_coin,total_play FROM up_level_info_%02d WHERE id > ? AND cdate = '%s' LIMIT ?"
// get up_level_info start & end
_baseInfoStartSQL = "SELECT id FROM up_level_info_%02d WHERE cdate=? ORDER BY id LIMIT 1"
_baseInfoEndSQL = "SELECT id FROM up_level_info_%02d WHERE cdate=? ORDER BY id DESC LIMIT 1"
)
// GetBaseInfo get rating info
func (d *Dao) GetBaseInfo(c context.Context, month time.Month, start, end, limit int) (bs []*model.BaseInfo, id int, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_baseInfoSQL, month), start, end, limit)
if err != nil {
log.Error("d.db.Query Rating Base Info error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
b := &model.BaseInfo{}
err = rows.Scan(&id, &b.MID, &b.TagID, &b.PlayIncr, &b.CoinIncr, &b.Avs, &b.MAAFans, &b.MAHFans, &b.OpenAvs, &b.LockedAvs, &b.Date)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
bs = append(bs, b)
}
return
}
// GetBaseTotal get total
func (d *Dao) GetBaseTotal(c context.Context, date time.Time, id, limit int64) (bs []*model.BaseInfo, err error) {
bs = make([]*model.BaseInfo, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_baseTotalSQL, date.Month(), date.Format("2006-01-02")), id, limit)
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
b := &model.BaseInfo{}
err = rows.Scan(&b.ID, &b.MID, &b.TotalFans, &b.TotalAvs, &b.TotalCoin, &b.TotalPlay)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
bs = append(bs, b)
}
return
}
// BaseInfoStart get start id by date
func (d *Dao) BaseInfoStart(c context.Context, date time.Time) (start int, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_baseInfoStartSQL, date.Month()), date)
if err = row.Scan(&start); err != nil {
if err == sql.ErrNoRows {
err = nil
}
}
return
}
// BaseInfoEnd get end id by date
func (d *Dao) BaseInfoEnd(c context.Context, date time.Time) (end int, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_baseInfoEndSQL, date.Month()), date)
if err = row.Scan(&end); err != nil {
if err == sql.ErrNoRows {
err = nil
}
}
return
}

View File

@@ -0,0 +1,83 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetBaseInfo(t *testing.T) {
convey.Convey("GetBaseInfo", t, func(ctx convey.C) {
var (
c = context.Background()
month time.Month = time.June
start = int(0)
end = int(1000)
limit = int(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_level_info_06(mid) VALUES(100) ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
bs, id, err := d.GetBaseInfo(c, month, start, end, limit)
ctx.Convey("Then err should be nil.bs,id should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(id, convey.ShouldNotBeNil)
ctx.So(bs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetBaseTotal(t *testing.T) {
convey.Convey("GetBaseTotal", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, time.June, 1, 0, 0, 0, 0, time.Local)
id = int64(0)
limit = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_level_info_06(mid) VALUES(101) ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
bs, err := d.GetBaseTotal(c, date, id, limit)
ctx.Convey("Then err should be nil.bs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(bs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoBaseInfoStart(t *testing.T) {
convey.Convey("BaseInfoStart", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, time.June, 1, 0, 0, 0, 0, time.Local)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_level_info_06(mid) VALUES(102) ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
start, err := d.BaseInfoStart(c, date)
ctx.Convey("Then err should be nil.start should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(start, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoBaseInfoEnd(t *testing.T) {
convey.Convey("BaseInfoEnd", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, time.June, 1, 0, 0, 0, 0, time.Local)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_level_info_06(mid) VALUES(103) ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
end, err := d.BaseInfoEnd(c, date)
ctx.Convey("Then err should be nil.end should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(end, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,44 @@
package dao
import (
"context"
"go-common/app/job/main/up-rating/conf"
"go-common/library/database/sql"
"go-common/library/log"
)
// Dao dao
type Dao struct {
c *conf.Config
db *sql.DB
}
// New fn
func New(c *conf.Config) (d *Dao) {
log.Info("dao start")
d = &Dao{
c: c,
db: sql.NewMySQL(c.MySQL.Rating),
}
//d.db.State = prom.LibClient
return
}
// Ping ping health.
func (d *Dao) Ping(c context.Context) (err error) {
return d.db.Ping(c)
}
// Close close connections of mc, redis, db.
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
}
// BeginTran begin transcation
func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
return d.db.Begin(c)
}

View File

@@ -0,0 +1,34 @@
package dao
import (
"flag"
"go-common/app/job/main/up-rating/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.up-rating-job")
flag.Set("conf_token", "b48530d9e9d1a3aa20e078e8c72932d8")
flag.Set("tree_id", "65764")
flag.Set("conf_version", "0.0.1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../cmd/up-rating-job.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,33 @@
package dao
import (
"context"
"go-common/library/log"
)
var (
_allParamterSQL = "SELECT name, value FROM rating_parameter"
)
// GetAllParamter get all paramter
func (d *Dao) GetAllParamter(c context.Context) (paramters map[string]int64, err error) {
paramters = make(map[string]int64)
rows, err := d.db.Query(c, _allParamterSQL)
if err != nil {
log.Error("d.db.Query GetAllParamter error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var name string
var vaule int64
err = rows.Scan(&name, &vaule)
if err != nil {
log.Error("rows.Scan GetAllParamter error(%v)", err)
return
}
paramters[name] = vaule
}
return
}

View File

@@ -0,0 +1,24 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetAllParamter(t *testing.T) {
convey.Convey("GetAllParamter", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO rating_parameter(name,value) VALUES('test', 123)")
paramters, err := d.GetAllParamter(c)
ctx.Convey("Then err should be nil.paramters should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(paramters, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,93 @@
package dao
import (
"context"
"fmt"
"time"
"go-common/app/job/main/up-rating/model"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_pastRatingRecordSQL = "SELECT times FROM past_rating_record WHERE cdate = '%s' AND is_deleted = 0"
_pastStatSQL = "SELECT id,mid,creativity_score,influence_score,credit_score FROM past_score_statistics WHERE id > ? ORDER BY id LIMIT ?"
// insert
_inPastRecordSQL = "INSERT INTO past_rating_record(times, cdate) VALUES(?,'%s') ON DUPLICATE KEY UPDATE times=VALUES(times)"
_pastScoreStatSQL = "INSERT INTO past_score_statistics(mid,creativity_score,influence_score,credit_score) VALUES %s ON DUPLICATE KEY UPDATE creativity_score=creativity_score+VALUES(creativity_score),influence_score=influence_score+VALUES(influence_score),credit_score=VALUES(credit_score)"
// delete
_delPastStatSQL = "DELETE FROM past_score_statistics ORDER BY ID LIMIT ?"
_delPastRecordSQL = "DELETE FROM past_rating_record WHERE cdate='%s'"
)
// DelPastRecord del past record
func (d *Dao) DelPastRecord(c context.Context, date time.Time) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_delPastRecordSQL, date.Format(_layout)))
if err != nil {
return
}
return res.RowsAffected()
}
// GetPastRecord batch insert past score stat
func (d *Dao) GetPastRecord(c context.Context, cdate string) (times int, err error) {
err = d.db.QueryRow(c, fmt.Sprintf(_pastRatingRecordSQL, cdate)).Scan(&times)
if err == sql.ErrNoRows {
err = nil
times = -1
}
return
}
// InsertPastRecord insert past record date and times
func (d *Dao) InsertPastRecord(c context.Context, times int, cdate string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inPastRecordSQL, cdate), times)
if err != nil {
return
}
return res.RowsAffected()
}
// InsertPastScoreStat batch insert past score stat
func (d *Dao) InsertPastScoreStat(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_pastScoreStatSQL, values))
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", fmt.Sprintf(_pastScoreStatSQL, values), err)
return
}
return res.RowsAffected()
}
// GetPasts get past statistics
func (d *Dao) GetPasts(c context.Context, offset, limit int64) (past []*model.Past, last int64, err error) {
past = make([]*model.Past, 0, limit)
rows, err := d.db.Query(c, _pastStatSQL, offset, limit)
if err != nil {
log.Error("d.db.Query GetPasts error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
p := &model.Past{}
err = rows.Scan(&last, &p.MID, &p.MetaCreativityScore, &p.MetaInfluenceScore, &p.CreditScore)
if err != nil {
log.Error("rows.Scan GetPasts error(%v)", err)
return
}
past = append(past, p)
}
return
}
// DelPastStat del past stat
func (d *Dao) DelPastStat(c context.Context, limit int64) (rows int64, err error) {
res, err := d.db.Exec(c, _delPastStatSQL, limit)
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,112 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoDelPastRecord(t *testing.T) {
convey.Convey("DelPastRecord", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, time.June, 1, 0, 0, 0, 0, time.Local)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO past_rating_record(times,date) VALUES(1, '2018-06-01') ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
rows, err := d.DelPastRecord(c, date)
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 TestDaoGetPastRecord(t *testing.T) {
convey.Convey("GetPastRecord", t, func(ctx convey.C) {
var (
c = context.Background()
cdate = "2018-06-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO past_rating_record(times,date) VALUES(1, '2018-06-01') ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
times, err := d.GetPastRecord(c, cdate)
ctx.Convey("Then err should be nil.times should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(times, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertPastRecord(t *testing.T) {
convey.Convey("InsertPastRecord", t, func(ctx convey.C) {
var (
c = context.Background()
times = int(1)
cdate = "2018-06-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertPastRecord(c, times, cdate)
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 TestDaoInsertPastScoreStat(t *testing.T) {
convey.Convey("InsertPastScoreStat", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(1, 100, 100, 100)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertPastScoreStat(c, values)
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 TestDaoGetPasts(t *testing.T) {
convey.Convey("GetPasts", t, func(ctx convey.C) {
var (
c = context.Background()
offset = int64(0)
limit = int64(1000)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO past_score_statistics(mid) VALUES(1) ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
past, last, err := d.GetPasts(c, offset, limit)
ctx.Convey("Then err should be nil.past,last should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(last, convey.ShouldNotBeNil)
ctx.So(past, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDelPastStat(t *testing.T) {
convey.Convey("DelPastStat", t, func(ctx convey.C) {
var (
c = context.Background()
limit = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO past_score_statistics(mid) VALUES(1) ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
rows, err := d.DelPastStat(c, limit)
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,118 @@
package dao
import (
"context"
"database/sql"
"fmt"
"time"
"go-common/app/job/main/up-rating/model"
"go-common/library/log"
)
const (
_layout = "2006-01-02"
// get up_rating start & end
_ratingStartSQL = "SELECT id FROM up_rating_%02d WHERE cdate='%s' ORDER BY id LIMIT 1"
_ratingEndSQL = "SELECT id FROM up_rating_%02d WHERE cdate='%s' ORDER BY id DESC LIMIT 1"
_ratingCountSQL = "SELECT COUNT(*) FROM up_rating_%02d WHERE cdate='%s'"
_ratingSQL = "SELECT id,mid,tag_id,creativity_score,influence_score,credit_score,meta_creativity_score,meta_influence_score,cdate FROM up_rating_%02d WHERE cdate= '%s' AND id > ? ORDER BY id LIMIT ?"
_ratingByIDSQL = "SELECT id,mid,tag_id,creativity_score,influence_score,credit_score,meta_creativity_score,meta_influence_score,magnetic_score,cdate FROM up_rating_%02d WHERE id > ? AND id <= ? LIMIT ?"
_ratingScoreSQL = "INSERT INTO up_rating_%02d(mid,tag_id,cdate,creativity_score,influence_score,credit_score,meta_creativity_score,meta_influence_score, magnetic_score) VALUES %s ON DUPLICATE KEY UPDATE tag_id=VALUES(tag_id), cdate=VALUES(cdate), creativity_score=VALUES(creativity_score),influence_score=VALUES(influence_score),credit_score=VALUES(credit_score),meta_creativity_score=VALUES(meta_creativity_score),meta_influence_score=VALUES(meta_influence_score),magnetic_score=VALUES(magnetic_score)"
_delRatingScoreSQL = "DELETE FROM up_rating_%02d WHERE cdate='%s' LIMIT ?"
)
// DelRatings del ratings
func (d *Dao) DelRatings(c context.Context, date time.Time, limit int) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_delRatingScoreSQL, date.Month(), date.Format(_layout)), limit)
if err != nil {
return
}
return res.RowsAffected()
}
// RatingStart get start id by date
func (d *Dao) RatingStart(c context.Context, date time.Time) (start int, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_ratingStartSQL, date.Month(), date.Format(_layout)))
if err = row.Scan(&start); err != nil {
if err == sql.ErrNoRows {
err = nil
}
}
return
}
// RatingEnd get end id by date
func (d *Dao) RatingEnd(c context.Context, date time.Time) (end int, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_ratingEndSQL, date.Month(), date.Format(_layout)))
if err = row.Scan(&end); err != nil {
if err == sql.ErrNoRows {
err = nil
}
}
return
}
// RatingCount get end id by date
func (d *Dao) RatingCount(c context.Context, date time.Time) (count int, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_ratingCountSQL, date.Month(), date.Format(_layout)))
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
}
}
return
}
// GetRatings get ratings by date
func (d *Dao) GetRatings(c context.Context, date time.Time, offset, limit int) (rs []*model.Rating, last int, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_ratingSQL, date.Month(), date.Format(_layout)), offset, limit)
if err != nil {
log.Error("d.db.Query Rating Info error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
r := &model.Rating{}
err = rows.Scan(&last, &r.MID, &r.TagID, &r.CreativityScore, &r.InfluenceScore, &r.CreditScore, &r.MetaCreativityScore, &r.MetaInfluenceScore, &r.Date)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
rs = append(rs, r)
}
return
}
// GetRatingsFast get rating fast
func (d *Dao) GetRatingsFast(c context.Context, date time.Time, start, end, limit int) (rs []*model.Rating, id int, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_ratingByIDSQL, date.Month()), start, end, limit)
if err != nil {
log.Error("d.db.Query Rating Info error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
r := &model.Rating{}
err = rows.Scan(&id, &r.MID, &r.TagID, &r.CreativityScore, &r.InfluenceScore, &r.CreditScore, &r.MetaCreativityScore, &r.MetaInfluenceScore, &r.MagneticScore, &r.Date)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
rs = append(rs, r)
}
return
}
// InsertRatingStat batch insert rating score stat
func (d *Dao) InsertRatingStat(c context.Context, month time.Month, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_ratingScoreSQL, month, values))
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", fmt.Sprintf(_ratingScoreSQL, month, values), err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,135 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoDelRatings(t *testing.T) {
convey.Convey("DelRatings", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, time.June, 1, 0, 0, 0, 0, time.Local)
limit = int(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_rating_06(mid,cdate) VALUES(1,'2018-06-01') ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
rows, err := d.DelRatings(c, date, limit)
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 TestDaoRatingStart(t *testing.T) {
convey.Convey("RatingStart", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, time.June, 1, 0, 0, 0, 0, time.Local)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_rating_06(mid,cdate) VALUES(1,'2018-06-01') ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
start, err := d.RatingStart(c, date)
ctx.Convey("Then err should be nil.start should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(start, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRatingEnd(t *testing.T) {
convey.Convey("RatingEnd", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, time.June, 1, 0, 0, 0, 0, time.Local)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_rating_06(mid,cdate) VALUES(1,'2018-06-01') ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
end, err := d.RatingEnd(c, date)
ctx.Convey("Then err should be nil.end should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(end, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRatingCount(t *testing.T) {
convey.Convey("RatingCount", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, time.June, 1, 0, 0, 0, 0, time.Local)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_rating_06(mid,cdate) VALUES(1,'2018-06-01') ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
count, err := d.RatingCount(c, date)
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 TestDaoGetRatings(t *testing.T) {
convey.Convey("GetRatings", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, time.June, 1, 0, 0, 0, 0, time.Local)
offset = int(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_rating_06(mid,cdate) VALUES(1,'2018-06-01') ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
rs, last, err := d.GetRatings(c, date, offset, limit)
ctx.Convey("Then err should be nil.rs,last should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(last, convey.ShouldNotBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetRatingsFast(t *testing.T) {
convey.Convey("GetRatingsFast", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, time.June, 1, 0, 0, 0, 0, time.Local)
start = int(0)
end = int(10000)
limit = int(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_rating_06(mid,cdate) VALUES(1,'2018-06-01') ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
rs, id, err := d.GetRatingsFast(c, date, start, end, limit)
ctx.Convey("Then err should be nil.rs,id should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(id, convey.ShouldNotBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertRatingStat(t *testing.T) {
convey.Convey("InsertRatingStat", t, func(ctx convey.C) {
var (
c = context.Background()
month time.Month = time.June
values = "(1,2,'2018-06-01',100,100,100,600,600,300)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.InsertRatingStat(c, month, values)
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,76 @@
package dao
import (
"context"
"fmt"
"time"
"go-common/library/log"
)
const (
_inRaingStatisticsSQL = "INSERT INTO up_rating_statistics(ups,section,tips,total_score,creativity_score,influence_score,credit_score,fans,avs,coin,play,tag_id,ctype,cdate) VALUES %s"
_inAscSQL = "INSERT INTO up_rating_trend_%s(mid,tag_id,creativity_score,creativity_diff,influence_score,influence_diff,credit_score,credit_diff,magnetic_score,magnetic_diff,date,ctype,section,tips) VALUES %s ON DUPLICATE KEY UPDATE tag_id=VALUES(tag_id),creativity_score=VALUES(creativity_score),influence_score=VALUES(influence_score),credit_score=VALUES(credit_score)"
_inRatingTopSQL = "INSERT INTO up_rating_top(mid,ctype,tag_id,score,fans,play,cdate) VALUES %s"
_delTrendSQL = "DELETE FROM up_rating_trend_%s LIMIT ?"
_delRatingComSQL = "DELETE FROM %s WHERE cdate='%s' LIMIT ?"
)
// DelTrend del trend limit x
func (d *Dao) DelTrend(c context.Context, table string, limit int) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_delTrendSQL, table), limit)
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", fmt.Sprintf(_delTrendSQL, table), err)
return
}
return res.RowsAffected()
}
// InsertRatingStatis batch insert rating statistics
func (d *Dao) InsertRatingStatis(c context.Context, values string) (rows int64, err error) {
if values == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inRaingStatisticsSQL, values))
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", fmt.Sprintf(_inRaingStatisticsSQL, values), err)
return
}
return res.RowsAffected()
}
// InsertTrend insert asc values
func (d *Dao) InsertTrend(c context.Context, table string, values string) (rows int64, err error) {
if values == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inAscSQL, table, values))
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", fmt.Sprintf(_inAscSQL, table, values), err)
return
}
return res.RowsAffected()
}
// InsertTopRating insert rating top
func (d *Dao) InsertTopRating(c context.Context, values string) (rows int64, err error) {
if values == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inRatingTopSQL, values))
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", fmt.Sprintf(_inRatingTopSQL, values), err)
return
}
return res.RowsAffected()
}
// DelRatingCom del rating common by date
func (d *Dao) DelRatingCom(c context.Context, table string, date time.Time, limit int) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_delRatingComSQL, table, date.Format(_layout)), limit)
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,75 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoDelTrend(t *testing.T) {
convey.Convey("DelTrend", t, func(ctx convey.C) {
var (
c = context.Background()
table = "asc"
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_rating_trend_asc(mid,tag_id) VALUES(1,2) ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
rows, err := d.DelTrend(c, table, limit)
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 TestDaoInsertRatingStatis(t *testing.T) {
convey.Convey("InsertRatingStatis", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(1,1,'10-20',160,60,50,50,100,100,100,200,2,1,'2018-06-01')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "DELETE FROM up_rating_statistics WHERE tag_id=2")
_, err := d.InsertRatingStatis(c, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoInsertTopRating(t *testing.T) {
convey.Convey("InsertTopRating", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(1,2,3,100,100,100,'2018-06-01')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "DELETE FROM up_rating_top WHERE mid=1")
_, err := d.InsertTopRating(c, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoDelRatingCom(t *testing.T) {
convey.Convey("DelRatingCom", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_rating_top"
date = time.Now()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.DelRatingCom(c, table, date, 2000)
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,23 @@
package dao
import (
"context"
"fmt"
)
const (
// insert
_inTaskStatusSQL = "INSERT INTO task_status(type, status, date, message) VALUES %s ON DUPLICATE KEY UPDATE status=VALUES(status), message=VALUES(message)"
)
// InsertTaskStatus insert task status
func (d *Dao) InsertTaskStatus(c context.Context, val string) (rows int64, err error) {
if val == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inTaskStatusSQL, val))
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,23 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoInsertTaskStatus(t *testing.T) {
convey.Convey("InsertTaskStatus", t, func(ctx convey.C) {
var (
c = context.Background()
val = "(1,2,'2018-06-01','test')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.InsertTaskStatus(c, val)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}