channel_monitor_history.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package schema
  2. import (
  3. "time"
  4. "github.com/Wei-Shaw/sub2api/internal/domain"
  5. "entgo.io/ent"
  6. "entgo.io/ent/dialect/entsql"
  7. "entgo.io/ent/schema"
  8. "entgo.io/ent/schema/edge"
  9. "entgo.io/ent/schema/field"
  10. "entgo.io/ent/schema/index"
  11. )
  12. // ChannelMonitorHistory holds the schema definition for the ChannelMonitorHistory entity.
  13. // 渠道监控历史:每次检测每个模型一行记录。明细只保留 1 天,超过 1 天由每日维护任务
  14. // 先聚合到 channel_monitor_daily_rollups,再分批物理删(不用软删除:日志类表无恢复
  15. // 需求,软删会让行和索引只增不减,徒增磁盘和查询开销)。
  16. type ChannelMonitorHistory struct {
  17. ent.Schema
  18. }
  19. func (ChannelMonitorHistory) Annotations() []schema.Annotation {
  20. return []schema.Annotation{
  21. entsql.Annotation{Table: "channel_monitor_histories"},
  22. }
  23. }
  24. func (ChannelMonitorHistory) Fields() []ent.Field {
  25. return []ent.Field{
  26. field.Int64("monitor_id"),
  27. field.String("model").
  28. NotEmpty().
  29. MaxLen(200),
  30. field.Enum("status").
  31. Values("operational", "degraded", "failed", "error"),
  32. field.Int("latency_ms").
  33. Optional().
  34. Nillable(),
  35. field.Int("ping_latency_ms").
  36. Optional().
  37. Nillable(),
  38. field.String("message").
  39. Optional().
  40. Default("").
  41. MaxLen(500),
  42. // quota: 配额模式(check_mode = quota / quota_probe)检测时附带的
  43. // 归一化配额快照(domain.MonitorQuotaSnapshot,JSONB);探活模式为 NULL。
  44. field.JSON("quota", &domain.MonitorQuotaSnapshot{}).
  45. Optional(),
  46. field.Time("checked_at").
  47. Default(time.Now),
  48. }
  49. }
  50. func (ChannelMonitorHistory) Edges() []ent.Edge {
  51. return []ent.Edge{
  52. edge.From("monitor", ChannelMonitor.Type).
  53. Ref("history").
  54. Field("monitor_id").
  55. Unique().
  56. Required(),
  57. }
  58. }
  59. func (ChannelMonitorHistory) Indexes() []ent.Index {
  60. return []ent.Index{
  61. index.Fields("monitor_id", "model", "checked_at"),
  62. index.Fields("checked_at"),
  63. }
  64. }