浏览代码

fix:预测指标分段逻辑调整

Roc 2 年之前
父节点
当前提交
8b4f78e3e8
共有 2 个文件被更改,包括 50 次插入28 次删除
  1. 4 4
      services/data/predict_edb_info.go
  2. 46 24
      services/data/predict_edb_info_rule.go

+ 4 - 4
services/data/predict_edb_info.go

@@ -269,7 +269,7 @@ func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*data_manage.
 				return
 			}
 			tbValue, _ := tmpValDecimal.Float64()
-			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, startDate, dataEndTime, frequency, predictEdbInfoData, existMap)
+			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
 		case 4: //4:同差
 			tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
 			if tmpErr != nil {
@@ -277,7 +277,7 @@ func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*data_manage.
 				return
 			}
 			tcValue, _ := tmpValDecimal.Float64()
-			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, startDate, dataEndTime, frequency, predictEdbInfoData, existMap)
+			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
 		case 5: //5:环比
 			tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
 			if tmpErr != nil {
@@ -285,7 +285,7 @@ func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*data_manage.
 				return
 			}
 			hbValue, _ := tmpValDecimal.Float64()
-			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, startDate, dataEndTime, frequency, predictEdbInfoData, existMap)
+			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
 		case 6: //6:环差
 			tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
 			if tmpErr != nil {
@@ -293,7 +293,7 @@ func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*data_manage.
 				return
 			}
 			hcValue, _ := tmpValDecimal.Float64()
-			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, startDate, dataEndTime, frequency, predictEdbInfoData, existMap)
+			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
 		case 7: //7:N期移动均值
 			nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
 			if tmpErr != nil {

+ 46 - 24
services/data/predict_edb_info_rule.go

@@ -29,9 +29,13 @@ func GetChartPredictEdbInfoDataListByRule1(edbInfoId int, dataValue float64, sta
 //	GetChartPredictEdbInfoDataListByRuleTb 根据同比值规则获取预测数据
 //	2.1 同比: 在未来某一个时间段内,给定一个固定的同比增速a,用去年同期值X乘以同比增速(1+a),得到预测值Y=X(1+a)
 //	例: 今年1-3月值,100,100,120。给定同比增速a=0.1,则明年1-3月预测值为: 100*1.1=110,100*1.1=110,120*1.1=132。
-func GetChartPredictEdbInfoDataListByRuleTb(edbInfoId int, tbValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
+func GetChartPredictEdbInfoDataListByRuleTb(edbInfoId int, tbValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
+	allDataList := make([]*models.EdbDataList, 0)
+	allDataList = append(allDataList, realPredictEdbInfoData...)
+	allDataList = append(allDataList, predictEdbInfoData...)
 	newPredictEdbInfoData = predictEdbInfoData
-	index := len(predictEdbInfoData)
+
+	index := len(allDataList)
 	//获取后面的预测数据
 	dayList := getPredictEdbDayList(startDate, endDate, frequency)
 	predictEdbInfoData = make([]*models.EdbDataList, 0)
@@ -113,12 +117,14 @@ func GetChartPredictEdbInfoDataListByRuleTb(edbInfoId int, tbValue float64, star
 		if calculateStatus {
 			tmpData.Value = val
 			newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
+			allDataList = append(allDataList, tmpData)
+			existMap[tmpData.DataTime] = val
 
 			// 最大最小值
 			if val < minValue {
 				minValue = val
 			}
-			if val < maxValue {
+			if val > maxValue {
 				maxValue = val
 			}
 		}
@@ -154,9 +160,13 @@ func TbzDiv(a, b float64) (result float64) {
 //	GetChartPredictEdbInfoDataListByRuleTc 根据同差值规则获取预测数据
 //	2.2 同差: 在未来某一个时间段内,给定一个固定的同比增加值a,用去年同期值X加上同比增加值A,得到预测值Y=X+a
 //	例: 今年1-3月值,100,100,120。给定同比增加值a=10,则明年1-3月预测值为: 100+10=110,100+10=110,120+10=130
-func GetChartPredictEdbInfoDataListByRuleTc(edbInfoId int, tcValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
+func GetChartPredictEdbInfoDataListByRuleTc(edbInfoId int, tcValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
+	allDataList := make([]*models.EdbDataList, 0)
+	allDataList = append(allDataList, realPredictEdbInfoData...)
+	allDataList = append(allDataList, predictEdbInfoData...)
 	newPredictEdbInfoData = predictEdbInfoData
-	index := len(predictEdbInfoData)
+
+	index := len(allDataList)
 	//获取后面的预测数据
 	dayList := getPredictEdbDayList(startDate, endDate, frequency)
 	predictEdbInfoData = make([]*models.EdbDataList, 0)
@@ -238,12 +248,14 @@ func GetChartPredictEdbInfoDataListByRuleTc(edbInfoId int, tcValue float64, star
 		if calculateStatus {
 			tmpData.Value = val
 			newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
+			allDataList = append(allDataList, tmpData)
+			existMap[tmpData.DataTime] = val
 
 			// 最大最小值
 			if val < minValue {
 				minValue = val
 			}
-			if val < maxValue {
+			if val > maxValue {
 				maxValue = val
 			}
 		}
@@ -273,32 +285,38 @@ func TczDiv(a, b float64) (result float64) {
 //	GetChartPredictEdbInfoDataListByRuleHb 根据环比值规则获取预测数据
 //	环比:在未来某一个时间段内,给定一个固定的环比增速a,用上一期值X乘以环比增速(1+a),得到预测值Y=X(1+a)
 //	例: 最近1期值为100,给定环比增速a=0.2,则未来3期预测值为: 100*1.2=120,120*1.2=144,144*1.2=172.8
-func GetChartPredictEdbInfoDataListByRuleHb(edbInfoId int, hbValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
+func GetChartPredictEdbInfoDataListByRuleHb(edbInfoId int, hbValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
+	allDataList := make([]*models.EdbDataList, 0)
+	allDataList = append(allDataList, realPredictEdbInfoData...)
+	allDataList = append(allDataList, predictEdbInfoData...)
 	newPredictEdbInfoData = predictEdbInfoData
-	index := len(predictEdbInfoData)
+
+	index := len(allDataList)
 	//获取后面的预测数据
 	dayList := getPredictEdbDayList(startDate, endDate, frequency)
 	for k, currentDate := range dayList {
 		tmpK := index + k - 1 //上1期的值
 
 		// 环比值计算
-		val := HbzDiv(newPredictEdbInfoData[tmpK].Value, hbValue)
+		val := HbzDiv(allDataList[tmpK].Value, hbValue)
 
 		currentDateStr := currentDate.Format(utils.FormatDate)
-		newPredictEdbInfoData = append(newPredictEdbInfoData, &models.EdbDataList{
+		tmpData := &models.EdbDataList{
 			EdbDataId:     edbInfoId + 10000000000 + index + k,
 			EdbInfoId:     edbInfoId,
 			DataTime:      currentDateStr,
 			Value:         val,
 			DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
-		})
+		}
+		newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
+		allDataList = append(allDataList, tmpData)
 		existMap[currentDateStr] = val
 
 		// 最大最小值
 		if val < minValue {
 			minValue = val
 		}
-		if val < maxValue {
+		if val > maxValue {
 			maxValue = val
 		}
 	}
@@ -333,32 +351,38 @@ func HbzDiv(a, b float64) (result float64) {
 //	GetChartPredictEdbInfoDataListByRuleHc 根据环差值规则获取预测数据
 //	2.4 环差:在未来某一个时间段内,给定一个固定的环比增加值a,用上一期值X加上环比增加值a,得到预测值Y=X+a
 //	例: 最近1期值为100,给定环比增加值a=10,则未来3期预测值为: 100+10=110,110+10=120,120+10=130
-func GetChartPredictEdbInfoDataListByRuleHc(edbInfoId int, hcValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
+func GetChartPredictEdbInfoDataListByRuleHc(edbInfoId int, hcValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
+	allDataList := make([]*models.EdbDataList, 0)
+	allDataList = append(allDataList, realPredictEdbInfoData...)
+	allDataList = append(allDataList, predictEdbInfoData...)
 	newPredictEdbInfoData = predictEdbInfoData
-	index := len(predictEdbInfoData)
+
+	index := len(allDataList)
 	//获取后面的预测数据
 	dayList := getPredictEdbDayList(startDate, endDate, frequency)
 	for k, currentDate := range dayList {
 		tmpK := index + k - 1 //上1期的值
 
 		// 环差别值计算
-		val := HczDiv(newPredictEdbInfoData[tmpK].Value, hcValue)
+		val := HczDiv(allDataList[tmpK].Value, hcValue)
 
 		currentDateStr := currentDate.Format(utils.FormatDate)
-		newPredictEdbInfoData = append(newPredictEdbInfoData, &models.EdbDataList{
+		tmpData := &models.EdbDataList{
 			EdbDataId:     edbInfoId + 10000000000 + index + k,
 			EdbInfoId:     edbInfoId,
 			DataTime:      currentDateStr,
 			Value:         val,
 			DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
-		})
+		}
+		newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
+		allDataList = append(allDataList, tmpData)
 		existMap[currentDateStr] = val
 
 		// 最大最小值
 		if val < minValue {
 			minValue = val
 		}
-		if val < maxValue {
+		if val > maxValue {
 			maxValue = val
 		}
 	}
@@ -391,7 +415,6 @@ func GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(edbInfoId int, nValue in
 	allDataList := make([]*models.EdbDataList, 0)
 	allDataList = append(allDataList, realPredictEdbInfoData...)
 	allDataList = append(allDataList, predictEdbInfoData...)
-
 	newPredictEdbInfoData = predictEdbInfoData
 
 	lenAllData := len(allDataList)
@@ -436,7 +459,7 @@ func GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(edbInfoId int, nValue in
 		if val < minValue {
 			minValue = val
 		}
-		if val < maxValue {
+		if val > maxValue {
 			maxValue = val
 		}
 	}
@@ -458,7 +481,6 @@ func GetChartPredictEdbInfoDataListByRuleNLinearRegression(edbInfoId int, nValue
 	allDataList := make([]*models.EdbDataList, 0)
 	allDataList = append(allDataList, realPredictEdbInfoData...)
 	allDataList = append(allDataList, predictEdbInfoData...)
-
 	newPredictEdbInfoData = predictEdbInfoData
 
 	lenAllData := len(allDataList)
@@ -466,7 +488,7 @@ func GetChartPredictEdbInfoDataListByRuleNLinearRegression(edbInfoId int, nValue
 		return
 	}
 
-	if nValue <= 0 {
+	if nValue <= 1 {
 		return
 	}
 
@@ -510,7 +532,7 @@ func GetChartPredictEdbInfoDataListByRuleNLinearRegression(edbInfoId int, nValue
 		if val < minValue {
 			minValue = val
 		}
-		if val < maxValue {
+		if val > maxValue {
 			maxValue = val
 		}
 	}
@@ -526,7 +548,7 @@ type Coordinate struct {
 }
 
 func getLinearResult(s []Coordinate) (gradient, intercept float64) {
-	if len(s) == 0 {
+	if len(s) <= 1 {
 		return
 	}