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,72 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"api_test.go",
"cache_test.go",
"creative_test.go",
"dao_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/mcn/conf:go_default_library",
"//app/interface/main/mcn/dao/global:go_default_library",
"//app/interface/main/mcn/model/datamodel:go_default_library",
"//app/interface/main/mcn/tool/datacenter:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/bouk/monkey:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"api.go",
"cache.go",
"creative.go",
"dao.go",
],
importpath = "go-common/app/interface/main/mcn/dao/datadao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/mcn/conf:go_default_library",
"//app/interface/main/mcn/dao/cache:go_default_library",
"//app/interface/main/mcn/dao/global:go_default_library",
"//app/interface/main/mcn/model/datamodel:go_default_library",
"//app/interface/main/mcn/model/mcnmodel:go_default_library",
"//app/interface/main/mcn/tool/cache:go_default_library",
"//app/interface/main/mcn/tool/datacenter:go_default_library",
"//app/interface/main/tag/api:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//library/sync/errgroup: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,577 @@
package datadao
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"go-common/app/interface/main/mcn/dao/cache"
"go-common/app/interface/main/mcn/dao/global"
"go-common/app/interface/main/mcn/model/datamodel"
"go-common/app/interface/main/mcn/model/mcnmodel"
"go-common/app/interface/main/mcn/tool/datacenter"
tagmdl "go-common/app/interface/main/tag/api"
"go-common/library/log"
"go-common/library/sync/errgroup"
)
// const url for api
const (
APIMcnSummary = "http://berserker.bilibili.co/avenger/api/155/query" // 7 see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690
APIIndexInc = "http://berserker.bilibili.co/avenger/api/156/query" // 3.1
APIIndexSource = "http://berserker.bilibili.co/avenger/api/159/query" // 3.2
APIPlaySource = "http://berserker.bilibili.co/avenger/api/161/query" // 3.3
APIMcnFans = "http://berserker.bilibili.co/avenger/api/168/query" // 3.4
APIMcnFansInc = "http://berserker.bilibili.co/avenger/api/171/query" // 3.5
APIMcnFansDec = "http://berserker.bilibili.co/avenger/api/169/query" // 3.6
APIMcnFansAttentionWay = "http://berserker.bilibili.co/avenger/api/170/query" // 3.7
APIMcnFansSex = "http://berserker.bilibili.co/avenger/api/162/query" // 3.8
APIMcnFansAge = "http://berserker.bilibili.co/avenger/api/163/query" // 3.8
APIMcnFansPlayWay = "http://berserker.bilibili.co/avenger/api/164/query" // 3.8
APIMcnFansArea = "http://berserker.bilibili.co/avenger/api/165/query" // 3.9
APIMcnFansType = "http://berserker.bilibili.co/avenger/api/166/query" // 3.10
APIMcnFansTag = "http://berserker.bilibili.co/avenger/api/167/query" // 3.11
)
func (d *Dao) callDataAPI(c context.Context, api string, query *datacenter.Query, res interface{}) (err error) {
var response = &datacenter.Response{
Result: res,
}
if query.Error() != nil {
err = query.Error()
log.Error("query error, err=%s", err)
return
}
var params = url.Values{}
params.Add("query", query.String())
if err = d.Client.Get(c, api, params, response); err != nil {
log.Error("fail to get response, err=%+v", err)
return
}
if response.Code != http.StatusOK {
err = fmt.Errorf("code:%d, msg:%s", response.Code, response.Msg)
return
}
return
}
// GetMcnSummary 7
// see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-7.mcn获取概要数据
func (d *Dao) GetMcnSummary(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetDataSummaryReply, err error) {
res = new(mcnmodel.McnGetDataSummaryReply)
var tmp []*datamodel.DmConMcnArchiveD
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
).Limit(1, 0).Order("log_date desc")
var api = APIMcnSummary
if err = d.callDataAPI(c, api, q, &tmp); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
if len(tmp) > 0 {
res.CopyFromDmConMcnArchiveD(tmp[0])
}
//log.Info("%s query arg(%d,%+v) res(%+v)", api, signID, date, tmp[0])
return
}
//GetMcnSummaryCache GetMcnSummary with cache
func (d *Dao) GetMcnSummaryCache(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetDataSummaryReply, err error) {
res = new(mcnmodel.McnGetDataSummaryReply)
var cache = NewCacheMcnDataSignID(signID, date, res, "McnGetDataSummaryReply", func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
return d.GetMcnSummary(c, signID, date)
})
if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
log.Error("cache get err, err=%v", err)
return
}
return
}
// GetIndexInc 3.1
// see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.1.查询MCN增量趋势
func (d *Dao) GetIndexInc(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetIndexIncReply, err error) {
res = new(mcnmodel.McnGetIndexIncReply)
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
).Limit(30, 0).Order("log_date desc")
var api = APIIndexInc
if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
//log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, res.Result[0])
return
}
//GetIndexIncCache GetIndexInc with cache
func (d *Dao) GetIndexIncCache(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetIndexIncReply, err error) {
res = new(mcnmodel.McnGetIndexIncReply)
var cache = NewCacheMcnDataWithTp(signID, date, tp, res, "McnGetIndexIncReply", func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
return d.GetIndexInc(c, signID, date, tp)
})
if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
log.Error("cache get err, err=%v", err)
return
}
return
}
// GetIndexSource 3.2
// see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.2.查询MCN下播放稿件来源所在分区
func (d *Dao) GetIndexSource(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetIndexSourceReply, err error) {
res = new(mcnmodel.McnGetIndexSourceReply)
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionIn(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
)
var api = APIIndexSource
if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
var tids []int64
for _, v := range res.Result {
tids = append(tids, v.TypeID)
}
tpNames := cache.GetTidNames(tids)
for _, v := range res.Result {
if tpName, ok := tpNames[v.TypeID]; ok {
v.TypeName = tpName
}
}
//log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, res.Result[0])
return
}
//GetIndexSourceCache GetIndexSource with cache
func (d *Dao) GetIndexSourceCache(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetIndexSourceReply, err error) {
res = new(mcnmodel.McnGetIndexSourceReply)
var cache = NewCacheMcnDataWithTp(signID, date, tp, res, "McnGetIndexSourceReply", func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
return d.GetIndexSource(c, signID, date, tp)
})
if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
log.Error("cache get err, err=%v", err)
return
}
return
}
// GetPlaySource 3.3
// see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.3.查询MCN播放设备占比
func (d *Dao) GetPlaySource(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetPlaySourceReply, err error) {
res = new(mcnmodel.McnGetPlaySourceReply)
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
).Limit(1, 0).Order("log_date desc")
var api = APIPlaySource
var tmp []*mcnmodel.McnGetPlaySourceReply
if err = d.callDataAPI(c, api, q, &tmp); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
if len(tmp) > 0 {
res = tmp[0]
}
//log.Info("%s query arg(%d,%+v) res(%+v)", api, signID, date, tmp)
return
}
//GetPlaySourceCache GetPlaySource with cache
func (d *Dao) GetPlaySourceCache(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetPlaySourceReply, err error) {
res = new(mcnmodel.McnGetPlaySourceReply)
var cache = NewCacheMcnDataSignID(signID, date, res, "McnGetPlaySourceReply", func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
return d.GetPlaySource(c, signID, date)
})
if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
log.Error("cache get err, err=%v", err)
return
}
return
}
// GetMcnFans 3.4
// see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.4.查询MCN粉丝数与活跃度
func (d *Dao) GetMcnFans(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansReply, err error) {
res = new(mcnmodel.McnGetMcnFansReply)
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
).Limit(1, 0).Order("log_date desc")
var tmp []*mcnmodel.McnGetMcnFansReply
var api = APIMcnFans
if err = d.callDataAPI(c, api, q, &tmp); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
if len(tmp) > 0 {
res = tmp[0]
}
//log.Info("%s query arg(%d,%+v) res(%+v)", api, signID, date, tmp[0])
return
}
//GetMcnFansCache GetMcnFans with cache
func (d *Dao) GetMcnFansCache(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansReply, err error) {
res = new(mcnmodel.McnGetMcnFansReply)
var cache = NewCacheMcnDataSignID(signID, date, res, "McnGetMcnFansReply", func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
return d.GetMcnFans(c, signID, date)
})
if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
log.Error("cache get err, err=%v", err)
return
}
return
}
// GetMcnFansInc 3.5
// see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.5.查询MCN粉丝按天增量
func (d *Dao) GetMcnFansInc(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansIncReply, err error) {
res = new(mcnmodel.McnGetMcnFansIncReply)
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
).Limit(30, 0).Order("log_date desc")
var api = APIMcnFansInc
if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
//log.Info("%s query arg(%d,%+v) res(%+v)", api, signID, date, res.Result[0])
return
}
//GetMcnFansIncCache GetMcnFansInc with cache
func (d *Dao) GetMcnFansIncCache(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansIncReply, err error) {
res = new(mcnmodel.McnGetMcnFansIncReply)
var cache = NewCacheMcnDataSignID(signID, date, res, "McnGetMcnFansIncReply", func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
return d.GetMcnFansInc(c, signID, date)
})
if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
log.Error("cache get err, err=%v", err)
return
}
return
}
// GetMcnFansDec 3.6
// see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.6.查询MCN粉丝取关数按天
func (d *Dao) GetMcnFansDec(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansDecReply, err error) {
res = new(mcnmodel.McnGetMcnFansDecReply)
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
).Limit(30, 0).Order("log_date desc")
var api = APIMcnFansDec
if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
//log.Info("%s query arg(%d,%+v) res(%+v)", api, signID, date, res.Result[0])
return
}
//GetMcnFansDecCache GetMcnFansDec with cache
func (d *Dao) GetMcnFansDecCache(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansDecReply, err error) {
res = new(mcnmodel.McnGetMcnFansDecReply)
var cache = NewCacheMcnDataSignID(signID, date, res, "McnGetMcnFansDecReply", func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
return d.GetMcnFansDec(c, signID, date)
})
if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
log.Error("cache get err, err=%v", err)
return
}
return
}
// GetMcnFansAttentionWay 3.7
// see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.7.查询MCN粉丝关注渠道
func (d *Dao) GetMcnFansAttentionWay(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansAttentionWayReply, err error) {
res = new(mcnmodel.McnGetMcnFansAttentionWayReply)
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
).Limit(1, 0).Order("log_date desc")
var tmp []*mcnmodel.McnGetMcnFansAttentionWayReply
var api = APIMcnFansAttentionWay
if err = d.callDataAPI(c, api, q, &tmp); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
if len(tmp) > 0 {
res = tmp[0]
}
//log.Info("%s query arg(%d,%+v) res(%+v)", api, signID, date, tmp[0])
return
}
//GetMcnFansAttentionWayCache GetMcnFansAttentionWay with cache
func (d *Dao) GetMcnFansAttentionWayCache(c context.Context, signID int64, date time.Time) (res *mcnmodel.McnGetMcnFansAttentionWayReply, err error) {
res = new(mcnmodel.McnGetMcnFansAttentionWayReply)
var cache = NewCacheMcnDataSignID(signID, date, res, "McnGetMcnFansAttentionWayReply", func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
return d.GetMcnFansAttentionWay(c, signID, date)
})
if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
log.Error("cache get err, err=%v", err)
return
}
return
}
// GetFansBaseFansAttr 3.8
// see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.8.查询MCN粉丝/游客基本属性分析(性别占比 观众年龄 观看途径)
func (d *Dao) GetFansBaseFansAttr(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetBaseFansAttrReply, err error) {
res = new(mcnmodel.McnGetBaseFansAttrReply)
var group, _ = errgroup.WithContext(c)
group.Go(func() (err error) {
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
).Limit(1, 0).Order("log_date desc")
var api = APIMcnFansSex
var tmp []*datamodel.DmConMcnFansSexW
if err = d.callDataAPI(context.Background(), api, q, &tmp); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
if len(tmp) > 0 {
res.FansSex = tmp[0]
}
//log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, tmp[0])
return
})
group.Go(func() (err error) {
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
).Limit(1, 0).Order("log_date desc")
var tmp []*datamodel.DmConMcnFansAgeW
var api = APIMcnFansAge
if err = d.callDataAPI(context.Background(), api, q, &tmp); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
if len(tmp) > 0 {
res.FansAge = tmp[0]
}
//log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, tmp[0])
return
})
group.Go(func() (err error) {
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionLte(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
).Limit(1, 0).Order("log_date desc")
var tmp []*datamodel.DmConMcnFansPlayWayW
var api = APIMcnFansPlayWay
if err = d.callDataAPI(context.Background(), api, q, &tmp); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
if len(tmp) > 0 {
res.FansPlayWay = tmp[0]
}
//log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, tmp[0])
return
})
err = group.Wait()
if err != nil {
log.Error("fail to get data, err=%v", err)
return
}
return
}
//GetFansBaseFansAttrCache GetFansBaseFansAttr with cache
func (d *Dao) GetFansBaseFansAttrCache(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetBaseFansAttrReply, err error) {
res = new(mcnmodel.McnGetBaseFansAttrReply)
var cache = NewCacheMcnDataWithTp(signID, date, tp, res, "McnGetBaseFansAttrReply", func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
return d.GetFansBaseFansAttr(c, signID, date, tp)
})
if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
log.Error("cache get err, err=%v", err)
return
}
return
}
// GetFansArea 3.9
// see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.9.查询MCN粉丝/游客地区分布分析
func (d *Dao) GetFansArea(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetFansAreaReply, err error) {
res = new(mcnmodel.McnGetFansAreaReply)
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionIn(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
)
var api = APIMcnFansArea
if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
//log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, res.Result[0])
return
}
//GetFansAreaCache GetFansArea with cache
func (d *Dao) GetFansAreaCache(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetFansAreaReply, err error) {
res = new(mcnmodel.McnGetFansAreaReply)
var cache = NewCacheMcnDataWithTp(signID, date, tp, res, "McnGetFansAreaReply", func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
return d.GetFansArea(c, signID, date, tp)
})
if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
log.Error("cache get err, err=%v", err)
return
}
return
}
// GetFansType 3.10
// see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.10.查询MCN粉丝/游客内容倾向分析
func (d *Dao) GetFansType(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetFansTypeReply, err error) {
res = new(mcnmodel.McnGetFansTypeReply)
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionIn(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
)
var api = APIMcnFansType
if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
var tids []int64
for _, v := range res.Result {
tids = append(tids, v.TypeID)
}
tpNames := cache.GetTidNames(tids)
for _, v := range res.Result {
if tpName, ok := tpNames[v.TypeID]; ok {
v.TypeName = tpName
}
}
//log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, res.Result[0])
return
}
//GetFansTypeCache GetFansType with cache
func (d *Dao) GetFansTypeCache(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetFansTypeReply, err error) {
res = new(mcnmodel.McnGetFansTypeReply)
var cache = NewCacheMcnDataWithTp(signID, date, tp, res, "McnGetFansTypeReply", func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
return d.GetFansType(c, signID, date, tp)
})
if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
log.Error("cache get err, err=%v", err)
return
}
return
}
// GetFansTag 3.11
// see doc http://info.bilibili.co/pages/viewpage.action?pageId=11545690#id-对外接口文档-3.11.查询MCN粉丝/游客标签地图分析
func (d *Dao) GetFansTag(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetFansTagReply, err error) {
res = new(mcnmodel.McnGetFansTagReply)
var q = &datacenter.Query{}
q.Select("*").Where(
datacenter.ConditionMapType{"log_date": datacenter.ConditionIn(date)},
datacenter.ConditionMapType{"sign_id": datacenter.ConditionIn(signID)},
datacenter.ConditionMapType{"type": datacenter.ConditionIn(tp)},
)
var api = APIMcnFansTag
if err = d.callDataAPI(c, api, q, &res.Result); err != nil {
log.Error("call data api fail, api=%s, err=%s", api, err)
return
}
var tagIDs []int64
for _, v := range res.Result {
tagIDs = append(tagIDs, v.TagID)
}
var tagsReply *tagmdl.TagsReply
if tagsReply, err = global.GetTagGRPC().Tags(c, &tagmdl.TagsReq{Tids: tagIDs}); err != nil {
log.Error("tag(%+v) grpc client fail, err=%s", tagIDs, err)
err = nil
}
for _, v := range res.Result {
if tagsReply == nil {
continue
}
if tag, ok := tagsReply.Tags[v.TagID]; ok {
v.TagName = tag.Name
}
}
//log.Info("%s query arg(%d,%+v,%s) res(%+v)", api, signID, date, tp, res.Result[0])
return
}
//GetFansTagCache GetFansTag with cache
func (d *Dao) GetFansTagCache(c context.Context, signID int64, date time.Time, tp string) (res *mcnmodel.McnGetFansTagReply, err error) {
res = new(mcnmodel.McnGetFansTagReply)
var cache = NewCacheMcnDataWithTp(signID, date, tp, res, "McnGetFansTagReply", func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
return d.GetFansTag(c, signID, date, tp)
})
if err = d.McWrapper.GetOrLoad(c, cache); err != nil {
log.Error("cache get err, err=%v", err)
return
}
return
}

View File

@ -0,0 +1,448 @@
package datadao
import (
"context"
"go-common/app/interface/main/mcn/tool/datacenter"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
"go-common/app/interface/main/mcn/model/datamodel"
)
func TestDatadaocallDataAPI(t *testing.T) {
convey.Convey("callDataAPI", t, func(ctx convey.C) {
var (
c = context.Background()
api = APIMcnSummary
query = &datacenter.Query{}
res []*datamodel.DmConMcnArchiveD
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.callDataAPI(c, api, query, res)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDatadaoGetMcnSummary(t *testing.T) {
convey.Convey("GetMcnSummary", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetMcnSummary(c, signID, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetMcnSummaryCache(t *testing.T) {
convey.Convey("GetMcnSummaryCache", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetMcnSummaryCache(c, signID, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetIndexInc(t *testing.T) {
convey.Convey("GetIndexInc", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
tp = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetIndexInc(c, signID, date, tp)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetIndexIncCache(t *testing.T) {
convey.Convey("GetIndexIncCache", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
tp = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetIndexIncCache(c, signID, date, tp)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetIndexSource(t *testing.T) {
convey.Convey("GetIndexSource", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
tp = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetIndexSource(c, signID, date, tp)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetIndexSourceCache(t *testing.T) {
convey.Convey("GetIndexSourceCache", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
tp = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetIndexSourceCache(c, signID, date, tp)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetPlaySource(t *testing.T) {
convey.Convey("GetPlaySource", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetPlaySource(c, signID, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetPlaySourceCache(t *testing.T) {
convey.Convey("GetPlaySourceCache", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetPlaySourceCache(c, signID, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetMcnFans(t *testing.T) {
convey.Convey("GetMcnFans", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetMcnFans(c, signID, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetMcnFansCache(t *testing.T) {
convey.Convey("GetMcnFansCache", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetMcnFansCache(c, signID, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetMcnFansInc(t *testing.T) {
convey.Convey("GetMcnFansInc", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetMcnFansInc(c, signID, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetMcnFansIncCache(t *testing.T) {
convey.Convey("GetMcnFansIncCache", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetMcnFansIncCache(c, signID, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetMcnFansDec(t *testing.T) {
convey.Convey("GetMcnFansDec", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetMcnFansDec(c, signID, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetMcnFansDecCache(t *testing.T) {
convey.Convey("GetMcnFansDecCache", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetMcnFansDecCache(c, signID, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetMcnFansAttentionWay(t *testing.T) {
convey.Convey("GetMcnFansAttentionWay", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetMcnFansAttentionWay(c, signID, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetMcnFansAttentionWayCache(t *testing.T) {
convey.Convey("GetMcnFansAttentionWayCache", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetMcnFansAttentionWayCache(c, signID, date)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetFansBaseFansAttr(t *testing.T) {
convey.Convey("GetFansBaseFansAttr", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
tp = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetFansBaseFansAttr(c, signID, date, tp)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetFansBaseFansAttrCache(t *testing.T) {
convey.Convey("GetFansBaseFansAttrCache", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
tp = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetFansBaseFansAttrCache(c, signID, date, tp)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetFansArea(t *testing.T) {
convey.Convey("GetFansArea", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
tp = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetFansArea(c, signID, date, tp)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetFansAreaCache(t *testing.T) {
convey.Convey("GetFansAreaCache", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
tp = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetFansAreaCache(c, signID, date, tp)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetFansType(t *testing.T) {
convey.Convey("GetFansType", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
tp = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetFansType(c, signID, date, tp)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetFansTypeCache(t *testing.T) {
convey.Convey("GetFansTypeCache", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
tp = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetFansTypeCache(c, signID, date, tp)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetFansTag(t *testing.T) {
convey.Convey("GetFansTag", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
tp = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetFansTag(c, signID, date, tp)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoGetFansTagCache(t *testing.T) {
convey.Convey("GetFansTagCache", t, func(ctx convey.C) {
var (
c = context.Background()
signID = int64(0)
date = time.Now()
tp = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.GetFansTagCache(c, signID, date, tp)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}

View File

@ -0,0 +1,143 @@
package datadao
import (
"context"
"encoding/json"
"fmt"
"go-common/app/interface/main/mcn/conf"
"go-common/app/interface/main/mcn/model/mcnmodel"
"go-common/app/interface/main/mcn/tool/cache"
"time"
)
var (
dateFmt = "20060102"
)
//CacheBaseLoader base loader
type CacheBaseLoader struct {
SignID int64
Date time.Time
Val interface{}
desc string
}
func descHelper(d cache.DataLoader) (key string) {
return d.Desc()
}
func newCacheBaseLoader(signID int64, date time.Time, val interface{}, desc string) CacheBaseLoader {
return CacheBaseLoader{SignID: signID, Date: date, Val: val, desc: desc}
}
//Key cache's key
func (s *CacheBaseLoader) Key() (key string) {
return fmt.Sprintf("%s_%d_%s", descHelper(s), s.SignID, s.Date.Format(dateFmt))
}
//Value cache's value
func (s *CacheBaseLoader) Value() (value interface{}) {
return s.Val
}
//LoadValue need load value
func (s *CacheBaseLoader) LoadValue(c context.Context) (value interface{}, err error) {
panic("implement me")
}
//Expire expiration
func (s *CacheBaseLoader) Expire() time.Duration {
return time.Duration(conf.Conf.Memcache.McnDataCacheExpire)
}
//Desc key desc
func (s *CacheBaseLoader) Desc() string {
return s.desc
}
// -----------------------------------------
//LoadFuncWithTp sign with type
type LoadFuncWithTp func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error)
//LoadFuncOnlySign only sign
type LoadFuncOnlySign func(c context.Context, signID int64, date time.Time) (res interface{}, err error)
//NewCacheMcnDataWithTp reply
func NewCacheMcnDataWithTp(signID int64, date time.Time, tp string, val interface{}, desc string, loadFunc LoadFuncWithTp) *cacheMcnDataWithTp {
return &cacheMcnDataWithTp{
CacheBaseLoader: newCacheBaseLoader(signID, date, val, desc),
Tp: tp,
LoadFunc: loadFunc,
}
}
//cacheMcnDataWithTp cache
type cacheMcnDataWithTp struct {
CacheBaseLoader
Tp string
LoadFunc LoadFuncWithTp
}
//Key key
func (s *cacheMcnDataWithTp) Key() (key string) {
return fmt.Sprintf("%s_%d_%s_%s", s.Desc(), s.SignID, s.Date.Format(dateFmt), s.Tp)
}
//LoadValue load
func (s *cacheMcnDataWithTp) LoadValue(c context.Context) (value interface{}, err error) {
value, err = s.LoadFunc(c, s.SignID, s.Date, s.Tp)
if err != nil {
s.Val = nil
return
}
if sorter, ok := value.(mcnmodel.Sorter); ok {
sorter.Sort()
}
// 如果s.Val存在则将结果populate到s.Val上因为外部会直接使用原始传入的s.Val值
if s.Val != nil {
var b, _ = json.Marshal(value)
json.Unmarshal(b, s.Val)
}
return
}
// NewCacheMcnDataSignID 请求只有sign id的情况
func NewCacheMcnDataSignID(signID int64, date time.Time, val interface{}, desc string, loadFunc LoadFuncOnlySign) *cacheMcnDataSignID {
return &cacheMcnDataSignID{
CacheBaseLoader: newCacheBaseLoader(signID, date, val, desc),
LoadFunc: loadFunc,
}
}
type cacheMcnDataSignID struct {
CacheBaseLoader
LoadFunc LoadFuncOnlySign
}
//Key key
func (s *cacheMcnDataSignID) Key() (key string) {
return fmt.Sprintf("%s_%d_%s", s.Desc(), s.SignID, s.Date.Format(dateFmt))
}
//LoadValue load
func (s *cacheMcnDataSignID) LoadValue(c context.Context) (value interface{}, err error) {
value, err = s.LoadFunc(c, s.SignID, s.Date)
if err != nil {
s.Val = nil
return
}
if sorter, ok := value.(mcnmodel.Sorter); ok {
sorter.Sort()
}
// 如果s.Val存在则将结果populate到s.Val上因为外部会直接使用原始传入的s.Val值
if s.Val != nil {
var b, _ = json.Marshal(value)
json.Unmarshal(b, s.Val)
}
return
}

View File

@ -0,0 +1,203 @@
package datadao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDatadaodescHelper(t *testing.T) {
convey.Convey("descHelper", t, func(ctx convey.C) {
var (
d = &CacheBaseLoader{}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
key := descHelper(d)
ctx.Convey("Then key should not be nil.", func(ctx convey.C) {
ctx.So(key, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaonewCacheBaseLoader(t *testing.T) {
convey.Convey("newCacheBaseLoader", t, func(ctx convey.C) {
var (
signID = int64(0)
date = time.Now()
val = interface{}(0)
desc = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := newCacheBaseLoader(signID, date, val, desc)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoKeyCacheBaseLoader(t *testing.T) {
var (
c = CacheBaseLoader{}
)
convey.Convey("Key", t, func(ctx convey.C) {
ctx.Convey("When everything gose positive", func(ctx convey.C) {
key := c.Key()
ctx.Convey("Then key should not be nil.", func(ctx convey.C) {
ctx.So(key, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoValueCacheBaseLoader(t *testing.T) {
var (
c = CacheBaseLoader{Val: 1}
)
convey.Convey("Value", t, func(ctx convey.C) {
ctx.Convey("When everything gose positive", func(ctx convey.C) {
value := c.Value()
ctx.Convey("Then value should not be nil.", func(ctx convey.C) {
ctx.So(value, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoExpireCacheBaseLoader(t *testing.T) {
var (
c = CacheBaseLoader{Val: 1}
)
convey.Convey("Expire", t, func(ctx convey.C) {
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := c.Expire()
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoDesc(t *testing.T) {
var (
c = CacheBaseLoader{Val: 1}
)
convey.Convey("Desc", t, func(ctx convey.C) {
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := c.Desc()
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoNewCacheMcnDataWithTp(t *testing.T) {
convey.Convey("NewCacheMcnDataWithTp", t, func(ctx convey.C) {
var (
signID = int64(0)
date = time.Now()
tp = ""
val = interface{}(0)
desc = ""
loadFunc LoadFuncWithTp
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := NewCacheMcnDataWithTp(signID, date, tp, val, desc, loadFunc)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoKeyCacheMcnDataWithTp(t *testing.T) {
var (
s = cacheMcnDataWithTp{CacheBaseLoader: CacheBaseLoader{Val: 1}}
)
convey.Convey("Key", t, func(ctx convey.C) {
ctx.Convey("When everything gose positive", func(ctx convey.C) {
key := s.Key()
ctx.Convey("Then key should not be nil.", func(ctx convey.C) {
ctx.So(key, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoLoadValuecacheMcnDataWithTp(t *testing.T) {
convey.Convey("LoadValue", t, func(ctx convey.C) {
var (
c = context.Background()
s = cacheMcnDataWithTp{
CacheBaseLoader: CacheBaseLoader{Val: 1},
LoadFunc: func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error) {
return d.GetIndexSource(c, signID, date, tp)
},
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
value, err := s.LoadValue(c)
ctx.Convey("Then err should be nil.value should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(value, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoNewCacheMcnDataSignID(t *testing.T) {
convey.Convey("NewCacheMcnDataSignID", t, func(ctx convey.C) {
var (
signID = int64(0)
date = time.Now()
val = interface{}(0)
desc = ""
loadFunc LoadFuncOnlySign
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := NewCacheMcnDataSignID(signID, date, val, desc, loadFunc)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoKeycacheMcnDataSignID(t *testing.T) {
var (
s = cacheMcnDataSignID{CacheBaseLoader: CacheBaseLoader{Val: 1}}
)
convey.Convey("Key", t, func(ctx convey.C) {
ctx.Convey("When everything gose positive", func(ctx convey.C) {
key := s.Key()
ctx.Convey("Then key should not be nil.", func(ctx convey.C) {
ctx.So(key, convey.ShouldNotBeNil)
})
})
})
}
func TestDatadaoLoadValuecacheMcnDataSignID(t *testing.T) {
convey.Convey("LoadValue", t, func(ctx convey.C) {
var (
c = context.Background()
s = cacheMcnDataSignID{
CacheBaseLoader: CacheBaseLoader{Val: 1},
LoadFunc: func(c context.Context, signID int64, date time.Time) (res interface{}, err error) {
return d.GetMcnFans(c, signID, date)
},
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
value, err := s.LoadValue(c)
ctx.Convey("Then err should be nil.value should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(value, convey.ShouldNotBeNil)
})
})
})
}

View File

@ -0,0 +1,118 @@
package datadao
import (
"context"
"encoding/json"
"fmt"
"net/url"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
// HTTPDataHandle .
func (d *Dao) HTTPDataHandle(c context.Context, params url.Values, key string) (data interface{}, err error) {
var (
uri string
res struct {
Code int `json:"code"`
Data json.RawMessage `json:"data"`
Message string `json:"message"`
}
)
if uri, err = d.getURI(key); err != nil {
return
}
if err = d.bmClient.Get(c, uri, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
return
}
if res.Code != 0 {
log.Error("d.bmClient.Get(%s,%d)", uri+"?"+params.Encode(), res.Code)
err = ecode.Error(ecode.Int(res.Code), res.Message)
return
}
data = res.Data
return
}
// getURI .
func (d *Dao) getURI(key string) (uri string, err error) {
var (
ok bool
url struct {
host string
uri string
}
_url = map[string]struct {
host string
uri string
}{
"archives": {
uri: "/x/internal/creative/archives",
host: d.Conf.Host.API,
},
"archiveHistoryList": {
uri: "/x/internal/creative/archive/history/list",
host: d.Conf.Host.API,
},
"archiveVideos": {
uri: "/x/internal/creative/archive/videos",
host: d.Conf.Host.API,
},
"dataArchive": {
uri: "/x/internal/creative/data/archive",
host: d.Conf.Host.API,
},
"dataVideoQuit": {
uri: "/x/internal/creative/data/videoquit",
host: d.Conf.Host.API,
},
"danmuDistri": {
uri: "/x/internal/creative/danmu/distri",
host: d.Conf.Host.API,
},
"dataBase": {
uri: "/x/internal/creative/data/base",
host: d.Conf.Host.API,
},
"dataTrend": {
uri: "/x/internal/creative/data/trend",
host: d.Conf.Host.API,
},
"dataAction": {
uri: "/x/internal/creative/data/action",
host: d.Conf.Host.API,
},
"dataFan": {
uri: "/x/internal/creative/data/fan",
host: d.Conf.Host.API,
},
"dataPandect": {
uri: "/x/internal/creative/data/pandect",
host: d.Conf.Host.API,
},
"dataSurvey": {
uri: "/x/internal/creative/data/survey",
host: d.Conf.Host.API,
},
"dataPlaySource": {
uri: "/x/internal/creative/data/playsource",
host: d.Conf.Host.API,
},
"dataPlayAnalysis": {
uri: "/x/internal/creative/data/playanalysis",
host: d.Conf.Host.API,
},
"dataArticleRank": {
uri: "/x/internal/creative/data/article/rank",
host: d.Conf.Host.API,
},
}
)
if url, ok = _url[key]; !ok {
return uri, fmt.Errorf("url(%s) not exist", key)
}
uri = url.host + url.uri
return uri, err
}

View File

@ -0,0 +1,56 @@
package datadao
import (
"context"
"encoding/json"
"github.com/bouk/monkey"
"go-common/library/net/http/blademaster"
"net/url"
"reflect"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDatadaoHTTPDataHandle(t *testing.T) {
var (
c = context.Background()
params url.Values
key = "archives"
)
convey.Convey("HTTPDataHandle", t, func(ctx convey.C) {
type result struct {
Code int `json:"code"`
Data json.RawMessage `json:"data"`
Message string `json:"message"`
}
res := new(result)
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.bmClient), "Get", func(_ *blademaster.Client, _ context.Context, _, _ string, _ url.Values, _ interface{}) error {
res.Code = 0
res.Data = json.RawMessage("play")
res.Message = "success"
return nil
})
defer guard.Unpatch()
_, err := d.HTTPDataHandle(c, params, key)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
}
func TestDatadaogetURI(t *testing.T) {
convey.Convey("getURI", t, func(ctx convey.C) {
var (
key = "archives"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
uri, err := d.getURI(key)
ctx.Convey("Then err should be nil.uri should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(uri, convey.ShouldNotBeNil)
})
})
})
}

View File

@ -0,0 +1,30 @@
package datadao
import (
"go-common/app/interface/main/mcn/conf"
"go-common/app/interface/main/mcn/dao/global"
"go-common/app/interface/main/mcn/tool/cache"
"go-common/app/interface/main/mcn/tool/datacenter"
"go-common/library/cache/memcache"
bm "go-common/library/net/http/blademaster"
)
//Dao data dao
type Dao struct {
Client *datacenter.HttpClient
Conf *conf.Config
mc *memcache.Pool
McWrapper *cache.MCWrapper
bmClient *bm.Client
}
//New .
func New(c *conf.Config) *Dao {
return &Dao{
Client: datacenter.New(c.DataClientConf),
Conf: c,
mc: global.GetMc(),
McWrapper: cache.New(global.GetMc()),
bmClient: global.GetBMClient(),
}
}

View File

@ -0,0 +1,60 @@
package datadao
import (
"flag"
"net/http"
"os"
"testing"
"go-common/app/interface/main/mcn/conf"
"go-common/app/interface/main/mcn/dao/global"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.mcn-interface")
flag.Set("conf_token", "49e4671bafbf93059aeb602685052ca0")
flag.Set("tree_id", "58909")
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/mcn-interface.toml")
}
if os.Getenv("UT_LOCAL_TEST") != "" {
flag.Set("conf", "../../cmd/mcn-interface.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
global.Init(conf.Conf)
d = New(conf.Conf)
d.Client.SetTransport(gock.DefaultTransport)
var result = `{
"code": 200,
"msg": "success",
"result": [ ]
}`
gock.New("http://berserker.bilibili.co/avenger/api").Get("/").AddMatcher(
func(request *http.Request, request2 *gock.Request) (bool, error) {
return true, nil
}).Persist().Reply(200).JSON(result)
defer gock.OffAll()
os.Exit(m.Run())
}
// func httpMock(method, url string) *gock.Request {
// r := gock.New(url)
// r.Method = strings.ToUpper(method)
// return r
// }