Skip to content

Commit

Permalink
get nodes by status
Browse files Browse the repository at this point in the history
  • Loading branch information
p-shubh committed Jun 14, 2024
1 parent cb136c4 commit 3739cdc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
64 changes: 64 additions & 0 deletions api/v1/nodes/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func ApplyRoutes(r *gin.RouterGroup) {
g := r.Group("/nodes")
{
g.GET("/all", FetchAllNodes)
g.GET("/:status", FetchAllNodesByStatus)
}
}

Expand Down Expand Up @@ -79,5 +80,68 @@ func FetchAllNodes(c *gin.Context) {
}

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

func FetchAllNodesByStatus(c *gin.Context) {
status := c.Param("status") // active , inactive
db := dbconfig.GetDb()
var nodes *[]models.Node
// var node *models.Node
if err := db.Where("status = ?", status).Find(&nodes).Error; err != nil {
logwrapper.Errorf("failed to get nodes from DB: %s", err)
httpo.NewErrorResponse(http.StatusInternalServerError, err.Error()).SendD(c)
return
}

// Unmarshal SystemInfo into OSInfo struct

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

responses = append(responses, response)
}

httpo.NewSuccessResponseP(200, "Nodes fetched succesfully", responses).SendD(c)
}
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/NetSepio/erebrus-gateway/api"
"github.com/NetSepio/erebrus-gateway/app"
"github.com/NetSepio/erebrus-gateway/config/dbconfig"
"github.com/NetSepio/erebrus-gateway/util/pkg/logwrapper"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
Expand All @@ -20,7 +19,7 @@ func main() {
logwrapper.Init()
app.Init()
ginApp := gin.Default()
dbconfig.DbInit()
// dbconfig.DbInit()

// cors middleware
config := cors.DefaultConfig()
Expand Down

0 comments on commit 3739cdc

Please sign in to comment.