Skip to content

Commit

Permalink
feat: add geolocation data to websocket events (#147)
Browse files Browse the repository at this point in the history
* feat: add geolocation data to websocket events

* feat: add deals tracking (#151)

* feat: add function to track deals

* feat: add tracking deals

* chore: fix typo
  • Loading branch information
AquiGorka authored Jun 11, 2024
1 parent cbc42d8 commit 939c1b2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
12 changes: 8 additions & 4 deletions pkg/http/websocket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ type ConnectionWrapper struct {
}

type WSConnectionParams struct {
ID string
Type string
ID string
Type string
CountryCode string
IP string
}

// StartWebSocketServer starts a WebSocket server
Expand Down Expand Up @@ -97,8 +99,10 @@ func StartWebSocketServer(
}
params := r.URL.Query()
connParams := WSConnectionParams{
ID: params.Get("ID"),
Type: params.Get("Type"),
ID: params.Get("ID"),
Type: params.Get("Type"),
CountryCode: r.Header.Get("Cf-Ipcountry"),
IP: r.Header.Get("Cf-Connecting-Ip"),
}
defer conn.Close()
connectCB(connParams)
Expand Down
9 changes: 9 additions & 0 deletions pkg/jobcreator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/lilypad-tech/lilypad/pkg/data"
"github.com/lilypad-tech/lilypad/pkg/http"
"github.com/lilypad-tech/lilypad/pkg/metricsDashboard"
"github.com/lilypad-tech/lilypad/pkg/solver"
"github.com/lilypad-tech/lilypad/pkg/solver/store"
"github.com/lilypad-tech/lilypad/pkg/system"
Expand Down Expand Up @@ -98,6 +99,14 @@ func (controller *JobCreatorController) SubscribeToJobOfferUpdates(sub JobOfferS
*/
func (controller *JobCreatorController) subscribeToSolver() error {
controller.solverClient.SubscribeEvents(func(ev solver.SolverEvent) {
if ev.EventType == "DealStateUpdated" {
metricsDashboard.TrackDeal(metricsDashboard.DealPayload{
ID: ev.Deal.ID,
JobID: ev.Deal.JobOffer,
JobCreator: ev.Deal.JobCreator,
ResourceProvider: ev.Deal.ResourceProvider,
})
}
switch ev.EventType {
case solver.DealAdded:
if ev.Deal == nil {
Expand Down
33 changes: 29 additions & 4 deletions pkg/metricsDashboard/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
const jobsEndpoint = "jobs"
const nodeInfoEndpoint = "nodes"
const nodeConnectionEndpoint = "uptimes"
const dealsEndpoint = "deals"

var host = os.Getenv("API_HOST") + "metrics-dashboard/"

Expand Down Expand Up @@ -71,15 +72,39 @@ func TrackNodeInfo(resourceOffer data.ResourceOffer) {
TrackEvent(url, payload)
}

func TrackNodeConnectionEvent(event string, ID string) {
type NodeConnectionParams struct {
Event string
ID string
CountryCode string
IP string
}

func TrackNodeConnectionEvent(params NodeConnectionParams) {
var url = host + nodeConnectionEndpoint
data := map[string]interface{}{
"ID": ID,
"Event": event,
"Time": time.Now().UnixMilli(),
"ID": params.ID,
"Event": params.Event,
"CountryCode": params.CountryCode,
"IP": params.IP,
"Time": time.Now().UnixMilli(),
}
byts, _ := json.Marshal(data)
payload := string(byts)

TrackEvent(url, payload)
}

type DealPayload struct {
ID string
JobCreator string
ResourceProvider string
JobID string
}

func TrackDeal(params DealPayload) {
var url = host + dealsEndpoint
byts, _ := json.Marshal(params)
payload := string(byts)

TrackEvent(url, payload)
}
2 changes: 1 addition & 1 deletion pkg/solver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (controller *SolverController) addResourceOffer(resourceOffer data.Resource
return ret, nil
}

func (controller *SolverController) removeResourceOfferBYResourceProvider(ID string) error {
func (controller *SolverController) removeResourceOfferByResourceProvider(ID string) error {
controller.log.Info("remove resource offer", ID)
resourceOffers, err := controller.store.GetResourceOffers(store.GetResourceOffersQuery{
ResourceProvider: ID,
Expand Down
16 changes: 13 additions & 3 deletions pkg/solver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,24 @@ func (solverServer *solverServer) ListenAndServe(ctx context.Context, cm *system
// WS connect events
func (solverServer *solverServer) connectCB(connParams http.WSConnectionParams) {
if connParams.Type == "ResourceProvider" {
metricsDashboard.TrackNodeConnectionEvent("Connect", connParams.ID)
metricsDashboard.TrackNodeConnectionEvent(metricsDashboard.NodeConnectionParams{
Event: "Connect",
ID: connParams.ID,
CountryCode: connParams.CountryCode,
IP: connParams.IP,
})
}
}

func (solverServer *solverServer) disconnectCB(connParams http.WSConnectionParams) {
if connParams.Type == "ResourceProvider" {
metricsDashboard.TrackNodeConnectionEvent("Disconnect", connParams.ID)
solverServer.controller.removeResourceOfferBYResourceProvider(connParams.ID)
metricsDashboard.TrackNodeConnectionEvent(metricsDashboard.NodeConnectionParams{
Event: "Disconnect",
ID: connParams.ID,
CountryCode: connParams.CountryCode,
IP: connParams.IP,
})
solverServer.controller.removeResourceOfferByResourceProvider(connParams.ID)
}
}

Expand Down

0 comments on commit 939c1b2

Please sign in to comment.