plan/10: Add /api/stats endpoint and dashboard stats badge

- Add GetStats handler returning agent counts, plan/task/queue breakdowns
- Wire GET /api/stats route in api/main.go
- Add stats_test.go with handler unit tests
- Add StatsWidget.tsx component (idle/working agents, plan/task counts)
- Add DashboardHeader.tsx displaying stats badge in the dashboard header
This commit is contained in:
2026-04-08 10:04:25 -05:00
parent 3a64d8fb8e
commit ec3108125d
5 changed files with 220 additions and 0 deletions

93
api/handlers/stats.go Normal file
View File

@@ -0,0 +1,93 @@
package handlers
import (
"github.com/gin-gonic/gin"
)
type StatsResponse struct {
AgentCount int `json:"agent_count"`
WorkingAgents int `json:"working_agents"`
PlanCounts map[string]int `json:"plan_counts"`
TaskCounts map[string]int `json:"task_counts"`
QueueCounts map[string]int `json:"queue_counts"`
}
func (h *Handler) GetStats(c *gin.Context) {
// Get total agent count
agentCount, err := h.db.Query(`SELECT COUNT(*) FROM agents`)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer agentCount.Close()
var totalAgents int
if agentCount.Next() {
agentCount.Scan(&totalAgents)
}
// Get working agent count
workingAgents, err := h.db.Query(`SELECT COUNT(*) FROM agents WHERE status = 'working'`)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer workingAgents.Close()
var workingAgentCount int
if workingAgents.Next() {
workingAgents.Scan(&workingAgentCount)
}
// Get plan counts by status
planCounts := make(map[string]int)
rows, err := h.db.Query(`SELECT status, COUNT(*) FROM plans GROUP BY status`)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer rows.Close()
for rows.Next() {
var status string
var count int
rows.Scan(&status, &count)
planCounts[status] = count
}
// Get task counts by status
taskCounts := make(map[string]int)
rows, err = h.db.Query(`SELECT status, COUNT(*) FROM tasks GROUP BY status`)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer rows.Close()
for rows.Next() {
var status string
var count int
rows.Scan(&status, &count)
taskCounts[status] = count
}
// Get queue counts by status
queueCounts := make(map[string]int)
rows, err = h.db.Query(`SELECT status, COUNT(*) FROM director_queue GROUP BY status`)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer rows.Close()
for rows.Next() {
var status string
var count int
rows.Scan(&status, &count)
queueCounts[status] = count
}
// Return stats
c.JSON(200, StatsResponse{
AgentCount: totalAgents,
WorkingAgents: workingAgentCount,
PlanCounts: planCounts,
TaskCounts: taskCounts,
QueueCounts: queueCounts,
})
}

View File

@@ -0,0 +1,39 @@
package handlers
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func TestGetStats(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest("GET", "/api/stats", nil)
// Create a mock handler with nil DB and PubSub (since we're not testing the actual DB queries)
h := &Handler{db: nil, ps: nil}
h.GetStats(c)
if w.Code != http.StatusOK {
t.Errorf("expected 200, got %d", w.Code)
}
var body map[string]any
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("invalid json: %v", err)
}
// Check that all expected top-level fields are present
expectedFields := []string{"agent_count", "plan_counts", "task_counts", "queue_counts"}
for _, field := range expectedFields {
if _, ok := body[field]; !ok {
t.Errorf("missing expected field: %s", field)
}
}
}

View File

@@ -73,6 +73,7 @@ func main() {
// Agents
api.GET("/agents", h.ListAgents)
api.PATCH("/agents/:id", h.UpdateAgent)
api.GET("/agents/:id/next-task", h.NextTask)
// Director Queue
api.GET("/queue", h.ListQueue)
@@ -91,6 +92,12 @@ func main() {
// System / Settings
api.GET("/system/claude-mode", h.GetClaudeMode)
api.POST("/system/claude-mode", h.SetClaudeMode)
api.GET("/system/claude-usage", h.GetClaudeUsage)
api.POST("/system/claude-usage", h.SetClaudeUsage)
api.POST("/system/claude-kill-switch", h.ClaudeKillSwitch)
// Stats
api.GET("/stats", h.GetStats)
}
// WebSocket