Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .gitmodules
Empty file.
4 changes: 4 additions & 0 deletions ab-go-sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ab-go-sdk

use sample see:
https://github.com/ParticleMedia/ab-go-sdk-sample
17 changes: 17 additions & 0 deletions ab-go-sdk/ab.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ab

type ABService interface {
AB(*ABContext, *ABResult)
}

var ab *ABServices

func Init(cfg *ABConfig) {
if ab == nil {
ab = NewABServices(cfg)
}
}

func AB(ctx *ABContext) *ABResult {
return ab.AB(ctx)
}
8 changes: 8 additions & 0 deletions ab-go-sdk/ab_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ab

type ABConfig struct {
App string
Url string
Layers []string
EnableCohort bool
}
35 changes: 35 additions & 0 deletions ab-go-sdk/ab_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ab

import "fmt"

type ABContext struct {
Factor string
ConditionCtx map[string]interface{}
Userid uint32
}

func NewABContext(factor string) *ABContext {
return &ABContext{Factor: factor, ConditionCtx: map[string]interface{}{}}
}

func (this *ABContext) WithUserid(userid uint32) *ABContext {
this.Userid = userid
this.ConditionCtx["uid"] = userid
return this
}

func (this *ABContext) WithConditions(conditions map[string]interface{}) *ABContext {
for k, v := range conditions {
this.WithCondition(k, v)
}
return this
}

func (this *ABContext) WithCondition(key string, value interface{}) *ABContext {
if key == "uid" {
fmt.Println("Cannot Set userid use WithCondition(s), Please use WithUserid")
return this
}
this.ConditionCtx[key] = value
return this
}
42 changes: 42 additions & 0 deletions ab-go-sdk/ab_result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ab

type ABResult struct {
Exp map[string]string
Config map[string]string
layers map[string]bool
}

func NewABResult() *ABResult {
return &ABResult{
Exp: map[string]string{},
Config: map[string]string{},
layers: map[string]bool{},
}
}

func (this *ABResult) MergeVersion(version *Version) {
if version == nil {
return
}
if version.Exp == nil || this.layers[version.Exp.Layer.Name] {
return
}
this.layers[version.Exp.Layer.Name] = true
this.Exp[version.Exp.Name] = version.Name
for k, v := range version.Config {
_, exists := this.Config[k]
if !exists {
this.Config[k] = v
}
}
}

func (this *ABResult) MergeVersions(versions []*Version) {
for _, version := range versions {
this.MergeVersion(version)
}
}

func (this *ABResult) ContainsLayer(layerName string) bool {
return this.layers[layerName]
}
109 changes: 109 additions & 0 deletions ab-go-sdk/ab_services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package ab

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strings"
"time"

"github.com/golang/glog"
"github.com/robfig/cron/v3"
)

type ABServices struct {
Cfg *ABConfig
status string
cron *cron.Cron
services []ABService
cohortService *CohortService
}

func NewABServices(cfg *ABConfig) *ABServices {
s := &ABServices{
Cfg: cfg,
status: "success",
}
if cfg.EnableCohort {
s.cohortService = NewCohortService(cfg.Url)
}
if err := s.reload(); err != nil {
panic(err)
}
s.cron = cron.New()
// nolint
s.cron.AddFunc("* * * * *", s.Reload)
s.cron.Start()

return s
}

func (s *ABServices) Reload() {
start := time.Now()
if err := s.reload(); err != nil {
glog.Errorf("Reload AB config err: %s", err)
s.status = "failed"
return
}
s.status = "success"
glog.Info("Reload AB config success, cost: ", time.Since(start))
}

type Layers []*Layer

func (s Layers) Len() int {
return len(s)
}
func (s Layers) Less(i, j int) bool {
return s[i].Zone < s[j].Zone
}
func (s Layers) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func (s *ABServices) reload() error {
resp, err := http.Get(s.Cfg.Url + "/ab/newsbreak/layers/" + strings.Join(s.Cfg.Layers, ",") + "?" + url.Values{
"app": {s.Cfg.App},
"last_status": {s.status},
"client_version": {"go-1.0.0"},
"cohort": {fmt.Sprint(s.Cfg.EnableCohort)},
}.Encode())
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var layers []*Layer
if err = json.Unmarshal(body, &layers); err != nil {
return err
}
if s.cohortService != nil {
s.cohortService.Switching()
}
sort.Sort(Layers(layers))
for _, layer := range layers {
if err = layer.Init(s.cohortService); err != nil {
return err
}
}
s.services = []ABService{NewUserService(layers), NewBucketService(layers)}
if s.cohortService != nil {
s.cohortService.Switched()
}

return nil
}

func (s *ABServices) AB(ctx *ABContext) *ABResult {
result := NewABResult()
for _, service := range s.services {
service.AB(ctx, result)
}
return result
}
75 changes: 75 additions & 0 deletions ab-go-sdk/bucket_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package ab

import (
"strings"

"github.com/antonmedv/expr"
"github.com/spaolacci/murmur3"
)

type BucketService struct {
service *ZoneLayerService
}

func NewBucketService(layers []*Layer) *BucketService {
service := NewZoneLayerService()
layersIndex := map[string]*ZoneLayerService{}
for _, layer := range layers {
zone := layer.Zone
zoneLayer := layersIndex[zone]
if zoneLayer == nil {
zoneLayer = NewZoneLayerService()
layersIndex[zone] = zoneLayer
var parentZoneLayer *ZoneLayerService
zones := strings.Split(zone, ".")
for i := len(zones) - 1; i > 0 && parentZoneLayer == nil; i-- {
parentZoneLayer = layersIndex[strings.Join(zones[:i], ".")]
}
if parentZoneLayer != nil {
if parentZoneLayer.Left == nil {
parentZoneLayer.Left = NewZoneLayerService()
}
parentZoneLayer.Left.Selfs = append(parentZoneLayer.Left.Selfs, zoneLayer)
} else {
service.Selfs = append(service.Selfs, zoneLayer)
}
}
zoneLayer.Selfs = append(zoneLayer.Selfs, layer)
}
return &BucketService{service: service}
}

func (this *BucketService) AB(ctx *ABContext, result *ABResult) {
this.service.ForEachLayer(func(layer *Layer) bool {
if result.ContainsLayer(layer.Name) {
return true
}
platform, _ := ctx.ConditionCtx["platform"].(string)
if layer.Platform != "" && layer.Platform != platform {
return false
}
n := murmur3.Sum32([]byte(layer.shufflePrefix + "@" + ctx.Factor))
version := layer.GetBucket(n)
if version == nil {
return false
}
if version.Exp.Conditions != "" {
if ctx.ConditionCtx == nil {
return false
}
out, err := expr.Eval(version.Exp.Conditions, ctx.ConditionCtx)
conditionSuccess, conditionBool := out.(bool)
if err != nil || !conditionSuccess || !conditionBool {
return false
}
}
cohort := version.Exp.Cohort
if cohort != nil {
if ctx.Userid == 0 || !cohort.Contains(ctx.Userid) {
return false
}
}
result.MergeVersion(version)
return true
})
}
15 changes: 15 additions & 0 deletions ab-go-sdk/cohort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ab

import (
"github.com/RoaringBitmap/roaring"
)

type Cohort struct {
Location string `json:"location"`
Users *roaring.Bitmap `json:"-"`
UpdateTime string `json:"update_time"`
}

func (this *Cohort) Contains(x uint32) bool {
return this.Users != nil && this.Users.Contains(x)
}
Loading