Browse Source

面积图 空值填充 前值填充

gmy 3 days ago
parent
commit
280f3f9e49
1 changed files with 241 additions and 29 deletions
  1. 241 29
      services/data/area_graph/processor_business_logic.go

+ 241 - 29
services/data/area_graph/processor_business_logic.go

@@ -17,6 +17,40 @@ func (i *InterpolateStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataL
 	for _, v := range edbDataList {
 		if v.EdbInfoId != tmpConfig.StandardEdbInfoId {
 			if dataList, ok := v.DataList.([]*data_manage.EdbDataList); ok {
+
+				// 存放补充数据
+				var replenishDataList []*data_manage.EdbDataList
+
+				// 处理从 startDate 到第一个数据的日期补充
+				if len(dataList) > 0 {
+					firstData := dataList[0]
+					// 将 startDate 到第一个数据日期之间的自然日填充补充数据,值为 0
+					startDataTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+					firstDataTime, _ := time.ParseInLocation(utils.FormatDate, firstData.DataTime, time.Local)
+
+					// 计算两个日期之间的天数差
+					if !startDataTime.Equal(firstDataTime) {
+						for startDataTime.Before(firstDataTime) {
+							// 补充数据
+							nextDay := startDataTime.Format(utils.FormatDate)
+
+							// 生成补充数据,值为 0
+							replenishIndexData := data_manage.EdbDataList{
+								EdbInfoId:     v.EdbInfoId,
+								DataTime:      nextDay,
+								DataTimestamp: startDataTime.UnixMilli(),
+								Value:         0,
+							}
+
+							// 将补充数据加入补充数据列表
+							replenishDataList = append(replenishDataList, &replenishIndexData)
+
+							// 更新 startDataTime 到下一个日期
+							startDataTime = startDataTime.AddDate(0, 0, 1)
+						}
+					}
+				}
+
 				// 插值法补充数据
 				var startEdbInfoData *data_manage.EdbDataList
 				for index := 0; index < len(dataList)-1; index++ {
@@ -81,11 +115,45 @@ func (i *InterpolateStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataL
 						}
 
 						// 将补充数据加入补充数据列表
-						dataList = append(dataList[:index+1], append([]*data_manage.EdbDataList{&replenishIndexData}, dataList[index+1:]...)...)
+						replenishDataList = append(replenishDataList, &replenishIndexData)
 					}
 					startEdbInfoData = afterIndexData
 				}
 
+				// 处理从最后一个数据到 endDate 的日期补充
+				if len(dataList) > 0 {
+					lastData := dataList[len(dataList)-1]
+					// 将最后一个数据日期到 endDate 之间的自然日填充补充数据,值为 0
+					lastDataTime, _ := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
+					endDataTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
+
+					// 如果 lastDataTime 不等于 endDate,进行补充
+					if !lastDataTime.Equal(endDataTime) {
+						// 补充数据直到 endDate
+						for lastDataTime.Before(endDataTime) {
+							// 补充数据
+							addDate := lastDataTime.AddDate(0, 0, 1)
+							nextDay := addDate.Format(utils.FormatDate)
+
+							// 生成补充数据,值为 0
+							replenishIndexData := data_manage.EdbDataList{
+								EdbInfoId:     v.EdbInfoId,
+								DataTime:      nextDay,
+								DataTimestamp: addDate.UnixMilli(),
+								Value:         0,
+							}
+
+							// 将补充数据加入补充数据列表
+							replenishDataList = append(replenishDataList, &replenishIndexData)
+
+							// 更新 lastDataTime 到下一个日期
+							lastDataTime = addDate
+						}
+					}
+				}
+
+				dataList = append(dataList, replenishDataList...)
+
 				// 根据基准指标筛选出符合数据
 				var resultDataList []*data_manage.EdbDataList
 				for _, dataObject := range dataList {
@@ -126,23 +194,25 @@ func (f *FillWithPreviousStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edb
 					firstDataTime, _ := time.ParseInLocation(utils.FormatDate, firstData.DataTime, time.Local)
 
 					// 计算两个日期之间的天数差
-					for startDataTime.Before(firstDataTime) {
-						// 补充数据
-						nextDay := startDataTime.Format(utils.FormatDate)
+					if !startDataTime.Equal(firstDataTime) {
+						for startDataTime.Before(firstDataTime) {
+							// 补充数据
+							nextDay := startDataTime.Format(utils.FormatDate)
 
-						// 生成补充数据,值为 0
-						replenishIndexData := data_manage.EdbDataList{
-							EdbInfoId:     v.EdbInfoId,
-							DataTime:      nextDay,
-							DataTimestamp: startDataTime.UnixMilli(),
-							Value:         0,
-						}
+							// 生成补充数据,值为 0
+							replenishIndexData := data_manage.EdbDataList{
+								EdbInfoId:     v.EdbInfoId,
+								DataTime:      nextDay,
+								DataTimestamp: startDataTime.UnixMilli(),
+								Value:         0,
+							}
 
-						// 将补充数据加入补充数据列表
-						replenishDataList = append(replenishDataList, &replenishIndexData)
+							// 将补充数据加入补充数据列表
+							replenishDataList = append(replenishDataList, &replenishIndexData)
 
-						// 更新 startDataTime 到下一个日期
-						startDataTime = startDataTime.AddDate(0, 0, 1)
+							// 更新 startDataTime 到下一个日期
+							startDataTime = startDataTime.AddDate(0, 0, 1)
+						}
 					}
 				}
 
@@ -166,7 +236,6 @@ func (f *FillWithPreviousStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edb
 
 						// 将补充数据加入补充数据列表
 						replenishDataList = append(replenishDataList, &replenishIndexData)
-						//dataList = append(dataList[:index+1], append([]*data_manage.EdbDataList{&replenishIndexData}, dataList[index+1:]...)...)
 
 						// 更新 beforeIndexData 为新创建的补充数据
 						beforeIndexData = &replenishIndexData
@@ -183,15 +252,16 @@ func (f *FillWithPreviousStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edb
 					// 如果 lastDataTime 不等于 endDate,进行补充
 					if !lastDataTime.Equal(endDataTime) {
 						// 补充数据直到 endDate
-						for !lastDataTime.After(endDataTime) {
+						for lastDataTime.Before(endDataTime) {
 							// 补充数据
-							nextDay := lastDataTime.Format(utils.FormatDate)
+							addDate := lastDataTime.AddDate(0, 0, 1)
+							nextDay := addDate.Format(utils.FormatDate)
 
 							// 生成补充数据,值为 0
 							replenishIndexData := data_manage.EdbDataList{
 								EdbInfoId:     v.EdbInfoId,
 								DataTime:      nextDay,
-								DataTimestamp: lastDataTime.UnixMilli(),
+								DataTimestamp: addDate.UnixMilli(),
 								Value:         0,
 							}
 
@@ -199,7 +269,7 @@ func (f *FillWithPreviousStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edb
 							replenishDataList = append(replenishDataList, &replenishIndexData)
 
 							// 更新 lastDataTime 到下一个日期
-							lastDataTime = lastDataTime.AddDate(0, 0, 1)
+							lastDataTime = addDate
 						}
 					}
 				}
@@ -236,33 +306,103 @@ func (f *FillWithNextStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbData
 	for _, v := range edbDataList {
 		if v.EdbInfoId != tmpConfig.StandardEdbInfoId {
 			if dataList, ok := v.DataList.([]*data_manage.EdbDataList); ok {
+
+				// 存放补充数据
+				var replenishDataList []*data_manage.EdbDataList
+
+				// 处理从 startDate 到第一个数据的日期补充
+				if len(dataList) > 0 {
+					firstData := dataList[0]
+					// 将 startDate 到第一个数据日期之间的自然日填充补充数据,值为 0
+					startDataTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+					firstDataTime, _ := time.ParseInLocation(utils.FormatDate, firstData.DataTime, time.Local)
+
+					// 计算两个日期之间的天数差
+					if !startDataTime.Equal(firstDataTime) {
+						for !startDataTime.After(firstDataTime) {
+							// 补充数据
+							nextDay := startDataTime.Format(utils.FormatDate)
+
+							// 生成补充数据,值为 0
+							replenishIndexData := data_manage.EdbDataList{
+								EdbInfoId:     v.EdbInfoId,
+								DataTime:      nextDay,
+								DataTimestamp: startDataTime.UnixMilli(),
+								Value:         0,
+							}
+
+							// 将补充数据加入补充数据列表
+							replenishDataList = append(replenishDataList, &replenishIndexData)
+
+							// 更新 startDataTime 到下一个日期
+							startDataTime = startDataTime.AddDate(0, 0, 1)
+						}
+					}
+				}
+
+				// 处理从最后一个数据到 endDate 的日期补充
+				if len(dataList) > 0 {
+					lastData := dataList[len(dataList)-1]
+					// 将最后一个数据日期到 endDate 之间的自然日填充补充数据,值为 0
+					lastDataTime, _ := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
+					endDataTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
+
+					// 如果 lastDataTime 不等于 endDate,进行补充
+					if !lastDataTime.Equal(endDataTime) {
+						// 补充数据直到 endDate
+						for lastDataTime.Before(endDataTime) {
+							// 补充数据
+							addDate := lastDataTime.AddDate(0, 0, 1)
+							nextDay := addDate.Format(utils.FormatDate)
+
+							// 生成补充数据,值为 0
+							replenishIndexData := data_manage.EdbDataList{
+								EdbInfoId:     v.EdbInfoId,
+								DataTime:      nextDay,
+								DataTimestamp: addDate.UnixMilli(),
+								Value:         0,
+							}
+
+							// 将补充数据加入补充数据列表
+							replenishDataList = append(replenishDataList, &replenishIndexData)
+
+							// 更新 lastDataTime 到下一个日期
+							lastDataTime = addDate
+						}
+					}
+				}
+
 				// 将切片数据倒序
 				reverseSlice(dataList)
 
+				// 处理指标中空值数据
 				for index := 0; index < len(dataList)-1; index++ {
 					// 获取当前数据和下一个数据
 					beforeIndexData := dataList[index]
 					afterIndexData := dataList[index+1]
 
-					for utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
+					for utils.IsMoreThanOneDay(afterIndexData.DataTime, beforeIndexData.DataTime) {
 						// 创建补充数据
-						nextDay := utils.GetNextDay(beforeIndexData.DataTime)
+						nextDay := utils.GetNextDay(afterIndexData.DataTime)
+
 						toTime := utils.StringToTime(nextDay)
 						replenishIndexData := data_manage.EdbDataList{
 							EdbInfoId:     v.EdbInfoId,
 							DataTime:      nextDay,
 							DataTimestamp: toTime.UnixMilli(),
-							Value:         beforeIndexData.Value,
+							Value:         afterIndexData.Value,
 						}
 
 						// 将补充数据加入补充数据列表
-						dataList = append(dataList[:index+1], append([]*data_manage.EdbDataList{&replenishIndexData}, dataList[index+1:]...)...)
+						replenishDataList = append(replenishDataList, &replenishIndexData)
 
 						// 更新 beforeIndexData 为新创建的补充数据
-						beforeIndexData = &replenishIndexData
+						afterIndexData = &replenishIndexData
 					}
 				}
 
+				dataList = append(dataList, replenishDataList...)
+
 				// 根据基准指标筛选出符合数据
 				var resultDataList []*data_manage.EdbDataList
 				for _, dataObject := range dataList {
@@ -293,6 +433,40 @@ func (s *SetToZeroStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataLis
 	for _, v := range edbDataList {
 		if v.EdbInfoId != tmpConfig.StandardEdbInfoId {
 			if dataList, ok := v.DataList.([]*data_manage.EdbDataList); ok {
+				// 存放补充数据
+				var replenishDataList []*data_manage.EdbDataList
+
+				// 处理从 startDate 到第一个数据的日期补充
+				if len(dataList) > 0 {
+					firstData := dataList[0]
+					// 将 startDate 到第一个数据日期之间的自然日填充补充数据,值为 0
+					startDataTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+					firstDataTime, _ := time.ParseInLocation(utils.FormatDate, firstData.DataTime, time.Local)
+
+					// 计算两个日期之间的天数差
+					if !startDataTime.Equal(firstDataTime) {
+						for startDataTime.Before(firstDataTime) {
+							// 补充数据
+							nextDay := startDataTime.Format(utils.FormatDate)
+
+							// 生成补充数据,值为 0
+							replenishIndexData := data_manage.EdbDataList{
+								EdbInfoId:     v.EdbInfoId,
+								DataTime:      nextDay,
+								DataTimestamp: startDataTime.UnixMilli(),
+								Value:         0,
+							}
+
+							// 将补充数据加入补充数据列表
+							replenishDataList = append(replenishDataList, &replenishIndexData)
+
+							// 更新 startDataTime 到下一个日期
+							startDataTime = startDataTime.AddDate(0, 0, 1)
+						}
+					}
+				}
+
+				// 处理指标中空值数据
 				for index := 0; index < len(dataList)-1; index++ {
 					// 获取当前数据和下一个数据
 					beforeIndexData := dataList[index]
@@ -301,6 +475,7 @@ func (s *SetToZeroStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataLis
 					for utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
 						// 创建补充数据
 						nextDay := utils.GetNextDay(beforeIndexData.DataTime)
+
 						toTime := utils.StringToTime(nextDay)
 						replenishIndexData := data_manage.EdbDataList{
 							EdbInfoId:     v.EdbInfoId,
@@ -310,13 +485,47 @@ func (s *SetToZeroStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataLis
 						}
 
 						// 将补充数据加入补充数据列表
-						dataList = append(dataList[:index+1], append([]*data_manage.EdbDataList{&replenishIndexData}, dataList[index+1:]...)...)
+						replenishDataList = append(replenishDataList, &replenishIndexData)
 
 						// 更新 beforeIndexData 为新创建的补充数据
 						beforeIndexData = &replenishIndexData
 					}
 				}
 
+				// 处理从最后一个数据到 endDate 的日期补充
+				if len(dataList) > 0 {
+					lastData := dataList[len(dataList)-1]
+					// 将最后一个数据日期到 endDate 之间的自然日填充补充数据,值为 0
+					lastDataTime, _ := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
+					endDataTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
+
+					// 如果 lastDataTime 不等于 endDate,进行补充
+					if !lastDataTime.Equal(endDataTime) {
+						// 补充数据直到 endDate
+						for lastDataTime.Before(endDataTime) {
+							// 补充数据
+							addDate := lastDataTime.AddDate(0, 0, 1)
+							nextDay := addDate.Format(utils.FormatDate)
+
+							// 生成补充数据,值为 0
+							replenishIndexData := data_manage.EdbDataList{
+								EdbInfoId:     v.EdbInfoId,
+								DataTime:      nextDay,
+								DataTimestamp: addDate.UnixMilli(),
+								Value:         0,
+							}
+
+							// 将补充数据加入补充数据列表
+							replenishDataList = append(replenishDataList, &replenishIndexData)
+
+							// 更新 lastDataTime 到下一个日期
+							lastDataTime = addDate
+						}
+					}
+				}
+
+				dataList = append(dataList, replenishDataList...)
+
 				// 根据基准指标筛选出符合数据
 				var resultDataList []*data_manage.EdbDataList
 				for _, dataObject := range dataList {
@@ -344,16 +553,19 @@ type DeleteDateStrategy struct{}
 // Deal 删除日期
 func (d *DeleteDateStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList, startDate string, endDate string) error {
 	// 取所有指标的时间交集
+	// 创建一个 map 来保存每个时间点的出现次数
+	timeMap := make(map[string]int)
 	for _, v := range edbDataList {
 		if dataList, ok := v.DataList.([]*data_manage.EdbDataList); ok {
-			// 创建一个 map 来保存每个时间点的出现次数
-			timeMap := make(map[string]int)
-
 			// 遍历所有的 dataList,为每个 DataTime 增加一个计数
 			for _, dataObject := range dataList {
 				timeMap[dataObject.DataTime]++
 			}
+		}
+	}
 
+	for _, v := range edbDataList {
+		if dataList, ok := v.DataList.([]*data_manage.EdbDataList); ok {
 			// 遍历所有的 dataList,保留所有时间点在所有指标中都存在的数据
 			var resultDataList []*data_manage.EdbDataList
 			for _, dataObject := range dataList {