Skip to content

Commit

Permalink
Added new API endpoint to fetch nodes by chain and wallet address
Browse files Browse the repository at this point in the history
  • Loading branch information
p-shubh committed Dec 20, 2024
1 parent 32382b2 commit 6a4a19c
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions api/v1/nodes/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func ApplyRoutes(r *gin.RouterGroup) {
g.GET("/all", FetchAllNodes)
g.GET("/:status", FetchAllNodesByStatus)
g.GET("/status_wallet_address/:status/:wallet_address", FetchAllNodesByStatusAndWalletAddress)
g.GET("/nodes_details_by_wallet_adddress_and_chain", HandlerGetNodesByChainAndWallet())

}
}

Expand Down Expand Up @@ -247,3 +249,83 @@ func FetchAllNodesByStatusAndWalletAddress(c *gin.Context) {

httpo.NewSuccessResponseP(200, "Nodes fetched succesfully", responses).SendD(c)
}

// NodesHandler handles the API request to fetch nodes
func HandlerGetNodesByChainAndWallet() gin.HandlerFunc {
return func(c *gin.Context) {
chain := c.Query("chain")
walletAddress := c.Query("wallet_address")

if chain == "" || walletAddress == "" {
logwrapper.Errorf("chain and wallet_address are required { HandlerGetNodesByChainAndWallet }")

return
}
db := dbconfig.GetDb()
var nodes *[]models.Node

err := db.Where("chain = ? AND wallet_address = ?", chain, walletAddress).Find(&nodes).Error

// nodes, err := GetNodesByChainAndWallet(db, chain, walletAddress)
if err != nil {
logwrapper.Errorf("failed to get nodes from DB { HandlerGetNodesByChainAndWallet }: %s", err)
return
}

var responses []models.NodeResponse
var response models.NodeResponse

for _, i := range *nodes {
var osInfo models.OSInfo
if len(i.SystemInfo) > 0 {
err := json.Unmarshal([]byte(i.SystemInfo), &osInfo)
if err != nil {
logwrapper.Errorf("failed to get nodes from DB OSInfo: %s", err)
// httpo.NewErrorResponse(http.StatusInternalServerError, err.Error()).SendD(c)
}
}
// Unmarshal IpInfo into IPInfo struct
var ipGeoAddress models.IpGeoAddress
if len(i.IpGeoData) > 0 {
err := json.Unmarshal([]byte(i.IpGeoData), &ipGeoAddress)
if err != nil {
logwrapper.Errorf("failed to get nodes from DB IpGeoAddress: %s", err)
// httpo.NewErrorResponse(http.StatusInternalServerError, err.Error()).SendD(c)
}
}

response.Id = i.PeerId
response.Name = i.Name
response.HttpPort = i.HttpPort
response.Domain = i.Host
response.NodeName = i.Name
response.Address = i.PeerAddress
response.Region = i.Region
response.Status = i.Status
response.DownloadSpeed = i.DownloadSpeed
response.UploadSpeed = i.UploadSpeed
response.StartTimeStamp = i.RegistrationTime
response.LastPingedTimeStamp = i.LastPing
response.Chain = i.Chain
response.WalletAddressSui = i.WalletAddress
response.WalletAddressSolana = i.WalletAddress
response.IpInfoIP = ipGeoAddress.IpInfoIP
response.IpInfoCity = ipGeoAddress.IpInfoCity
response.IpInfoCountry = ipGeoAddress.IpInfoCountry
response.IpInfoLocation = ipGeoAddress.IpInfoLocation
response.IpInfoOrg = ipGeoAddress.IpInfoOrg
response.IpInfoPostal = ipGeoAddress.IpInfoPostal
response.IpInfoTimezone = ipGeoAddress.IpInfoTimezone
// Round TotalActiveDuration and TodayActiveDuration to two decimal places
// response.TotalActiveDuration = math.Round(i.TotalActiveDuration*100) / 100
// response.TodayActiveDuration = math.Round(i.TodayActiveDuration*100) / 100

response.TotalActiveDuration, response.TodayActiveDuration = nodeactivity.CalculateTotalAndTodayActiveDuration(i.PeerId)

responses = append(responses, response)
}

httpo.NewSuccessResponseP(200, "Nodes fetched succesfully", responses).SendD(c)

}
}

0 comments on commit 6a4a19c

Please sign in to comment.