ilm 策略生成相关函数测试通过
This commit is contained in:
		
							parent
							
								
									ac694e7c92
								
							
						
					
					
						commit
						14810ba8c0
					
				| @ -44,12 +44,12 @@ | |||||||
|                        "delete": 365 |                        "delete": 365 | ||||||
|                     }, |                     }, | ||||||
|                     "cooling_model_config": { |                     "cooling_model_config": { | ||||||
|                         "timeDecayFactor":         0.5,  // Controls logarithmic growth rate |                         "timeDecayFactor":         10, | ||||||
|                         "baseRetention":           300.0, // Base retention multiplier |                         "baseRetention":           1.0, | ||||||
|                         "periodGranularityFactor": 7,  // Exponent for period scaling |                         "periodGranularityFactor": 0.1, | ||||||
|                         "warmPhaseMultiplier":     10.0,  // Kept for compatibility |                         "warmPhaseMultiplier":     1.0, | ||||||
|                         "coldPhaseMultiplier":     20.0,  // Kept for compatibility |                         "coldPhaseMultiplier":     2.0, | ||||||
|                         "deletePhaseMultiplier":   40.0,  // Kept for compatibility |                         "deletePhaseMultiplier":   5.0 | ||||||
|                     } |                     } | ||||||
|                 }, |                 }, | ||||||
|                 "ma": { |                 "ma": { | ||||||
|  | |||||||
| @ -50,15 +50,26 @@ func NonLinearCoolingModel(daysDiff int, period string, config map[string]float6 | |||||||
| 		cfg = periodBaseConfig["1m"] // 默认配置 | 		cfg = periodBaseConfig["1m"] // 默认配置 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	// 从配置获取参数,设置默认值 | ||||||
|  | 	timeDecayFactor := 0.5 | ||||||
|  | 	if td, ok := config["timeDecayFactor"]; ok { | ||||||
|  | 		timeDecayFactor = td | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	// 反转时间衰减因子:数据越新,保留时间越长 | 	// 反转时间衰减因子:数据越新,保留时间越长 | ||||||
| 	// 使用反比例函数:1/(x+1) 确保新数据(小daysDiff)获得更大值 | 	// 使用反比例函数:1/(x+1) 确保新数据(小daysDiff)获得更大值 | ||||||
| 	ageFactor := 1.0 / (float64(daysDiff)/365.0 + 1.0) | 	ageFactor := timeDecayFactor / (float64(daysDiff)/365.0 + 1.0) | ||||||
| 
 | 
 | ||||||
| 	// 计算基础保留时间(与时间框架相关) | 	// 计算基础保留时间(与时间框架相关) | ||||||
| 	base := cfg.Base | 	base := cfg.Base | ||||||
| 
 | 
 | ||||||
| 	// 应用年龄因子 - 新数据获得更长保留时间 | 	// 应用年龄因子和阶段乘数 - 新数据获得更长保留时间 | ||||||
| 	warm := int(float64(base) * (1.0 + ageFactor)) | 	warmPhaseMultiplier := 1.0 | ||||||
|  | 	if wm, ok := config["warmPhaseMultiplier"]; ok { | ||||||
|  | 		warmPhaseMultiplier = wm | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	warm := int(float64(base) * warmPhaseMultiplier * (1.0 + ageFactor)) | ||||||
| 	cold := warm * 2 | 	cold := warm * 2 | ||||||
| 	delete := warm * 3 | 	delete := warm * 3 | ||||||
| 
 | 
 | ||||||
| @ -73,6 +84,9 @@ func NonLinearCoolingModel(daysDiff int, period string, config map[string]float6 | |||||||
| 		delete = warm * 3 | 		delete = warm * 3 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	// 打印计算结果 | ||||||
|  | 	fmt.Printf("[Cooling Model] Period: %s, DaysDiff: %d => warm=%dd, cold=%dd, delete=%dd\n", | ||||||
|  | 		period, daysDiff, warm, cold, delete) | ||||||
| 	return warm, cold, delete | 	return warm, cold, delete | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -2,12 +2,33 @@ package elasticilm | |||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	cfg "gitea.zjmud.xyz/phyer/tanya/config" // 导入你的 config 包 | ||||||
|  | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	// "math" |  | ||||||
| 	"testing" | 	"testing" | ||||||
| 	"time" | 	"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格式) | // TestEnsureILMPolicy 测试 ensureILMPolicy 函数并生成warm阶段时间矩阵(CSV格式) | ||||||
| func TestEnsureILMPolicy(t *testing.T) { | func TestEnsureILMPolicy(t *testing.T) { | ||||||
| 	// 定义时间框架 | 	// 定义时间框架 | ||||||
| @ -15,7 +36,7 @@ func TestEnsureILMPolicy(t *testing.T) { | |||||||
| 
 | 
 | ||||||
| 	// 定义日期范围 | 	// 定义日期范围 | ||||||
| 	startDate := time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC) | 	startDate := time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC) | ||||||
| 	endDate := time.Date(2025, 3, 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) | 	currentDate := time.Date(2025, 4, 1, 0, 0, 0, 0, time.UTC) | ||||||
| 
 | 
 | ||||||
| 	// 初始化HTML输出 | 	// 初始化HTML输出 | ||||||
| @ -50,21 +71,17 @@ func TestEnsureILMPolicy(t *testing.T) { | |||||||
| 		daysDiff := int(currentDate.Sub(d).Hours() / 24) | 		daysDiff := int(currentDate.Sub(d).Hours() / 24) | ||||||
| 
 | 
 | ||||||
| 		htmlOutput.WriteString(`<tr><td class="month">` + month + `</td>`) | 		htmlOutput.WriteString(`<tr><td class="month">` + month + `</td>`) | ||||||
| 
 | 		config := loadConfig(t) | ||||||
| 		// 遍历每个时间框架 | 		// 遍历每个时间框架 | ||||||
| 		for _, period := range timeFrames { | 		for _, period := range timeFrames { | ||||||
| 			// 默认配置 | 			// 默认配置 | ||||||
| 			config := map[string]float64{ | 			// 加载配置文件 | ||||||
| 				"timeDecayFactor":         0.3, | 			// 尝试从不同层级加载配置 | ||||||
| 				"periodGranularityFactor": 0.7, |  | ||||||
| 				"warmPhaseMultiplier":     1.0, |  | ||||||
| 				"coldPhaseMultiplier":     2.0, |  | ||||||
| 				"deletePhaseMultiplier":   3.0, |  | ||||||
| 			} |  | ||||||
| 
 | 
 | ||||||
|  | 			// 获取candle类型的冷却模型配置 | ||||||
|  | 			coolingConfig := config.Elasticsearch.ILM.DataTypes["candle"].CoolingModelConfig | ||||||
| 			// 计算warm阶段时间 | 			// 计算warm阶段时间 | ||||||
| 			warmDays, _, _ := NonLinearCoolingModel(daysDiff, period, config) | 			warmDays, _, _ := NonLinearCoolingModel(daysDiff, period, coolingConfig) | ||||||
| 
 |  | ||||||
| 			// 计算颜色 (0-255范围) 基于0-1800的范围 | 			// 计算颜色 (0-255范围) 基于0-1800的范围 | ||||||
| 			colorValue := int(float64(warmDays) / 2500 * 255) | 			colorValue := int(float64(warmDays) / 2500 * 255) | ||||||
| 			if colorValue > 255 { | 			if colorValue > 255 { | ||||||
| @ -82,9 +99,113 @@ func TestEnsureILMPolicy(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	htmlOutput.WriteString(`</table> | 	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> | </body> | ||||||
| </html>`) | </html>`) | ||||||
| 
 | 
 | ||||||
| 	// 打印HTML结果 | 	// 写入到 warm.html 文件 | ||||||
| 	fmt.Println(htmlOutput.String()) | 	if err := os.WriteFile("warm.html", []byte(htmlOutput.String()), 0644); err != nil { | ||||||
|  | 		t.Fatalf("Failed to write HTML file: %v", err) | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										29
									
								
								elasticilm/warm.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								elasticilm/warm.html
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @ -80,7 +80,7 @@ import ( | |||||||
| func TestCandleListI_CalculateCrossPair(t *testing.T) { | func TestCandleListI_CalculateCrossPair(t *testing.T) { | ||||||
| 	// 使用更早的时间范围来触发不同的phase | 	// 使用更早的时间范围来触发不同的phase | ||||||
| 	startTime := time.Date(2024, 3, 1, 0, 0, 0, 0, time.UTC) | 	startTime := time.Date(2024, 3, 1, 0, 0, 0, 0, time.UTC) | ||||||
| 	endTime := time.Date(2024, 3, 31, 0, 0, 0, 0, time.UTC) | 	endTime := time.Date(2024, 3, 5, 0, 0, 0, 0, time.UTC) | ||||||
| 
 | 
 | ||||||
| 	// 打印测试时间范围 | 	// 打印测试时间范围 | ||||||
| 	t.Logf("Test time range: %s to %s", startTime, endTime) | 	t.Logf("Test time range: %s to %s", startTime, endTime) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zhangkun9038@dingtalk.com
						zhangkun9038@dingtalk.com