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,47 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["guide_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-resource/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["guide.go"],
importpath = "go-common/app/interface/main/app-resource/service/guide",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-resource/conf:go_default_library",
"//app/interface/main/app-resource/model/guide:go_default_library",
"//library/log:go_default_library",
"//library/log/infoc: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,116 @@
package guide
import (
"encoding/json"
"hash/crc32"
"io/ioutil"
"os"
"strconv"
"time"
"go-common/app/interface/main/app-resource/conf"
"go-common/app/interface/main/app-resource/model/guide"
"go-common/library/log"
"go-common/library/log/infoc"
)
var (
_emptyinterest = []*guide.Interest{}
)
// Service interest service.
type Service struct {
c *conf.Config
cache []*guide.Interest
interestPath string
// infoc
logCh chan interface{}
inf2 *infoc.Infoc
}
// New new a interest service.
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
cache: []*guide.Interest{},
interestPath: c.InterestJSONFile,
// infoc
logCh: make(chan interface{}, 1024),
inf2: infoc.New(c.InterestInfoc),
}
s.loadInterestJSON()
return
}
// Interest buvid or time gray
func (s *Service) Interest(mobiApp, buvid string, now time.Time) (res []*guide.Interest) {
res = s.cache
// if buvid != "" && mobiApp == "android" {
// if crc32.ChecksumIEEE([]byte(reverseString(buvid)))%5 < 2 {
// log.Info("interest_buvid_miss")
// res = _emptyinterest
// return
// }
// }
if len(res) == 0 {
log.Info("interest_null")
res = _emptyinterest
return
}
log.Info("interest_hit")
return
}
// Interest2 is
func (s *Service) Interest2(mobiApp, device, deviceid, buvid string, build int, now time.Time) (res *guide.InterestTM) {
res = &guide.InterestTM{}
// switch group := int(crc32.ChecksumIEEE([]byte(buvid)) % 20); group {
// case 9, 18:
// res = &guide.InterestTM{
// Interests: s.cache,
// }
// }
id := crc32.ChecksumIEEE([]byte(reverseString(buvid))) % 100
if id < 5 {
res.FeedType = 1
}
switch mobiApp {
case "iphone_b", "android_b":
res.FeedType = 0
}
//infoc
b, _ := json.Marshal(&s.cache)
s.inf2.Info(mobiApp, device, strconv.Itoa(build), buvid, deviceid, now.Format("2006-01-02 15:04:05"), strconv.Itoa(res.FeedType), string(b))
log.Info("interest_infoc_index(%s,%s,%s,%s,%s,%s,%s)_list(%s)", mobiApp, device, strconv.Itoa(build), buvid, deviceid,
now.Format("2006-01-02 15:04:05"), strconv.Itoa(res.FeedType), string(b))
return
}
func reverseString(s string) string {
runes := []rune(s)
for from, to := 0, len(runes)-1; from < to; from, to = from+1, to-1 {
runes[from], runes[to] = runes[to], runes[from]
}
return string(runes)
}
// loadInterestJSON load interest json
func (s *Service) loadInterestJSON() {
file, err := os.Open(s.interestPath)
if err != nil {
log.Error("os.Open(%s) error(%v)", s.interestPath, err)
return
}
defer file.Close()
bs, err := ioutil.ReadAll(file)
if err != nil {
log.Error("ioutil.ReadAll err %v", err)
return
}
res := []*guide.Interest{}
if err = json.Unmarshal(bs, &res); err != nil {
log.Error("json.Unmarshal() file(%s) error(%v)", s.interestPath, err)
}
s.cache = res
log.Info("loadInterestJSON success")
}

View File

@@ -0,0 +1,50 @@
package guide
import (
"encoding/json"
"flag"
"fmt"
"path/filepath"
"testing"
"time"
"go-common/app/interface/main/app-resource/conf"
. "github.com/smartystreets/goconvey/convey"
)
var (
s *Service
)
func WithService(f func(s *Service)) func() {
return func() {
f(s)
}
}
func init() {
dir, _ := filepath.Abs("../../cmd/app-resource-test.toml")
flag.Set("conf", dir)
conf.Init()
s = New(conf.Conf)
time.Sleep(time.Second)
}
func TestGuide(t *testing.T) {
Convey("get guide data", t, WithService(func(s *Service) {
res := s.Interest("iphone", "", time.Now())
result, _ := json.Marshal(res)
fmt.Printf("test guide (%v) \n", string(result))
So(res, ShouldNotBeEmpty)
}))
}
func Test_Guide2(t *testing.T) {
Convey("Guide2", t, WithService(func(s *Service) {
res := s.Interest2("ssss11", "ssss11", "ssss11", "ssss11", 1, time.Now())
result, _ := json.Marshal(res)
Printf("test guide (%v) \n", string(result))
So(res, ShouldNotBeEmpty)
}))
}