渐变衰减
This commit is contained in:
		
							parent
							
								
									6c64c0438b
								
							
						
					
					
						commit
						f4a720cec8
					
				| @ -6,7 +6,6 @@ import ( | ||||
| 	"fmt" | ||||
| 	cfg "gitea.zjmud.xyz/phyer/tanya/config" // 导入你的 config 包 | ||||
| 	"io" | ||||
| 	"math" | ||||
| 	"net/http" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| @ -24,45 +23,57 @@ type DefaultRetentionCalculator struct { | ||||
| 	MinRetention int | ||||
| } | ||||
| 
 | ||||
| // NonLinearCoolingModel 非线性冷却模型 | ||||
| var periodBaseConfig = map[string]struct { | ||||
| 	Base     int     // 基础值 | ||||
| 	Interval float64 // 时间间隔系数 | ||||
| }{ | ||||
| 	"1m":  {30, 1.0}, | ||||
| 	"3m":  {45, 1.5}, | ||||
| 	"5m":  {60, 2.0}, | ||||
| 	"15m": {90, 3.0}, | ||||
| 	"30m": {120, 4.0}, | ||||
| 	"1H":  {150, 5.0}, | ||||
| 	"2H":  {180, 6.0}, | ||||
| 	"4H":  {240, 8.0}, | ||||
| 	"6H":  {300, 10.0}, | ||||
| 	"12H": {360, 12.0}, | ||||
| 	"1D":  {480, 16.0}, | ||||
| 	"2D":  {600, 20.0}, | ||||
| 	"5D":  {720, 24.0}, | ||||
| 	"1W":  {840, 28.0}, | ||||
| } | ||||
| 
 | ||||
| func NonLinearCoolingModel(daysDiff int, period string, config map[string]float64) (int, int, int) { | ||||
| 	minutes := periodToMinutes(period) | ||||
| 
 | ||||
| 	// 可配置的系数 | ||||
| 	timeDecayFactor := config["timeDecayFactor"] // 距今时间因子 | ||||
| 	// periodGranularityFactor := config["periodGranularityFactor"] // 时间框架粒度因子 | ||||
| 	// warmPhaseMultiplier := config["warmPhaseMultiplier"]         // warm阶段乘数 | ||||
| 	// coldPhaseMultiplier := config["coldPhaseMultiplier"]         // cold阶段乘数 | ||||
| 	// deletePhaseMultiplier := config["deletePhaseMultiplier"]     // delete阶段乘数 | ||||
| 
 | ||||
| 	// 时间衰减因子反转:越新的数据保留越久 | ||||
| 	timeFactor := math.Pow(100.0, timeDecayFactor) / math.Pow(float64(daysDiff+1), timeDecayFactor) | ||||
| 	if timeFactor > 1 { | ||||
| 		timeFactor = 1 | ||||
| 	// 获取基础配置 | ||||
| 	cfg, ok := periodBaseConfig[period] | ||||
| 	if !ok { | ||||
| 		cfg = periodBaseConfig["1m"] // 默认配置 | ||||
| 	} | ||||
| 
 | ||||
| 	// 时间框架因子调整:粒度越细保留时间越长 | ||||
| 	periodFactor := math.Pow(float64(minutes)/1440, 0.5) // 基于天数的平方根比例 | ||||
| 	// 反转时间衰减因子:数据越新,保留时间越长 | ||||
| 	// 使用反比例函数:1/(x+1) 确保新数据(小daysDiff)获得更大值 | ||||
| 	ageFactor := 1.0 / (float64(daysDiff)/365.0 + 1.0) | ||||
| 
 | ||||
| 	// 基础保留天数计算(与时间框架成正比) | ||||
| 	baseDays := int(30 * periodFactor) // 30天基础值 | ||||
| 	// 计算基础保留时间(与时间框架相关) | ||||
| 	base := cfg.Base | ||||
| 
 | ||||
| 	// 阶段持续时间计算(与距今时间成反比) | ||||
| 	warmDays := baseDays + int(float64(1000)/float64(daysDiff+1)) | ||||
| 	coldDays := 2*baseDays + int(float64(2000)/float64(daysDiff+1)) | ||||
| 	deleteDays := 3*baseDays + int(float64(3000)/float64(daysDiff+1)) | ||||
| 	// 应用年龄因子 - 新数据获得更长保留时间 | ||||
| 	warm := int(float64(base) * (1.0 + ageFactor)) | ||||
| 	cold := warm * 2 | ||||
| 	delete := warm * 3 | ||||
| 
 | ||||
| 	// 保证最小阶段关系 | ||||
| 	if warmDays < baseDays { | ||||
| 		warmDays = baseDays | ||||
| 	// 确保最小值 | ||||
| 	if warm < cfg.Base { | ||||
| 		warm = cfg.Base | ||||
| 	} | ||||
| 	if coldDays < 2*baseDays { | ||||
| 		coldDays = 2 * baseDays | ||||
| 	if cold < warm*2 { | ||||
| 		cold = warm * 2 | ||||
| 	} | ||||
| 	if deleteDays < 3*baseDays { | ||||
| 		deleteDays = 3 * baseDays | ||||
| 	if delete < warm*3 { | ||||
| 		delete = warm * 3 | ||||
| 	} | ||||
| 	return warmDays, coldDays, deleteDays | ||||
| 
 | ||||
| 	return warm, cold, delete | ||||
| } | ||||
| 
 | ||||
| // Calculate 使用非线性冷却模型计算保留时间 | ||||
|  | ||||
| @ -1,14 +1,14 @@ | ||||
| package elasticilm | ||||
| 
 | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"strings" | ||||
| 	// "math" | ||||
| 	"testing" | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| // TestEnsureILMPolicy 测试 ensureILMPolicy 函数并生成退化阶段时间矩阵 | ||||
| // 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"} | ||||
| @ -18,52 +18,73 @@ func TestEnsureILMPolicy(t *testing.T) { | ||||
| 	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) | ||||
| 	// 初始化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 { | ||||
| 		row := make([]string, 0) | ||||
| 		// 遍历每个月 | ||||
| 		for d := startDate; !d.After(endDate); d = d.AddDate(0, 1, 0) { | ||||
| 			daysDiff := int(currentDate.Sub(d).Hours() / 24) | ||||
| 		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>`) | ||||
| 
 | ||||
| 		// 遍历每个时间框架 | ||||
| 		for _, period := range timeFrames { | ||||
| 			// 默认配置 | ||||
| 			config := map[string]float64{ | ||||
| 				"timeDecayFactor":         0.5, | ||||
| 				"periodGranularityFactor": 0.5, | ||||
| 				"timeDecayFactor":         0.3, | ||||
| 				"periodGranularityFactor": 0.7, | ||||
| 				"warmPhaseMultiplier":     1.0, | ||||
| 				"coldPhaseMultiplier":     2.0, | ||||
| 				"deletePhaseMultiplier":   3.0, | ||||
| 			} | ||||
| 
 | ||||
| 			// 使用非线性冷却模型计算各个阶段的时间 | ||||
| 			warmDays, coldDays, deleteDays := NonLinearCoolingModel(daysDiff, period, config) | ||||
| 			// 计算warm阶段时间 | ||||
| 			warmDays, _, _ := NonLinearCoolingModel(daysDiff, period, config) | ||||
| 
 | ||||
| 			// 格式化结果 | ||||
| 			result := fmt.Sprintf("Warm: %d, Cold: %d, Delete: %d", warmDays, coldDays, deleteDays) | ||||
| 			row = append(row, result) | ||||
| 			// 计算颜色 (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, | ||||
| 			)) | ||||
| 		} | ||||
| 		results = append(results, row) | ||||
| 		htmlOutput.WriteString(`</tr>`) | ||||
| 	} | ||||
| 
 | ||||
| 	// 将结果存储为结构化数据 | ||||
| 	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 | ||||
| 	} | ||||
| 	htmlOutput.WriteString(`</table> | ||||
| </body> | ||||
| </html>`) | ||||
| 
 | ||||
| 	// 将结果编码为JSON格式 | ||||
| 	jsonData, err := json.MarshalIndent(resultMap, "", "  ") | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Failed to encode results to JSON: %v", err) | ||||
| 	} | ||||
| 
 | ||||
| 	// 打印JSON结果 | ||||
| 	fmt.Println(string(jsonData)) | ||||
| 	// 打印HTML结果 | ||||
| 	fmt.Println(htmlOutput.String()) | ||||
| } | ||||
|  | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zhangkun9038@dingtalk.com
						zhangkun9038@dingtalk.com