From 315d07a1a344a163a6b5c9fc2f71e71853d0e3cb Mon Sep 17 00:00:00 2001 From: mendezr Date: Fri, 30 Oct 2026 19:02:27 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feature:=20carry=20abandoned=20coun?= =?UTF-8?q?ts=20through=20the=20hub=20leaderboard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Share heartbeat and task-status row conversion, merge abandoned counts across public spokes, and document the additive wire field. Include the prerequisite dashboard row field shared with #15859; profile population remains in that PR. Hive-Run: hivecommons/hive#15799 Hive-Plan: 15799-heartbeat-and-hub-leaderboard Hive-Spec: issue-15799#acceptance Signed-off-by: mendezr --- .../added-abandoned-hub-leaderboard.md | 1 + src/cmd/hive/leaderboard_heartbeat.go | 25 ++++++ src/cmd/hive/leaderboard_heartbeat_test.go | 44 ++++++++++ src/cmd/hive/main.go | 29 +------ src/docs/api-reference/hub-server.md | 13 +++ src/pkg/dashboard/contribute_leaderboard.go | 1 + src/pkg/hub/heartbeat.go | 1 + src/pkg/hub/leaderboard_abandoned_test.go | 83 +++++++++++++++++++ src/pkg/hub/server.go | 3 +- src/pkg/hub/spoke/heartbeat.go | 1 + src/pkg/hub/static/openapi.yaml | 3 + 11 files changed, 176 insertions(+), 28 deletions(-) create mode 100644 changelog.d/added-abandoned-hub-leaderboard.md create mode 100644 src/cmd/hive/leaderboard_heartbeat.go create mode 100644 src/cmd/hive/leaderboard_heartbeat_test.go create mode 100644 src/pkg/hub/leaderboard_abandoned_test.go diff --git a/changelog.d/added-abandoned-hub-leaderboard.md b/changelog.d/added-abandoned-hub-leaderboard.md new file mode 100644 index 0000000000..3599b13e9c --- /dev/null +++ b/changelog.d/added-abandoned-hub-leaderboard.md @@ -0,0 +1 @@ +- Carry contributor abandoned PR counts through spoke heartbeats and task-status pushes into the hub's merged leaderboard, treating older spokes as zero ([#15799](https://github.com/hivecommons/hive/issues/15799)). diff --git a/src/cmd/hive/leaderboard_heartbeat.go b/src/cmd/hive/leaderboard_heartbeat.go new file mode 100644 index 0000000000..b7564523fd --- /dev/null +++ b/src/cmd/hive/leaderboard_heartbeat.go @@ -0,0 +1,25 @@ +package main + +import ( + "github.com/hivecommons/hive/pkg/dashboard" + "github.com/hivecommons/hive/pkg/hub/spoke" +) + +// leaderboardForSpoke keeps the full heartbeat and lightweight task-status push +// on the same leaderboard wire shape. +func leaderboardForSpoke(entries []dashboard.LeaderboardEntry) []spoke.LeaderboardEntry { + out := make([]spoke.LeaderboardEntry, len(entries)) + for i, e := range entries { + out[i] = spoke.LeaderboardEntry{ + GitHubUsername: e.GitHubUsername, + AvatarURL: e.AvatarURL, + TrustTier: e.TrustTier, + TasksCompleted: e.TasksCompleted, + TasksFailed: e.TasksFailed, + Abandoned: e.Abandoned, + Active: e.Active, + CurrentTask: e.CurrentTask, + } + } + return out +} diff --git a/src/cmd/hive/leaderboard_heartbeat_test.go b/src/cmd/hive/leaderboard_heartbeat_test.go new file mode 100644 index 0000000000..f15922161a --- /dev/null +++ b/src/cmd/hive/leaderboard_heartbeat_test.go @@ -0,0 +1,44 @@ +package main + +import ( + "encoding/json" + "testing" + + "github.com/hivecommons/hive/pkg/dashboard" + "github.com/hivecommons/hive/pkg/hub/spoke" +) + +func TestLeaderboardForSpokeAbandonedSerialization(t *testing.T) { + entries := leaderboardForSpoke([]dashboard.LeaderboardEntry{ + {GitHubUsername: "alice", TasksCompleted: 7, TasksFailed: 1, Abandoned: 3}, + {GitHubUsername: "bob"}, + }) + for name, payload := range map[string]any{ + "heartbeat": spoke.HeartbeatPayload{Leaderboard: entries}, + "task-status": spoke.TaskStatusPayload{Leaderboard: entries}, + } { + t.Run(name, func(t *testing.T) { + data, err := json.Marshal(payload) + if err != nil { + t.Fatal(err) + } + var decoded struct { + Leaderboard []struct { + Abandoned *int `json:"abandoned"` + } `json:"leaderboard"` + } + if err := json.Unmarshal(data, &decoded); err != nil { + t.Fatal(err) + } + if len(decoded.Leaderboard) != 2 { + t.Fatalf("leaderboard = %s", data) + } + for i, want := range []int{3, 0} { + got := decoded.Leaderboard[i].Abandoned + if got == nil || *got != want { + t.Errorf("row %d abandoned = %v, want %d; JSON: %s", i, got, want, data) + } + } + }) + } +} diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index d5ad531f8e..aa7f75b504 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -979,20 +979,7 @@ func (b *boot) wireBootClosures() { } b.leaderboardForHeartbeat = func() []spoke.LeaderboardEntry { - lb := b.dashSrv.LeaderboardForHub() - out := make([]spoke.LeaderboardEntry, len(lb)) - for i, e := range lb { - out[i] = spoke.LeaderboardEntry{ - GitHubUsername: e.GitHubUsername, - AvatarURL: e.AvatarURL, - TrustTier: e.TrustTier, - TasksCompleted: e.TasksCompleted, - TasksFailed: e.TasksFailed, - Active: e.Active, - CurrentTask: e.CurrentTask, - } - } - return out + return leaderboardForSpoke(b.dashSrv.LeaderboardForHub()) } var ownerHeartbeatTokenPath string @@ -5619,19 +5606,7 @@ func (b *boot) bootHeartbeatWith(deps bootHeartbeatDeps) { if b.cfg.Hub.TaskStatusPushEnabled() { deps.startTaskStatusPush(b.ctx, b.hubURL, func() *spoke.TaskStatusPayload { reg, active := b.dashSrv.ContributorSummary() - lb := b.dashSrv.LeaderboardForHub() - out := make([]spoke.LeaderboardEntry, len(lb)) - for i, e := range lb { - out[i] = spoke.LeaderboardEntry{ - GitHubUsername: e.GitHubUsername, - AvatarURL: e.AvatarURL, - TrustTier: e.TrustTier, - TasksCompleted: e.TasksCompleted, - TasksFailed: e.TasksFailed, - Active: e.Active, - CurrentTask: e.CurrentTask, - } - } + out := leaderboardForSpoke(b.dashSrv.LeaderboardForHub()) return &spoke.TaskStatusPayload{ HiveID: b.cfg.HiveID, Leaderboard: out, diff --git a/src/docs/api-reference/hub-server.md b/src/docs/api-reference/hub-server.md index 1dc90cdfc7..27e15153fd 100644 --- a/src/docs/api-reference/hub-server.md +++ b/src/docs/api-reference/hub-server.md @@ -1,5 +1,18 @@ # Dashboard REST API reference — Hub server +## Merged contributor leaderboard + +`GET /api/hub/leaderboard` returns `{"leaderboard": [...]}`. Rows merge +contributors by `github_username` across public spokes, summing +`tasks_completed`, `tasks_failed`, and `abandoned`. The integer `abandoned` +field counts abandoned PRs; older spokes that omit it contribute zero, and +merged rows explicitly include `"abandoned": 0` when none are reported. +Both full heartbeats and lightweight task-status pushes carry this field. +Private hives, agent rows, and empty usernames are excluded. Contributors +with only abandoned PRs remain visible; rows with no counts and no activity +are omitted. The added count does not change ranking. + + Part of the [Dashboard REST API reference](../api-reference.md); see that page for how auth levels and `pkg/:` source citations are read and checked. Edit this file for routes in this area and run `src/scripts/check-api-reference-citations.sh --fix` to resync citations. diff --git a/src/pkg/dashboard/contribute_leaderboard.go b/src/pkg/dashboard/contribute_leaderboard.go index 113af97ab0..2454930ab6 100644 --- a/src/pkg/dashboard/contribute_leaderboard.go +++ b/src/pkg/dashboard/contribute_leaderboard.go @@ -17,6 +17,7 @@ type LeaderboardEntry struct { TrustTier string `json:"trust_tier"` TasksCompleted int `json:"tasks_completed"` TasksFailed int `json:"tasks_failed"` + Abandoned int `json:"abandoned"` Findings int `json:"findings,omitempty"` RegisteredAt string `json:"registered_at"` Team *ContributorTeamMetadata `json:"team,omitempty"` diff --git a/src/pkg/hub/heartbeat.go b/src/pkg/hub/heartbeat.go index 6602b1c29b..a24e4b9a04 100644 --- a/src/pkg/hub/heartbeat.go +++ b/src/pkg/hub/heartbeat.go @@ -167,6 +167,7 @@ type LeaderboardEntry struct { TrustTier string `json:"trust_tier"` TasksCompleted int `json:"tasks_completed"` TasksFailed int `json:"tasks_failed"` + Abandoned int `json:"abandoned"` Active bool `json:"active"` CurrentTask string `json:"current_task,omitempty"` HiveName string `json:"hive_name,omitempty"` diff --git a/src/pkg/hub/leaderboard_abandoned_test.go b/src/pkg/hub/leaderboard_abandoned_test.go new file mode 100644 index 0000000000..d92073b3d9 --- /dev/null +++ b/src/pkg/hub/leaderboard_abandoned_test.go @@ -0,0 +1,83 @@ +package hub + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" +) + +func abandonedLeaderboardServer(t *testing.T) *HubServer { + t.Helper() + // Decode the wire payloads, including an older spoke without abandoned. + payloads := []string{ + `{"leaderboard":[{"github_username":"alice","tasks_completed":5,"tasks_failed":1,"abandoned":2},{"github_username":"only-abandoned","abandoned":4}]}`, + `{"leaderboard":[{"github_username":"alice","tasks_completed":3,"abandoned":3}]}`, + `{"leaderboard":[{"github_username":"alice","tasks_completed":1},{"github_username":"legacy","tasks_completed":2}]}`, + } + s := newTestHubServer("") + for _, raw := range payloads { + var p HeartbeatPayload + if err := json.Unmarshal([]byte(raw), &p); err != nil { + t.Fatal(err) + } + s.registry.Hives = append(s.registry.Hives, RegistryEntry{IsPublic: true, Leaderboard: p.Leaderboard}) + } + s.registry.Hives = append(s.registry.Hives, + RegistryEntry{IsPublic: false, Leaderboard: []LeaderboardEntry{{GitHubUsername: "alice", Abandoned: 100}}}, + RegistryEntry{IsPublic: true, Leaderboard: []LeaderboardEntry{ + {GitHubUsername: "agent", TrustTier: "agent", Abandoned: 100}, + {GitHubUsername: "", Abandoned: 100}, + {GitHubUsername: "empty"}, + }}, + ) + return s +} + +func TestMergeLeaderboardsAbandoned(t *testing.T) { + s := abandonedLeaderboardServer(t) + rows := s.mergeLeaderboards() + if len(rows) != 3 { + t.Fatalf("merged rows = %+v, want alice, legacy, only-abandoned", rows) + } + want := map[string]int{"alice": 5, "legacy": 0, "only-abandoned": 4} + for _, row := range rows { + count, ok := want[row.GitHubUsername] + if !ok || row.Abandoned != count { + t.Errorf("unexpected merged row: %+v", row) + } + if row.GitHubUsername == "alice" && (row.TasksCompleted != 9 || row.TasksFailed != 1) { + t.Errorf("existing counts changed: %+v", row) + } + } +} + +func TestLeaderboardEndpointAbandonedJSON(t *testing.T) { + s := abandonedLeaderboardServer(t) + mux := http.NewServeMux() + mux.HandleFunc("GET /api/hub/leaderboard", s.handleLeaderboard) + w := httptest.NewRecorder() + mux.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/hub/leaderboard", nil)) + if w.Code != http.StatusOK { + t.Fatalf("status = %d: %s", w.Code, w.Body.String()) + } + var response struct { + Leaderboard []struct { + Login string `json:"github_username"` + Abandoned *int `json:"abandoned"` + } `json:"leaderboard"` + } + if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil { + t.Fatal(err) + } + if len(response.Leaderboard) != 3 { + t.Fatalf("unexpected response: %s", w.Body.String()) + } + want := map[string]int{"alice": 5, "legacy": 0, "only-abandoned": 4} + for _, row := range response.Leaderboard { + count, ok := want[row.Login] + if !ok || row.Abandoned == nil || *row.Abandoned != count { + t.Errorf("incorrect abandoned count for %q: %s", row.Login, w.Body.String()) + } + } +} diff --git a/src/pkg/hub/server.go b/src/pkg/hub/server.go index 33926d44b5..116b3e65e1 100644 --- a/src/pkg/hub/server.go +++ b/src/pkg/hub/server.go @@ -4452,6 +4452,7 @@ func (s *HubServer) mergeLeaderboards() []LeaderboardEntry { if existing, ok := merged[lb.GitHubUsername]; ok { existing.TasksCompleted += lb.TasksCompleted existing.TasksFailed += lb.TasksFailed + existing.Abandoned += lb.Abandoned if lb.Active { existing.Active = true existing.CurrentTask = lb.CurrentTask @@ -4465,7 +4466,7 @@ func (s *HubServer) mergeLeaderboards() []LeaderboardEntry { } result := make([]LeaderboardEntry, 0, len(merged)) for _, v := range merged { - if v.TasksCompleted == 0 && v.TasksFailed == 0 && !v.Active { + if v.TasksCompleted == 0 && v.TasksFailed == 0 && v.Abandoned == 0 && !v.Active { continue } result = append(result, *v) diff --git a/src/pkg/hub/spoke/heartbeat.go b/src/pkg/hub/spoke/heartbeat.go index 9a019dc83e..f42c67607e 100644 --- a/src/pkg/hub/spoke/heartbeat.go +++ b/src/pkg/hub/spoke/heartbeat.go @@ -560,6 +560,7 @@ type LeaderboardEntry struct { TrustTier string `json:"trust_tier"` TasksCompleted int `json:"tasks_completed"` TasksFailed int `json:"tasks_failed"` + Abandoned int `json:"abandoned"` Active bool `json:"active"` CurrentTask string `json:"current_task,omitempty"` HiveName string `json:"hive_name,omitempty"` diff --git a/src/pkg/hub/static/openapi.yaml b/src/pkg/hub/static/openapi.yaml index 8d1c1e6ed0..6a486b95aa 100644 --- a/src/pkg/hub/static/openapi.yaml +++ b/src/pkg/hub/static/openapi.yaml @@ -557,6 +557,9 @@ components: type: integer tasks_failed: type: integer + abandoned: + type: integer + description: Abandoned PR count summed per GitHub username across public spokes. Older spokes that omit this field contribute zero. active: type: boolean current_task: