ip
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -130,3 +132,57 @@ func MarcarNotifsFacturasLeidas(usuarioID uint) error {
|
||||
Where("tipo_usuario = 'portal_user' AND usuario_id = ? AND icono = '🧾'", usuarioID).
|
||||
Update("leida", true).Error
|
||||
}
|
||||
|
||||
// ─── ServidorAlertaUmbral ─────────────────────────────────────────────────────
|
||||
// Configuración global de umbrales para alertas de servidores.
|
||||
// Solo existe un registro (singleton, ID = 1).
|
||||
|
||||
type ServidorAlertaUmbral struct {
|
||||
gorm.Model
|
||||
Activo bool `json:"activo" gorm:"column:activo;default:true"`
|
||||
MinutosSinPing int `json:"minutos_sin_ping" gorm:"column:minutos_sin_ping;default:10"`
|
||||
UmbralCPU int `json:"umbral_cpu" gorm:"column:umbral_cpu;default:90"`
|
||||
UmbralRAM int `json:"umbral_ram" gorm:"column:umbral_ram;default:90"`
|
||||
UmbralDisco int `json:"umbral_disco" gorm:"column:umbral_disco;default:90"`
|
||||
DiasAnteVencimiento int `json:"dias_ante_vencimiento" gorm:"column:dias_ante_vencimiento;default:7"`
|
||||
}
|
||||
|
||||
func (ServidorAlertaUmbral) TableName() string { return "servidor_alerta_umbral" }
|
||||
|
||||
// GetServidorAlertaUmbral obtiene el singleton de umbrales; crea uno con defaults si no existe.
|
||||
func GetServidorAlertaUmbral() *ServidorAlertaUmbral {
|
||||
var cfg ServidorAlertaUmbral
|
||||
db := app.Http.Database.DB
|
||||
if err := db.First(&cfg).Error; err != nil {
|
||||
cfg = ServidorAlertaUmbral{Activo: true, MinutosSinPing: 10, UmbralCPU: 90, UmbralRAM: 90, UmbralDisco: 90, DiasAnteVencimiento: 7}
|
||||
db.Create(&cfg)
|
||||
}
|
||||
return &cfg
|
||||
}
|
||||
|
||||
// SaveServidorAlertaUmbral guarda el singleton.
|
||||
func SaveServidorAlertaUmbral(cfg *ServidorAlertaUmbral) error {
|
||||
db := app.Http.Database.DB
|
||||
var existing ServidorAlertaUmbral
|
||||
if err := db.First(&existing).Error; err != nil {
|
||||
return db.Create(cfg).Error
|
||||
}
|
||||
return db.Model(&existing).Updates(map[string]interface{}{
|
||||
"activo": cfg.Activo,
|
||||
"minutos_sin_ping": cfg.MinutosSinPing,
|
||||
"umbral_cpu": cfg.UmbralCPU,
|
||||
"umbral_ram": cfg.UmbralRAM,
|
||||
"umbral_disco": cfg.UmbralDisco,
|
||||
"dias_ante_vencimiento": cfg.DiasAnteVencimiento,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// YaExisteAlertaServidor evita spam: true si ya se creó una notificación con el
|
||||
// mismo título para este servidor en las últimas `horas` horas.
|
||||
func YaExisteAlertaServidor(titulo string, horas int) bool {
|
||||
var count int64
|
||||
app.Http.Database.DB.Model(&SistemaNotificacion{}).
|
||||
Where("titulo = ? AND created_at > ?", titulo, time.Now().Add(-time.Duration(horas)*time.Hour)).
|
||||
Count(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user