chore: 统一本地服务器与远程仓库源码
- 合并 main 与 master 历史并以服务器结构为基线 - 保留迁移、数据保留、AWS 账单与部署安全能力 - 建立 SDD 规格和 CI 验证闭环
This commit is contained in:
commit
fd60361d93
460 files changed
+37492
-23871
No files matched your search
@@ -0,0 +1,88 @@
|
||||
package retention
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"ezops/config"
|
||||
"ezops/model"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TestRun 验证高频数据按保留期分批清理且不影响新数据。 // Logan.Li
|
||||
func TestRun(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file:retention?mode=memory&cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.AgentReport{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := model.AgentReport{Hostname: "old", ReportTime: time.Now().AddDate(0, 0, -8)}
|
||||
current := model.AgentReport{Hostname: "new", ReportTime: time.Now()}
|
||||
if err := db.Create(&old).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(¤t).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
result, err := Run(db, config.RetentionConfig{AgentReportsDays: 7, CleanupBatchSize: 1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Total != 1 {
|
||||
t.Fatalf("应清理 1 条记录,实际 %d", result.Total)
|
||||
}
|
||||
var count int64
|
||||
if err := db.Model(&model.AgentReport{}).Count(&count).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("应保留 1 条新记录,实际 %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunCleansLegacyMetricTables 验证保留期覆盖尚未下线的旧时序表。 // Logan.Li
|
||||
func TestRunCleansLegacyMetricTables(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file:retention_legacy?mode=memory&cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.MetricData{}, &legacyMetricsAggregate{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.MetricData{Name: "old", Timestamp: time.Now().AddDate(0, 0, -31)}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.MetricData{Name: "current", Timestamp: time.Now()}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Exec("INSERT INTO monitor_metrics_aggregate (bucket_time) VALUES (?), (?)", time.Now().AddDate(0, 0, -366), time.Now()).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
result, err := Run(db, config.RetentionConfig{MetricHistoryDays: 30, MetricAggregateDays: 365, CleanupBatchSize: 1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Total != 2 {
|
||||
t.Fatalf("应清理两条旧指标,实际 %d", result.Total)
|
||||
}
|
||||
assertTableCount(t, db, "monitor_metric_data", 1)
|
||||
assertTableCount(t, db, "monitor_metrics_aggregate", 1)
|
||||
}
|
||||
|
||||
// assertTableCount 断言时序表保留数据量。 // Logan.Li
|
||||
func assertTableCount(t *testing.T, db *gorm.DB, table string, want int64) {
|
||||
t.Helper()
|
||||
var count int64
|
||||
if err := db.Table(table).Count(&count).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != want {
|
||||
t.Fatalf("表 %s 记录数错误: got %d want %d", table, count, want)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user