Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change variant key should also change distribution #42

Merged
merged 1 commit into from Oct 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions buildscripts/demo.py
@@ -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"))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only for demo, but good to have a client example here

8 changes: 8 additions & 0 deletions docker-compose.yml
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
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
Expand Up @@ -404,6 +404,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
Expand Up @@ -135,8 +135,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
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
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
}