tanya/elasticilm/ilm_test.go
2025-03-30 19:32:12 +08:00

212 lines
5.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package elasticilm
import (
"fmt"
cfg "gitea.zjmud.xyz/phyer/tanya/config" // 导入你的 config 包
"os"
"strings"
"testing"
"time"
)
func loadConfig(t *testing.T) *cfg.Config {
configPaths := []string{
"config/config.json",
"../config/config.json",
"../../config/config.json",
}
var config *cfg.Config
var err error
for _, path := range configPaths {
config, err = cfg.LoadConfig(path)
if err == nil {
return config
}
}
t.Fatalf("failed to load config after trying paths: %v. Tried paths: %v", err, configPaths)
return nil // 不会到达这里
}
// TestEnsureILMPolicy 测试 ensureILMPolicy 函数并生成warm阶段时间矩阵(CSV格式)
func TestEnsureILMPolicy(t *testing.T) {
// 定义时间框架
timeFrames := []string{"1m", "3m", "5m", "15m", "30m", "1H", "2H", "4H", "6H", "12H", "1D", "2D", "5D", "1W"}
// 定义日期范围
startDate := time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)
endDate := time.Date(2025, 6, 1, 0, 0, 0, 0, time.UTC)
currentDate := time.Date(2025, 4, 1, 0, 0, 0, 0, time.UTC)
// 初始化HTML输出
var htmlOutput strings.Builder
htmlOutput.WriteString(`<!DOCTYPE html>
<html>
<head>
<title>ILM Warm Phase Matrix</title>
<style>
body { font-family: Arial, sans-serif; }
table { border-collapse: collapse; margin: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: center; }
th { background-color: #f2f2f2; }
.month { font-weight: bold; }
</style>
</head>
<body>
<h1>ILM Warm Phase Duration Matrix</h1>
<table>
<tr>
<th>Month</th>`)
// 写入表头
for _, period := range timeFrames {
htmlOutput.WriteString(fmt.Sprintf(`<th>%s</th>`, period))
}
htmlOutput.WriteString(`</tr>`)
// 遍历每个月
for d := startDate; !d.After(endDate); d = d.AddDate(0, 1, 0) {
month := d.Format("2006-01")
daysDiff := int(currentDate.Sub(d).Hours() / 24)
htmlOutput.WriteString(`<tr><td class="month">` + month + `</td>`)
config := loadConfig(t)
// 遍历每个时间框架
for _, period := range timeFrames {
// 默认配置
// 加载配置文件
// 尝试从不同层级加载配置
// 获取candle类型的冷却模型配置
coolingConfig := config.Elasticsearch.ILM.DataTypes["candle"].CoolingModelConfig
// 计算warm阶段时间
warmDays, _, _ := NonLinearCoolingModel(daysDiff, period, coolingConfig)
// 计算颜色 (0-255范围) 基于0-1800的范围
colorValue := int(float64(warmDays) / 2500 * 255)
if colorValue > 255 {
colorValue = 255
}
bgColor := fmt.Sprintf("rgb(%d, 0, %d)", colorValue, 255-colorValue)
htmlOutput.WriteString(fmt.Sprintf(
`<td style="background-color: %s">%d</td>`,
bgColor,
warmDays,
))
}
htmlOutput.WriteString(`</tr>`)
}
htmlOutput.WriteString(`</table>
<h1>ILM Cold Phase Duration Matrix</h1>
<table>
<tr>
<th>Month</th>`)
// 写入cold表头
for _, period := range timeFrames {
htmlOutput.WriteString(fmt.Sprintf(`<th>%s</th>`, period))
}
htmlOutput.WriteString(`</tr>`)
// 遍历每个月生成cold矩阵
for d := startDate; !d.After(endDate); d = d.AddDate(0, 1, 0) {
month := d.Format("2006-01")
daysDiff := int(currentDate.Sub(d).Hours() / 24)
htmlOutput.WriteString(`<tr><td class="month">` + month + `</td>`)
// 遍历每个时间框架
for _, period := range timeFrames {
// 默认配置
config := map[string]float64{
"timeDecayFactor": 10,
"baseRetention": 1,
"periodGranularityFactor": 0.1,
"warmPhaseMultiplier": 1.0,
"coldPhaseMultiplier": 2.0,
"deletePhaseMultiplier": 5.0,
}
// 计算cold阶段时间
_, coldDays, _ := NonLinearCoolingModel(daysDiff, period, config)
// 计算颜色 (0-255范围) 基于0-2000的范围
colorValue := int(float64(coldDays) / 2000 * 255)
if colorValue > 255 {
colorValue = 255
}
bgColor := fmt.Sprintf("rgb(%d, 0, %d)", colorValue, 255-colorValue)
htmlOutput.WriteString(fmt.Sprintf(
`<td style="background-color: %s">%d</td>`,
bgColor,
coldDays,
))
}
htmlOutput.WriteString(`</tr>`)
}
htmlOutput.WriteString(`</table>
<h1>ILM Delete Phase Duration Matrix</h1>
<table>
<tr>
<th>Month</th>`)
// 写入delete表头
for _, period := range timeFrames {
htmlOutput.WriteString(fmt.Sprintf(`<th>%s</th>`, period))
}
htmlOutput.WriteString(`</tr>`)
// 遍历每个月生成delete矩阵
for d := startDate; !d.After(endDate); d = d.AddDate(0, 1, 0) {
month := d.Format("2006-01")
daysDiff := int(currentDate.Sub(d).Hours() / 24)
htmlOutput.WriteString(`<tr><td class="month">` + month + `</td>`)
// 遍历每个时间框架
for _, period := range timeFrames {
// 默认配置
config := map[string]float64{
"timeDecayFactor": 10,
"baseRetention": 1,
"periodGranularityFactor": 0.1,
"warmPhaseMultiplier": 1.0,
"coldPhaseMultiplier": 2.0,
"deletePhaseMultiplier": 5.0,
}
// 计算delete阶段时间
_, _, deleteDays := NonLinearCoolingModel(daysDiff, period, config)
// 计算颜色 (0-255范围) 基于0-5000的范围
colorValue := int(float64(deleteDays) / 5000 * 255)
if colorValue > 255 {
colorValue = 255
}
bgColor := fmt.Sprintf("rgb(%d, 0, %d)", colorValue, 255-colorValue)
htmlOutput.WriteString(fmt.Sprintf(
`<td style="background-color: %s">%d</td>`,
bgColor,
deleteDays,
))
}
htmlOutput.WriteString(`</tr>`)
}
htmlOutput.WriteString(`</table>
</body>
</html>`)
// 写入到 warm.html 文件
if err := os.WriteFile("warm.html", []byte(htmlOutput.String()), 0644); err != nil {
t.Fatalf("Failed to write HTML file: %v", err)
}
}