Skip to content

Commit

Permalink
chore: add client date
Browse files Browse the repository at this point in the history
  • Loading branch information
suthar26 committed Apr 18, 2024
1 parent 8b9aabd commit 24092ab
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
30 changes: 29 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package devcycle
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -17,6 +18,8 @@ import (
"strings"
"time"

"github.com/google/uuid"

"github.com/devcyclehq/go-server-sdk/v2/util"

"github.com/devcyclehq/go-server-sdk/v2/api"
Expand Down Expand Up @@ -55,6 +58,7 @@ type Client struct {
// Set to true when the client has been initialized, regardless of whether the config has loaded successfully.
isInitialized bool
internalOnInitializedChannel chan bool
clientUUID string
}

type LocalBucketing interface {
Expand Down Expand Up @@ -151,6 +155,7 @@ func (c *Client) IsLocalBucketing() bool {

func (c *Client) handleInitialization() {
c.isInitialized = true
c.clientUUID = uuid.NewString()
if c.DevCycleOptions.OnInitializedChannel != nil {
go func() {
c.DevCycleOptions.OnInitializedChannel <- true
Expand All @@ -173,8 +178,13 @@ func (c *Client) GetRawConfig() (config []byte, etag string, err error) {
if c.configManager == nil {
return nil, "", errors.New("cannot read raw config; config manager is nil")
}
configMap := map[string]interface{}{}
err = json.Unmarshal(c.configManager.rawConfig, &configMap)
if err != nil {
return nil, "", err
}
if c.configManager.HasConfig() {
return c.configManager.rawConfig, c.configManager.configETag, nil
return c.configManager.rawConfig, configMap["eTag"].(string), nil
}
return nil, "", errors.New("cannot read raw config; config manager has no config")
}
Expand Down Expand Up @@ -477,8 +487,23 @@ func (c *Client) Track(user User, event Event) (bool, error) {
return false, errors.New("event type is required")
}

if(event.MetaData == nil) {
event.MetaData = make(map[string]interface{})
}
event.MetaData["ClientId"] = c.clientUUID
event.ClientDate = time.Now()

if c.IsLocalBucketing() {
if c.hasConfig() {
fmt.Println("configManager.rawconfig, ",string( c.configManager.rawConfig))
var configMap map[string]interface{}
marshalErr := json.Unmarshal(c.configManager.rawConfig, &configMap)
if marshalErr != nil {
util.Errorf("Error marshaling config: %v", marshalErr)
return false, marshalErr
}
fmt.Println("etag: ", configMap["eTag"])
event.MetaData["configEtag"] = configMap["eTag"]
err := c.eventQueue.QueueEvent(user, event)
if err != nil {
util.Errorf("Error queuing event: %v", err)
Expand Down Expand Up @@ -510,6 +535,7 @@ func (c *Client) Track(user User, event Event) (bool, error) {

r, rBody, err := c.performRequest(path, httpMethod, postBody, headers, queryParams)
if err != nil {
fmt.Println("error in track: ", err)
return false, err
}

Expand All @@ -535,6 +561,8 @@ func (c *Client) FlushEvents() error {
return nil
}

fmt.Println("sending events")

err := c.eventQueue.FlushEvents()
if err != nil {
util.Errorf("Error flushing events: %v", err)
Expand Down
17 changes: 15 additions & 2 deletions configmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,29 @@ func (e *EnvironmentConfigManager) fetchConfig(numRetriesRemaining int) (err err

func (e *EnvironmentConfigManager) setConfigFromResponse(response *http.Response) error {
config, err := io.ReadAll(response.Body)

if err != nil {
return err
}

configMap := make(map[string]interface{})
err = json.Unmarshal(config, &configMap)
if err != nil {
return err
}

configMap["eTag"] = response.Header.Get("Etag")
configWithTag, err := json.Marshal(configMap)
if err != nil {
return err
}
// Check
valid := json.Valid(config)
valid := json.Valid(configWithTag)
if !valid {
return fmt.Errorf("invalid JSON data received for config")
}

err = e.setConfig(config, e.configETag)
err = e.setConfig(configWithTag, response.Header.Get("Etag"))

if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions event_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (e *EventManager) QueueEvent(user User, event Event) error {
default:
}
}
fmt.Println("QueueEvent")
err = e.internalQueue.QueueEvent(user, event)
if err != nil && errors.Is(err, ErrQueueFull) {
return fmt.Errorf("event queue is full, dropping event: %+v", event)
Expand All @@ -122,6 +123,7 @@ func (e *EventManager) FlushEvents() (err error) {
defer e.flushMutex.Unlock()

util.Debugf("Started flushing events")
fmt.Println("FlushEvents")

defer func() {
if r := recover(); r != nil {
Expand Down
2 changes: 1 addition & 1 deletion example/cloud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
if variableKey == "" {
log.Fatal("DEVCYCLE_VARIABLE_KEY env var not set: set it to a variable key")
}
user := devcycle.User{UserId: "test"}
user := devcycle.User{UserId: "suthar-test-user"}
dvcOptions := devcycle.Options{
EnableEdgeDB: false,
EnableCloudBucketing: true,
Expand Down
9 changes: 7 additions & 2 deletions example/local/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
log.Fatal("DEVCYCLE_VARIABLE_KEY env var not set: set it to a variable key")
}

user := devcycle.User{UserId: "test"}
user := devcycle.User{UserId: "suthar-test-user"}
dvcOptions := devcycle.Options{
EnableEdgeDB: false,
EnableCloudBucketing: false,
Expand All @@ -32,7 +32,9 @@ func main() {

client, err := devcycle.NewClient(sdkKey, &dvcOptions)
time.Sleep(10 * time.Second)
fmt.Println("Error? ", err)
if(err != nil) {
log.Fatalf("Error initializing client: %v", err)
}
fmt.Println(client.GetRawConfig())
log.Printf("client initialized")

Expand Down Expand Up @@ -78,4 +80,7 @@ func main() {
if err != nil {
log.Fatalf("Error tracking event: %v", err)
}

time.Sleep(10 * time.Second)

}

0 comments on commit 24092ab

Please sign in to comment.