Skip to content

Commit

Permalink
feat : Improve event tracking with TrackWorkRequest with Host peer ID…
Browse files Browse the repository at this point in the history
… for work requests

- Add TrackWorkRequest in event_library.go to use host node's peer ID
- Modify SearchTweetsRecent in handlers_data.go to pass Host peer ID
- Add null checks for EventTracker and Node in API handlers
- Ensure proper initialization of EventTracker in NewAPI function
- Update API struct to include EventTracker field
- Add debug logging for EventTracker creation and API initialization

This commit enhances the accuracy of event tracking by using the host node's
peer ID instead of the client IP when logging work requests. It also improves
robustness with additional null checks and error handling.
  • Loading branch information
teslashibe committed Aug 24, 2024
1 parent e892096 commit 333951c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/masa-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (

func main() {

logrus.SetLevel(logrus.DebugLevel)
logrus.Debug("Log level set to Debug")

if len(os.Args) > 1 && os.Args[1] == "--version" {
logrus.Infof("Masa Oracle Node Version: %s\nMasa Oracle Protocol verison: %s", versioning.ApplicationVersion, versioning.ProtocolVersion)
os.Exit(0)
Expand Down
20 changes: 18 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,33 @@ import (
"strconv"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"

masa "github.com/masa-finance/masa-oracle/pkg"
"github.com/masa-finance/masa-oracle/pkg/event"
)

type API struct {
Node *masa.OracleNode
Node *masa.OracleNode
EventTracker *event.EventTracker
}

// NewAPI creates a new API instance with the given OracleNode.
func NewAPI(node *masa.OracleNode) *API {
return &API{Node: node}
eventTracker := event.NewEventTracker(nil)
if eventTracker == nil {
logrus.Error("Failed to create EventTracker")
} else {
logrus.Debug("EventTracker created successfully")
}

api := &API{
Node: node,
EventTracker: eventTracker,
}

logrus.Debugf("Created API instance with EventTracker: %v", api.EventTracker)
return api
}

// GetPathInt converts the path parameter with name to an int.
Expand Down
11 changes: 9 additions & 2 deletions pkg/api/handlers_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/masa-finance/masa-oracle/pkg/scrapers/discord"
"github.com/masa-finance/masa-oracle/pkg/scrapers/telegram"
"github.com/masa-finance/masa-oracle/pkg/workers"
"github.com/masa-finance/masa-oracle/pkg/workers/types"
data_types "github.com/masa-finance/masa-oracle/pkg/workers/types"
)

type LLMChat struct {
Expand Down Expand Up @@ -770,7 +770,14 @@ func (api *API) SearchTweetsRecent() gin.HandlerFunc {
return
}

// worker handler implementation
if api.EventTracker != nil && api.Node != nil {
peerID := api.Node.Host.ID().String()
api.EventTracker.TrackWorkRequest("SearchTweetsRecent", peerID)
} else {
logrus.Warn("EventTracker or Node is nil in SearchTweetsRecent")
}

// Rest of the function remains the same
bodyBytes, err := json.Marshal(reqBody)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
Expand Down
1 change: 1 addition & 0 deletions pkg/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

const (
WorkRequest = "work_request"
WorkCompletion = "work_completion"
WorkFailure = "worker_failure"
WorkDistribution = "work_distribution"
Expand Down
20 changes: 20 additions & 0 deletions pkg/event/event_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ import (
"github.com/sirupsen/logrus"
)

// Add this function to the existing file

// TrackWorkRequest records when a work request is initiated.
//
// Parameters:
// - workType: String indicating the type of work being requested (e.g., "SearchTweetsRecent")
// - peerId: String containing the peer ID (or client IP in this case)
func (a *EventTracker) TrackWorkRequest(workType string, peerId string) {
event := Event{
Name: WorkRequest,
PeerID: peerId,
WorkType: workType,
Timestamp: time.Now().UTC(),
}
err := a.TrackAndSendEvent(event, nil)
if err != nil {
logrus.Errorf("error tracking work request event: %s", err)
}
}

// TrackWorkDistribution records the distribution of work to a worker.
//
// Parameters:
Expand Down

0 comments on commit 333951c

Please sign in to comment.