22b429f247
- Grid: nouvel agent ajouté en temps réel dès le 1er paquet WebSocket (plus besoin d'actualiser la page) - Grid: ip/status mis à jour depuis chaque metrics_update (adresse DHCP fraîche) - WS: diffuse agent_removed lors de la suppression d'un agent (sync multi-onglets) - Popup détail: min/max RAM sur la période affichée (calculé depuis l'historique déjà chargé) - CSS: classe .chart-minmax pour l'affichage min/max sous le graphe Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/user/nanometrics/server/db"
|
|
"github.com/user/nanometrics/server/models"
|
|
)
|
|
|
|
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, broadcast func(interface{})) 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
|
|
}
|
|
broadcast(models.WSMessage{Type: "agent_removed", AgentID: agentID})
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
}
|