Skip to content

Commit 7caa355

Browse files
committed
respect the data shape of the response
1 parent d1558b4 commit 7caa355

File tree

6 files changed

+28
-47
lines changed

6 files changed

+28
-47
lines changed

node-registrar/client/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Node struct {
3131
SerialNumber string `json:"serial_number"`
3232
UptimeReports []UptimeReport `json:"uptime"`
3333
LastSeen *time.Time `json:"last_seen"`
34+
Online bool `json:"online"`
3435
Approved bool
3536
}
3637

node-registrar/docs/docs.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,7 @@ const docTemplate = `{
503503
"schema": {
504504
"type": "array",
505505
"items": {
506-
"type": "object",
507-
"additionalProperties": true
506+
"$ref": "#/definitions/db.Node"
508507
}
509508
}
510509
},
@@ -607,8 +606,7 @@ const docTemplate = `{
607606
"200": {
608607
"description": "Node details with online status and last_seen information",
609608
"schema": {
610-
"type": "object",
611-
"additionalProperties": true
609+
"$ref": "#/definitions/db.Node"
612610
}
613611
},
614612
"400": {
@@ -1008,6 +1006,10 @@ const docTemplate = `{
10081006
"node_id": {
10091007
"type": "integer"
10101008
},
1009+
"online": {
1010+
"description": "Computed field, not stored in database",
1011+
"type": "boolean"
1012+
},
10111013
"resources": {
10121014
"description": "PublicConfig PublicConfig ` + "`" + `json:\"public_config\" gorm:\"type:json\"` + "`" + `",
10131015
"allOf": [

node-registrar/docs/swagger.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,7 @@
496496
"schema": {
497497
"type": "array",
498498
"items": {
499-
"type": "object",
500-
"additionalProperties": true
499+
"$ref": "#/definitions/db.Node"
501500
}
502501
}
503502
},
@@ -600,8 +599,7 @@
600599
"200": {
601600
"description": "Node details with online status and last_seen information",
602601
"schema": {
603-
"type": "object",
604-
"additionalProperties": true
602+
"$ref": "#/definitions/db.Node"
605603
}
606604
},
607605
"400": {
@@ -1001,6 +999,10 @@
1001999
"node_id": {
10021000
"type": "integer"
10031001
},
1002+
"online": {
1003+
"description": "Computed field, not stored in database",
1004+
"type": "boolean"
1005+
},
10041006
"resources": {
10051007
"description": "PublicConfig PublicConfig `json:\"public_config\" gorm:\"type:json\"`",
10061008
"allOf": [

node-registrar/docs/swagger.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ definitions:
9999
$ref: '#/definitions/db.Location'
100100
node_id:
101101
type: integer
102+
online:
103+
description: Computed field, not stored in database
104+
type: boolean
102105
resources:
103106
allOf:
104107
- $ref: '#/definitions/db.Resources'
@@ -581,8 +584,7 @@ paths:
581584
description: List of nodes with online status
582585
schema:
583586
items:
584-
additionalProperties: true
585-
type: object
587+
$ref: '#/definitions/db.Node'
586588
type: array
587589
"400":
588590
description: Bad request
@@ -652,8 +654,7 @@ paths:
652654
"200":
653655
description: Node details with online status and last_seen information
654656
schema:
655-
additionalProperties: true
656-
type: object
657+
$ref: '#/definitions/db.Node'
657658
"400":
658659
description: Invalid node ID
659660
schema:

node-registrar/pkg/db/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Node struct {
4949

5050
UptimeReports []UptimeReport `json:"uptime" gorm:"foreignKey:NodeID;references:NodeID;constraint:OnDelete:CASCADE"`
5151
LastSeen *time.Time `json:"last_seen" gorm:"index"` // Last time the node sent an uptime report
52+
Online bool `json:"online" gorm:"-"` // Computed field, not stored in database
5253

5354
CreatedAt time.Time `json:"created_at"`
5455
UpdatedAt time.Time `json:"updated_at"`

node-registrar/pkg/server/handlers.go

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (s Server) updateFarmHandler(c *gin.Context) {
215215
// @Param last_seen query int false "Filter nodes last seen within this many minutes"
216216
// @Param page query int false "Page number" default(1)
217217
// @Param size query int false "Results per page" default(10)
218-
// @Success 200 {object} []map[string]interface{} "List of nodes with online status"
218+
// @Success 200 {object} []db.Node "List of nodes with online status"
219219
// @Failure 400 {object} map[string]any "Bad request"
220220
// @Router /nodes [get]
221221
func (s Server) listNodesHandler(c *gin.Context) {
@@ -234,28 +234,15 @@ func (s Server) listNodesHandler(c *gin.Context) {
234234
return
235235
}
236236

237-
// Enhance nodes with online status information
238-
response := make([]gin.H, len(nodes))
237+
// Set online status for each node
239238
cutoffTime := time.Now().Add(-30 * time.Minute)
240-
241-
for i, node := range nodes {
242-
// Determine if the node is online
243-
isOnline := false
244-
var lastSeenFormatted string
245-
246-
if node.LastSeen != nil {
247-
isOnline = node.LastSeen.After(cutoffTime)
248-
lastSeenFormatted = node.LastSeen.Format(time.RFC3339)
249-
}
250-
251-
response[i] = gin.H{
252-
"node": node,
253-
"online": isOnline,
254-
"last_seen": lastSeenFormatted,
239+
for i := range nodes {
240+
if nodes[i].LastSeen != nil {
241+
nodes[i].Online = nodes[i].LastSeen.After(cutoffTime)
255242
}
256243
}
257244

258-
c.JSON(http.StatusOK, response)
245+
c.JSON(http.StatusOK, nodes)
259246
}
260247

261248
// @Summary Get node details
@@ -264,7 +251,7 @@ func (s Server) listNodesHandler(c *gin.Context) {
264251
// @Accept json
265252
// @Produce json
266253
// @Param node_id path int true "Node ID"
267-
// @Success 200 {object} map[string]interface{} "Node details with online status and last_seen information"
254+
// @Success 200 {object} db.Node "Node details with online status and last_seen information"
268255
// @Failure 400 {object} map[string]any "Invalid node ID"
269256
// @Failure 404 {object} map[string]any "Node not found"
270257
// @Router /nodes/{node_id} [get]
@@ -289,26 +276,13 @@ func (s Server) getNodeHandler(c *gin.Context) {
289276
}
290277

291278
// Determine if the node is online (has sent an uptime report in the last 30 minutes)
292-
isOnline := false
293-
var lastSeenFormatted string
294-
295279
if node.LastSeen != nil {
296280
// Calculate if the node is online (last seen within the last 30 minutes)
297281
cutoffTime := time.Now().Add(-30 * time.Minute)
298-
isOnline = node.LastSeen.After(cutoffTime)
299-
300-
// Format the last seen time
301-
lastSeenFormatted = node.LastSeen.Format(time.RFC3339)
302-
}
303-
304-
// Create a response with the node data plus online status
305-
response := gin.H{
306-
"node": node,
307-
"online": isOnline,
308-
"last_seen": lastSeenFormatted,
282+
node.Online = node.LastSeen.After(cutoffTime)
309283
}
310284

311-
c.JSON(http.StatusOK, response)
285+
c.JSON(http.StatusOK, node)
312286
}
313287

314288
type NodeRegistrationRequest struct {

0 commit comments

Comments
 (0)