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,58 @@
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",
"search_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/model/search:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"reply.go",
"search.go",
],
importpath = "go-common/app/interface/main/creative/dao/search",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/dao/tool:go_default_library",
"//app/interface/main/creative/model/resource:go_default_library",
"//app/interface/main/creative/model/search:go_default_library",
"//library/database/elastic:go_default_library",
"//library/ecode: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,40 @@
package search
import (
"context"
"go-common/app/interface/main/creative/conf"
"go-common/library/database/elastic"
bm "go-common/library/net/http/blademaster"
)
// Dao is search dao.
type Dao struct {
c *conf.Config
// http client
client *bm.Client
// searchURI string
// memberURI string
es *elastic.Elastic
}
// New new a search dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
// client
client: bm.NewClient(c.HTTPClient.Slow),
// uri
// searchURI: c.Host.Search + _spaceURL,
// memberURI: c.Host.Search + _memberURL,
es: elastic.NewElastic(&elastic.Config{
Host: c.Host.MainSearch,
HTTPClient: c.HTTPClient.Slow,
}),
}
return d
}
// Ping ping success.
func (d *Dao) Ping(c context.Context) (err error) {
return
}

View File

@ -0,0 +1,35 @@
package search
import (
"flag"
"go-common/app/interface/main/creative/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.creative")
flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
flag.Set("tree_id", "2305")
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/creative.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@ -0,0 +1,150 @@
package search
import (
"context"
"fmt"
"go-common/app/interface/main/creative/dao/tool"
resMdl "go-common/app/interface/main/creative/model/resource"
"go-common/app/interface/main/creative/model/search"
"go-common/library/database/elastic"
"go-common/library/ecode"
"go-common/library/log"
"time"
)
var (
// ReplyOrderMap map
ReplyOrderMap = map[string]string{
"ctime": "ctime",
"like": "like",
"count": "count",
}
daysMap = map[string]int{
filterCtimeOneDayAgo: -1,
filterCtimeOneWeekAgo: -7,
filterCtimeOneMonthAgo: -30,
filterCtimeOneYearAgo: -365,
}
filterCtimeOneDayAgo = "0"
filterCtimeOneWeekAgo = "1"
filterCtimeOneMonthAgo = "2"
filterCtimeOneYearAgo = "3"
)
// ReplyES fn
// order: ctime/like/count
// filter: 1/2/3
func (d *Dao) ReplyES(c context.Context, p *search.ReplyParam) (sres *search.Replies, err error) {
r := d.es.NewRequest("creative_reply").Fields(
"count",
"ctime",
"floor",
"hate",
"id",
"like",
"message",
"mid",
"mtime",
"o_mid",
"oid",
"parent",
"rcount",
"root",
"state",
"type",
)
idx := fmt.Sprintf("%s_%02d", "creative_reply", p.OMID%100)
r.Index(idx).Pn(p.Pn).Ps(p.Ps).OrderScoreFirst(true)
if p.FilterCtime != "" {
if dayNum, ok := daysMap[p.FilterCtime]; ok {
begin := time.Now().AddDate(0, 0, dayNum).Format("2006-01-02 15:04:05")
r.WhereRange("ctime", begin, "", elastic.RangeScopeLcRc)
}
}
if p.IsReport > 0 {
r.WhereEq("isreport", p.IsReport)
}
// 如果指定了oid就不需要传递o_mid
if p.OID > 0 {
r.WhereEq("oid", p.OID)
} else {
r.WhereEq("o_mid", p.OMID)
}
if p.Kw != "" {
r.WhereLike([]string{"message"}, []string{p.Kw}, true, elastic.LikeLevelLow)
}
if p.Type > 0 {
r.WhereEq("type", p.Type)
}
if p.ResMdlPlat == resMdl.PlatIPad {
r.WhereIn("type", []int8{search.Article})
r.WhereNot(elastic.NotTypeIn, "type")
}
r.WhereIn("state", []int{0, 1, 2, 5, 6})
if p.Order != "" {
if o, ok := ReplyOrderMap[p.Order]; ok {
r.Order(o, "desc")
} else {
r.Order("ctime", "desc") //默认按发布时间倒序排
}
} else {
r.Order("ctime", "desc") //默认按发布时间倒序排
}
log.Info("ReplyES params(%+v)|p(%+v)", r.Params(), p)
var res = &search.ReliesES{}
if err = r.Scan(c, res); err != nil {
log.Error("ReplyES r.Scan p(%+v)|error(%v)", p, err)
err = ecode.CreativeSearchErr
return
}
sres = &search.Replies{
Order: res.Order,
Keyword: p.Kw,
Repliers: []int64{},
DeriveOids: []int64{},
DeriveIds: []int64{},
Oids: []int64{},
TyOids: make(map[int][]int64),
Result: make([]*search.Reply, 0),
}
for _, v := range res.Result {
sres.Result = append(sres.Result, &search.Reply{
Message: v.Message,
ID: v.ID,
Floor: v.Floor,
Count: v.Count,
Root: v.Root,
Oid: v.Oid,
CTime: v.CTime,
MTime: v.MTime,
State: v.State,
Parent: v.Parent,
Mid: v.Mid,
Like: v.Like,
Type: v.Type,
})
}
if res.Page != nil {
sres.Total = res.Page.Total
sres.PageCount = res.Page.Num
}
replyMids := make(map[int64]int64, len(res.Result))
for _, v := range res.Result {
_, ok := replyMids[v.Mid]
if !ok {
sres.Repliers = append(sres.Repliers, v.Mid)
replyMids[v.Mid] = v.Mid
}
sres.Oids = append(sres.Oids, v.Oid)
sres.TyOids[v.Type] = sres.Oids
if v.Root > 0 {
sres.DeriveOids = append(sres.DeriveOids, v.Oid)
sres.DeriveIds = append(sres.DeriveIds, v.Parent)
}
}
sres.Oids = tool.DeDuplicationSlice(sres.Oids)
for k, v := range sres.TyOids {
sres.TyOids[k] = tool.DeDuplicationSlice(v)
}
return
}

View File

@ -0,0 +1,192 @@
package search
import (
"context"
"strconv"
"strings"
"go-common/app/interface/main/creative/model/search"
"go-common/library/database/elastic"
"go-common/library/ecode"
"go-common/library/log"
)
var (
orderMap = map[string]string{
"senddate": "pubdate", //发布时间
"click": "click", //点击数
"scores": "review", //评论
"stow": "favorite", //收藏
"dm_count": "dm_count", //弹幕
}
applyStateMap = map[string]string{
"pending": "pending",
"processed": "processed",
"neglected": "neglected",
}
)
// ArchivesES search archives by es.
func (d *Dao) ArchivesES(c context.Context, mid int64, tid int16, keyword, order, class, ip string, pn, ps, coop int) (sres *search.Result, err error) {
r := d.es.NewRequest("creative_archive_staff").Fields(
"id",
"pid",
"typeid",
"title",
"state",
"cover",
"description",
"duration",
"pubdate",
)
r.Index("creative_archive").Pn(pn).Ps(ps).OrderScoreFirst(false)
if mid > 0 && coop == 0 {
cmbup := &elastic.Combo{}
cmbup.ComboEQ([]map[string]interface{}{
{"mid": mid},
})
r.WhereCombo(cmbup.MinEQ(1))
} else if mid > 0 && coop == 1 {
cmbup := &elastic.Combo{}
cmbup.ComboEQ([]map[string]interface{}{
{"mid": mid},
{"staff_mid": mid},
})
r.WhereCombo(cmbup.MinEQ(1))
}
if keyword != "" { //筛选稿件标题或者描述
r.WhereLike([]string{"title", "description"}, []string{keyword}, true, "low")
}
if tid > 0 {
r.WhereEq("pid", tid)
}
if class != "" {
if len(strings.Split(class, ",")) == 1 { //如果筛选全部则不传参数
r.WhereEq("state", class) //state: is_pubing,pubed,not_pubed全部 pubed (已通过) not_pubed(未通过) is_pubing进行中
}
}
if order != "" {
if o, ok := orderMap[order]; ok {
r.Order(o, "desc")
}
} else {
r.Order("pubdate", "desc") //默认按发布时间倒序排
}
log.Info("ArchivesES params(%s)", r.Params())
var res = &search.ArcResult{}
if err = r.Scan(c, res); err != nil {
log.Error("ArchivesES r.Scan error(%v)", err)
err = ecode.CreativeSearchErr
return
}
sres = &search.Result{}
sres.Page.Pn = res.Page.Num
sres.Page.Ps = res.Page.Size
sres.Page.Count = res.Page.Total
if res.Result.PList != nil {
sres.Class = &search.ClassCount{ //获取按稿件状态计数
Pubed: res.Result.PList.Pubed.Count,
NotPubed: res.Result.PList.NotPubed.Count,
Pubing: res.Result.PList.IsPubing.Count,
}
}
tcs := make(map[int16]*search.TypeCount)
for _, v := range res.Result.TList { //获取按一级分区稿件计数
if v != nil {
key, err := strconv.ParseInt(v.Key, 10, 16)
if err != nil {
log.Error("strconv.ParseInt(%s)|error(%v)", v.Key, err)
return nil, err
}
tid = int16(key)
tc := &search.TypeCount{
Tid: tid,
Count: int64(v.Count),
}
tcs[tid] = tc
}
}
sres.Type = tcs
for _, v := range res.Result.Vlist {
if v != nil {
sres.Aids = append(sres.Aids, v.ID)
}
}
return
}
// ArchivesStaffES search staff applies by es.
func (d *Dao) ArchivesStaffES(c context.Context, mid int64, tid int16, keyword, state string, pn, ps int) (sres *search.StaffApplyResult, err error) {
r := d.es.NewRequest("creative_archive_apply").Fields(
"id",
"pid",
"typeid",
"title",
"state",
"cover",
"description",
"duration",
"pubdate",
)
r.Index("creative_archive").Pn(pn).Ps(ps).OrderScoreFirst(false)
if mid > 0 {
r.WhereEq("apply_staff.apply_staff_mid", mid)
}
if state != "" {
if o, ok := applyStateMap[state]; ok {
r.WhereEq("apply_staff.deal_state", o)
}
} else {
r.WhereEq("apply_staff.deal_state", "pending")
}
if keyword != "" { //筛选稿件标题或者描述
r.WhereLike([]string{"title", "description"}, []string{keyword}, true, "low")
}
if tid > 0 {
r.WhereEq("pid", tid)
}
log.Info("ArchivesStaffES params(%s)", r.Params())
var res = &search.ApplyResult{}
if err = r.Scan(c, res); err != nil {
log.Error("ArchivesStaffES r.Scan error(%v)", err)
err = ecode.CreativeSearchErr
return
}
sres = &search.StaffApplyResult{}
sres.Page.Pn = res.Page.Num
sres.Page.Ps = res.Page.Size
sres.Page.Count = res.Page.Total
//tlist
if res.Result.ApplyPList != nil {
sres.StateCount = &search.ApplyStateCount{ //获取按稿件状态计数
Pending: res.Result.ApplyPList.Pending.Count,
Processed: res.Result.ApplyPList.Processed.Count,
Neglected: res.Result.ApplyPList.Neglected.Count,
}
}
// vlist
tcs := make(map[int16]*search.TypeCount)
for _, v := range res.Result.TList { //获取按一级分区稿件计数
if v != nil {
key, err := strconv.ParseInt(v.Key, 10, 16)
if err != nil {
log.Error("strconv.ParseInt(%s)|error(%v)", v.Key, err)
return nil, err
}
tid = int16(key)
tc := &search.TypeCount{
Tid: tid,
Count: int64(v.Count),
}
tcs[tid] = tc
}
}
sres.Type = tcs
for _, v := range res.Result.Vlist {
if v != nil {
sres.Aids = append(sres.Aids, v.ID)
}
}
return
}

View File

@ -0,0 +1,74 @@
package search
import (
"context"
"testing"
"go-common/app/interface/main/creative/model/search"
"github.com/smartystreets/goconvey/convey"
)
func TestSearchArchivesES(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
tid = int16(160)
keyword = ""
order = "click"
class = ""
ip = "127.0.0.1"
pn = int(1)
ps = int(10)
)
convey.Convey("ArchivesES", t, func(ctx convey.C) {
sres, err := d.ArchivesES(c, mid, tid, keyword, order, class, ip, pn, ps, 1)
ctx.Convey("Then err should be nil.sres should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(sres, convey.ShouldNotBeNil)
})
})
}
func TestSearchReplyES(t *testing.T) {
var (
c = context.TODO()
)
p := &search.ReplyParam{
Ak: "1",
Ck: "1",
OMID: 1,
OID: 1,
Pn: 1,
Ps: 1,
IP: "",
IsReport: 1,
Type: 1,
FilterCtime: "",
Kw: "",
Order: "",
}
convey.Convey("ArchivesES", t, func(ctx convey.C) {
_, err := d.ReplyES(c, p)
ctx.Convey("Then err should be nil.sres should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestSearchStaffES(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
tid = int16(160)
keyword = ""
pn = int(1)
ps = int(10)
)
convey.Convey("ArchivesES", t, func(ctx convey.C) {
_, err := d.ArchivesStaffES(c, mid, tid, keyword, "0", pn, ps)
ctx.Convey("Then err should be nil.sres should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}