70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package elasticilm
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
// "math"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestEnsureILMPolicy 测试 ensureILMPolicy 函数并生成退化阶段时间矩阵
|
|
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, 3, 1, 0, 0, 0, 0, time.UTC)
|
|
currentDate := time.Date(2025, 4, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
// 初始化结果矩阵
|
|
results := make([][]string, 0)
|
|
|
|
// 遍历每个时间框架
|
|
for _, period := range timeFrames {
|
|
row := make([]string, 0)
|
|
// 遍历每个月
|
|
for d := startDate; !d.After(endDate); d = d.AddDate(0, 1, 0) {
|
|
daysDiff := int(currentDate.Sub(d).Hours() / 24)
|
|
|
|
// 默认配置
|
|
config := map[string]float64{
|
|
"timeDecayFactor": 0.5,
|
|
"periodGranularityFactor": 0.5,
|
|
"warmPhaseMultiplier": 1.0,
|
|
"coldPhaseMultiplier": 2.0,
|
|
"deletePhaseMultiplier": 3.0,
|
|
}
|
|
|
|
// 使用非线性冷却模型计算各个阶段的时间
|
|
warmDays, coldDays, deleteDays := NonLinearCoolingModel(daysDiff, period, config)
|
|
|
|
// 格式化结果
|
|
result := fmt.Sprintf("Warm: %d, Cold: %d, Delete: %d", warmDays, coldDays, deleteDays)
|
|
row = append(row, result)
|
|
}
|
|
results = append(results, row)
|
|
}
|
|
|
|
// 将结果存储为结构化数据
|
|
resultMap := make(map[string][]map[string]string)
|
|
for i, period := range timeFrames {
|
|
periodResults := make([]map[string]string, 0)
|
|
for j, result := range results[i] {
|
|
month := startDate.AddDate(0, j, 0).Format("2006-01")
|
|
periodResults = append(periodResults, map[string]string{"Month": month, "Result": result})
|
|
}
|
|
resultMap[period] = periodResults
|
|
}
|
|
|
|
// 将结果编码为JSON格式
|
|
jsonData, err := json.MarshalIndent(resultMap, "", " ")
|
|
if err != nil {
|
|
t.Fatalf("Failed to encode results to JSON: %v", err)
|
|
}
|
|
|
|
// 打印JSON结果
|
|
fmt.Println(string(jsonData))
|
|
}
|