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

fix: evaluate specific flags, topo sort before evaluate #14

Merged
merged 3 commits into from
Aug 4, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions pkg/experiment/local/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Client struct {
config *Config
client *http.Client
poller *poller
flags *string
flags map[string]interface{}
}

func Initialize(apiKey string, config *Config) *Client {
Expand Down Expand Up @@ -62,25 +62,29 @@ func (c *Client) Start() error {
}
c.flags = result
})

return nil
}

func (c *Client) Evaluate(user *experiment.User, flagKeys []string) (map[string]experiment.Variant, error) {
variants := make(map[string]experiment.Variant)
if len(*c.flags) == 0 {
if len(c.flags) == 0 {
c.log.Debug("evaluate: no flags")
return variants, nil

}
userJson, err := json.Marshal(user)
if err != nil {
return nil, err
}

c.log.Debug("evaluate:\n\t- user: %v\n\t- rules: %v\n", string(userJson), *c.flags)

resultJson := evaluation.Evaluate(*c.flags, string(userJson))
sortedFlags, err := topologicalSort(c.flags, flagKeys)
if err != nil {
return nil, err
}
flagsJson, err := json.Marshal(sortedFlags)
if err != nil {
return nil, err
}
c.log.Debug("evaluate:\n\t- user: %v\n\t- rules: %v\n", string(userJson), string(flagsJson))
resultJson := evaluation.Evaluate(string(flagsJson), string(userJson))
c.log.Debug("evaluate result: %v\n", resultJson)
var interopResult *interopResult
err = json.Unmarshal([]byte(resultJson), &interopResult)
Expand Down Expand Up @@ -149,10 +153,19 @@ func (c *Client) doRules() (map[string]interface{}, error) {
}

func (c *Client) Flags() (*string, error) {
return c.doFlags()
flags, err := c.doFlags()
if err != nil {
return nil, err
}
flagsJson, err := json.Marshal(flags)
if err != nil {
return nil, err
}
flagsString := string(flagsJson)
return &flagsString, nil
}

func (c *Client) doFlags() (*string, error) {
func (c *Client) doFlags() (map[string]interface{}, error) {
endpoint, err := url.Parse(c.config.ServerUrl)
if err != nil {
return nil, err
Expand All @@ -177,9 +190,24 @@ func (c *Client) doFlags() (*string, error) {
if err != nil {
return nil, err
}
flags := string(body)
c.log.Debug("flags: %v", flags)
return &flags, nil
c.log.Debug("flags: %v", string(body))
flagsArray := make([]interface{}, 0)
err = json.Unmarshal(body, &flagsArray)
if err != nil {
return nil, err
}
// Extract keys and create flags map
flags := make(map[string]interface{})
for _, flagAny := range flagsArray {
switch flag := flagAny.(type) {
case map[string]interface{}:
switch flagKey := flag["flagKey"].(type) {
case string:
flags[flagKey] = flag
}
}
}
return flags, nil
}

func contains(s []string, e string) bool {
Expand Down
87 changes: 87 additions & 0 deletions pkg/experiment/local/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package local

import "fmt"

func topologicalSort(flags map[string]interface{}, flagKeys []string) ([]interface{}, error) {
result := make([]interface{}, 0)
// Extract keys and copy flags map
keys := make([]string, 0)
available := make(map[string]interface{})
for k, v := range flags {
keys = append(keys, k)
available[k] = v
}
// Get the starting keys
var startingKeys []string
if len(flagKeys) > 0 {
startingKeys = flagKeys
} else {
startingKeys = keys
}
// Sort into result
for _, flagKey := range startingKeys {
traversal, err := parentTraversal(flagKey, available, []string{})
if err != nil {
return nil, err
}
if len(traversal) > 0 {
result = append(result, traversal...)
}
}
return result, nil
}

func parentTraversal(flagKey string, available map[string]interface{}, path []string) ([]interface{}, error) {
flag := available[flagKey]
if flag == nil {
return nil, nil
}
dependencies := extractDependencies(flag)
if len(dependencies) == 0 {
delete(available, flagKey)
return []interface{}{flag}, nil
}
path = append(path, flagKey)
result := make([]interface{}, 0)
for _, parentKey := range dependencies {
if contains(path, parentKey) {
return nil, fmt.Errorf("detected a cycle between flags %v", path)
}
traversal, err := parentTraversal(parentKey, available, path)
if err != nil {
return nil, err
}
if len(traversal) > 0 {
result = append(result, traversal...)
}
}
result = append(result, flag)
delete(available, flagKey)
return result, nil
}

func extractDependencies(flag interface{}) []string {
switch f := flag.(type) {
case map[string]interface{}:
parentDependenciesAny := f["parentDependencies"]
if parentDependenciesAny == nil {
return nil
}
switch parentDependencies := parentDependenciesAny.(type) {
case map[string]interface{}:
flagsAny := parentDependencies["flags"]
if flagsAny == nil {
return nil
}
switch flags := flagsAny.(type) {
case map[string]interface{}:
result := make([]string, 0)
for k := range flags {
result = append(result, k)
}
return result
}
}
}
return nil
}
Loading