channel_monitor_daily_rollup.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package schema
  2. import (
  3. "time"
  4. "entgo.io/ent"
  5. "entgo.io/ent/dialect"
  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. // ChannelMonitorDailyRollup 按 (monitor_id, model, bucket_date) 维度聚合的渠道监控日统计。
  13. // 每天的明细被收敛为一行(保留 status 分布 + 延迟和),用于 7d/15d/30d 窗口的可用率
  14. // 加权计算(avg_latency = sum_latency_ms / count_latency;availability = ok_count / total_checks)。
  15. // 超过保留期由每日维护任务分批物理删(不用软删除,理由同 channel_monitor_history)。
  16. type ChannelMonitorDailyRollup struct {
  17. ent.Schema
  18. }
  19. func (ChannelMonitorDailyRollup) Annotations() []schema.Annotation {
  20. return []schema.Annotation{
  21. entsql.Annotation{Table: "channel_monitor_daily_rollups"},
  22. }
  23. }
  24. func (ChannelMonitorDailyRollup) Fields() []ent.Field {
  25. return []ent.Field{
  26. field.Int64("monitor_id"),
  27. field.String("model").
  28. NotEmpty().
  29. MaxLen(200),
  30. field.Time("bucket_date").
  31. SchemaType(map[string]string{dialect.Postgres: "date"}),
  32. field.Int("total_checks").Default(0),
  33. field.Int("ok_count").Default(0),
  34. field.Int("operational_count").Default(0),
  35. field.Int("degraded_count").Default(0),
  36. field.Int("failed_count").Default(0),
  37. field.Int("error_count").Default(0),
  38. field.Int64("sum_latency_ms").Default(0),
  39. field.Int("count_latency").Default(0),
  40. field.Int64("sum_ping_latency_ms").Default(0),
  41. field.Int("count_ping_latency").Default(0),
  42. field.Time("computed_at").Default(time.Now).UpdateDefault(time.Now),
  43. }
  44. }
  45. func (ChannelMonitorDailyRollup) Edges() []ent.Edge {
  46. return []ent.Edge{
  47. edge.From("monitor", ChannelMonitor.Type).
  48. Ref("daily_rollups").
  49. Field("monitor_id").
  50. Unique().
  51. Required(),
  52. }
  53. }
  54. func (ChannelMonitorDailyRollup) Indexes() []ent.Index {
  55. return []ent.Index{
  56. index.Fields("monitor_id", "model", "bucket_date").Unique(),
  57. index.Fields("bucket_date"),
  58. }
  59. }