明细只保留 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 中复用平台主题色 + 倍率/专属倍率 (顺手优化)。
290 lines
11 KiB
Go
290 lines
11 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"entgo.io/ent"
|
|
"entgo.io/ent/dialect/sql"
|
|
"github.com/Wei-Shaw/sub2api/ent/channelmonitor"
|
|
)
|
|
|
|
// ChannelMonitor is the model entity for the ChannelMonitor schema.
|
|
type ChannelMonitor struct {
|
|
config `json:"-"`
|
|
// ID of the ent.
|
|
ID int64 `json:"id,omitempty"`
|
|
// CreatedAt holds the value of the "created_at" field.
|
|
CreatedAt time.Time `json:"created_at,omitempty"`
|
|
// UpdatedAt holds the value of the "updated_at" field.
|
|
UpdatedAt time.Time `json:"updated_at,omitempty"`
|
|
// Name holds the value of the "name" field.
|
|
Name string `json:"name,omitempty"`
|
|
// Provider holds the value of the "provider" field.
|
|
Provider channelmonitor.Provider `json:"provider,omitempty"`
|
|
// Provider base origin, e.g. https://api.openai.com
|
|
Endpoint string `json:"endpoint,omitempty"`
|
|
// AES-256-GCM encrypted API key
|
|
APIKeyEncrypted string `json:"-"`
|
|
// PrimaryModel holds the value of the "primary_model" field.
|
|
PrimaryModel string `json:"primary_model,omitempty"`
|
|
// Additional model names to test alongside primary_model
|
|
ExtraModels []string `json:"extra_models,omitempty"`
|
|
// GroupName holds the value of the "group_name" field.
|
|
GroupName string `json:"group_name,omitempty"`
|
|
// Enabled holds the value of the "enabled" field.
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
// IntervalSeconds holds the value of the "interval_seconds" field.
|
|
IntervalSeconds int `json:"interval_seconds,omitempty"`
|
|
// LastCheckedAt holds the value of the "last_checked_at" field.
|
|
LastCheckedAt *time.Time `json:"last_checked_at,omitempty"`
|
|
// CreatedBy holds the value of the "created_by" field.
|
|
CreatedBy int64 `json:"created_by,omitempty"`
|
|
// Edges holds the relations/edges for other nodes in the graph.
|
|
// The values are being populated by the ChannelMonitorQuery when eager-loading is set.
|
|
Edges ChannelMonitorEdges `json:"edges"`
|
|
selectValues sql.SelectValues
|
|
}
|
|
|
|
// ChannelMonitorEdges holds the relations/edges for other nodes in the graph.
|
|
type ChannelMonitorEdges struct {
|
|
// History holds the value of the history edge.
|
|
History []*ChannelMonitorHistory `json:"history,omitempty"`
|
|
// DailyRollups holds the value of the daily_rollups edge.
|
|
DailyRollups []*ChannelMonitorDailyRollup `json:"daily_rollups,omitempty"`
|
|
// loadedTypes holds the information for reporting if a
|
|
// type was loaded (or requested) in eager-loading or not.
|
|
loadedTypes [2]bool
|
|
}
|
|
|
|
// HistoryOrErr returns the History value or an error if the edge
|
|
// was not loaded in eager-loading.
|
|
func (e ChannelMonitorEdges) HistoryOrErr() ([]*ChannelMonitorHistory, error) {
|
|
if e.loadedTypes[0] {
|
|
return e.History, nil
|
|
}
|
|
return nil, &NotLoadedError{edge: "history"}
|
|
}
|
|
|
|
// DailyRollupsOrErr returns the DailyRollups value or an error if the edge
|
|
// was not loaded in eager-loading.
|
|
func (e ChannelMonitorEdges) DailyRollupsOrErr() ([]*ChannelMonitorDailyRollup, error) {
|
|
if e.loadedTypes[1] {
|
|
return e.DailyRollups, nil
|
|
}
|
|
return nil, &NotLoadedError{edge: "daily_rollups"}
|
|
}
|
|
|
|
// scanValues returns the types for scanning values from sql.Rows.
|
|
func (*ChannelMonitor) scanValues(columns []string) ([]any, error) {
|
|
values := make([]any, len(columns))
|
|
for i := range columns {
|
|
switch columns[i] {
|
|
case channelmonitor.FieldExtraModels:
|
|
values[i] = new([]byte)
|
|
case channelmonitor.FieldEnabled:
|
|
values[i] = new(sql.NullBool)
|
|
case channelmonitor.FieldID, channelmonitor.FieldIntervalSeconds, channelmonitor.FieldCreatedBy:
|
|
values[i] = new(sql.NullInt64)
|
|
case channelmonitor.FieldName, channelmonitor.FieldProvider, channelmonitor.FieldEndpoint, channelmonitor.FieldAPIKeyEncrypted, channelmonitor.FieldPrimaryModel, channelmonitor.FieldGroupName:
|
|
values[i] = new(sql.NullString)
|
|
case channelmonitor.FieldCreatedAt, channelmonitor.FieldUpdatedAt, channelmonitor.FieldLastCheckedAt:
|
|
values[i] = new(sql.NullTime)
|
|
default:
|
|
values[i] = new(sql.UnknownType)
|
|
}
|
|
}
|
|
return values, nil
|
|
}
|
|
|
|
// assignValues assigns the values that were returned from sql.Rows (after scanning)
|
|
// to the ChannelMonitor fields.
|
|
func (_m *ChannelMonitor) assignValues(columns []string, values []any) error {
|
|
if m, n := len(values), len(columns); m < n {
|
|
return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
|
|
}
|
|
for i := range columns {
|
|
switch columns[i] {
|
|
case channelmonitor.FieldID:
|
|
value, ok := values[i].(*sql.NullInt64)
|
|
if !ok {
|
|
return fmt.Errorf("unexpected type %T for field id", value)
|
|
}
|
|
_m.ID = int64(value.Int64)
|
|
case channelmonitor.FieldCreatedAt:
|
|
if value, ok := values[i].(*sql.NullTime); !ok {
|
|
return fmt.Errorf("unexpected type %T for field created_at", values[i])
|
|
} else if value.Valid {
|
|
_m.CreatedAt = value.Time
|
|
}
|
|
case channelmonitor.FieldUpdatedAt:
|
|
if value, ok := values[i].(*sql.NullTime); !ok {
|
|
return fmt.Errorf("unexpected type %T for field updated_at", values[i])
|
|
} else if value.Valid {
|
|
_m.UpdatedAt = value.Time
|
|
}
|
|
case channelmonitor.FieldName:
|
|
if value, ok := values[i].(*sql.NullString); !ok {
|
|
return fmt.Errorf("unexpected type %T for field name", values[i])
|
|
} else if value.Valid {
|
|
_m.Name = value.String
|
|
}
|
|
case channelmonitor.FieldProvider:
|
|
if value, ok := values[i].(*sql.NullString); !ok {
|
|
return fmt.Errorf("unexpected type %T for field provider", values[i])
|
|
} else if value.Valid {
|
|
_m.Provider = channelmonitor.Provider(value.String)
|
|
}
|
|
case channelmonitor.FieldEndpoint:
|
|
if value, ok := values[i].(*sql.NullString); !ok {
|
|
return fmt.Errorf("unexpected type %T for field endpoint", values[i])
|
|
} else if value.Valid {
|
|
_m.Endpoint = value.String
|
|
}
|
|
case channelmonitor.FieldAPIKeyEncrypted:
|
|
if value, ok := values[i].(*sql.NullString); !ok {
|
|
return fmt.Errorf("unexpected type %T for field api_key_encrypted", values[i])
|
|
} else if value.Valid {
|
|
_m.APIKeyEncrypted = value.String
|
|
}
|
|
case channelmonitor.FieldPrimaryModel:
|
|
if value, ok := values[i].(*sql.NullString); !ok {
|
|
return fmt.Errorf("unexpected type %T for field primary_model", values[i])
|
|
} else if value.Valid {
|
|
_m.PrimaryModel = value.String
|
|
}
|
|
case channelmonitor.FieldExtraModels:
|
|
if value, ok := values[i].(*[]byte); !ok {
|
|
return fmt.Errorf("unexpected type %T for field extra_models", values[i])
|
|
} else if value != nil && len(*value) > 0 {
|
|
if err := json.Unmarshal(*value, &_m.ExtraModels); err != nil {
|
|
return fmt.Errorf("unmarshal field extra_models: %w", err)
|
|
}
|
|
}
|
|
case channelmonitor.FieldGroupName:
|
|
if value, ok := values[i].(*sql.NullString); !ok {
|
|
return fmt.Errorf("unexpected type %T for field group_name", values[i])
|
|
} else if value.Valid {
|
|
_m.GroupName = value.String
|
|
}
|
|
case channelmonitor.FieldEnabled:
|
|
if value, ok := values[i].(*sql.NullBool); !ok {
|
|
return fmt.Errorf("unexpected type %T for field enabled", values[i])
|
|
} else if value.Valid {
|
|
_m.Enabled = value.Bool
|
|
}
|
|
case channelmonitor.FieldIntervalSeconds:
|
|
if value, ok := values[i].(*sql.NullInt64); !ok {
|
|
return fmt.Errorf("unexpected type %T for field interval_seconds", values[i])
|
|
} else if value.Valid {
|
|
_m.IntervalSeconds = int(value.Int64)
|
|
}
|
|
case channelmonitor.FieldLastCheckedAt:
|
|
if value, ok := values[i].(*sql.NullTime); !ok {
|
|
return fmt.Errorf("unexpected type %T for field last_checked_at", values[i])
|
|
} else if value.Valid {
|
|
_m.LastCheckedAt = new(time.Time)
|
|
*_m.LastCheckedAt = value.Time
|
|
}
|
|
case channelmonitor.FieldCreatedBy:
|
|
if value, ok := values[i].(*sql.NullInt64); !ok {
|
|
return fmt.Errorf("unexpected type %T for field created_by", values[i])
|
|
} else if value.Valid {
|
|
_m.CreatedBy = value.Int64
|
|
}
|
|
default:
|
|
_m.selectValues.Set(columns[i], values[i])
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Value returns the ent.Value that was dynamically selected and assigned to the ChannelMonitor.
|
|
// This includes values selected through modifiers, order, etc.
|
|
func (_m *ChannelMonitor) Value(name string) (ent.Value, error) {
|
|
return _m.selectValues.Get(name)
|
|
}
|
|
|
|
// QueryHistory queries the "history" edge of the ChannelMonitor entity.
|
|
func (_m *ChannelMonitor) QueryHistory() *ChannelMonitorHistoryQuery {
|
|
return NewChannelMonitorClient(_m.config).QueryHistory(_m)
|
|
}
|
|
|
|
// QueryDailyRollups queries the "daily_rollups" edge of the ChannelMonitor entity.
|
|
func (_m *ChannelMonitor) QueryDailyRollups() *ChannelMonitorDailyRollupQuery {
|
|
return NewChannelMonitorClient(_m.config).QueryDailyRollups(_m)
|
|
}
|
|
|
|
// Update returns a builder for updating this ChannelMonitor.
|
|
// Note that you need to call ChannelMonitor.Unwrap() before calling this method if this ChannelMonitor
|
|
// was returned from a transaction, and the transaction was committed or rolled back.
|
|
func (_m *ChannelMonitor) Update() *ChannelMonitorUpdateOne {
|
|
return NewChannelMonitorClient(_m.config).UpdateOne(_m)
|
|
}
|
|
|
|
// Unwrap unwraps the ChannelMonitor entity that was returned from a transaction after it was closed,
|
|
// so that all future queries will be executed through the driver which created the transaction.
|
|
func (_m *ChannelMonitor) Unwrap() *ChannelMonitor {
|
|
_tx, ok := _m.config.driver.(*txDriver)
|
|
if !ok {
|
|
panic("ent: ChannelMonitor is not a transactional entity")
|
|
}
|
|
_m.config.driver = _tx.drv
|
|
return _m
|
|
}
|
|
|
|
// String implements the fmt.Stringer.
|
|
func (_m *ChannelMonitor) String() string {
|
|
var builder strings.Builder
|
|
builder.WriteString("ChannelMonitor(")
|
|
builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
|
|
builder.WriteString("created_at=")
|
|
builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
|
|
builder.WriteString(", ")
|
|
builder.WriteString("updated_at=")
|
|
builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
|
|
builder.WriteString(", ")
|
|
builder.WriteString("name=")
|
|
builder.WriteString(_m.Name)
|
|
builder.WriteString(", ")
|
|
builder.WriteString("provider=")
|
|
builder.WriteString(fmt.Sprintf("%v", _m.Provider))
|
|
builder.WriteString(", ")
|
|
builder.WriteString("endpoint=")
|
|
builder.WriteString(_m.Endpoint)
|
|
builder.WriteString(", ")
|
|
builder.WriteString("api_key_encrypted=<sensitive>")
|
|
builder.WriteString(", ")
|
|
builder.WriteString("primary_model=")
|
|
builder.WriteString(_m.PrimaryModel)
|
|
builder.WriteString(", ")
|
|
builder.WriteString("extra_models=")
|
|
builder.WriteString(fmt.Sprintf("%v", _m.ExtraModels))
|
|
builder.WriteString(", ")
|
|
builder.WriteString("group_name=")
|
|
builder.WriteString(_m.GroupName)
|
|
builder.WriteString(", ")
|
|
builder.WriteString("enabled=")
|
|
builder.WriteString(fmt.Sprintf("%v", _m.Enabled))
|
|
builder.WriteString(", ")
|
|
builder.WriteString("interval_seconds=")
|
|
builder.WriteString(fmt.Sprintf("%v", _m.IntervalSeconds))
|
|
builder.WriteString(", ")
|
|
if v := _m.LastCheckedAt; v != nil {
|
|
builder.WriteString("last_checked_at=")
|
|
builder.WriteString(v.Format(time.ANSIC))
|
|
}
|
|
builder.WriteString(", ")
|
|
builder.WriteString("created_by=")
|
|
builder.WriteString(fmt.Sprintf("%v", _m.CreatedBy))
|
|
builder.WriteByte(')')
|
|
return builder.String()
|
|
}
|
|
|
|
// ChannelMonitors is a parsable slice of ChannelMonitor.
|
|
type ChannelMonitors []*ChannelMonitor
|