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",
"log_test.go",
"record_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/reply/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"log.go",
"record.go",
],
importpath = "go-common/app/interface/main/reply/dao/search",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/reply/conf:go_default_library",
"//app/interface/main/reply/model/adminlog:go_default_library",
"//app/interface/main/reply/model/reply:go_default_library",
"//library/database/elastic:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/xstr: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,31 @@
package search
import (
"go-common/app/interface/main/reply/conf"
es "go-common/library/database/elastic"
bm "go-common/library/net/http/blademaster"
)
const (
_searchURL = "/x/internal/search/reply"
_searchLogURL = "/api/reply/external/search"
)
// Dao search dao.
type Dao struct {
logURL string
searchURL string
httpCli *bm.Client
es *es.Elastic
}
// New new a dao and return.
func New(c *conf.Config) *Dao {
d := &Dao{
logURL: c.Host.Search + _searchLogURL,
searchURL: c.Host.API + _searchURL,
httpCli: bm.NewClient(c.HTTPClient),
es: es.NewElastic(c.Es),
}
return d
}

View File

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

View File

@@ -0,0 +1,65 @@
package search
import (
"context"
"net/url"
"strconv"
"time"
"go-common/app/interface/main/reply/model/adminlog"
"go-common/library/log"
"go-common/library/xstr"
)
type searchAdminLog struct {
ReplyID int64 `json:"rpid"` // 评论id
AdminID int64 `json:"adminid"` // 操作人
State int32 `json:"state"` // 操作人身份
ReplyMid int64 `json:"replymid"` // 评论人
CTime string `json:"ctime"` // 删除时间
}
// LogPaginate paginating the admin logs for size of 'pageSize', and returning the number of reporting, the number of admin logs delete by administrator
func (dao *Dao) LogPaginate(c context.Context, oid int64, tp int, states []int64, curPage, pageSize int, startTime string, now time.Time) (logs []*adminlog.AdminLog, replyCount, reportCount, pageCount, total int64, err error) {
params := url.Values{}
params.Set("appid", "replylog")
params.Set("oid", strconv.FormatInt(oid, 10))
params.Set("type", strconv.Itoa(tp))
params.Set("delstats", xstr.JoinInts(states))
params.Set("start_time", startTime)
params.Set("pagesize", strconv.Itoa(pageSize))
params.Set("page", strconv.Itoa(curPage))
var res struct {
Code int `json:"code"`
Logs []*searchAdminLog `json:"result"`
ReplyCount int64 `json:"adminDeletedNum"`
ReportCount int64 `json:"reportNum"`
Page int64 `json:"page"`
PageSize int64 `json:"pagesize"`
PageCount int64 `json:"pagecount"`
Total int64 `json:"total"`
}
if err = dao.httpCli.Get(c, dao.logURL, "", params, &res); err != nil {
log.Error("adminlog url(%v),err (%v)", dao.logURL+"?"+params.Encode(), err)
return
}
if res.Logs == nil {
logs = make([]*adminlog.AdminLog, 0)
}
for _, log := range res.Logs {
var (
tmp = &adminlog.AdminLog{}
)
tmp.ReplyID = log.ReplyID
tmp.State = log.State
tmp.AdminID = log.AdminID
tmp.CTime = log.CTime
tmp.ReplyMid = log.ReplyMid
logs = append(logs, tmp)
}
replyCount = res.ReplyCount
reportCount = res.ReportCount
pageCount = res.PageCount
total = res.Total
return
}

View File

@@ -0,0 +1,35 @@
package search
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestSearchLogPaginate(t *testing.T) {
convey.Convey("LogPaginate", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
tp = int(0)
states = []int64{}
curPage = int(0)
pageSize = int(0)
startTime = ""
now = time.Now()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
logs, replyCount, reportCount, pageCount, total, err := d.LogPaginate(c, oid, tp, states, curPage, pageSize, startTime, now)
ctx.Convey("Then err should be nil.logs,replyCount,reportCount,pageCount,total should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(total, convey.ShouldNotBeNil)
ctx.So(pageCount, convey.ShouldNotBeNil)
ctx.So(reportCount, convey.ShouldNotBeNil)
ctx.So(replyCount, convey.ShouldNotBeNil)
ctx.So(logs, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,45 @@
package search
import (
"context"
"time"
"fmt"
model "go-common/app/interface/main/reply/model/reply"
es "go-common/library/database/elastic"
"go-common/library/log"
)
var (
recordStates = []int64{0, 1, 2, 5, 6, 7, 9, 11}
)
type recordResult struct {
Page struct {
Num int32 `json:"num"`
Size int32 `json:"size"`
Total int32 `json:"total"`
} `json:"page"`
Result []*model.Record `json:"result"`
}
// RecordPaginate return a page of records from es.
func (d *Dao) RecordPaginate(c context.Context, types []int64, mid, stime, etime int64, order, sort string, pn, ps int32) (records []*model.Record, total int32, err error) {
r := d.es.NewRequest("reply_record").Index(fmt.Sprintf("%s_%d", "replyrecord", mid%100)).
WhereRange("ctime", time.Unix(stime, 0).Format(model.RecordTimeLayout), time.Unix(etime, 0).Format(model.RecordTimeLayout), es.RangeScopeLcRc).
WhereIn("state", recordStates).
WhereEq("mid", mid).
Order(order, sort).Pn(int(pn)).Ps(int(ps))
if len(types) > 0 {
r = r.WhereIn("type", types)
}
var res recordResult
err = r.Scan(c, &res)
if err != nil {
log.Error("r.Scan(%v) error(%v)", c, err)
return
}
records = res.Result
total = int32(res.Page.Total)
return
}

View File

@@ -0,0 +1,32 @@
package search
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestSearchRecordPaginate(t *testing.T) {
convey.Convey("RecordPaginate", t, func(ctx convey.C) {
var (
c = context.Background()
types = []int64{}
mid = int64(0)
stime = int64(0)
etime = int64(0)
order = ""
sort = ""
pn = int32(0)
ps = int32(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
records, total, err := d.RecordPaginate(c, types, mid, stime, etime, order, sort, pn, ps)
ctx.Convey("Then err should be nil.records,total should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(total, convey.ShouldNotBeNil)
ctx.So(records, convey.ShouldBeNil)
})
})
})
}