fix(servidor-dashboard): show DB connection count on server cards
- GetServidor: query conx_count per server with GROUP BY and attach to response - servidor_dashboard.html: use conx_count from API (fallback to _conexiones.length) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot
parent
a44fb490f7
commit
c834e40e83
@@ -77,7 +77,7 @@
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 7v10c0 2.21 3.582 4 8 4s8-1.79 8-4V7M4 7c0 2.21 3.582 4 8 4s8-1.79 8-4M4 7c0-2.21 3.582 4 8 4"/>
|
||||
</svg>
|
||||
<span class="text-sm text-slate-600">
|
||||
<span class="font-bold text-blue-600" x-text="servidor._conexiones?.length || 0"></span>
|
||||
<span class="font-bold text-blue-600" x-text="servidor.conx_count ?? servidor._conexiones?.length ?? 0"></span>
|
||||
conexiones BD
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@@ -63,9 +63,42 @@ func GetServidor(c *fiber.Ctx) error {
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
// Build connection count map per server
|
||||
type conxCount struct {
|
||||
ServidorID uint `gorm:"column:servidor_id"`
|
||||
Count int64 `gorm:"column:count"`
|
||||
}
|
||||
var counts []conxCount
|
||||
serverIDs := make([]uint, len(records))
|
||||
for i, s := range records {
|
||||
serverIDs[i] = s.ID
|
||||
}
|
||||
if len(serverIDs) > 0 {
|
||||
app.Http.Database.DB.Table("conx_db").
|
||||
Select("servidor_id, count(*) as count").
|
||||
Where("servidor_id IN ? AND deleted_at IS NULL", serverIDs).
|
||||
Group("servidor_id").
|
||||
Scan(&counts)
|
||||
}
|
||||
countMap := make(map[uint]int64)
|
||||
for _, cc := range counts {
|
||||
countMap[cc.ServidorID] = cc.Count
|
||||
}
|
||||
|
||||
// Attach conx_count to each record
|
||||
type servidorWithCount struct {
|
||||
models.Servidor
|
||||
ConxCount int64 `json:"conx_count"`
|
||||
}
|
||||
enriched := make([]servidorWithCount, len(records))
|
||||
for i, s := range records {
|
||||
enriched[i] = servidorWithCount{Servidor: s, ConxCount: countMap[s.ID]}
|
||||
}
|
||||
|
||||
totalPages := int(math.Ceil(float64(total) / float64(limit)))
|
||||
return c.JSON(fiber.Map{
|
||||
"registros": records,
|
||||
"registros": enriched,
|
||||
"total": total,
|
||||
"totalPages": totalPages,
|
||||
"page": page,
|
||||
|
||||
Reference in New Issue
Block a user