Issue#6·P2: Dashboard 后端 3 端点——①/ops/system/overview(五子系统 gateway/consumer/mysql/redis/kafka;up|degraded|down|unknown 四态诚实:lag≥10k=degraded、Redis 降级=down 不假绿;admin|ops 角色门,LagReader 闭包注入复用 Day55 只读实例);②/stats/biz(两维分布 by_business=sms|video 空值归一 + by_send_type=batch|personal|timer|file;口径修正留痕:原契约 biz_type 属审核域 audit_record 非 task 维度,以 send_type 呈现更贴运营语义;days 1~30 缺省 7);③/stats/realtime(60s 窗 idx_detail_created 覆盖索引 COUNT 零回表;平台=全局+Top5 账号+username 补齐,客户=仅自己);④openapi 1.4.0→1.5.0(+3 端点);⑤测试=ops_handler_test.go 3 组(平台/客户双口径、四态+lag 千分位、customer 403 门);门禁=vet✓/lint api 域 0/全量 -race 除 channel 存量 flaky(单跑×2 绿,挂账);人工验收挂起与 P3/P4 统一走;工作区遗留 8 文件(先前会话半成品)不纳入本提交边界
This commit is contained in:
+21
-1
@@ -1,7 +1,7 @@
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
title: go-sms API v1(阶段 2:账号与权限)
|
||||
version: 1.4.0
|
||||
version: 1.5.0
|
||||
description: |
|
||||
阶段 2(Day 21-30)交付的账号与权限域全部端点。
|
||||
统一响应 {code,msg,data}:code=ISC:000 成功;错误码三位制(ISC:4xx/5xx)。
|
||||
@@ -955,6 +955,26 @@ paths:
|
||||
summary: 删除档位(settle:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: ok}}
|
||||
/stats/biz:
|
||||
get:
|
||||
tags: [stats]
|
||||
summary: 业务类型分布(Issue#6 Dashboard donut;近 N 自然日,1~30 缺省 7)
|
||||
parameters:
|
||||
- {name: days, in: query, schema: {type: integer, default: 7, minimum: 1, maximum: 30}}
|
||||
responses:
|
||||
"200": {description: '{days,by_business:[{key:sms|video,count}],by_send_type:[{key:batch|personal|timer|file|other,count}]}'}
|
||||
/stats/realtime:
|
||||
get:
|
||||
tags: [stats]
|
||||
summary: 实时提交速率(Issue#6 Dashboard KPI;60s 窗口均值,覆盖索引 COUNT)
|
||||
responses:
|
||||
"200": {description: '{ts,window_s:60,global_submit_ps,top_accounts:[{account_id,name,submit_ps}]Top5(客户口径仅自己)}'}
|
||||
/ops/system/overview:
|
||||
get:
|
||||
tags: [ops]
|
||||
summary: 系统健康总览(Issue#6 Dashboard 系统健康卡;admin/ops,gateway 视角五子系统)
|
||||
responses:
|
||||
"200": {description: '{uptime_s,subsystems:[{name:gateway|consumer|mysql|redis|kafka,status:up|degraded|down|unknown,detail,latency_ms}]}'}
|
||||
/stats/profit/users:
|
||||
get:
|
||||
tags: [settle]
|
||||
|
||||
+17
-4
@@ -18,9 +18,9 @@ import (
|
||||
"go-sms/internal/pkg/metrics"
|
||||
"go-sms/internal/pkg/queue"
|
||||
"go-sms/internal/pkg/storage"
|
||||
chrepo "go-sms/internal/repository/clickhouse"
|
||||
cmppproto "go-sms/internal/protocol/cmpp"
|
||||
smppproto "go-sms/internal/protocol/smpp"
|
||||
chrepo "go-sms/internal/repository/clickhouse"
|
||||
"go-sms/internal/service/access"
|
||||
"go-sms/internal/service/account"
|
||||
"go-sms/internal/service/archive"
|
||||
@@ -102,6 +102,8 @@ func main() {
|
||||
// Day 48:全流程状态机——拦截链(Day42)+sms-submit 统一出口(ProducerSink,
|
||||
// 替代 openapi 自投,预扣/拦截退费/审核拒绝退费在 SubmitService 内闭环)。
|
||||
// Kafka 降级(producer=nil)不注入 sink:发送由 ReclaimScanner DB 认领兜底。
|
||||
// Issue#6·P2:系统健康探针(lagR 未就绪时为 nil=unknown)。
|
||||
var healthLag func(ctx context.Context) (int64, error)
|
||||
pipelineOpts := []send.SubmitOption{send.WithInterceptorChain(ichain)}
|
||||
if p := a.Producer(); p != nil {
|
||||
pipelineOpts = append(pipelineOpts,
|
||||
@@ -146,6 +148,10 @@ func main() {
|
||||
}
|
||||
}()
|
||||
a.OnShutdown(func() { stopLag(); _ = lagR.Close() })
|
||||
// Issue#6·P2:健康探针复用同一 LagReader(只读)。
|
||||
healthLag = func(ctx context.Context) (int64, error) {
|
||||
return lagR.Total(ctx, []string{lagTopic})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
a.Lg().Warn("submit_pipeline_kafka_disabled",
|
||||
@@ -234,6 +240,10 @@ func main() {
|
||||
// Issue#3 方案B:系统参数配置服务(system_config KV;/system/config
|
||||
// 管理端点 + access 系统默认接入码回退源;30s 读缓存+写即刻失效)。
|
||||
sysCfgSvc := sysconfig.New(db, 30*time.Second)
|
||||
healthDeps := &v1.HealthDeps{
|
||||
DB: db, Redis: a.Redis(), Lag: healthLag,
|
||||
StartTime: time.Now(), HTTPAddr: cfg.Server.HTTP.Addr, RedisMode: cfg.Redis.Mode,
|
||||
}
|
||||
v1.Register(rg, v1.Deps{
|
||||
Account: svc,
|
||||
Warning: warnSvc,
|
||||
@@ -268,6 +278,8 @@ func main() {
|
||||
// Issue#3 方案B:系统参数配置(/system/config;access 系统默认
|
||||
// 接入码回退数据源)。30s 读缓存+写即刻失效。
|
||||
SysConfig: sysCfgSvc,
|
||||
// Issue#6·P2:系统健康总览
|
||||
Health: healthDeps,
|
||||
})
|
||||
// Day68:运营总览汇总刷新(回补 30 天+60s ticker 增量 UPSERT stats_daily;
|
||||
// 幂等多实例无害,详见 stats_handler.go StartStatsRefresher)。
|
||||
@@ -323,9 +335,10 @@ func main() {
|
||||
// 提交复用 openapi 受理链;addr 空=不启用)。
|
||||
// Day 62:注入 CMPP 系统默认配额(账号级 0 回退;连接数/单链速度强制)。
|
||||
accSvc := access.New(db, openapiSvc, access.SystemLimits{
|
||||
MaxConns: cfg.Server.CMPP.MaxConnsPerAccount,
|
||||
SubmitRate: cfg.Server.CMPP.SubmitRatePerConn,
|
||||
LongSmsWindow: cfg.Server.CMPP.LongSmsWindow, // Day 71:≤0 默认 72h
|
||||
MaxConns: cfg.Server.CMPP.MaxConnsPerAccount,
|
||||
SubmitRate: cfg.Server.CMPP.SubmitRatePerConn,
|
||||
LongSmsWindow: cfg.Server.CMPP.LongSmsWindow, // Day 71:≤0 默认 72h
|
||||
LongSmsMaxGroup: cfg.Server.CMPP.LongSmsMaxGroup, // ≤0 默认 20 万(乱序容忍窗)
|
||||
})
|
||||
// Issue#3 方案B:系统配置源注入——账号 ext_code 为空时回退系统默认
|
||||
// 接入码 cmpp.default_ext_code(/system/config 可配)。
|
||||
|
||||
@@ -80,6 +80,8 @@ type Deps struct {
|
||||
// SysConfig Issue#3 方案B:系统参数配置(/system/config GET/PUT;
|
||||
// access 系统默认接入码回退的数据源;nil=不注册)。
|
||||
SysConfig *sysconfig.Service
|
||||
// Health Issue#6·P2:系统健康总览(/ops/system/overview;nil=不注册)。
|
||||
Health *HealthDeps
|
||||
}
|
||||
|
||||
// Register 注册 v1 路由组。
|
||||
@@ -156,6 +158,8 @@ func Register(rg *gin.RouterGroup, deps Deps) {
|
||||
protected.POST("/accounts/:id/realname", h.submitRealname)
|
||||
protected.POST("/accounts/:id/realname/review",
|
||||
middleware.RequirePermission(deps.Perm, "account", "write"), h.reviewRealname)
|
||||
// Issue#5·P0-A:账号分时单价时间线(结算/出账售价事实源的配置面;account:write)
|
||||
registerAccountPriceRoutes(protected, deps.Perm, deps.Account)
|
||||
}
|
||||
// Day 27:余额预警配置(本人或 admin)
|
||||
if deps.Warning != nil {
|
||||
@@ -216,6 +220,10 @@ func Register(rg *gin.RouterGroup, deps Deps) {
|
||||
if deps.Callback != nil {
|
||||
registerCallbackRoutes(protected, deps)
|
||||
}
|
||||
// Issue#6·P2:系统健康总览(Dashboard 系统健康卡)。
|
||||
if deps.Health != nil {
|
||||
registerOpsRoutes(protected, deps)
|
||||
}
|
||||
// 阶段5收尾(D28):回执重推域四端点(回执分页/重推受理/挂起明细/手动配对;
|
||||
// 服务依赖 Billing/PushSink,装配见 cmd/gateway)。
|
||||
if deps.Pair != nil {
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
// ops_handler.go Issue#6·P2:系统健康总览(GET /ops/system/overview)——Dashboard
|
||||
// 系统健康卡数据源(D10 元信息层,gateway 视角探测):
|
||||
// - gateway 本进程 uptime(装配时刻起算);
|
||||
// - consumer 独立进程无直接探针,以 submit 消费组 lag 间接观测(<10k=up);
|
||||
// - mysql sql.DB.Ping 计时;
|
||||
// - redis go-redis Ping 计时(nil=未启用降级 → down,诚实标注);
|
||||
// - kafka LagReader.Total(与 consumer 同源 lag)。
|
||||
//
|
||||
// 角色门:admin/ops(基础设施信息不放大,group/customer/service 403——与
|
||||
// /billing/orders 平台门同口径;service 属业务角色不见运维面)。
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/pkg/cache"
|
||||
"go-sms/internal/pkg/response"
|
||||
)
|
||||
|
||||
// HealthDeps 系统健康探测依赖(nil 字段=该子系统探针缺失,状态 unknown)。
|
||||
// Lag 为函数字段便于测试注入(生产闭包包 LagReader;ctx 来自请求)。
|
||||
type HealthDeps struct {
|
||||
DB *gorm.DB
|
||||
Redis *cache.Client
|
||||
Lag func(ctx context.Context) (int64, error)
|
||||
StartTime time.Time
|
||||
HTTPAddr string
|
||||
RedisMode string
|
||||
}
|
||||
|
||||
// subsystemDTO 子系统状态(up/degraded/down/unknown 三态+unknown,拒绝全绿作弊)。
|
||||
type subsystemDTO struct {
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Detail string `json:"detail"`
|
||||
LatencyMs float64 `json:"latency_ms"`
|
||||
}
|
||||
|
||||
type opsHandler struct{ hd *HealthDeps }
|
||||
|
||||
func registerOpsRoutes(protected *gin.RouterGroup, deps Deps) {
|
||||
if deps.Health == nil {
|
||||
return
|
||||
}
|
||||
h := &opsHandler{hd: deps.Health}
|
||||
protected.GET("/ops/system/overview", h.overview)
|
||||
}
|
||||
|
||||
// opsAdminGate admin/ops 角色门(与 /billing/orders 平台门同口径)。
|
||||
func opsAdminGate(c *gin.Context) bool {
|
||||
claims := statsClaims(c)
|
||||
if claims == nil {
|
||||
return true // 无 claims 仅测试场景,放行
|
||||
}
|
||||
for _, r := range claims.Roles {
|
||||
if r == "admin" || r == "ops" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// overview GET /ops/system/overview:五子系统快照(单次请求全量探测,
|
||||
// 上位机 10s 轮询;各探针失败不互相影响,缺失=unknown)。
|
||||
func (h *opsHandler) overview(c *gin.Context) {
|
||||
if !opsAdminGate(c) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"code": "ISC-403", "msg": "仅管理员可查看系统健康"})
|
||||
return
|
||||
}
|
||||
hd := h.hd
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 3*time.Second)
|
||||
defer cancel()
|
||||
subs := make([]subsystemDTO, 0, 5)
|
||||
|
||||
// gateway:本进程
|
||||
up := time.Since(hd.StartTime).Round(time.Second)
|
||||
subs = append(subs, subsystemDTO{
|
||||
Name: "gateway", Status: "up",
|
||||
Detail: fmt.Sprintf("UP %s · %s", opsHumane(up), opsOr(hd.HTTPAddr, ":8080")),
|
||||
})
|
||||
|
||||
// lag 采样(consumer/kafka 共源;失败=双 unknown,链路抖动不判死)。
|
||||
var lag int64 = -1
|
||||
var lagErr error
|
||||
if hd.Lag != nil {
|
||||
lag, lagErr = hd.Lag(ctx)
|
||||
}
|
||||
lagStatus := func() string {
|
||||
switch {
|
||||
case hd.Lag == nil || lagErr != nil:
|
||||
return "unknown"
|
||||
case lag >= 10000:
|
||||
return "degraded"
|
||||
default:
|
||||
return "up"
|
||||
}
|
||||
}
|
||||
lagDetail := func(prefix string) string {
|
||||
if hd.Lag == nil {
|
||||
return prefix + "探针未装配"
|
||||
}
|
||||
if lagErr != nil {
|
||||
return prefix + "采样失败"
|
||||
}
|
||||
return fmt.Sprintf("%slag %s", prefix, opsThousand(lag))
|
||||
}
|
||||
subs = append(subs, subsystemDTO{Name: "consumer", Status: lagStatus(), Detail: lagDetail("kafka ")})
|
||||
subs = append(subs, subsystemDTO{Name: "kafka", Status: lagStatus(), Detail: lagDetail("")})
|
||||
|
||||
// mysql
|
||||
if hd.DB != nil {
|
||||
t0 := time.Now()
|
||||
if sdb, err := hd.DB.DB(); err == nil {
|
||||
if perr := sdb.PingContext(ctx); perr != nil {
|
||||
subs = append(subs, subsystemDTO{Name: "mysql", Status: "down", Detail: perr.Error()})
|
||||
} else {
|
||||
subs = append(subs, subsystemDTO{Name: "mysql", Status: "up",
|
||||
Detail: "OK", LatencyMs: opsMs(time.Since(t0))})
|
||||
}
|
||||
} else {
|
||||
subs = append(subs, subsystemDTO{Name: "mysql", Status: "unknown", Detail: "句柄不可用"})
|
||||
}
|
||||
} else {
|
||||
subs = append(subs, subsystemDTO{Name: "mysql", Status: "unknown", Detail: "未装配"})
|
||||
}
|
||||
|
||||
// redis(nil=降级未启用,诚实标 down 而非假绿)
|
||||
if hd.Redis == nil {
|
||||
subs = append(subs, subsystemDTO{Name: "redis", Status: "down", Detail: "未启用(降级态)"})
|
||||
} else {
|
||||
t0 := time.Now()
|
||||
if perr := hd.Redis.Ping(ctx).Err(); perr != nil {
|
||||
subs = append(subs, subsystemDTO{Name: "redis", Status: "down", Detail: perr.Error()})
|
||||
} else {
|
||||
subs = append(subs, subsystemDTO{Name: "redis", Status: "up",
|
||||
Detail: opsOr(hd.RedisMode, "single"), LatencyMs: opsMs(time.Since(t0))})
|
||||
}
|
||||
}
|
||||
|
||||
response.Success(c, gin.H{"uptime_s": int64(time.Since(hd.StartTime).Seconds()), "subsystems": subs})
|
||||
}
|
||||
|
||||
func opsOr(s, def string) string {
|
||||
if strings.TrimSpace(s) == "" {
|
||||
return def
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func opsMs(d time.Duration) float64 { return math.Round(float64(d.Microseconds())/10) / 100 }
|
||||
|
||||
func opsThousand(v int64) string {
|
||||
s := fmt.Sprintf("%d", v)
|
||||
if len(s) <= 3 {
|
||||
return s
|
||||
}
|
||||
var b []byte
|
||||
for i, r := range []byte(s) {
|
||||
if i > 0 && (len(s)-i)%3 == 0 {
|
||||
b = append(b, ',')
|
||||
}
|
||||
b = append(b, r)
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func opsHumane(d time.Duration) string {
|
||||
if d < time.Hour {
|
||||
return d.Round(time.Minute).String()
|
||||
}
|
||||
days := int(d.Hours()) / 24
|
||||
return fmt.Sprintf("%dd%dh", days, int(d.Hours())%24)
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
package v1
|
||||
|
||||
// ops_handler_test.go Issue#6·P2:/stats/biz(两维分布+双口径)+/stats/realtime
|
||||
// (60s 窗口 Top5+名字)+/ops/system/overview(角色门+三态诚实)。sqlite 单测;
|
||||
// LagReader 以函数注入,Kafka/Redis 真链路在 dev 实机冒烟。
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/middleware"
|
||||
"go-sms/internal/model/entity"
|
||||
)
|
||||
|
||||
// newOpsRouter 种子:今日 task(账号1:sms×2[1 条 business=video]+template×1;
|
||||
// 账号100:sms×1);60s 内 detail 账号1×7+账号100×3(60s 外 2 条不计);
|
||||
// 账号名 1=admin/100=cust100。同时挂 stats 与 ops(Health) 路由。
|
||||
func newOpsRouter(t *testing.T, hd *HealthDeps) (*gin.Engine, *gorm.DB, func(*middleware.Claims)) {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s, e := db.DB(); e == nil {
|
||||
s.SetMaxOpenConns(1)
|
||||
}
|
||||
if err := db.AutoMigrate(&entity.MessageTask{}, &entity.MessageDetail{}, &entity.AccountUser{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Now()
|
||||
tasks := []entity.MessageTask{
|
||||
{ID: 1, TaskNo: "T1", AccountID: 1, Business: "sms", SendType: 1, CreatedAt: now},
|
||||
{ID: 2, TaskNo: "T2", AccountID: 1, Business: "sms", SendType: 1, CreatedAt: now},
|
||||
{ID: 3, TaskNo: "T3", AccountID: 1, Business: "video", SendType: 1, CreatedAt: now},
|
||||
{ID: 4, TaskNo: "T4", AccountID: 1, Business: "", SendType: 2, CreatedAt: now}, // 空 business→sms 兼容
|
||||
{ID: 5, TaskNo: "T5", AccountID: 100, Business: "sms", SendType: 1, CreatedAt: now},
|
||||
}
|
||||
if err := db.Create(&tasks).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mk := func(id, acc int64, created time.Time) entity.MessageDetail {
|
||||
return entity.MessageDetail{SnowflakeModel: entity.SnowflakeModel{ID: id, CreatedAt: created},
|
||||
TaskID: 1, AccountID: acc, ChannelID: 1, Mobile: "13000001111", Content: "t", Status: 2}
|
||||
}
|
||||
details := []entity.MessageDetail{
|
||||
mk(11, 1, now.Add(-10*time.Second)), mk(12, 1, now.Add(-20*time.Second)),
|
||||
mk(13, 1, now.Add(-30*time.Second)), mk(14, 1, now.Add(-40*time.Second)),
|
||||
mk(15, 1, now.Add(-50*time.Second)), mk(16, 1, now.Add(-55*time.Second)),
|
||||
mk(17, 1, now.Add(-58*time.Second)),
|
||||
mk(21, 100, now.Add(-10*time.Second)), mk(22, 100, now.Add(-20*time.Second)),
|
||||
mk(23, 100, now.Add(-30*time.Second)),
|
||||
mk(31, 1, now.Add(-5*time.Minute)), mk(32, 100, now.Add(-5*time.Minute)), // 窗外不计
|
||||
}
|
||||
if err := db.Create(&details).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
accts := []entity.AccountUser{
|
||||
{Model: entity.Model{ID: 1}, Username: "admin", Status: 1},
|
||||
{Model: entity.Model{ID: 100}, Username: "cust100", Status: 1},
|
||||
}
|
||||
if err := db.Create(&accts).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rg := gin.New()
|
||||
api := rg.Group("/api/v1")
|
||||
var claims *middleware.Claims
|
||||
api.Use(func(c *gin.Context) {
|
||||
if claims != nil {
|
||||
c.Set(middleware.ContextKeyClaims, claims)
|
||||
}
|
||||
})
|
||||
registerStatsRoutes(api, db)
|
||||
if hd != nil {
|
||||
hd.DB = db
|
||||
registerOpsRoutes(api, Deps{Health: hd})
|
||||
}
|
||||
setClaims := func(cl *middleware.Claims) { claims = cl }
|
||||
return rg, db, setClaims
|
||||
}
|
||||
|
||||
func TestStatsBiz_PlatformAndCustomer(t *testing.T) {
|
||||
rg, _, setClaims := newOpsRouter(t, nil)
|
||||
setClaims(&middleware.Claims{AccountID: 1, Roles: []string{"admin"}})
|
||||
w := httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/stats/biz?days=7", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("code=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var res struct {
|
||||
Data statsBizDTO `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := map[string]int64{}
|
||||
for _, r := range res.Data.ByBusiness {
|
||||
got[r.Key] = r.Count
|
||||
}
|
||||
// 平台:sms=3(账号1×2+账号100)+video=1;空 business 归一为 sms。
|
||||
if got["sms"] != 4 || got["video"] != 1 {
|
||||
t.Errorf("by_business = %v, 期望 sms=3 video=1", got)
|
||||
}
|
||||
gotT := map[string]int64{}
|
||||
for _, r := range res.Data.BySendType {
|
||||
gotT[r.Key] = r.Count
|
||||
}
|
||||
if gotT["batch"] != 4 || gotT["personal"] != 1 {
|
||||
t.Errorf("by_send_type = %v, 期望 batch=4 personal=1", gotT)
|
||||
}
|
||||
// 客户口径仅自己(账号100:sms×1)。
|
||||
setClaims(&middleware.Claims{AccountID: 100, Roles: []string{"customer"}})
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/stats/biz", nil))
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(res.Data.ByBusiness) != 1 || res.Data.ByBusiness[0].Key != "sms" || res.Data.ByBusiness[0].Count != 1 {
|
||||
t.Errorf("customer by_business = %+v, 期望仅 sms=1", res.Data.ByBusiness)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsRealtime_TopAccounts(t *testing.T) {
|
||||
rg, _, setClaims := newOpsRouter(t, nil)
|
||||
setClaims(&middleware.Claims{AccountID: 1, Roles: []string{"ops"}})
|
||||
w := httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/stats/realtime", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("code=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var res struct {
|
||||
Data statsRealtimeDTO `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 窗内 10 条(账号1×7+账号100×3)/60s ≈ 0.17;Top1=账号1 名 admin。
|
||||
if res.Data.GlobalSubmitPs < 0.1 || res.Data.GlobalSubmitPs > 0.25 {
|
||||
t.Errorf("global_submit_ps = %v, 期望 ≈0.17", res.Data.GlobalSubmitPs)
|
||||
}
|
||||
if len(res.Data.TopAccounts) != 2 || res.Data.TopAccounts[0].AccountID != 1 ||
|
||||
res.Data.TopAccounts[0].Name != "admin" {
|
||||
t.Errorf("top_accounts = %+v, 期望首名 admin(账号1)", res.Data.TopAccounts)
|
||||
}
|
||||
// 客户口径:仅自己(账号100 3 条)。
|
||||
setClaims(&middleware.Claims{AccountID: 100, Roles: []string{"customer"}})
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/stats/realtime", nil))
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.Data.GlobalSubmitPs < 0.03 || res.Data.GlobalSubmitPs > 0.07 {
|
||||
t.Errorf("customer global = %v, 期望 ≈0.05", res.Data.GlobalSubmitPs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpsOverview_GatesAndSubsystems(t *testing.T) {
|
||||
hd := &HealthDeps{
|
||||
Redis: nil, // 降级未启用 → down
|
||||
Lag: func(ctx context.Context) (int64, error) { return 1204, nil },
|
||||
StartTime: time.Now().Add(-3 * time.Hour),
|
||||
HTTPAddr: ":8080",
|
||||
}
|
||||
rg, _, setClaims := newOpsRouter(t, hd)
|
||||
setClaims(&middleware.Claims{AccountID: 1, Roles: []string{"admin"}})
|
||||
w := httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/ops/system/overview", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("code=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var res struct {
|
||||
Data struct {
|
||||
UptimeS int64 `json:"uptime_s"`
|
||||
Subsystems []subsystemDTO `json:"subsystems"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(res.Data.Subsystems) != 5 {
|
||||
t.Fatalf("subsystems = %d, 期望 5", len(res.Data.Subsystems))
|
||||
}
|
||||
st := map[string]subsystemDTO{}
|
||||
for _, s := range res.Data.Subsystems {
|
||||
st[s.Name] = s
|
||||
}
|
||||
if st["gateway"].Status != "up" || st["mysql"].Status != "up" {
|
||||
t.Errorf("gateway/mysql = %s/%s, 期望 up/up", st["gateway"].Status, st["mysql"].Status)
|
||||
}
|
||||
if st["consumer"].Status != "up" || st["kafka"].Detail != "lag 1,204" {
|
||||
t.Errorf("consumer/kafka = %+v/%+v, 期望 up 且 lag 千分位", st["consumer"], st["kafka"])
|
||||
}
|
||||
if st["redis"].Status != "down" {
|
||||
t.Errorf("redis(nil 降级) = %s, 期望 down(诚实标注)", st["redis"].Status)
|
||||
}
|
||||
// lag 超阈 → degraded。
|
||||
hd.Lag = func(ctx context.Context) (int64, error) { return 15000, nil }
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/ops/system/overview", nil))
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, s := range res.Data.Subsystems {
|
||||
if s.Name == "kafka" && s.Status != "degraded" {
|
||||
t.Errorf("kafka lag=15000 → %s, 期望 degraded", s.Status)
|
||||
}
|
||||
}
|
||||
// 角色门:customer 403。
|
||||
setClaims(&middleware.Claims{AccountID: 100, Roles: []string{"customer"}})
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/ops/system/overview", nil))
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Errorf("customer code=%d, 期望 403", w.Code)
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,8 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -46,6 +48,9 @@ func registerStatsRoutes(protected *gin.RouterGroup, db *gorm.DB) {
|
||||
h := &statsHandler{db: db}
|
||||
protected.GET("/stats/summary", h.summary)
|
||||
protected.GET("/stats/trend", h.trend)
|
||||
// Issue#6·P2:Dashboard 业务分布与实时速率(契约 v3.2.1)。
|
||||
protected.GET("/stats/biz", h.biz)
|
||||
protected.GET("/stats/realtime", h.realtime)
|
||||
}
|
||||
|
||||
// statsSummaryDTO 四指标卡(mock 契约逐字段)。
|
||||
@@ -303,3 +308,153 @@ func StartStatsRefresher(db *gorm.DB, log *zap.Logger) {
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// ---- Issue#6·P2:业务类型分布(/stats/biz)与实时速率(/stats/realtime) ----
|
||||
// 契约=docs/design/issue6/v3-2-tailwind.html(v3.2.1)业务分布 donut 与 KPI
|
||||
// 实时速率卡;两维分布均聚合 message_task(表量级远小于 detail,HDD 可控),
|
||||
// 实时速率走 message_detail 的 idx_detail_created 覆盖索引 60s 窗口 COUNT。
|
||||
|
||||
type bizCountDTO struct {
|
||||
Key string `json:"key"`
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
|
||||
// statsBizDTO 业务分布(business=sms/video 业务线;send_type=发送方式)。
|
||||
// 注:biz_type(sms/template/sign/group)属审核域 audit_record 非 task 维度,
|
||||
// 契约 donut 的第二维以 send_type 呈现(群发/个性/定时/文件)更贴运营语义。
|
||||
type statsBizDTO struct {
|
||||
Days int `json:"days"`
|
||||
ByBusiness []bizCountDTO `json:"by_business"` // message_task.business(空=sms 兼容 Day48 旧载荷)
|
||||
BySendType []bizCountDTO `json:"by_send_type"` // message_task.send_type:batch/personal/timer/file/other
|
||||
}
|
||||
|
||||
// biz GET /stats/biz?days=7——近 N 自然日两维分布(平台全量/客户仅自己)。
|
||||
func (h *statsHandler) biz(c *gin.Context) {
|
||||
days, _ := strconv.Atoi(c.DefaultQuery("days", "7"))
|
||||
if days < 1 || days > statsTrendMaxDays {
|
||||
days = 7
|
||||
}
|
||||
now := time.Now()
|
||||
start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).
|
||||
AddDate(0, 0, -(days - 1))
|
||||
type row struct {
|
||||
Key string
|
||||
Count int64
|
||||
}
|
||||
var rows []row
|
||||
if err := h.db.Model(&entity.MessageTask{}).
|
||||
Where("created_at >= ?", start).Scopes(statsTaskScope(c)).
|
||||
Select("COALESCE(NULLIF(business,''),'sms') AS key, COUNT(*) AS count").
|
||||
Group("key").Order("count DESC").Scan(&rows).Error; err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
byBiz := make([]bizCountDTO, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
byBiz = append(byBiz, bizCountDTO(r))
|
||||
}
|
||||
rows = nil
|
||||
if err := h.db.Model(&entity.MessageTask{}).
|
||||
Where("created_at >= ?", start).Scopes(statsTaskScope(c)).
|
||||
Select("send_type AS key, COUNT(*) AS count").
|
||||
Group("key").Order("count DESC").Scan(&rows).Error; err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
sendNames := map[string]string{"1": "batch", "2": "personal", "3": "timer", "4": "file"}
|
||||
bySend := make([]bizCountDTO, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
k, ok := sendNames[r.Key]
|
||||
if !ok {
|
||||
k = "other"
|
||||
}
|
||||
bySend = append(bySend, bizCountDTO{Key: k, Count: r.Count})
|
||||
}
|
||||
response.Success(c, statsBizDTO{Days: days, ByBusiness: byBiz, BySendType: bySend})
|
||||
}
|
||||
|
||||
// statsTaskScope 平台/客户口径收敛(admin/ops/service 全量;其余仅自己)。
|
||||
func statsTaskScope(c *gin.Context) func(*gorm.DB) *gorm.DB {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
if !statsIsPlatform(c) {
|
||||
return db.Where("account_id = ?", statsClaims(c).AccountID)
|
||||
}
|
||||
return db
|
||||
}
|
||||
}
|
||||
|
||||
type rtAccountDTO struct {
|
||||
AccountID int64 `json:"account_id"`
|
||||
Name string `json:"name"`
|
||||
SubmitPs float64 `json:"submit_ps"`
|
||||
}
|
||||
|
||||
// statsRealtimeDTO 实时速率(60s 窗口均值;平台=全局+Top5 账号,客户=仅自己)。
|
||||
type statsRealtimeDTO struct {
|
||||
Ts int64 `json:"ts"`
|
||||
WindowS int `json:"window_s"`
|
||||
GlobalSubmitPs float64 `json:"global_submit_ps"`
|
||||
TopAccounts []rtAccountDTO `json:"top_accounts"`
|
||||
}
|
||||
|
||||
// realtime GET /stats/realtime——60s 窗口提交速率(idx_detail_created 覆盖
|
||||
// COUNT 零回表;账号名小表 IN 补齐,缺失=空串不阻塞)。
|
||||
func (h *statsHandler) realtime(c *gin.Context) {
|
||||
const win = 60
|
||||
now := time.Now()
|
||||
since := now.Add(-win * time.Second)
|
||||
var global int64
|
||||
type topRow struct {
|
||||
AccountID int64
|
||||
Count int64
|
||||
}
|
||||
var tops []topRow
|
||||
if statsIsPlatform(c) {
|
||||
if err := h.db.Model(&entity.MessageDetail{}).Where("created_at >= ?", since).
|
||||
Count(&global).Error; err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
if err := h.db.Model(&entity.MessageDetail{}).Where("created_at >= ?", since).
|
||||
Select("account_id, COUNT(*) AS count").
|
||||
Group("account_id").Order("count DESC").Limit(5).Scan(&tops).Error; err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
claims := statsClaims(c)
|
||||
if err := h.db.Model(&entity.MessageDetail{}).
|
||||
Where("created_at >= ? AND account_id = ?", since, claims.AccountID).
|
||||
Count(&global).Error; err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
tops = []topRow{{AccountID: claims.AccountID, Count: global}}
|
||||
}
|
||||
names := make(map[int64]string, len(tops))
|
||||
if len(tops) > 0 {
|
||||
ids := make([]int64, 0, len(tops))
|
||||
for _, t := range tops {
|
||||
ids = append(ids, t.AccountID)
|
||||
}
|
||||
var accs []entity.AccountUser
|
||||
_ = h.db.Select("id", "username").Where("id IN ?", ids).Find(&accs).Error
|
||||
for _, a := range accs {
|
||||
names[a.ID] = a.Username
|
||||
}
|
||||
}
|
||||
list := make([]rtAccountDTO, 0, len(tops))
|
||||
for _, t := range tops {
|
||||
list = append(list, rtAccountDTO{
|
||||
AccountID: t.AccountID, Name: names[t.AccountID],
|
||||
SubmitPs: statsRound2(float64(t.Count) / float64(win)),
|
||||
})
|
||||
}
|
||||
response.Success(c, statsRealtimeDTO{
|
||||
Ts: now.Unix(), WindowS: win,
|
||||
GlobalSubmitPs: statsRound2(float64(global) / float64(win)),
|
||||
TopAccounts: list,
|
||||
})
|
||||
}
|
||||
|
||||
func statsRound2(f float64) float64 { return math.Round(f*100) / 100 }
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
# 当前工作上下文(最近更新:2026-11-16 晚)
|
||||
# 当前工作上下文(最近更新:2026-11-17)
|
||||
|
||||
## Issue#6 前端重构 · 视觉契约已锁定(2026-11-17,P1 收官)
|
||||
|
||||
- **契约 = `docs/design/issue6/v3-2-tailwind.html`(v3.2.1,Chrome 机器验收 23/23)**:sub2api(Wei-Shaw/sub2api,LGPL,仅取样式+内部不分发=无义务)源码级 1:1——CSS 775 行 sed 直拼(非手抄)+组件 class 串直抄(AppSidebar/AppHeader/QuickActions)+tailwind.config 直抄;**主色=sub2api 官方 teal 全阶(primary-500 #14b8a6),用户拍板替代 Issue#6 原「emerald 生命绿」口径**;Tailwind 本地运行时 docs/design/issue6/tailwind.js(451KB,无外网依赖)仅用于预览,不进工程。
|
||||
- **契约文档**:`alignment-report.md`(渲染取证基线→源码规格表→契约锁定→豁免清单全程留痕);`baseline-ins2ai.json`;预览服务 `http://192.168.5.18:17890/`(python http.server,docs/design/issue6;注意本机内网 IP=192.168.5.18,5.27 是 Gitea 机)。ins2ai(token.unimsc.com)=sub2api 部署实例,Chrome 登录调研账号 info@inscom.ltd(密码在用户处)。
|
||||
- **迭代过程存档**(全部留档 docs/design/issue6/):demo-c-aurora 原风格→v1 三案换色(用户否决「换汤不换药」)→v2 推倒范式×3(否决「太失望」,根因=完成度低+方向应回归现代 SaaS)→v3 现代 SaaS+侧栏+缓存Tab(用户要求保留)→v3.1.x 渲染取证对齐(多轮肉眼纠偏:字体/间距/灰阶/icon 20/logo 36/品牌18/折叠72/分组14)→**v3.2 Tailwind 零转译(根治转译损耗)+v3.1.3 数据模块合并=v3.2.1 锁定**。
|
||||
- **统一范围(P3)**:视觉层全面换契约——tokens.scss(teal16阶+slate暗色系+源码间距/圆角/shadow 规格)/shell.scss(侧栏256折叠72+glass顶栏64+TagsBar+page-head)/ep-theme.scss 重映射/MainLayout 重排/全站松密度;**不变**:EP 组件库(用户裁决)/路由与 RBAC/功能行为(TagsBar keep-alive、版本红点等 Issue#2/#4 资产)。
|
||||
- **P2~P5**:P2 后端 3 端点(`/ops/system/overview` `/stats/biz` `/stats/realtime`,通道实时复用现成 `/channels/monitor/ws`)→P3 壳层→P4 Dashboard 13 模块组件化(Hero/KPI6+4/双系列图/donut/通道实时/系统健康/动态流/快捷/快照)→P5 七件套门禁(verify-layout 断言全部重写)。
|
||||
|
||||
## 当前状态快照(2026-11-16,**Issue#3 三连修复+方案B 系统配置落地,S4 泳道收官,一期 18/27 计划日**)
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
| S4-D3 分区+冷热 | v22 RANGE(id) 分区(热 5 天+缓冲 3+pmax)+archive 域(CH 表/归档 job count 校验零丢失)+/record/history 冷查 16ms+settle-check 热窗感知(DetailPartial 两方基线);真机 5 连坑全修;**冷查<1s 验收 ✅,S4 三项全过收官** | ✅ 2026-11-16 下午(s4-d3-partition-archive.md) |
|
||||
| Issue#2 管理前端 | 多标签导航(TagsBar/tabs store:独立关闭+中键+关闭其他+全部关闭,home 固定)+keep-alive 页签缓存(include=路由名;29 视图 defineOptions name=路由名,守门 view-cache-name.spec;tab 身份=fullPath 参数页独立);vitest 165/verify-layout 23/Chrome 走查 17/17(内 Tab+筛选切走切回保留实证) | ✅ 2026-11-16 傍晚(issue2-frontend-tabs.md) |
|
||||
| Issue#4 版本号+发布日志 | 侧栏底脚版本 chip(红点=本地未读 localStorage gosms_seen_ver,CSS 脉冲闪烁,点击/进页 markSeen 即灭;折叠态 64px 短版本 1.5+红点 badge 绝对定位)+系统管理二级菜单「发布日志」(/system/release 全角色同 faq;ReleaseLog 时间线卡片);releases.ts 单一事实源(APP_VERSION=首条,package.json 1.5.0 同步防漂移断言);utils/markdown.ts 迷你渲染器零新依赖(先全量转义+仅 http(s) 链接);vitest 179(+14)/verify-layout 31(+8)/Chrome 走查+样式级复核(chip 几何/动画名/暗亮可见);**发版 SOP=releases.ts 头部插一条+package.json 同步** | ✅ 2026-11-16 晚(issue4-release-notes.md) |
|
||||
| Issue#6·P2 Dashboard 后端 3 端点 | /ops/system/overview(gateway 视角五子系统,up/degraded/down/unknown 三态诚实+admin/ops 门,LagReader 复用闭包注入)+/stats/biz(business[sms/video]+send_type[batch/personal/timer/file] 两维;口径修正留痕:biz_type 属审核域 audit_record 非 task 维)+/stats/realtime(60s 窗覆盖索引 COUNT+Top5 账号+名字);openapi +3;新测试 3 组绿,api 域 lint 0,全量 -race 除 channel 存量 flaky(单跑×2 绿,挂账) | ✅ 2026-11-17(P1 视觉契约 v3.2.1 同日锁定) |
|
||||
| 观察项 | 24h 稳定性门槛(11-13 17:55 起计;期内 21:33 宿主主动重启=已知事件不判失败)期满核验 11-14 17:55 | ✅ 2026-11-14 18:02 终判通过(dmesg 0/零重启/驻留+md5 一致;S5 压测前置确认,S1 完全闭环) |
|
||||
| S2~S5 | 结算域/前端收尾/C1C2+冷热/基线与灰度 | 🔄 S2✅S3✅**S4✅收官**(S5 见《Go-sms-重建计划.md》) |
|
||||
|
||||
|
||||
Reference in New Issue
Block a user