Add hostname editing feature to agent detail page
- Create enhanced agent detail template with all stats and hostname edit form
- Add UpdateHostname method to store for agent hostname updates
- Add UpdateAgentHostname handler in UI for web form submissions
- Add UpdateHostname endpoint in API handler for JSON requests
- Register new POST /agents/{id}/hostname route for hostname updates
- Improve agent detail page layout with Settings and Danger Zone sections
- Add error handling for missing agents and empty hostname values
This commit is contained in:
@@ -45,6 +45,7 @@ func main() {
|
||||
protectedRoutes.Use(authMgr.Middleware)
|
||||
protectedRoutes.Get("/", uiHandler.Dashboard)
|
||||
protectedRoutes.Get("/agents/{id}", uiHandler.AgentDetail)
|
||||
protectedRoutes.Post("/agents/{id}/hostname", uiHandler.UpdateAgentHostname)
|
||||
protectedRoutes.Post("/logout", uiHandler.Logout)
|
||||
protectedRoutes.Post("/api/agents/remove-stale", uiHandler.RemoveStaleAgents)
|
||||
protectedRoutes.Post("/api/agents/{id}/delete", uiHandler.DeleteAgent)
|
||||
|
||||
@@ -73,3 +73,32 @@ func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "deleted"})
|
||||
}
|
||||
|
||||
// UpdateHostname updates the hostname for an agent.
|
||||
func (h *Handler) UpdateHostname(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
agentID := r.PathValue("id")
|
||||
if agentID == "" {
|
||||
http.Error(w, "missing agent id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
hostname := r.FormValue("hostname")
|
||||
if hostname == "" {
|
||||
http.Error(w, "missing hostname", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := h.store.UpdateHostname(agentID, hostname)
|
||||
if err != nil {
|
||||
http.Error(w, "agent not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "updated"})
|
||||
}
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"nerd-monitor/internal/stats"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrAgentNotFound is returned when an agent is not found.
|
||||
ErrAgentNotFound = errors.New("agent not found")
|
||||
)
|
||||
|
||||
// AgentStats represents the latest stats for an agent.
|
||||
type AgentStats struct {
|
||||
ID string
|
||||
@@ -76,3 +82,17 @@ func (s *Store) DeleteAgent(id string) {
|
||||
|
||||
delete(s.agents, id)
|
||||
}
|
||||
|
||||
// UpdateHostname updates the hostname for an agent.
|
||||
func (s *Store) UpdateHostname(id string, hostname string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
agent, exists := s.agents[id]
|
||||
if !exists {
|
||||
return ErrAgentNotFound
|
||||
}
|
||||
|
||||
agent.Hostname = hostname
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -133,6 +133,30 @@ func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// UpdateAgentHostname handles hostname updates for agents.
|
||||
func (h *Handler) UpdateAgentHostname(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
agentID := r.PathValue("id")
|
||||
hostname := r.FormValue("hostname")
|
||||
|
||||
if hostname == "" {
|
||||
http.Error(w, "Hostname cannot be empty", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := h.store.UpdateHostname(agentID, hostname)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/agents/"+agentID, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// getStaleAgents returns agents that haven't reported in 6 months.
|
||||
func (h *Handler) getStaleAgents() []*store.AgentStats {
|
||||
const staleThreshold = 6 * 30 * 24 * time.Hour
|
||||
|
||||
@@ -17,6 +17,7 @@ templ agentDetailContent(agent *store.AgentStats) {
|
||||
@AgentStatusBadge(agent.LastSeen)
|
||||
</div>
|
||||
|
||||
<!-- CPU Usage Card -->
|
||||
<div class="grid" style="grid-template-columns: 1fr;">
|
||||
<div class="card">
|
||||
<div class="card-title">CPU Usage</div>
|
||||
@@ -32,6 +33,7 @@ templ agentDetailContent(agent *store.AgentStats) {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Memory and Disk Usage Cards -->
|
||||
<div class="grid" style="margin-top: 1.5rem;">
|
||||
<div class="card">
|
||||
<div class="card-title">Memory Usage</div>
|
||||
@@ -43,24 +45,49 @@ templ agentDetailContent(agent *store.AgentStats) {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Agent Information and Settings -->
|
||||
<div class="card" style="margin-top: 1.5rem;">
|
||||
<div class="card-title">Agent Information</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Agent ID</span>
|
||||
<span class="metric-value" style="font-family: monospace; font-size: 0.875rem;">{ agent.ID }</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Hostname</span>
|
||||
<span class="metric-value">{ agent.Hostname }</span>
|
||||
</div>
|
||||
<div class="metric-row">
|
||||
<span class="metric-label">Last Seen</span>
|
||||
<span class="metric-value">{ agent.LastSeen.Format("2006-01-02 15:04:05") }</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="btn-group" style="margin-top: 1.5rem;">
|
||||
<form method="POST" action={ templ.SafeURL(fmt.Sprintf("/api/agents/%s/delete", agent.ID)) } style="margin: 0;" onsubmit="return confirm('Are you sure you want to remove this agent?');">
|
||||
<!-- Hostname Settings -->
|
||||
<div class="card" style="margin-top: 1.5rem;">
|
||||
<div class="card-title">Settings</div>
|
||||
<form method="POST" action={ templ.SafeURL(fmt.Sprintf("/agents/%s/hostname", agent.ID)) } style="display: flex; flex-direction: column; gap: 1rem;">
|
||||
<div style="display: flex; flex-direction: column; gap: 0.5rem;">
|
||||
<label for="hostname" style="font-size: 0.875rem; font-weight: 500; color: #cbd5e1;">
|
||||
Hostname
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
id="hostname"
|
||||
name="hostname"
|
||||
value={ agent.Hostname }
|
||||
required
|
||||
style="padding: 0.5rem 0.75rem; background-color: #1e293b; border: 1px solid #475569; border-radius: 0.375rem; color: #e2e8f0; font-size: 0.875rem;"
|
||||
/>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary" style="align-self: flex-start;">
|
||||
Update Hostname
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- Danger Zone -->
|
||||
<div class="card" style="margin-top: 1.5rem; border-left: 3px solid #ef4444;">
|
||||
<div class="card-title" style="color: #ef4444;">Danger Zone</div>
|
||||
<p style="margin: 0 0 1rem 0; color: #94a3b8; font-size: 0.875rem;">
|
||||
Deleting this agent will remove all its data and history.
|
||||
</p>
|
||||
<form method="POST" action={ templ.SafeURL(fmt.Sprintf("/api/agents/%s/delete", agent.ID)) } style="margin: 0;" onsubmit="return confirm('Are you sure you want to remove this agent? This action cannot be undone.');">
|
||||
<button type="submit" class="btn btn-danger">Delete Agent</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user