d8f395cb53
GET /api/agents inclut désormais last_metrics (dernière ligne de la table metrics) pour chaque agent. grid.js l'utilise lors du refresh initial, ce qui peuple les tuiles sans attendre le prochain message WebSocket. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
41 lines
940 B
Go
41 lines
940 B
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/user/nanometrics/server/db"
|
|
)
|
|
|
|
func AgentsHandler(database *db.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
agents, err := database.GetAgents()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
for i := range agents {
|
|
agents[i].LastMetrics, _ = database.GetLastMetrics(agents[i].ID)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(agents)
|
|
}
|
|
}
|
|
|
|
func DeleteAgentHandler(database *db.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
|
|
if len(parts) < 3 {
|
|
http.Error(w, "invalid path", 400)
|
|
return
|
|
}
|
|
agentID := parts[2]
|
|
if err := database.DeleteAgent(agentID); err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
}
|