feat(channel-monitor): aggregate history to daily rollups + soft delete

明细只保留 1 天,超过 1 天聚合到新表 channel_monitor_daily_rollups(按
monitor_id/model/bucket_date 维度),聚合保留 30 天。两张表都用 SoftDeleteMixin
软删除(DELETE 自动改为 UPDATE deleted_at = NOW())。

聚合 + 清理任务由 OpsCleanupService 的 cron 统一调度,与运维监控的清理共享
schedule(默认 0 2 * * *)和 leader lock。ChannelMonitorRunner 的 cleanupLoop
被移除,只保留 dueCheckLoop。

读取路径 ComputeAvailability* 改为 UNION 明细(今天 deleted_at IS NULL)+
聚合(过去 windowDays 天 deleted_at IS NULL),SUM(ok)/SUM(total) 自然加权
计算可用率,AVG latency 用 SUM(sum_latency_ms)/SUM(count_latency)。

watermark 表 channel_monitor_aggregation_watermark 单行(id=1),记录
last_aggregated_date,重启后从该日期 +1 继续聚合,首次为 nil 则从
today - 30d 开始回填,单次最多 35 天上限避免长事务。

raw SQL 的 ListLatestPerModel / ListLatestForMonitorIDs / ListRecentHistoryForMonitors
都补上 deleted_at IS NULL 过滤(SoftDeleteMixin interceptor 只对 ent query 生效)。

bump version to 0.1.114.28

GroupBadge 在 MonitorKeyPickerDialog 中复用平台主题色 + 倍率/专属倍率
(顺手优化)。
This commit is contained in:
erio
2026-04-21 10:10:56 +08:00
parent ba98243cc2
commit 8cf83c984e
41 changed files with 7925 additions and 321 deletions

View File

@@ -13,6 +13,7 @@ import (
"entgo.io/ent/dialect/sql/sqljson"
"entgo.io/ent/schema/field"
"github.com/Wei-Shaw/sub2api/ent/channelmonitor"
"github.com/Wei-Shaw/sub2api/ent/channelmonitordailyrollup"
"github.com/Wei-Shaw/sub2api/ent/channelmonitorhistory"
"github.com/Wei-Shaw/sub2api/ent/predicate"
)
@@ -229,6 +230,21 @@ func (_u *ChannelMonitorUpdate) AddHistory(v ...*ChannelMonitorHistory) *Channel
return _u.AddHistoryIDs(ids...)
}
// AddDailyRollupIDs adds the "daily_rollups" edge to the ChannelMonitorDailyRollup entity by IDs.
func (_u *ChannelMonitorUpdate) AddDailyRollupIDs(ids ...int64) *ChannelMonitorUpdate {
_u.mutation.AddDailyRollupIDs(ids...)
return _u
}
// AddDailyRollups adds the "daily_rollups" edges to the ChannelMonitorDailyRollup entity.
func (_u *ChannelMonitorUpdate) AddDailyRollups(v ...*ChannelMonitorDailyRollup) *ChannelMonitorUpdate {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.AddDailyRollupIDs(ids...)
}
// Mutation returns the ChannelMonitorMutation object of the builder.
func (_u *ChannelMonitorUpdate) Mutation() *ChannelMonitorMutation {
return _u.mutation
@@ -255,6 +271,27 @@ func (_u *ChannelMonitorUpdate) RemoveHistory(v ...*ChannelMonitorHistory) *Chan
return _u.RemoveHistoryIDs(ids...)
}
// ClearDailyRollups clears all "daily_rollups" edges to the ChannelMonitorDailyRollup entity.
func (_u *ChannelMonitorUpdate) ClearDailyRollups() *ChannelMonitorUpdate {
_u.mutation.ClearDailyRollups()
return _u
}
// RemoveDailyRollupIDs removes the "daily_rollups" edge to ChannelMonitorDailyRollup entities by IDs.
func (_u *ChannelMonitorUpdate) RemoveDailyRollupIDs(ids ...int64) *ChannelMonitorUpdate {
_u.mutation.RemoveDailyRollupIDs(ids...)
return _u
}
// RemoveDailyRollups removes "daily_rollups" edges to ChannelMonitorDailyRollup entities.
func (_u *ChannelMonitorUpdate) RemoveDailyRollups(v ...*ChannelMonitorDailyRollup) *ChannelMonitorUpdate {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.RemoveDailyRollupIDs(ids...)
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *ChannelMonitorUpdate) Save(ctx context.Context) (int, error) {
_u.defaults()
@@ -441,6 +478,51 @@ func (_u *ChannelMonitorUpdate) sqlSave(ctx context.Context) (_node int, err err
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.DailyRollupsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: channelmonitor.DailyRollupsTable,
Columns: []string{channelmonitor.DailyRollupsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(channelmonitordailyrollup.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.RemovedDailyRollupsIDs(); len(nodes) > 0 && !_u.mutation.DailyRollupsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: channelmonitor.DailyRollupsTable,
Columns: []string{channelmonitor.DailyRollupsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(channelmonitordailyrollup.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.DailyRollupsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: channelmonitor.DailyRollupsTable,
Columns: []string{channelmonitor.DailyRollupsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(channelmonitordailyrollup.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{channelmonitor.Label}
@@ -660,6 +742,21 @@ func (_u *ChannelMonitorUpdateOne) AddHistory(v ...*ChannelMonitorHistory) *Chan
return _u.AddHistoryIDs(ids...)
}
// AddDailyRollupIDs adds the "daily_rollups" edge to the ChannelMonitorDailyRollup entity by IDs.
func (_u *ChannelMonitorUpdateOne) AddDailyRollupIDs(ids ...int64) *ChannelMonitorUpdateOne {
_u.mutation.AddDailyRollupIDs(ids...)
return _u
}
// AddDailyRollups adds the "daily_rollups" edges to the ChannelMonitorDailyRollup entity.
func (_u *ChannelMonitorUpdateOne) AddDailyRollups(v ...*ChannelMonitorDailyRollup) *ChannelMonitorUpdateOne {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.AddDailyRollupIDs(ids...)
}
// Mutation returns the ChannelMonitorMutation object of the builder.
func (_u *ChannelMonitorUpdateOne) Mutation() *ChannelMonitorMutation {
return _u.mutation
@@ -686,6 +783,27 @@ func (_u *ChannelMonitorUpdateOne) RemoveHistory(v ...*ChannelMonitorHistory) *C
return _u.RemoveHistoryIDs(ids...)
}
// ClearDailyRollups clears all "daily_rollups" edges to the ChannelMonitorDailyRollup entity.
func (_u *ChannelMonitorUpdateOne) ClearDailyRollups() *ChannelMonitorUpdateOne {
_u.mutation.ClearDailyRollups()
return _u
}
// RemoveDailyRollupIDs removes the "daily_rollups" edge to ChannelMonitorDailyRollup entities by IDs.
func (_u *ChannelMonitorUpdateOne) RemoveDailyRollupIDs(ids ...int64) *ChannelMonitorUpdateOne {
_u.mutation.RemoveDailyRollupIDs(ids...)
return _u
}
// RemoveDailyRollups removes "daily_rollups" edges to ChannelMonitorDailyRollup entities.
func (_u *ChannelMonitorUpdateOne) RemoveDailyRollups(v ...*ChannelMonitorDailyRollup) *ChannelMonitorUpdateOne {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.RemoveDailyRollupIDs(ids...)
}
// Where appends a list predicates to the ChannelMonitorUpdate builder.
func (_u *ChannelMonitorUpdateOne) Where(ps ...predicate.ChannelMonitor) *ChannelMonitorUpdateOne {
_u.mutation.Where(ps...)
@@ -902,6 +1020,51 @@ func (_u *ChannelMonitorUpdateOne) sqlSave(ctx context.Context) (_node *ChannelM
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.DailyRollupsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: channelmonitor.DailyRollupsTable,
Columns: []string{channelmonitor.DailyRollupsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(channelmonitordailyrollup.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.RemovedDailyRollupsIDs(); len(nodes) > 0 && !_u.mutation.DailyRollupsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: channelmonitor.DailyRollupsTable,
Columns: []string{channelmonitor.DailyRollupsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(channelmonitordailyrollup.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.DailyRollupsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: channelmonitor.DailyRollupsTable,
Columns: []string{channelmonitor.DailyRollupsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(channelmonitordailyrollup.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_node = &ChannelMonitor{config: _u.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues