渐变衰减
This commit is contained in:
		
							parent
							
								
									6c64c0438b
								
							
						
					
					
						commit
						f4a720cec8
					
				| @ -6,7 +6,6 @@ import ( | |||||||
| 	"fmt" | 	"fmt" | ||||||
| 	cfg "gitea.zjmud.xyz/phyer/tanya/config" // 导入你的 config 包 | 	cfg "gitea.zjmud.xyz/phyer/tanya/config" // 导入你的 config 包 | ||||||
| 	"io" | 	"io" | ||||||
| 	"math" |  | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| @ -24,45 +23,57 @@ type DefaultRetentionCalculator struct { | |||||||
| 	MinRetention int | 	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) { | func NonLinearCoolingModel(daysDiff int, period string, config map[string]float64) (int, int, int) { | ||||||
| 	minutes := periodToMinutes(period) | 	// 获取基础配置 | ||||||
| 
 | 	cfg, ok := periodBaseConfig[period] | ||||||
| 	// 可配置的系数 | 	if !ok { | ||||||
| 	timeDecayFactor := config["timeDecayFactor"] // 距今时间因子 | 		cfg = periodBaseConfig["1m"] // 默认配置 | ||||||
| 	// 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 |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// 时间框架因子调整:粒度越细保留时间越长 | 	// 反转时间衰减因子:数据越新,保留时间越长 | ||||||
| 	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)) | 	warm := int(float64(base) * (1.0 + ageFactor)) | ||||||
| 	coldDays := 2*baseDays + int(float64(2000)/float64(daysDiff+1)) | 	cold := warm * 2 | ||||||
| 	deleteDays := 3*baseDays + int(float64(3000)/float64(daysDiff+1)) | 	delete := warm * 3 | ||||||
| 
 | 
 | ||||||
| 	// 保证最小阶段关系 | 	// 确保最小值 | ||||||
| 	if warmDays < baseDays { | 	if warm < cfg.Base { | ||||||
| 		warmDays = baseDays | 		warm = cfg.Base | ||||||
| 	} | 	} | ||||||
| 	if coldDays < 2*baseDays { | 	if cold < warm*2 { | ||||||
| 		coldDays = 2 * baseDays | 		cold = warm * 2 | ||||||
| 	} | 	} | ||||||
| 	if deleteDays < 3*baseDays { | 	if delete < warm*3 { | ||||||
| 		deleteDays = 3 * baseDays | 		delete = warm * 3 | ||||||
| 	} | 	} | ||||||
| 	return warmDays, coldDays, deleteDays | 
 | ||||||
|  | 	return warm, cold, delete | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Calculate 使用非线性冷却模型计算保留时间 | // Calculate 使用非线性冷却模型计算保留时间 | ||||||
|  | |||||||
| @ -1,14 +1,14 @@ | |||||||
| package elasticilm | package elasticilm | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"encoding/json" |  | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"strings" | ||||||
| 	// "math" | 	// "math" | ||||||
| 	"testing" | 	"testing" | ||||||
| 	"time" | 	"time" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // TestEnsureILMPolicy 测试 ensureILMPolicy 函数并生成退化阶段时间矩阵 | // TestEnsureILMPolicy 测试 ensureILMPolicy 函数并生成warm阶段时间矩阵(CSV格式) | ||||||
| func TestEnsureILMPolicy(t *testing.T) { | func TestEnsureILMPolicy(t *testing.T) { | ||||||
| 	// 定义时间框架 | 	// 定义时间框架 | ||||||
| 	timeFrames := []string{"1m", "3m", "5m", "15m", "30m", "1H", "2H", "4H", "6H", "12H", "1D", "2D", "5D", "1W"} | 	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) | 	endDate := time.Date(2025, 3, 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输出 | ||||||
| 	results := make([][]string, 0) | 	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>`) | ||||||
| 
 | 
 | ||||||
| 		// 遍历每个时间框架 | 		// 遍历每个时间框架 | ||||||
| 		for _, period := range timeFrames { | 		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{ | 			config := map[string]float64{ | ||||||
| 				"timeDecayFactor":         0.5, | 				"timeDecayFactor":         0.3, | ||||||
| 				"periodGranularityFactor": 0.5, | 				"periodGranularityFactor": 0.7, | ||||||
| 				"warmPhaseMultiplier":     1.0, | 				"warmPhaseMultiplier":     1.0, | ||||||
| 				"coldPhaseMultiplier":     2.0, | 				"coldPhaseMultiplier":     2.0, | ||||||
| 				"deletePhaseMultiplier":   3.0, | 				"deletePhaseMultiplier":   3.0, | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			// 使用非线性冷却模型计算各个阶段的时间 | 			// 计算warm阶段时间 | ||||||
| 			warmDays, coldDays, deleteDays := NonLinearCoolingModel(daysDiff, period, config) | 			warmDays, _, _ := NonLinearCoolingModel(daysDiff, period, config) | ||||||
| 
 | 
 | ||||||
| 			// 格式化结果 | 			// 计算颜色 (0-255范围) 基于0-1800的范围 | ||||||
| 			result := fmt.Sprintf("Warm: %d, Cold: %d, Delete: %d", warmDays, coldDays, deleteDays) | 			colorValue := int(float64(warmDays) / 2500 * 255) | ||||||
| 			row = append(row, result) | 			if colorValue > 255 { | ||||||
|  | 				colorValue = 255 | ||||||
| 			} | 			} | ||||||
| 		results = append(results, row) | 			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> | ||||||
| 	resultMap := make(map[string][]map[string]string) | </body> | ||||||
| 	for i, period := range timeFrames { | </html>`) | ||||||
| 		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格式 | 	// 打印HTML结果 | ||||||
| 	jsonData, err := json.MarshalIndent(resultMap, "", "  ") | 	fmt.Println(htmlOutput.String()) | ||||||
| 	if err != nil { |  | ||||||
| 		t.Fatalf("Failed to encode results to JSON: %v", err) |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	// 打印JSON结果 |  | ||||||
| 	fmt.Println(string(jsonData)) |  | ||||||
| } | } | ||||||
|  | |||||||
										
											
												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