Skip to content

Commit b2f2b2a

Browse files
committed
use json - to skip unmarshalling unnecessary fields instead of removing them manully
1 parent 7d38af6 commit b2f2b2a

File tree

2 files changed

+15
-93
lines changed

2 files changed

+15
-93
lines changed

node-registrar/pkg/db/models.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Account struct {
1919
PublicKey string `gorm:"type:text;not null;unique" json:"public_key"`
2020
// Relations | likely we need to use OnDelete:RESTRICT (Prevent Twin deletion if farms exist)
2121
// @swagger:ignore
22-
Farms []Farm `gorm:"foreignKey:TwinID;references:TwinID;constraint:OnDelete:RESTRICT" json:"farms"`
22+
Farms []Farm `gorm:"foreignKey:TwinID;references:TwinID;constraint:OnDelete:RESTRICT" json:"-"`
2323
}
2424

2525
type Farm struct {
@@ -31,7 +31,7 @@ type Farm struct {
3131
CreatedAt time.Time `json:"created_at"`
3232
UpdatedAt time.Time `json:"updated_at"`
3333
// @swagger:ignore
34-
Nodes []Node `gorm:"foreignKey:FarmID;references:FarmID;constraint:OnDelete:RESTRICT" json:"nodes"`
34+
Nodes []Node `gorm:"foreignKey:FarmID;references:FarmID;constraint:OnDelete:RESTRICT" json:"-"`
3535
}
3636

3737
type Node struct {
@@ -49,7 +49,7 @@ type Node struct {
4949
Virtualized bool `json:"virtualized"`
5050
SerialNumber string `json:"serial_number"`
5151

52-
UptimeReports []UptimeReport `json:"uptime" gorm:"foreignKey:NodeID;references:NodeID;constraint:OnDelete:CASCADE"`
52+
UptimeReports []UptimeReport `json:"-" gorm:"foreignKey:NodeID;references:NodeID;constraint:OnDelete:CASCADE"`
5353
LastSeen time.Time `json:"last_seen" gorm:"index"` // Last time the node sent Uptime report
5454
Online bool `json:"online" gorm:"-"` // Computed field, not stored in database
5555
CreatedAt time.Time `json:"created_at"`

node-registrar/pkg/server/handlers.go

Lines changed: 12 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package server
22

33
import (
44
"encoding/base64"
5-
"encoding/json"
65
"errors"
76
"fmt"
87
"net/http"
@@ -36,7 +35,7 @@ const (
3635
// @Param twin_id query int false "Filter by twin ID"
3736
// @Param page query int false "Page number" default(1)
3837
// @Param size query int false "Results per page" default(10)
39-
// @Success 200 {object} []db.Farm "List of farms"]
38+
// @Success 200 {object} []db.Farm "List of farms"
4039
// @Failure 400 {object} map[string]any "Bad request"
4140
// @Router /farms [get]
4241
func (s Server) listFarmsHandler(c *gin.Context) {
@@ -55,19 +54,7 @@ func (s Server) listFarmsHandler(c *gin.Context) {
5554
return
5655
}
5756

58-
// dorp extra db field
59-
var res []map[string]any
60-
for _, farm := range farms {
61-
data, err := toMap(farm)
62-
if err != nil {
63-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
64-
return
65-
}
66-
delete(data, "nodes")
67-
res = append(res, data)
68-
}
69-
70-
c.JSON(http.StatusOK, res)
57+
c.JSON(http.StatusOK, farms)
7158
}
7259

7360
// @Summary Get farm details
@@ -101,15 +88,7 @@ func (s Server) getFarmHandler(c *gin.Context) {
10188
return
10289
}
10390

104-
// dorp extra db field
105-
res, err := toMap(farm)
106-
if err != nil {
107-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
108-
return
109-
}
110-
delete(res, "nodes")
111-
112-
c.JSON(http.StatusOK, res)
91+
c.JSON(http.StatusOK, farm)
11392
}
11493

11594
// @Summary Create new farm
@@ -257,23 +236,13 @@ func (s Server) listNodesHandler(c *gin.Context) {
257236
return
258237
}
259238

239+
// Set online status for each node
260240
cutoffTime := time.Now().Add(-OnlineCutoffTime)
261-
// dorp extra db field
262-
var res []map[string]any
263-
for _, node := range nodes {
264-
node.Online = !node.LastSeen.IsZero() && node.LastSeen.After(cutoffTime)
265-
266-
data, err := toMap(node)
267-
if err != nil {
268-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
269-
return
270-
}
271-
272-
delete(data, "uptime")
273-
res = append(res, data)
241+
for i := range nodes {
242+
nodes[i].Online = !nodes[i].LastSeen.IsZero() && nodes[i].LastSeen.After(cutoffTime)
274243
}
275244

276-
c.JSON(http.StatusOK, res)
245+
c.JSON(http.StatusOK, nodes)
277246
}
278247

279248
// @Summary Get node details
@@ -305,20 +274,11 @@ func (s Server) getNodeHandler(c *gin.Context) {
305274
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
306275
return
307276
}
308-
277+
// Determine if the node is online (has sent an uptime report in the last 30 minutes)
309278
cutoffTime := time.Now().Add(-OnlineCutoffTime)
310279
node.Online = !node.LastSeen.IsZero() && node.LastSeen.After(cutoffTime)
311280

312-
// dorp extra db field
313-
data, err := toMap(node)
314-
if err != nil {
315-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
316-
return
317-
}
318-
319-
delete(data, "uptime")
320-
321-
c.JSON(http.StatusOK, data)
281+
c.JSON(http.StatusOK, node)
322282
}
323283

324284
type NodeRegistrationRequest struct {
@@ -632,15 +592,7 @@ func (s *Server) createAccountHandler(c *gin.Context) {
632592
return
633593
}
634594

635-
// dorp extra db field
636-
data, err := toMap(account)
637-
if err != nil {
638-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
639-
return
640-
}
641-
642-
delete(data, "farms")
643-
c.JSON(http.StatusCreated, data)
595+
c.JSON(http.StatusCreated, account)
644596
}
645597

646598
type UpdateAccountRequest struct {
@@ -741,15 +693,7 @@ func (s *Server) getAccountHandler(c *gin.Context) {
741693
return
742694
}
743695

744-
// dorp extra db field
745-
data, err := toMap(account)
746-
if err != nil {
747-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
748-
return
749-
}
750-
751-
delete(data, "farms")
752-
c.JSON(http.StatusCreated, data)
696+
c.JSON(http.StatusOK, account)
753697
return
754698
}
755699

@@ -763,16 +707,7 @@ func (s *Server) getAccountHandler(c *gin.Context) {
763707
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get account"})
764708
return
765709
}
766-
767-
// dorp extra db field
768-
data, err := toMap(account)
769-
if err != nil {
770-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
771-
return
772-
}
773-
774-
delete(data, "farms")
775-
c.JSON(http.StatusCreated, data)
710+
c.JSON(http.StatusOK, account)
776711
return
777712
}
778713
}
@@ -890,16 +825,3 @@ func validateTimestampHint(timestampHint int64) error {
890825

891826
return nil
892827
}
893-
894-
func toMap(val any) (map[string]any, error) {
895-
bytes, err := json.Marshal(val)
896-
if err != nil {
897-
return nil, err
898-
}
899-
data := map[string]any{}
900-
err = json.Unmarshal(bytes, &data)
901-
if err != nil {
902-
return nil, err
903-
}
904-
return data, nil
905-
}

0 commit comments

Comments
 (0)