Skip to content

Commit

Permalink
Merge pull request #42 from checkr/zz/change-variant-key-in-distribution
Browse files Browse the repository at this point in the history
Change variant key should also change distribution
  • Loading branch information
zhouzhuojie authored Oct 19, 2017
2 parents 2f6c55c + 005e70d commit d94b20a
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
38 changes: 38 additions & 0 deletions buildscripts/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import httplib
import random
import json
import datetime

conn = httplib.HTTPConnection("localhost:18000")
conn_elastic = httplib.HTTPConnection("localhost:9200")
headers = { 'content-type': "application/json" }

def random_payload():
entity_id = random.randint(1, 1000000)
d = {
"entityID": str(entity_id),
"entityType": "user",
"entityContext": {
"state": random.choice(['CA', 'NY', 'VA']),
"dl_state": random.choice(['CA', 'NY', 'VA']),
},
"flagID": 2,
"enableDebug": True
}
return json.dumps(d)

def index_elastic(payload):
conn_elastic.request("POST", "/flagr/flagr-records", payload, headers)
res = conn_elastic.getresponse()
data = res.read()
print(data.decode("utf-8"))

while 1:
t = datetime.datetime.now()
conn.request("POST", "/api/v1/evaluation", random_payload(), headers)
res = conn.getresponse()
print(str((datetime.datetime.now() - t).total_seconds() * 1000) + 'ms')

data = res.read()
index_elastic(data)
print(data.decode("utf-8"))
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@ kafka-manager:
ZK_HOSTS: "zookeeper:2181"
extra_hosts:
- "moby:127.0.0.1"

elk:
image: sebp/elk
container_name: flagr-elk
ports:
- "5601:5601"
- "9200:9200"
- "5044:5044"
2 changes: 1 addition & 1 deletion pkg/entity/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
type Distribution struct {
gorm.Model
SegmentID uint `gorm:"index:idx_distribution_segmentid"`
VariantID uint
VariantID uint `gorm:"index:idx_distribution_variantid"`
VariantKey string

Percent uint // Percent is an uint from 0 to 100, percent is always derived from Bitmap
Expand Down
4 changes: 4 additions & 0 deletions pkg/handler/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ func (c *crud) PutVariant(params variant.PutVariantParams) middleware.Responder
return variant.NewPutVariantDefault(500).WithPayload(ErrorMessage("%s", err))
}

if err := validatePutVariantForDistributions(&v); err != nil {
return variant.NewPutVariantDefault(err.StatusCode).WithPayload(ErrorMessage("%s", err))
}

resp := variant.NewPutVariantOK()
resp.SetPayload(e2r.MapVariant(&v))
return resp
Expand Down
7 changes: 5 additions & 2 deletions pkg/handler/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ func evalSegment(
expr := segment.SegmentEvaluation.ConditionsExpr
match, err := conditions.Evaluate(expr, m)
if err != nil {
evalErr = NewError(400, "invalid entity_context: %s. reason: %s.", spew.Sdump(evalContext.EntityContext), err)
return nil, nil, evalErr
log = &models.SegmentDebugLog{
Msg: err.Error(),
SegmentID: int64(segment.ID),
}
return nil, log, nil
}
if !match {
log = &models.SegmentDebugLog{
Expand Down
4 changes: 2 additions & 2 deletions pkg/handler/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func TestEvalSegment(t *testing.T) {
}, s)

assert.Nil(t, vID)
assert.Empty(t, log)
assert.Error(t, err)
assert.NotEmpty(t, log)
assert.Nil(t, err)
})

t.Run("test constraint not match", func(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/handler/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ func validateDeleteVariant(params variant.DeleteVariantParams) *Error {

return nil
}

func validatePutVariantForDistributions(v *entity.Variant) *Error {
q := entity.NewDistributionQuerySet(repo.GetDB())
if err := q.VariantIDEq(v.ID).GetUpdater().SetVariantKey(v.Key).Update(); err != nil {
return NewError(500, "error updating distribution to sync with variantID %v with variantKey %v. reason: %s", v.ID, v.Key, err)
}
return nil
}

0 comments on commit d94b20a

Please sign in to comment.