new-api/model/channel_monitor_history.go
zizi 959ee42bcb feat: add channel monitor rollups
Aggregate monitor history into daily rollups and add maintenance helpers for rollup refresh and history cleanup.
2026-05-20 13:51:22 +08:00

67 lines
2.3 KiB
Go

package model
import "time"
type ChannelMonitorHistory struct {
Id int `json:"id" gorm:"primaryKey;autoIncrement"`
MonitorId int `json:"monitor_id" gorm:"index;not null"`
ChannelId int `json:"channel_id" gorm:"index"`
ModelName string `json:"model_name" gorm:"size:128;not null;index"`
Status string `json:"status" gorm:"size:16;not null;index"`
StatusCode int `json:"status_code"`
LatencyMs int `json:"latency_ms"`
ErrorMessage string `json:"error_message" gorm:"type:text"`
RequestBody string `json:"request_body" gorm:"type:text"`
ResponseSnippet string `json:"response_snippet" gorm:"type:text"`
CheckedAt time.Time `json:"checked_at" gorm:"index;not null"`
}
func (ChannelMonitorHistory) TableName() string {
return "channel_monitor_histories"
}
func (h *ChannelMonitorHistory) Insert() error {
return DB.Create(h).Error
}
func GetChannelMonitorHistories(monitorId int, page int, pageSize int, modelName string, status string, startTime *time.Time, endTime *time.Time) ([]ChannelMonitorHistory, int64, error) {
var histories []ChannelMonitorHistory
var total int64
query := DB.Model(&ChannelMonitorHistory{})
if monitorId > 0 {
query = query.Where("monitor_id = ?", monitorId)
}
if modelName != "" {
query = query.Where("model_name = ?", modelName)
}
if status != "" {
query = query.Where("status = ?", status)
}
if startTime != nil {
query = query.Where("checked_at >= ?", *startTime)
}
if endTime != nil {
query = query.Where("checked_at <= ?", *endTime)
}
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
if page < 1 {
page = 1
}
if pageSize < 1 {
pageSize = 20
}
err := query.Order("id DESC").Offset((page - 1) * pageSize).Limit(pageSize).Find(&histories).Error
return histories, total, err
}
func GetMonitorHistory(monitorId int, page int, pageSize int, modelName string, status string, startTime *time.Time, endTime *time.Time) ([]ChannelMonitorHistory, int64, error) {
return GetChannelMonitorHistories(monitorId, page, pageSize, modelName, status, startTime, endTime)
}
func DeleteChannelMonitorHistoriesBefore(cutoff time.Time) (int64, error) {
result := DB.Where("checked_at < ?", cutoff).Delete(&ChannelMonitorHistory{})
return result.RowsAffected, result.Error
}