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,60 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"archive_test.go",
"bangumi_test.go",
"click_test.go",
"dao_test.go",
"forbid_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/click/conf:go_default_library",
"//app/job/main/click/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"archive.go",
"bangumi.go",
"click.go",
"dao.go",
"forbid.go",
],
importpath = "go-common/app/job/main/click/dao",
tags = ["automanaged"],
deps = [
"//app/job/main/click/conf:go_default_library",
"//app/job/main/click/model:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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,30 @@
package dao
import (
"context"
"go-common/library/log"
)
const (
_maxAIDPath = "http://api.bilibili.co/x/internal/v2/archive/maxAid"
)
// MaxAID return max aid
func (d *Dao) MaxAID(c context.Context) (id int64, err error) {
var res struct {
Code int `json:"code"`
Data int64 `json:"data"`
}
if err = d.client.Get(c, _maxAIDPath, "", nil, &res); err != nil {
log.Error("d.client.MaxAid error(%+v)", err)
return
}
if res.Code != 0 {
log.Error("d.client.MaxAid Code(%d)", res.Code)
return
}
log.Info("got MaxAid(%d)", res.Data)
id = res.Data
return
}

View File

@@ -0,0 +1,14 @@
package dao
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_MaxAID(t *testing.T) {
Convey("MaxAID", t, func() {
d.MaxAID(context.TODO())
})
}

View File

@@ -0,0 +1,80 @@
package dao
import (
"context"
"net/url"
"strconv"
"go-common/library/log"
)
const (
_aid2epid = "http://bangumi.bilibili.co/ext/internal/archive/aid2epid"
_epidExist = "http://bangumi.bilibili.co/ext/internal/archive/aid/play"
_isLegal = int(1)
)
var seasonType = []int{1, 2, 3, 4, 5}
// LoadAllBangumi load all bangumi epid -> aid to map
func (d *Dao) LoadAllBangumi(c context.Context) (etam map[int64]int64, err error) {
etam = make(map[int64]int64)
for _, t := range seasonType {
pageNum := 1
for {
var resp struct {
Code int `json:"code"`
Message string `json:"message"`
Result map[int64]int64 `json:"result"`
}
p := url.Values{}
p.Set("build", "0")
p.Set("platform", "golang")
p.Set("season_type", strconv.Itoa(t))
p.Set("page_size", "1000")
p.Set("page_no", strconv.Itoa(pageNum))
// one time error,all return,wait for next update
if err = d.client.Get(c, _aid2epid, "", p, &resp); err != nil {
log.Error("d.client.Get(%s) error(%v)", _aid2epid+"?"+p.Encode(), err)
return
}
// record the page number when result is empty
if len(resp.Result) == 0 {
log.Info("bangumi seasonType(%d) pageNo(%d) is end", t, pageNum)
break
}
for epid, aid := range resp.Result {
etam[epid] = aid
}
pageNum++
}
}
return
}
// IsLegal check legal by aid epID seasonType
func (d *Dao) IsLegal(c context.Context, aid, epID int64, seasonType int) (islegal bool, err error) {
var resp struct {
Code int `json:"code"`
Message string `json:"message"`
Result struct {
Status int `json:"status"`
} `json:"result"`
}
p := url.Values{}
p.Set("build", "0")
p.Set("platform", "golang")
p.Set("season_type", strconv.Itoa(seasonType))
p.Set("epid", strconv.FormatInt(epID, 10))
p.Set("aid", strconv.FormatInt(aid, 10))
if err = d.client.Get(c, _epidExist, "", p, &resp); err != nil {
log.Error("d.client.Get(%s) error(%v)", _epidExist+"?"+p.Encode(), err)
return
}
if resp.Result.Status != _isLegal {
log.Error("aid(%d) epid(%d) seasonType(%d) is unlegal", aid, epID, seasonType)
return
}
islegal = true
return
}

View File

@@ -0,0 +1,27 @@
package dao
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_LoadAllBangumi(t *testing.T) {
Convey("LoadAllBangumi", t, func() {
etam, err := d.LoadAllBangumi(context.TODO())
So(err, ShouldBeNil)
So(etam, ShouldNotBeNil)
for epid, aid := range etam {
Printf("epid:%d,aid:%d\n", epid, aid)
}
})
}
func Test_IsLegal(t *testing.T) {
Convey("IsLegal", t, func() {
isLegal, err := d.IsLegal(context.TODO(), 11696747, 157927, 1)
So(err, ShouldBeNil)
Println(isLegal)
})
}

View File

@@ -0,0 +1,66 @@
package dao
import (
"context"
"database/sql"
"fmt"
"go-common/app/job/main/click/model"
"go-common/library/log"
)
const (
_cliSQL = "SELECT aid,web,h5,outside,ios,android,android_tv FROM %s WHERE aid=?"
_addCliSQL = "INSERT INTO %s(aid,web,h5,outside,ios,android,android_tv) VALUES(?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE web=?,h5=?,outside=?,ios=?,android=?,android_tv=?"
_upCliSQL = "UPDATE %s SET web=web+?,h5=h5+?,outside=outside+?,ios=ios+?,android=android+?,android_tv=android_tv+? WHERE aid=?"
_upSpecialCliSQL = "UPDATE %s SET %s=? WHERE aid=?"
)
func getTable(aid int64) string {
return fmt.Sprintf("archive_click_%02d", aid%100)
}
// Click get click
func (d *Dao) Click(c context.Context, aid int64) (cli *model.ClickInfo, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_cliSQL, getTable(aid)), aid)
cli = &model.ClickInfo{}
if err = row.Scan(&cli.Aid, &cli.Web, &cli.H5, &cli.Outer, &cli.Ios, &cli.Android, &cli.AndroidTV); err != nil {
if err == sql.ErrNoRows {
err = nil
cli = nil
} else {
log.Error("row.Scan error(%v)")
}
}
return
}
// AddClick add av clicks
func (d *Dao) AddClick(c context.Context, aid, web, h5, out, ios, android, androidTV int64) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_addCliSQL, getTable(aid)), aid, web, h5, out, ios, android, androidTV, web, h5, out, ios, android, androidTV)
if err != nil {
log.Error("d.addCliStmt.Exec(%d) error(%v)", aid, err)
return
}
return res.RowsAffected()
}
// UpClick update av clicks
func (d *Dao) UpClick(c context.Context, cli *model.ClickInfo) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_upCliSQL, getTable(cli.Aid)), cli.Web, cli.H5, cli.Outer, cli.Ios, cli.Android, cli.AndroidTV, cli.Aid)
if err != nil {
log.Error("d.upCliStmt.Exec(+%v) error(%v)", cli, err)
return
}
return res.RowsAffected()
}
// UpSpecial update special platform click
func (d *Dao) UpSpecial(c context.Context, aid int64, tp string, num int64) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_upSpecialCliSQL, getTable(aid), tp), num, aid)
if err != nil {
log.Error("d.db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,38 @@
package dao
import (
"context"
"testing"
"go-common/app/job/main/click/model"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Click(t *testing.T) {
Convey("Click", t, func() {
_, err := d.Click(context.TODO(), 1)
So(err, ShouldBeNil)
})
}
func Test_AddClick(t *testing.T) {
Convey("AddClick", t, func() {
_, err := d.AddClick(context.TODO(), 3, 1, 1, 1, 1, 1, 100)
So(err, ShouldBeNil)
})
}
func Test_UpClick(t *testing.T) {
Convey("UpClick", t, func() {
rows, err := d.UpClick(context.TODO(), &model.ClickInfo{Aid: 2, AndroidTV: 22222})
Println(rows, err)
})
}
func Test_UpSpecial(t *testing.T) {
Convey("UpSpecial", t, func() {
_, err := d.UpSpecial(context.TODO(), 1, model.TypeForAndroid, 1)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,24 @@
package dao
import (
"go-common/app/job/main/click/conf"
"go-common/library/database/sql"
bm "go-common/library/net/http/blademaster"
)
// Dao is
type Dao struct {
c *conf.Config
db *sql.DB
client *bm.Client
}
// New is
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.DB),
client: bm.NewClient(c.HTTPClient),
}
return d
}

View File

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

View File

@@ -0,0 +1,76 @@
package dao
import (
"context"
"go-common/app/job/main/click/model"
"go-common/library/log"
)
const (
_forbidSQL = "SELECT aid,plat,lv,locked FROM archive_click_forbid WHERE locked = ?"
_upForbidSQL = "INSERT INTO archive_click_forbid(aid,plat,lv,locked) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE lv=?,locked=?"
_allForbidMidsSQL = "SELECT mid FROM archive_mid_forbid WHERE status=1"
_upForbidMidSQL = "INSERT INTO archive_mid_forbid(mid,status) VALUE(?,?) ON DUPLICATE KEY UPDATE status=?"
)
// ForbidMids is
func (d *Dao) ForbidMids(c context.Context) (mids map[int64]struct{}, err error) {
rows, err := d.db.Query(c, _allForbidMidsSQL)
if err != nil {
log.Error("d.db.Query(%s) error(%v)", _allForbidMidsSQL, err)
return
}
defer rows.Close()
mids = make(map[int64]struct{})
for rows.Next() {
var mid int64
if err = rows.Scan(&mid); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
mids[mid] = struct{}{}
}
err = rows.Err()
return
}
// UpMidForbidStatus is
func (d *Dao) UpMidForbidStatus(c context.Context, mid int64, status int8) (err error) {
_, err = d.db.Exec(c, _upForbidMidSQL, mid, status, status)
return
}
// Forbids is
func (d *Dao) Forbids(c context.Context) (forbids map[int64]map[int8]*model.Forbid, err error) {
rows, err := d.db.Query(c, _forbidSQL, model.ValueForLocked)
if err != nil {
log.Error("d.db.Query(%s) error(%v)", _forbidSQL, model.ValueForLocked, err)
return
}
defer rows.Close()
forbids = make(map[int64]map[int8]*model.Forbid)
for rows.Next() {
var f = &model.Forbid{}
if err = rows.Scan(&f.AID, &f.Plat, &f.Lv, &f.Locked); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
if _, ok := forbids[f.AID]; !ok {
forbids[f.AID] = make(map[int8]*model.Forbid)
}
forbids[f.AID][f.Plat] = f
}
err = rows.Err()
return
}
// UpForbid is
func (d *Dao) UpForbid(c context.Context, aid int64, plat, lock, lv int8) (rows int64, err error) {
res, err := d.db.Exec(c, _upForbidSQL, aid, plat, lv, lock, lv, lock)
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", _upForbidSQL, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,38 @@
package dao
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Forbids(t *testing.T) {
Convey("Forbids", t, func() {
fs, err := d.Forbids(context.TODO())
So(err, ShouldBeNil)
Println(fs)
})
}
func Test_UpForbid(t *testing.T) {
Convey("UpForbid", t, func() {
_, err := d.UpForbid(context.TODO(), 1, 1, 1, 1)
So(err, ShouldBeNil)
})
}
func Test_ForbidMids(t *testing.T) {
Convey("ForbidMids", t, func() {
mids, err := d.ForbidMids(context.TODO())
So(err, ShouldBeNil)
Println(mids)
})
}
func Test_UpForbidMid(t *testing.T) {
Convey("UpForbidMid", t, func() {
err := d.UpMidForbidStatus(context.TODO(), 1684013, 0)
So(err, ShouldBeNil)
})
}