测试ilm策略
This commit is contained in:
		
							parent
							
								
									78fd73aec0
								
							
						
					
					
						commit
						321e6a2402
					
				| @ -24,25 +24,47 @@ type DefaultRetentionCalculator struct { | |||||||
| 	MinRetention int | 	MinRetention int | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Calculate 计算保留时间(天) | // NonLinearCoolingModel 非线性冷却模型 | ||||||
| func (c DefaultRetentionCalculator) Calculate(daysDiff int, period string) int { | func NonLinearCoolingModel(daysDiff int, period string, config map[string]float64) (int, int, int) { | ||||||
| 	minutes := periodToMinutes(period) | 	minutes := periodToMinutes(period) | ||||||
| 	maxRetention := c.MaxRetention[period] |  | ||||||
| 	if maxRetention == 0 { |  | ||||||
| 		maxRetention = c.MaxRetention["default"] |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	// 非线性衰减公式 | 	// 可配置的系数 | ||||||
| 	timeFactor := 1 - math.Sqrt(float64(daysDiff))/50 // 距今时间因子 | 	timeDecayFactor := config["timeDecayFactor"]                 // 距今时间因子 | ||||||
|  | 	periodGranularityFactor := config["periodGranularityFactor"] // 时间框架粒度因子 | ||||||
|  | 	warmPhaseMultiplier := config["warmPhaseMultiplier"]         // warm阶段乘数 | ||||||
|  | 	coldPhaseMultiplier := config["coldPhaseMultiplier"]         // cold阶段乘数 | ||||||
|  | 	deletePhaseMultiplier := config["deletePhaseMultiplier"]     // delete阶段乘数 | ||||||
|  | 
 | ||||||
|  | 	// 距今时间因子:越久远,冷却越快 | ||||||
|  | 	timeFactor := 1 - math.Pow(float64(daysDiff), timeDecayFactor)/100 | ||||||
| 	if timeFactor < 0 { | 	if timeFactor < 0 { | ||||||
| 		timeFactor = 0 | 		timeFactor = 0 | ||||||
| 	} | 	} | ||||||
| 	periodFactor := math.Sqrt(float64(minutes)) / math.Sqrt(43200) // 时间框架因子 | 
 | ||||||
| 	retention := float64(maxRetention) * timeFactor * periodFactor | 	// 时间框架因子:粒度越细,冷却越快 | ||||||
| 	if retention < float64(c.MinRetention) { | 	periodFactor := math.Pow(float64(minutes), periodGranularityFactor) / math.Pow(43200, periodGranularityFactor) | ||||||
| 		return c.MinRetention | 
 | ||||||
|  | 	// 计算各个阶段的时间 | ||||||
|  | 	warmDays := int(float64(daysDiff) * warmPhaseMultiplier * timeFactor * periodFactor) | ||||||
|  | 	coldDays := int(float64(daysDiff) * coldPhaseMultiplier * timeFactor * periodFactor) | ||||||
|  | 	deleteDays := int(float64(daysDiff) * deletePhaseMultiplier * timeFactor * periodFactor) | ||||||
|  | 
 | ||||||
|  | 	return warmDays, coldDays, deleteDays | ||||||
| } | } | ||||||
| 	return int(retention) | 
 | ||||||
|  | // Calculate 使用非线性冷却模型计算保留时间 | ||||||
|  | func (c DefaultRetentionCalculator) Calculate(daysDiff int, period string) int { | ||||||
|  | 	// 默认配置 | ||||||
|  | 	config := map[string]float64{ | ||||||
|  | 		"timeDecayFactor":         0.5, | ||||||
|  | 		"periodGranularityFactor": 0.5, | ||||||
|  | 		"warmPhaseMultiplier":     1.0, | ||||||
|  | 		"coldPhaseMultiplier":     2.0, | ||||||
|  | 		"deletePhaseMultiplier":   3.0, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	_, _, deleteDays := NonLinearCoolingModel(daysDiff, period, config) | ||||||
|  | 	return deleteDays | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // periodToMinutes 将时间框架转换为分钟数 | // periodToMinutes 将时间框架转换为分钟数 | ||||||
| @ -96,7 +118,7 @@ func getPhase(daysDiff, retentionDays int) string { | |||||||
| 	fmt.Printf("[ILM Phase] 数据在7天到2年之间,判定为warm阶段。daysDiff: %d\n", daysDiff) | 	fmt.Printf("[ILM Phase] 数据在7天到2年之间,判定为warm阶段。daysDiff: %d\n", daysDiff) | ||||||
| 	return "warm" | 	return "warm" | ||||||
| } | } | ||||||
| func ensureAlias(client *http.Client, esConfig cfg.ElasticsearchConfig, alias, period, dataType, coinPair string, indexTime time.Time) error { | func ensureAlias(client *http.Client, esConfig cfg.ElasticsearchConfig, alias, period, dataType, coinPair string, indexTime time.Time, dataConfig cfg.DataTypeConfig, daysDiff int) error { | ||||||
| 	// 获取当前别名关联的索引 | 	// 获取当前别名关联的索引 | ||||||
| 	getAliasURL := fmt.Sprintf("%s/_alias/%s", esConfig.URL, alias) | 	getAliasURL := fmt.Sprintf("%s/_alias/%s", esConfig.URL, alias) | ||||||
| 	req, err := http.NewRequest("GET", getAliasURL, nil) | 	req, err := http.NewRequest("GET", getAliasURL, nil) | ||||||
| @ -242,6 +264,13 @@ func ensureAlias(client *http.Client, esConfig cfg.ElasticsearchConfig, alias, p | |||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	defer resp.Body.Close() | 	defer resp.Body.Close() | ||||||
|  | 	// 加载冷却模型配置 | ||||||
|  | 	coolingConfig := dataConfig.CoolingModelConfig // Ensure this matches the actual field name | ||||||
|  | 
 | ||||||
|  | 	// 使用非线性冷却模型计算各个阶段的时间 | ||||||
|  | 	warmDays, coldDays, deleteDays := NonLinearCoolingModel(daysDiff, period, coolingConfig) | ||||||
|  | 
 | ||||||
|  | 	fmt.Printf("[ILM Policy] Calculated phases: warm=%dd, cold=%dd, delete=%dd\n", warmDays, coldDays, deleteDays) | ||||||
| 
 | 
 | ||||||
| 	if resp.StatusCode != http.StatusOK { | 	if resp.StatusCode != http.StatusOK { | ||||||
| 		body, _ := io.ReadAll(resp.Body) | 		body, _ := io.ReadAll(resp.Body) | ||||||
| @ -341,7 +370,7 @@ func ConfigureILM(client *http.Client, config *cfg.Config, dataType, period, coi | |||||||
| 
 | 
 | ||||||
| 	// 设置别名,确保每次调用时传入正确的 aliasName 和 indexTime | 	// 设置别名,确保每次调用时传入正确的 aliasName 和 indexTime | ||||||
| 	// | 	// | ||||||
| 	if err := ensureAlias(client, esConfig, aliasName, period, dataType, coinPair, indexTime); err != nil { | 	if err := ensureAlias(client, esConfig, aliasName, period, dataType, coinPair, indexTime, dataConfig, daysDiff); err != nil { | ||||||
| 		return fmt.Errorf("failed to ensure alias: %v", err) | 		return fmt.Errorf("failed to ensure alias: %v", err) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										56
									
								
								elasticilm/ilm_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								elasticilm/ilm_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,56 @@ | |||||||
|  | package elasticilm | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"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) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// 打印结果矩阵 | ||||||
|  | 	for i, period := range timeFrames { | ||||||
|  | 		fmt.Printf("Time Frame: %s\n", period) | ||||||
|  | 		for j, result := range results[i] { | ||||||
|  | 			fmt.Printf("  Month: %s - %s\n", startDate.AddDate(0, j, 0).Format("2006-01"), result) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										1236
									
								
								elasticilm/warmDay.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1236
									
								
								elasticilm/warmDay.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												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