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,21 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_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,96 @@
#初始table
CREATE TABLE `up_rank` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`mid` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'up主id',
`type` smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT '排行榜类型',
`value` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '排行榜数值,根据type不同代表的含义不同',
`generate_date` date NOT NULL DEFAULT '0000-00-00' COMMENT '排行榜日',
`ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`mtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后修改时间',
`value2` int(11) NOT NULL DEFAULT '0' COMMENT '分数2',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_type_date` (`generate_date`,`type`),
KEY `ix_mtime` (`mtime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Up蹿升榜'
# fat uat表增加字段
# 增加默认值
alter table up_base_info alter column active_tid set default 0;
alter table up_base_info alter column attr set default 0;
alter table up_base_info alter column mid set default 0;
alter table up_base_info alter column mid set default 0;
alter table up_play_info alter column mid set default 0;
alter table up_play_info alter column business_type set default 0;
alter table up_play_info alter column article_count set default 0;
alter table up_play_info alter column play_count_90day set default 0;
alter table up_play_info alter column play_count_7day set default 0;
alter table up_play_info alter column play_count_30day set default 0;
alter table up_play_info alter column play_count_accumulate set default 0;
alter table up_stats_history alter column type set default 0;
alter table up_stats_history alter column sub_type set default 0;
alter table up_stats_history alter column generate_date set default '0000-00-00';
alter table up_rank alter column mid set default 0;
alter table up_rank alter column type set default 0;
alter table up_rank alter column value set default 0;
alter table up_rank alter column generate_date set default '0000-00-00';
alter table task_info alter column generate_date set default '0000-00-00';
alter table task_info alter column task_type set default 0;
alter table up_base_info add column active_tid smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT '最多稿件分区';
alter table up_base_info add column attr int(11) NOT NULL COMMENT '属性,以位区分';
alter table up_rank add column value2 int(11) NOT NULL DEFAULT 0 COMMENT '分数2';
ALTER TABLE up_base_info MODIFY active_tid smallint(6) unsigned NOT NULL DEFAULT 0 COMMENT '最多稿件分区';
#FAT
alter table up_play_info drop column play_count_avg;
alter table up_play_info drop column play_count_avg_90day;
alter table up_play_info add column `article_count` int(11) NOT NULL DEFAULT '0' COMMENT '总稿件数';
alter table up_play_info add column `play_count_90day` int(11) NOT NULL DEFAULT '0' COMMENT '90天内稿件总播放次数';
alter table up_play_info add column `play_count_30day` int(11) NOT NULL DEFAULT '0' COMMENT '30天内稿件总播放次数';
alter table up_play_info add column `play_count_7day` int(11) NOT NULL DEFAULT '0' COMMENT '7天内稿件总播放次数';
#FAT UAT
DROP INDEX ix_mid ON up_base_info;
alter table up_base_info add unique key uk_mid_type (`mid`,`business_type`);
#每天一条记录 增加分数段表, (uat 1, fat 1, prod 1)
create table score_section_history(
id int(11) unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
generate_date date NOT NULL COMMENT '生成日期',
score_type SMALLINT(6) NOT NULL DEFAULT 0 COMMENT '类型, 1质量分2影响分3信用分',
section_0 int(11) NOT NULL DEFAULT 0 COMMENT '0~100的人数',
section_1 int(11) NOT NULL DEFAULT 0 COMMENT '101~200',
section_2 int(11) NOT NULL DEFAULT 0 COMMENT '201~300',
section_3 int(11) NOT NULL DEFAULT 0 COMMENT '301~400',
section_4 int(11) NOT NULL DEFAULT 0 COMMENT '401~500',
section_5 int(11) NOT NULL DEFAULT 0 COMMENT '501~600',
section_6 int(11) NOT NULL DEFAULT 0 COMMENT '601~700',
section_7 int(11) NOT NULL DEFAULT 0 COMMENT '701~800',
section_8 int(11) NOT NULL DEFAULT 0 COMMENT '801~900',
section_9 int(11) NOT NULL DEFAULT 0 COMMENT '901~1000',
ctime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
mtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后修改时间',
UNIQUE uk_date_type(generate_date, score_type),
KEY ix_mtime (mtime)
) engine=innodb DEFAULT charset=utf8 comment='up分数段人数分布表';
#增加信用分、影响分、质量分字段 (uat 1, fat 1, prod 1)
alter table up_base_info add COLUMN credit_score INT NOT NULL DEFAULT 500 COMMENT '信用分';
alter table up_base_info add COLUMN pr_score INT NOT NULL DEFAULT 0 COMMENT '影响分';
alter table up_base_info add COLUMN quality_score INT NOT NULL DEFAULT 0 COMMENT '质量分';
#修复key错误 (uat 1, fat 1, prod 1)
DROP INDEX uk_type_date ON up_rank;
alter table up_rank add unique key uk_date_type_mid (`generate_date`,`type`, `mid`);
#增加生日、地域等字段 (uat 1, fat 1, prod 1)
alter table up_base_info add COLUMN birthday DATE NOT NULL DEFAULT '0000-00-00' COMMENT '生日';
alter table up_base_info add COLUMN active_province varchar(32) NOT NULL DEFAULT '' COMMENT '省份';
alter table up_base_info add COLUMN active_city varchar(32) NOT NULL DEFAULT '' COMMENT '城市';
#增加task info的unique key(uat 1, fat 1, prod 1)
alter table task_info add unique key uk_date_type (`generate_date`,`task_type`);

View File

@@ -0,0 +1,21 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_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,61 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"scoresection_test.go",
"up_base_info_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/upcredit/conf:go_default_library",
"//app/service/main/upcredit/model/calculator:go_default_library",
"//vendor/github.com/go-sql-driver/mysql:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"scoresection.go",
"up_base_info.go",
],
importmap = "go-common/app/service/main/upcredit/dao/upcrmdao",
importpath = "go-common/app/service/main/upcredit/dao/upcrmdao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/upcredit/conf:go_default_library",
"//app/service/main/upcredit/model/calculator:go_default_library",
"//app/service/main/upcredit/model/upcrmmodel:go_default_library",
"//library/log:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/jinzhu/gorm:go_default_library",
"//vendor/github.com/siddontang/go-mysql/mysql: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,98 @@
package upcrmdao
import (
"github.com/jinzhu/gorm"
"go-common/app/service/main/upcredit/conf"
"context"
"fmt"
"go-common/app/service/main/upcredit/model/upcrmmodel"
"go-common/library/log"
)
//Dao upcrm dao
type Dao struct {
conf *conf.Config
crmdb *gorm.DB
}
//New create
func New(c *conf.Config) *Dao {
var d = &Dao{
conf: c,
}
crmdb, err := gorm.Open("mysql", c.DB.Upcrm.DSN)
if crmdb == nil {
log.Error("connect to db fail, err=%v", err)
return nil
}
d.crmdb = crmdb
crmdb.SingularTable(true)
d.crmdb.LogMode(c.IsTest)
return d
}
//Close close
func (d *Dao) Close() {
if d.crmdb != nil {
d.crmdb.Close()
}
}
//AddLog add log
func (d *Dao) AddLog(arg *upcrmmodel.ArgCreditLogAdd) error {
var creditLog = &upcrmmodel.CreditLog{}
creditLog.CopyFrom(arg)
return d.crmdb.Create(creditLog).Error
}
//AddCreditScore add score
func (d *Dao) AddCreditScore(creditScore *upcrmmodel.UpScoreHistory) error {
return d.crmdb.Create(creditScore).Error
}
//AddOrUpdateCreditScore update score
func (d *Dao) AddOrUpdateCreditScore(creditScore *upcrmmodel.UpScoreHistory) (err error) {
var tablename = creditScore.TableName()
var insertSQL = fmt.Sprintf("insert into %s (mid, score_type, score, generate_date, ctime) values (?,?,?,?,?) "+
"on duplicate key update score=?", tablename)
err = d.crmdb.Exec(
insertSQL,
creditScore.Mid, creditScore.ScoreType, creditScore.Score, creditScore.GenerateDate, creditScore.CTime,
creditScore.Score).Error
if err != nil {
log.Error("add credit score fail, mid=%d, err=%+v", creditScore.Mid, err)
}
return
}
//GetCreditScore get score
func (d *Dao) GetCreditScore(c context.Context, arg *upcrmmodel.GetScoreParam) (results []*upcrmmodel.UpScoreHistory, err error) {
var mod = upcrmmodel.UpScoreHistory{
Mid: arg.Mid,
}
err = d.crmdb.Table(mod.TableName()).Select("score, generate_date").
Where("mid=? AND generate_date>=? AND generate_date<=? AND score_type=?", arg.Mid, arg.FromDate, arg.ToDate, arg.ScoreType).
Find(&results).Error
if err != nil {
log.Error("get score history fail, arg=%+v, err=%+v", arg, err)
}
return
}
//GetCreditLog get log
func (d *Dao) GetCreditLog(c context.Context, arg *upcrmmodel.ArgGetLogHistory) (results []*upcrmmodel.SimpleCreditLogWithContent, err error) {
var mod = upcrmmodel.SimpleCreditLogWithContent{}
mod.Mid = arg.Mid
err = d.crmdb.Table(mod.TableName()).Select("type, op_type, reason, business_type, ctime, content, oid").
//Limit(arg.Limit).
//Offset(arg.Offset).
Where("mid=? AND ctime>=? AND ctime<=?", arg.Mid, arg.FromDate, arg.ToDate).
Order("ctime").
Find(&results).Error
if err != nil {
log.Error("get log history fail, arg=%+v, err=%+v", arg, err)
}
return
}

View File

@@ -0,0 +1,50 @@
package upcrmdao
import (
"flag"
"github.com/go-sql-driver/mysql"
"go-common/app/service/main/upcredit/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.upcredit-service")
flag.Set("conf_token", "e85677843358d6c5dcd7246e6e3fc2de")
flag.Set("tree_id", "33287")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../../cmd/upcredit-service.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func IgnoreErr(err error) error {
if err == nil {
return err
}
var e, _ = err.(*mysql.MySQLError)
if e != nil {
switch e.Number {
case 1062:
return nil
}
}
return err
}

View File

@@ -0,0 +1,30 @@
package upcrmdao
import (
"go-common/app/service/main/upcredit/model/calculator"
"go-common/app/service/main/upcredit/model/upcrmmodel"
xtime "go-common/library/time"
"time"
)
//InsertScoreSection insert score section
func (d *Dao) InsertScoreSection(statis calculator.OverAllStatistic, scoreType int, date time.Time) error {
var history upcrmmodel.ScoreSectionHistory
history.Section0 = statis.GetScore(0)
history.Section1 = statis.GetScore(1)
history.Section2 = statis.GetScore(2)
history.Section3 = statis.GetScore(3)
history.Section4 = statis.GetScore(4)
history.Section5 = statis.GetScore(5)
history.Section6 = statis.GetScore(6)
history.Section7 = statis.GetScore(7)
history.Section8 = statis.GetScore(8)
history.Section9 = statis.GetScore(9)
history.ScoreType = scoreType
history.GenerateDate = xtime.Time(date.Unix())
var now = time.Now().Unix()
history.CTime = xtime.Time(now)
history.MTime = xtime.Time(now)
// insert or update
return d.crmdb.Create(&history).Error
}

View File

@@ -0,0 +1,24 @@
package upcrmdao
import (
"go-common/app/service/main/upcredit/model/calculator"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestUpcrmdaoInsertScoreSection(t *testing.T) {
var (
statis calculator.OverAllStatistic
scoreType = int(0)
date = time.Now()
)
convey.Convey("InsertScoreSection", t, func(ctx convey.C) {
err := d.InsertScoreSection(statis, scoreType, date)
err = IgnoreErr(err)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,99 @@
package upcrmdao
import (
"fmt"
"github.com/siddontang/go-mysql/mysql"
"go-common/app/service/main/upcredit/model/upcrmmodel"
"go-common/library/log"
xtime "go-common/library/time"
"strings"
"time"
)
const (
//TimeFmtMysql mysql time format
TimeFmtMysql = mysql.TimeFormat
//TimeFmtDate with only date
TimeFmtDate = "2006-01-02"
)
//UpQualityInfo struct
type UpQualityInfo struct {
Mid int64 `json:"mid"`
QualityValue int `json:"quality_value"`
PrValue int `json:"pr_value"`
Cdate string `json:"cdate"` // 产生时间 "2018-01-01"
}
// AsPrScore copy to db struct
func (u *UpQualityInfo) AsPrScore() (history *upcrmmodel.UpScoreHistory) {
if u == nil {
return &upcrmmodel.UpScoreHistory{}
}
history = &upcrmmodel.UpScoreHistory{
Mid: u.Mid,
ScoreType: upcrmmodel.ScoreTypePr,
Score: u.PrValue,
}
var date, _ = time.Parse(TimeFmtDate, u.Cdate)
history.GenerateDate = xtime.Time(date.Unix())
return
}
// AsQualityScore copy to db struct
func (u *UpQualityInfo) AsQualityScore() (history *upcrmmodel.UpScoreHistory) {
if u == nil {
return &upcrmmodel.UpScoreHistory{}
}
history = &upcrmmodel.UpScoreHistory{
Mid: u.Mid,
ScoreType: upcrmmodel.ScoreTypeQuality,
Score: u.QualityValue,
}
var date, _ = time.Parse(TimeFmtDate, u.Cdate)
history.GenerateDate = xtime.Time(date.Unix())
return
}
//UpdateCreditScore update score
func (d *Dao) UpdateCreditScore(score int, mid int64) (affectRow int64, err error) {
var db = d.crmdb.Model(upcrmmodel.UpBaseInfo{}).Where("mid = ? and business_type = 1", mid).Update("credit_score", score)
return db.RowsAffected, db.Error
}
//UpdateQualityAndPrScore update score
func (d *Dao) UpdateQualityAndPrScore(prScore int, qualityScore int, mid int64) (affectRow int64, err error) {
var db = d.crmdb.Model(upcrmmodel.UpBaseInfo{}).Where("mid = ? and business_type = 1", mid).Update(map[string]int{"pr_score": prScore, "quality_score": qualityScore})
return db.RowsAffected, db.Error
}
//InsertScoreHistory insert into score history
func (d *Dao) InsertScoreHistory(info *UpQualityInfo) (affectRow int64, err error) {
var qualityScoreSt = info.AsQualityScore()
err = d.crmdb.Save(qualityScoreSt).Error
if err != nil {
log.Error("insert quality score error, err=%+v", err)
}
var prScore = info.AsPrScore()
err = d.crmdb.Save(prScore).Error
if err != nil {
log.Error("insert pr score error, err=%+v", err)
}
return
}
//InsertBatchScoreHistory insert batch sql
func (d *Dao) InsertBatchScoreHistory(infoList []*UpQualityInfo, tablenum int) (affectRow int64, err error) {
var batchSQL = fmt.Sprintf("insert into up_scores_history_%02d (mid, score_type, score, generate_date) values ", tablenum)
var valueString []string
var valueArgs []interface{}
for _, info := range infoList {
valueString = append(valueString, "(?,?,?,?),(?,?,?,?)")
valueArgs = append(valueArgs, info.Mid, upcrmmodel.ScoreTypePr, info.PrValue, info.Cdate)
valueArgs = append(valueArgs, info.Mid, upcrmmodel.ScoreTypeQuality, info.QualityValue, info.Cdate)
}
var db = d.crmdb.Exec(batchSQL+strings.Join(valueString, ","), valueArgs...)
affectRow = db.RowsAffected
err = db.Error
return
}

View File

@@ -0,0 +1,88 @@
package upcrmdao
import (
"testing"
"github.com/smartystreets/goconvey/convey"
"time"
)
func TestUpcrmdaoAsPrScore(t *testing.T) {
convey.Convey("AsPrScore", t, func(ctx convey.C) {
var info *UpQualityInfo
history := info.AsPrScore()
ctx.Convey("Then history should not be nil.", func(ctx convey.C) {
ctx.So(history, convey.ShouldNotBeNil)
})
})
}
func TestUpcrmdaoAsQualityScore(t *testing.T) {
convey.Convey("AsQualityScore", t, func(ctx convey.C) {
var info *UpQualityInfo
history := info.AsQualityScore()
ctx.Convey("Then history should not be nil.", func(ctx convey.C) {
ctx.So(history, convey.ShouldNotBeNil)
})
})
}
func TestUpcrmdaoUpdateCreditScore(t *testing.T) {
var (
score = int(0)
mid = int64(0)
)
convey.Convey("UpdateCreditScore", t, func(ctx convey.C) {
affectRow, err := d.UpdateCreditScore(score, mid)
err = IgnoreErr(err)
ctx.Convey("Then err should be nil.affectRow should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affectRow, convey.ShouldNotBeNil)
})
})
}
func TestUpcrmdaoUpdateQualityAndPrScore(t *testing.T) {
var (
prScore = int(0)
qualityScore = int(0)
mid = int64(0)
)
convey.Convey("UpdateQualityAndPrScore", t, func(ctx convey.C) {
affectRow, err := d.UpdateQualityAndPrScore(prScore, qualityScore, mid)
err = IgnoreErr(err)
ctx.Convey("Then err should be nil.affectRow should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affectRow, convey.ShouldNotBeNil)
})
})
}
func TestUpcrmdaoInsertScoreHistory(t *testing.T) {
var (
info = &UpQualityInfo{}
)
convey.Convey("InsertScoreHistory", t, func(ctx convey.C) {
affectRow, err := d.InsertScoreHistory(info)
err = IgnoreErr(err)
ctx.Convey("Then err should be nil.affectRow should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affectRow, convey.ShouldNotBeNil)
})
})
}
func TestUpcrmdaoInsertBatchScoreHistory(t *testing.T) {
var (
infoList = []*UpQualityInfo{{Mid: 100, Cdate: time.Now().Format(TimeFmtDate)}}
tablenum = int(0)
)
convey.Convey("InsertBatchScoreHistory", t, func(ctx convey.C) {
affectRow, err := d.InsertBatchScoreHistory(infoList, tablenum)
err = IgnoreErr(err)
ctx.Convey("Then err should be nil.affectRow should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affectRow, convey.ShouldNotBeNil)
})
})
}