mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-12-06 07:24:19 +00:00
feat(api): send lastSeen on node list rows
This commit is contained in:
1 parent
3c5c249a83
commit
cb8f75fa73
7 files changed
+55
-3
No files matched your search
+2
-1
@@ -216,6 +216,7 @@ func (s *Store) nodeSummary(v sqlc.ListNodesRow) api.NodeSummary {
|
||||
DefaultScope: v.DefaultScopeName,
|
||||
KnownNeighborCount: v.KnownNeighborCount,
|
||||
NeighborIDs: v.NeighborIds,
|
||||
LastSeen: v.LastSeen.Time.UnixMilli(),
|
||||
Stale: v.LastSeen.Valid && v.LastSeen.Time.Before(time.Now().Add(-s.staleThreshold)),
|
||||
}
|
||||
if len(v.Iatas) > 0 {
|
||||
@@ -249,6 +250,7 @@ func (s *Store) GetNode(ctx context.Context, nodeID uuid.UUID) (*api.Node, error
|
||||
ObserverID: nullableUUID(row.ObserverID),
|
||||
DefaultScope: row.DefaultScopeName,
|
||||
KnownNeighborCount: row.KnownNeighborCount,
|
||||
LastSeen: row.LastSeen.Time.UnixMilli(),
|
||||
Stale: row.LastSeen.Valid && row.LastSeen.Time.Before(time.Now().Add(-s.staleThreshold)),
|
||||
},
|
||||
LocationSource: row.LocationSource,
|
||||
@@ -256,7 +258,6 @@ func (s *Store) GetNode(ctx context.Context, nodeID uuid.UUID) (*api.Node, error
|
||||
SupportsMultibyteTraces: row.SupportsMultibyteTraces,
|
||||
MinFirmwareVersion: row.MinFirmwareVersion,
|
||||
FirstSeen: row.FirstSeen.Time.UnixMilli(),
|
||||
LastSeen: row.LastSeen.Time.UnixMilli(),
|
||||
Metadata: row.Metadata,
|
||||
}
|
||||
neighbors, err := s.GetNodeNeighbors(ctx, nodeID)
|
||||
|
||||
@@ -811,3 +811,38 @@ func TestNodeListsCarryDefaultScope(t *testing.T) {
|
||||
t.Fatalf("sorted row scope %v, want %s", got, scope)
|
||||
}
|
||||
}
|
||||
|
||||
// List rows carry last_seen in epoch ms, as node detail does.
|
||||
func TestNodeListsCarryLastSeen(t *testing.T) {
|
||||
const ms = int64(1792442625019)
|
||||
seen := pgtype.Timestamptz{Time: time.UnixMicro(ms*1000 + 123).UTC(), Valid: true}
|
||||
id := uuid.New()
|
||||
ctrl := gomock.NewController(t)
|
||||
mock := mockdb.NewMockQuerier(ctrl)
|
||||
mock.EXPECT().ListNodes(gomock.Any(), gomock.Any()).Return([]sqlc.ListNodesRow{{ID: uuid.New(), PublicKey: []byte{1}, LastSeen: seen}}, nil)
|
||||
mock.EXPECT().ListNodesSorted(gomock.Any(), gomock.Any()).Return([]sqlc.ListNodesSortedRow{{ID: uuid.New(), PublicKey: []byte{2}, LastSeen: seen}}, nil)
|
||||
mock.EXPECT().GetNodeByID(gomock.Any(), id).Return(sqlc.GetNodeByIDRow{ID: id, PublicKey: []byte{3}, LastSeen: seen}, nil)
|
||||
mock.EXPECT().GetNodeNeighbors(gomock.Any(), id).Return([]sqlc.GetNodeNeighborsRow{}, nil)
|
||||
store := &Store{q: mock}
|
||||
page, err := store.ListNodes(context.Background(), 0, nil, nil, nil, nil, "", "", "", 0, nil, 50, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := page.Items[0].LastSeen; got != ms {
|
||||
t.Fatalf("list lastSeen %d, want %d", got, ms)
|
||||
}
|
||||
sortedPage, err := store.ListNodesSorted(context.Background(), 0, nil, nil, nil, nil, "", "", "", api.NodeSort{By: api.NodeSortName}, nil, 50, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := sortedPage.Items[0].LastSeen; got != ms {
|
||||
t.Fatalf("sorted lastSeen %d, want %d", got, ms)
|
||||
}
|
||||
node, err := store.GetNode(context.Background(), id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if node.LastSeen != ms {
|
||||
t.Fatalf("detail lastSeen %d, want %d", node.LastSeen, ms)
|
||||
}
|
||||
}
|
||||
@@ -4930,6 +4930,10 @@ const docTemplate = `{
|
||||
"knownNeighborCount": {
|
||||
"type": "integer"
|
||||
},
|
||||
"lastSeen": {
|
||||
"description": "epoch ms",
|
||||
"type": "integer"
|
||||
},
|
||||
"lat": {
|
||||
"description": "decimal degrees, from advert AppData",
|
||||
"type": "number"
|
||||
|
||||
@@ -4928,6 +4928,10 @@
|
||||
"knownNeighborCount": {
|
||||
"type": "integer"
|
||||
},
|
||||
"lastSeen": {
|
||||
"description": "epoch ms",
|
||||
"type": "integer"
|
||||
},
|
||||
"lat": {
|
||||
"description": "decimal degrees, from advert AppData",
|
||||
"type": "number"
|
||||
|
||||
@@ -634,6 +634,9 @@ definitions:
|
||||
type: boolean
|
||||
knownNeighborCount:
|
||||
type: integer
|
||||
lastSeen:
|
||||
description: epoch ms
|
||||
type: integer
|
||||
lat:
|
||||
description: decimal degrees, from advert AppData
|
||||
type: number
|
||||
|
||||
@@ -247,7 +247,7 @@ func TestGetNode_OK(t *testing.T) {
|
||||
r := chi.NewRouter()
|
||||
r.Get("/nodes/{nodeId}", getNode(stubReader{
|
||||
getNode: func(_ context.Context, id uuid.UUID) (*api.Node, error) {
|
||||
return &api.Node{NodeSummary: api.NodeSummary{ID: id}}, nil
|
||||
return &api.Node{NodeSummary: api.NodeSummary{ID: id, LastSeen: 1792442625019}, FirstSeen: 1792442600000}, nil
|
||||
},
|
||||
}))
|
||||
req := httptest.NewRequest(http.MethodGet, "/nodes/"+nodeID.String(), nil)
|
||||
@@ -259,6 +259,11 @@ func TestGetNode_OK(t *testing.T) {
|
||||
if strings.Contains(w.Body.String(), `"meshmapper"`) {
|
||||
t.Error("meshmapper should be omitted when MeshMapper doesn't list the node")
|
||||
}
|
||||
// lastSeen moved into the embedded summary: detail still sends it once.
|
||||
if body := w.Body.String(); strings.Count(body, `"lastSeen":`) != 1 || !strings.Contains(body, `"lastSeen":1792442625019`) ||
|
||||
!strings.Contains(body, `"firstSeen":1792442600000`) {
|
||||
t.Errorf("node detail times: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetNode_MeshMapper(t *testing.T) {
|
||||
|
||||
@@ -76,6 +76,7 @@ type NodeSummary struct {
|
||||
DefaultScope *string `json:"defaultScope,omitempty"` // most recently matched transport scope name e.g. "#bc"
|
||||
KnownNeighborCount int64 `json:"knownNeighborCount"`
|
||||
NeighborIDs []uuid.UUID `json:"neighborIds,omitempty"` // only populated when the list request opts in; see ?neighbors=true
|
||||
LastSeen int64 `json:"lastSeen"` // epoch ms
|
||||
// Stale is true when the node hasn't been seen (last_seen) within the configured
|
||||
// staleness window (default 24h; internal/config.ResolvedConfig.NodeStaleThreshold).
|
||||
// Applies to every node type, unlike ClockDriftSeconds/ClockOutOfSync on Node, which
|
||||
@@ -96,7 +97,6 @@ type Node struct {
|
||||
SupportsMultibyteTraces bool `json:"supportsMultibyteTraces"` // firmware >= 1.11.0; detected via trace hash size
|
||||
MinFirmwareVersion *string `json:"minFirmwareVersion,omitempty"` // derived from capability flags
|
||||
FirstSeen int64 `json:"firstSeen"` // epoch ms
|
||||
LastSeen int64 `json:"lastSeen"` // epoch ms
|
||||
Metadata any `json:"metadata,omitempty"` // raw JSONB metadata
|
||||
Neighbors []NodeNeighbor `json:"neighbors"`
|
||||
// Clock drift, repeaters/room servers only (nodeType 2/3); omitted entirely for other
|
||||
|
||||
Reference in new issue
Block a user