|
| 1 | +package localEvaluation |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/LambdaTest/lambda-featureflag-go-sdk/logger" |
| 6 | + "github.com/amplitude/experiment-go-server/pkg/experiment" |
| 7 | + "github.com/amplitude/experiment-go-server/pkg/experiment/local" |
| 8 | + "github.com/joho/godotenv" |
| 9 | + "os" |
| 10 | + "strconv" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + client *local.Client |
| 16 | + Logger = logger.GetLogger() |
| 17 | + LocalEvaluationConfigDebug = false |
| 18 | + LocalEvaluationConfigServerUrl = "https://api.lab.amplitude.com/" |
| 19 | + LocalEvaluationConfigPollInterval = 30 |
| 20 | + LocalEvaluationConfigPollerRequestTimeout = 10 |
| 21 | + LocalEvaluationDeploymentKey = "" |
| 22 | +) |
| 23 | + |
| 24 | +type variant struct { |
| 25 | + Value string `json:"value,omitempty"` |
| 26 | + Payload interface{} `json:"payload,omitempty"` |
| 27 | +} |
| 28 | + |
| 29 | +type UserProperties struct { |
| 30 | + OrgId string `json:"org_id,omitempty"` |
| 31 | + OrgName string `json:"org_name,omitempty"` |
| 32 | + OrgStatus string `json:"org_status,omitempty"` |
| 33 | + Username string `json:"username,omitempty"` |
| 34 | + Email string `json:"email,omitempty"` |
| 35 | + Plan string `json:"plan,omitempty"` |
| 36 | + SubscriptionType string `json:"subscription_type,omitempty"` |
| 37 | + SubscriptionStatus string `json:"subscription_status,omitempty"` |
| 38 | + HubRegion string `json:"hub_region,omitempty"` |
| 39 | +} |
| 40 | + |
| 41 | +func init() { |
| 42 | + err := godotenv.Load() |
| 43 | + if err != nil { |
| 44 | + Logger.Infof("No .env file found") |
| 45 | + } else { |
| 46 | + Logger.Infof(".env file loaded") |
| 47 | + } |
| 48 | + |
| 49 | + if os.Getenv("LOCAL_EVALUATION_CONFIG_DEBUG") != "" { |
| 50 | + LocalEvaluationConfigDebug, _ = strconv.ParseBool(os.Getenv("LOCAL_EVALUATION_CONFIG_DEBUG")) |
| 51 | + } |
| 52 | + if os.Getenv("LOCAL_EVALUATION_CONFIG_SERVER_URL") != "" { |
| 53 | + LocalEvaluationConfigServerUrl = os.Getenv("LOCAL_EVALUATION_CONFIG_SERVER_URL") |
| 54 | + } |
| 55 | + if os.Getenv("LOCAL_EVALUATION_CONFIG_POLL_INTERVAL") != "" { |
| 56 | + LocalEvaluationConfigPollInterval, _ = strconv.Atoi(os.Getenv("LOCAL_EVALUATION_CONFIG_POLL_INTERVAL")) |
| 57 | + } |
| 58 | + if os.Getenv("LOCAL_EVALUATION_CONFIG_POLLER_REQUEST_TIMEOUT") != "" { |
| 59 | + LocalEvaluationConfigPollerRequestTimeout, _ = strconv.Atoi(os.Getenv("LOCAL_EVALUATION_CONFIG_POLLER_REQUEST_TIMEOUT")) |
| 60 | + } |
| 61 | + if os.Getenv("LOCAL_EVALUATION_DEPLOYMENT_KEY") != "" { |
| 62 | + LocalEvaluationDeploymentKey = os.Getenv("LOCAL_EVALUATION_DEPLOYMENT_KEY") |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func Initialize() error { |
| 67 | + config := local.Config{ |
| 68 | + Debug: LocalEvaluationConfigDebug, |
| 69 | + ServerUrl: LocalEvaluationConfigServerUrl, |
| 70 | + FlagConfigPollerInterval: time.Duration(LocalEvaluationConfigPollInterval) * time.Second, |
| 71 | + FlagConfigPollerRequestTimeout: time.Duration(LocalEvaluationConfigPollerRequestTimeout) * time.Second, |
| 72 | + } |
| 73 | + Logger.Debugf("config payload: %v, deployment key:%s", config, LocalEvaluationDeploymentKey) |
| 74 | + client = local.Initialize(LocalEvaluationDeploymentKey, &config) |
| 75 | + err := client.Start() |
| 76 | + if err != nil { |
| 77 | + err = fmt.Errorf("unable to create local evaluation client with given config %v with error %s", config, err.Error()) |
| 78 | + return err |
| 79 | + } |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func fetch(flagName string, user UserProperties) variant { |
| 84 | + flagKeys := []string{flagName} |
| 85 | + userProp := map[string]interface{}{ |
| 86 | + "org_id": user.OrgId, |
| 87 | + "org_name": user.OrgName, |
| 88 | + "org_status": user.OrgStatus, |
| 89 | + "username": user.Username, |
| 90 | + "email": user.Email, |
| 91 | + "plan": user.Plan, |
| 92 | + "subscription_type": user.SubscriptionType, |
| 93 | + "subscription_status": user.SubscriptionStatus, |
| 94 | + "hub_region": user.HubRegion, |
| 95 | + } |
| 96 | + |
| 97 | + expUser := experiment.User{ |
| 98 | + UserProperties: userProp, |
| 99 | + } |
| 100 | + |
| 101 | + Logger.Debugf("user payload: %v, flag: %s", expUser, flagName) |
| 102 | + |
| 103 | + variants, err := client.Evaluate(&expUser, flagKeys) |
| 104 | + if err != nil { |
| 105 | + Logger.Errorf("error in evaluating flag: %s error: %s", flagName, err.Error()) |
| 106 | + return variant{} |
| 107 | + } |
| 108 | + |
| 109 | + return variant(variants[flagName]) |
| 110 | +} |
| 111 | + |
| 112 | +func GetFeatureFlagString(flagName string, user UserProperties) string { |
| 113 | + data := fetch(flagName, user) |
| 114 | + return data.Value |
| 115 | +} |
| 116 | + |
| 117 | +func GetFeatureFlagBool(flagName string, user UserProperties) bool { |
| 118 | + data := fetch(flagName, user) |
| 119 | + if val, err := strconv.ParseBool(data.Value); err == nil { |
| 120 | + return val |
| 121 | + } |
| 122 | + return false |
| 123 | +} |
| 124 | + |
| 125 | +func GetFeatureFlagPayload(flagName string, user UserProperties) map[string]interface{} { |
| 126 | + data := fetch(flagName, user) |
| 127 | + mapData := make(map[string]interface{}) |
| 128 | + mapData["value"] = data.Value |
| 129 | + mapData["payload"] = data.Payload |
| 130 | + return mapData |
| 131 | +} |
0 commit comments