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,50 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["stra_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//vendor/github.com/smartystreets/goconvey/convey:go_default_library"],
)
go_library(
name = "go_default_library",
srcs = [
"abtest.go",
"group.go",
"stra.go",
"version.go",
],
importpath = "go-common/app/service/openplatform/abtest/model",
tags = ["automanaged"],
deps = [
"//library/log:go_default_library",
"//library/time:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/service/openplatform/abtest/model/jump:all-srcs",
"//app/service/openplatform/abtest/model/validator:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,31 @@
package model
import (
"go-common/library/time"
)
// AB AB测试实验
type AB struct {
ID int `json:"id"`
Name string `json:"name"`
Desc string `json:"desc"`
Stra Stra `json:"stra"`
Seed int `json:"seed"`
Result int `json:"result"`
Status int `json:"status"`
Version int `json:"version"`
Group int `json:"group"`
Author string `json:"author"`
Modifier string `json:"modifier"`
CreateTime time.Time `json:"ctime"`
ModifyTime time.Time `json:"mtime"`
}
// Stat .
type Stat struct {
New map[int]map[int]int `json:"now"`
Old map[int]map[int]int `json:"last"`
}
// Empty .
type Empty struct{}

View File

@@ -0,0 +1,8 @@
package model
//Group info of abtest
type Group struct {
ID int
Name string
Desc string
}

View File

@@ -0,0 +1,37 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["jump_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//vendor/github.com/smartystreets/goconvey/convey:go_default_library"],
)
go_library(
name = "go_default_library",
srcs = ["jump.go"],
importpath = "go-common/app/service/openplatform/abtest/model/jump",
tags = ["automanaged"],
)
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 jump
import (
"bytes"
"crypto/md5"
"encoding/binary"
)
//Hash get result by hash
func Hash(key uint64, numBuckets int) int32 {
var b int64 = -1
var j int64
for j < int64(numBuckets) {
b = j
key = key*2862933555777941757 + 1
j = int64(float64(b+1) * (float64(int64(1)<<31) / float64((key>>33)+1)))
}
return int32(b)
}
//Md5 get result by Md5
func Md5(key string) uint64 {
var x uint64
s := md5.Sum([]byte(key))
b := bytes.NewBuffer(s[:])
binary.Read(b, binary.BigEndian, &x)
return x
}

View File

@@ -0,0 +1,40 @@
package jump
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
type hashStruct struct {
value uint64
exp int32
}
func Test_Hash(t *testing.T) {
Convey("Test_Hash: ", t, func() {
h := hashStruct{
value: uint64(314978625),
exp: int32(18),
}
bucket := 100
v := Hash(h.value, bucket)
So(v, ShouldEqual, h.exp)
})
}
type md5Struct struct {
value string
exp uint64
}
func Test_Md5(t *testing.T) {
Convey("Test_Hash: ", t, func() {
h := md5Struct{
value: "987654321",
exp: uint64(7979946199622949865),
}
v := Md5(h.value)
So(v, ShouldEqual, h.exp)
})
}

View File

@@ -0,0 +1,53 @@
package model
import (
"errors"
"go-common/library/log"
)
//Stra 实验策略
type Stra struct {
//精度
Precision int `json:"precision"`
//依次比例
Ratio []int `json:"ratio"`
}
func (s *Stra) check() (isValid bool) {
sum := 0
for _, r := range s.Ratio {
sum += r
}
isValid = (sum == s.Precision)
return
}
//Check ensure stra valid
func (s *Stra) Check() (isValid bool) {
return s.check()
}
//Version calculate version by score
func (s *Stra) Version(score int) (version int, err error) {
if !s.check() {
err = errors.New("the sum of ratio is not equal to precision")
log.Error("[model.stra|Version] s.check failed")
return
}
if score >= s.Precision || score < 0 {
err = errors.New("score should between 0 and s.Precision")
return
}
for i, r := range s.Ratio {
if score >= r {
score -= r
} else {
version = i
break
}
}
return
}

View File

@@ -0,0 +1,40 @@
package model
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var stra = []Stra{
Stra{Precision: 100, Ratio: []int{10, 90}},
Stra{Precision: 100, Ratio: []int{10, 9}},
}
func TestCheck(t *testing.T) {
Convey("TestCheck: ", t, func() {
var checks = []bool{true, false}
for i, s := range stra {
got := s.Check()
So(got, ShouldEqual, checks[i])
}
})
}
func TestVersion(t *testing.T) {
testCase := map[int]int{9: 0, 20: 1}
s := stra[0]
Convey("TestVersion: ", t, func() {
for j, k := range testCase {
got, _ := s.Version(j)
So(got, ShouldEqual, k)
}
_, err := s.Version(101)
So(err, ShouldNotBeNil)
_, err = stra[1].Version(101)
So(err, ShouldNotBeNil)
})
}

View File

@@ -0,0 +1,30 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"group.go",
"stra.go",
],
importpath = "go-common/app/service/openplatform/abtest/model/validator",
tags = ["automanaged"],
)
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,19 @@
package validator
//AddGroupParams add group params validate
type AddGroupParams struct {
Name string `form:"name" validate:"required"`
Desc string `form:"desc" validate:"required"`
}
//UpdateGroupParams update group params validate
type UpdateGroupParams struct {
ID int `form:"id" validate:"required"`
Name string `form:"name" validate:"required"`
Desc string `form:"desc" validate:"required"`
}
//DeleteGroupParams delete group params validate
type DeleteGroupParams struct {
ID int `form:"id" validate:"required"`
}

View File

@@ -0,0 +1,41 @@
package validator
//VerionParams presents version params
type VerionParams struct {
Group int `form:"group" validate:"min=0,required"`
}
//ListParams presents listAb params
type ListParams struct {
Pn int `form:"pn" validate:"min=1,required"`
Ps int `form:"ps" validate:"min=1,required"`
Mstatus string `form:"mstatus" validate:"required"`
Group int `form:"group"`
}
//AddAbParams presents addAbtest params
type AddAbParams struct {
Data string `form:"data" validate:"required"`
Group int `form:"group"`
}
//UpdateAbParams presents updateAbtest params
type UpdateAbParams struct {
ID int `form:"id" validate:"required"`
Data string `form:"data" validate:"required"`
Group int `form:"group"`
}
//DelAbParams presents deleteAbtest params
type DelAbParams struct {
ID int `form:"id" validate:"required"`
Group int `form:"group"`
}
//UpdateStatusAbParams presents updateStatusAbtest params
type UpdateStatusAbParams struct {
ID int `form:"id" validate:"required"`
Status int `form:"status"`
Modifier string `form:"modifier" validate:"required"`
Group int `form:"group"`
}

View File

@@ -0,0 +1,7 @@
package model
// Version Cookie中保存的版本信息
type Version struct {
VersionID int64 `json:"v"`
Data map[int]int `json:"d"`
}