Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/added-abandoned-hub-leaderboard.md
Original file line number Diff line number Diff line change
@@ -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)).
25 changes: 25 additions & 0 deletions src/cmd/hive/leaderboard_heartbeat.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 44 additions & 0 deletions src/cmd/hive/leaderboard_heartbeat_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
})
}
}
29 changes: 2 additions & 27 deletions src/cmd/hive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions src/docs/api-reference/hub-server.md
Original file line number Diff line number Diff line change
@@ -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/<file>:<line>` 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.


Expand Down
1 change: 1 addition & 0 deletions src/pkg/dashboard/contribute_leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions src/pkg/hub/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
83 changes: 83 additions & 0 deletions src/pkg/hub/leaderboard_abandoned_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
}
3 changes: 2 additions & 1 deletion src/pkg/hub/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/pkg/hub/spoke/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
3 changes: 3 additions & 0 deletions src/pkg/hub/static/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading