diff --git a/db/nodes.go b/db/nodes.go index 3f9d5e27..38f79866 100644 --- a/db/nodes.go +++ b/db/nodes.go @@ -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) diff --git a/db/nodes_test.go b/db/nodes_test.go index a909b017..62e83c25 100644 --- a/db/nodes_test.go +++ b/db/nodes_test.go @@ -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) + } +} diff --git a/docs/docs.go b/docs/docs.go index 3ec4399d..635836f3 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -4930,6 +4930,10 @@ const docTemplate = `{ "knownNeighborCount": { "type": "integer" }, + "lastSeen": { + "description": "epoch ms", + "type": "integer" + }, "lat": { "description": "decimal degrees, from advert AppData", "type": "number" diff --git a/docs/swagger.json b/docs/swagger.json index 4684c63d..b05341c8 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -4928,6 +4928,10 @@ "knownNeighborCount": { "type": "integer" }, + "lastSeen": { + "description": "epoch ms", + "type": "integer" + }, "lat": { "description": "decimal degrees, from advert AppData", "type": "number" diff --git a/docs/swagger.yaml b/docs/swagger.yaml index dc38df23..9c9414d9 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -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 diff --git a/internal/api/handlers/nodes_test.go b/internal/api/handlers/nodes_test.go index 9a442f40..4aa66c2a 100644 --- a/internal/api/handlers/nodes_test.go +++ b/internal/api/handlers/nodes_test.go @@ -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) { diff --git a/internal/api/nodes.go b/internal/api/nodes.go index c32a3a77..ffb85280 100644 --- a/internal/api/nodes.go +++ b/internal/api/nodes.go @@ -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