AdminList handles GET /hosts/admin/list.
(w http.ResponseWriter, r *http.Request)
| 61 | |
| 62 | // AdminList handles GET /hosts/admin/list. |
| 63 | func (h *HostsHandler) AdminList(w http.ResponseWriter, r *http.Request) { |
| 64 | returnAll := r.URL.Query().Get("all") == "true" |
| 65 | page := parseIntQuery(r, "page", 1) |
| 66 | pageSize := parseIntQuery(r, "pageSize", 100) |
| 67 | if pageSize > 500 { |
| 68 | pageSize = 500 |
| 69 | } |
| 70 | if returnAll { |
| 71 | pageSize = 10000 |
| 72 | } |
| 73 | offset := (page - 1) * pageSize |
| 74 | |
| 75 | hosts, err := h.hosts.ListPaginated(r.Context(), pageSize, offset) |
| 76 | if err != nil { |
| 77 | Error(w, http.StatusInternalServerError, "Failed to load hosts") |
| 78 | return |
| 79 | } |
| 80 | total, _ := h.hosts.Count(r.Context()) |
| 81 | |
| 82 | // Batch-fetch host groups for all hosts |
| 83 | hostIDs := make([]string, len(hosts)) |
| 84 | for i := range hosts { |
| 85 | hostIDs[i] = hosts[i].ID |
| 86 | } |
| 87 | groupsByHost, _ := h.hosts.GetHostGroupsForHosts(r.Context(), hostIDs) |
| 88 | |
| 89 | // Enrich with host groups |
| 90 | data := make([]map[string]interface{}, len(hosts)) |
| 91 | for i, host := range hosts { |
| 92 | groups := groupsByHost[host.ID] |
| 93 | if groups == nil { |
| 94 | groups = []models.HostGroup{} |
| 95 | } |
| 96 | data[i] = hostToResponse(&host, groups) |
| 97 | } |
| 98 | |
| 99 | totalPages := 1 |
| 100 | if !returnAll && pageSize > 0 { |
| 101 | totalPages = (total + pageSize - 1) / pageSize |
| 102 | if totalPages < 1 { |
| 103 | totalPages = 1 |
| 104 | } |
| 105 | } |
| 106 | if returnAll { |
| 107 | pageSize = total |
| 108 | } |
| 109 | |
| 110 | JSON(w, http.StatusOK, map[string]interface{}{ |
| 111 | "data": data, |
| 112 | "pagination": map[string]interface{}{ |
| 113 | "total": total, "page": page, "pageSize": pageSize, "totalPages": totalPages, |
| 114 | }, |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | // GetByID handles GET /hosts/:hostId. |
| 119 | func (h *HostsHandler) GetByID(w http.ResponseWriter, r *http.Request) { |
nothing calls this directly
no test coverage detected