123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628 |
- package services
- import (
- "encoding/json"
- "errors"
- "eta/eta_forum_admin/models"
- "eta/eta_forum_admin/utils"
- "fmt"
- "github.com/shopspring/decimal"
- "math"
- "sort"
- "strings"
- "time"
- )
- // TraceEdbInfoByEdbInfoId 指标追溯
- func TraceEdbInfoByEdbInfoId(edbInfoId int) (traceEdbInfo models.TraceEdbInfoResp, err error) {
- edbInfo, err := models.GetEdbInfoById(edbInfoId)
- if err != nil {
- return
- }
- edbInfoRuleMap := make(map[int]string, 0)
- edbMappingMap := make(map[int][]*models.EdbInfoCalculateMappingInfo)
- //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
- traceEdbInfo = models.TraceEdbInfoResp{
- //EdbInfoId: edbInfo.EdbInfoId,
- EdbInfoId: edbInfoId,
- EdbInfoType: edbInfo.EdbInfoType,
- EdbName: edbInfo.EdbName,
- EdbType: edbInfo.EdbType,
- //Source: edbInfo.Source,
- UniqueCode: edbInfo.UniqueCode,
- ClassifyId: edbInfo.ClassifyId,
- IsStop: edbInfo.NoUpdate,
- EdbInfo: edbInfo,
- }
- findIdMap := make(map[int]int)
- findIdMap[edbInfoId] = edbInfoId
- existMap := make(map[int]models.TraceEdbInfoResp)
- traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
- edbInfoIdList := make([]int, 0)
- for _, v := range findIdMap {
- edbInfoIdList = append(edbInfoIdList, v)
- }
- classifyIdList := make([]int, 0)
- edbInfoList, err := models.GetEdbInfoByIdList(edbInfoIdList)
- if err != nil {
- return
- }
- edbInfoMap := make(map[int]*models.EdbInfo)
- for _, tmpEdbInfo := range edbInfoList {
- edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
- classifyIdList = append(classifyIdList, tmpEdbInfo.ClassifyId)
- }
- traceEdbInfo, err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap)
- // 权限校验
- return
- }
- // TraceEdbInfoByEdbInfoIdList 指标追溯
- func TraceEdbInfoByEdbInfoIdList(edbInfoIdList []int) (traceEdbInfoList []models.TraceEdbInfoResp, err error) {
- traceEdbInfoList = make([]models.TraceEdbInfoResp, 0)
- edbInfoList, err := models.GetEdbInfoByIdList(edbInfoIdList)
- if err != nil {
- return
- }
- edbInfoRuleMap := make(map[int]string, 0)
- edbMappingMap := make(map[int][]*models.EdbInfoCalculateMappingInfo)
- findIdMap := make(map[int]int)
- existMap := make(map[int]models.TraceEdbInfoResp)
- for _, edbInfo := range edbInfoList {
- findIdMap[edbInfo.EdbInfoId] = edbInfo.EdbInfoId
- //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
- traceEdbInfo := models.TraceEdbInfoResp{
- //EdbInfoId: edbInfo.EdbInfoId,
- EdbInfoId: edbInfo.EdbInfoId,
- EdbInfoType: edbInfo.EdbInfoType,
- EdbName: edbInfo.EdbName,
- EdbType: edbInfo.EdbType,
- //Source: edbInfo.Source,
- UniqueCode: edbInfo.UniqueCode,
- ClassifyId: edbInfo.ClassifyId,
- IsStop: edbInfo.NoUpdate,
- EdbInfo: edbInfo,
- }
- traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfo.EdbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
- traceEdbInfoList = append(traceEdbInfoList, traceEdbInfo)
- }
- //findEdbInfoIdList := make([]int, 0)
- //for _, v := range findIdMap {
- // findEdbInfoIdList = append(findEdbInfoIdList, v)
- //}
- //findEdbInfoList, err := models.GetEdbInfoByIdList(findEdbInfoIdList)
- //if err != nil {
- // return
- //}
- //edbInfoMap := make(map[int]*models.EdbInfo)
- //for _, tmpEdbInfo := range findEdbInfoList {
- // edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
- //}
- //for k, traceEdbInfo := range traceEdbInfoList {
- // traceEdbInfoList[k], err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap)
- //}
- return
- }
- // traceEdbInfoByEdbInfoId 指标追溯
- func traceEdbInfoByEdbInfoId(edbInfoId int, traceEdbInfo models.TraceEdbInfoResp, edbInfoRuleMap map[int]string, findIdMap map[int]int, existMap map[int]models.TraceEdbInfoResp, edbMappingMap map[int][]*models.EdbInfoCalculateMappingInfo) (child []models.TraceEdbInfoResp, err error) {
- traceEdbInfo, ok := existMap[edbInfoId]
- if ok {
- return
- }
- child = make([]models.TraceEdbInfoResp, 0)
- edbInfoMappingList, e := models.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
- if e != nil {
- err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", e.Error())
- return
- }
- // 指标信息map
- edbInfoMap := make(map[int]*models.EdbInfo)
- if len(edbInfoMappingList) > 0 {
- fromEdbInfoIdList := make([]int, 0)
- for _, v := range edbInfoMappingList {
- fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
- }
- edbInfoList, tmpErr := models.GetEdbInfoByIdList(fromEdbInfoIdList)
- if tmpErr != nil {
- err = fmt.Errorf("traceEdbInfoByEdbInfoId GetEdbInfoByIdList err: %s", tmpErr.Error())
- return
- }
- for _, v := range edbInfoList {
- edbInfoMap[v.EdbInfoId] = v
- }
- }
- edbMappingMap[edbInfoId] = edbInfoMappingList
- for _, v := range edbInfoMappingList {
- tmpEdbInfoId := v.FromEdbInfoId
- tmpTraceEdbInfo := models.TraceEdbInfoResp{
- EdbInfoId: tmpEdbInfoId,
- EdbInfoType: v.FromEdbInfoType,
- EdbType: v.FromEdbType,
- UniqueCode: v.FromUniqueCode,
- ClassifyId: v.FromClassifyId,
- IsStop: v.NoUpdate,
- EdbInfo: edbInfoMap[v.FromEdbInfoId],
- }
- // 计算指标/预测指标继续溯源
- if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
- // 查过了就不查了
- if _, ok2 := findIdMap[tmpEdbInfoId]; !ok2 {
- tmpTraceEdbInfo.Child, e = traceEdbInfoByEdbInfoId(tmpEdbInfoId, tmpTraceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
- if e != nil {
- err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", e.Error())
- return
- }
- }
- }
- child = append(child, tmpTraceEdbInfo)
- findIdMap[tmpEdbInfoId] = tmpEdbInfoId
- }
- existMap[edbInfoId] = traceEdbInfo
- return
- }
- func handleTraceEdbInfo(traceEdbInfoResp models.TraceEdbInfoResp, parentEdbInfoId int, edbInfoMap map[int]*models.EdbInfo, edbMappingMap map[int][]*models.EdbInfoCalculateMappingInfo) (newTraceEdbInfoResp models.TraceEdbInfoResp, err error) {
- edbInfo, ok := edbInfoMap[traceEdbInfoResp.EdbInfoId]
- if !ok {
- err = errors.New("指标异常")
- return
- }
- var parentEdbInfo *models.EdbInfo
- if parentEdbInfoId > 0 {
- parentEdbInfo, ok = edbInfoMap[parentEdbInfoId]
- if !ok {
- err = errors.New("指标异常")
- return
- }
- }
- //traceEdbInfoResp.EdbName = edbInfo.EdbName
- traceEdbInfoResp.EdbName, traceEdbInfoResp.RuleTitle = getEdbRuleTitle(edbInfo, parentEdbInfo, traceEdbInfoResp.Child, edbInfoMap, edbMappingMap)
- if traceEdbInfoResp.Child != nil && len(traceEdbInfoResp.Child) > 0 {
- for k, v := range traceEdbInfoResp.Child {
- traceEdbInfoResp.Child[k], err = handleTraceEdbInfo(v, traceEdbInfoResp.EdbInfoId, edbInfoMap, edbMappingMap)
- if err != nil {
- return
- }
- }
- }
- newTraceEdbInfoResp = traceEdbInfoResp
- return
- }
- // 获取频度的英文版
- func GetFrequencyEn(frequency string) (frequencyEn string) {
- switch frequency {
- case "日度":
- frequencyEn = "day"
- return
- case "周度":
- frequencyEn = "week"
- return
- case "旬度":
- frequencyEn = "ten days"
- return
- case "月度":
- frequencyEn = "month"
- return
- case "季度":
- frequencyEn = "quarter"
- return
- case "年度":
- frequencyEn = "year"
- return
- }
- return
- }
- func GetLeadUnitEn(unit string) (unitEn string) {
- switch unit {
- case "天":
- unitEn = "day"
- return
- case "周":
- unitEn = "week"
- return
- case "月":
- unitEn = "month"
- return
- case "季":
- unitEn = "quarter"
- return
- case "年":
- unitEn = "year"
- return
- }
- return
- }
- func GetRefreshEdbInfoFromBase(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*models.EdbInfo, err error) {
- calculateList, err := models.GetEdbInfoCalculateMap(edbInfoId, source)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- for _, item := range calculateList {
- if item.EdbInfoId == edbInfoId { // 如果查出来关联的指标就是自己的话,那么就过滤
- continue
- }
- if item.EdbType == 1 {
- baseEdbInfoArr = append(baseEdbInfoArr, item)
- } else {
- calculateInfoArr = append(calculateInfoArr, item)
- newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromBase(item.EdbInfoId, item.Source)
- baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
- calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
- }
- }
- return
- }
- // getEdbRule 获取规则名称
- func getEdbRuleTitle(edbInfo, parentEdbInfo *models.EdbInfo, childList []models.TraceEdbInfoResp, edbInfoMap map[int]*models.EdbInfo, edbMappingMap map[int][]*models.EdbInfoCalculateMappingInfo) (edbName, ruleTitle string) {
- edbName = edbInfo.EdbName
- ruleTitle = `来源于` + edbInfo.SourceName
- if parentEdbInfo != nil {
- edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
- if !ok {
- edbMappingList = []*models.EdbInfoCalculateMappingInfo{}
- }
- // 指标名称
- switch parentEdbInfo.Source {
- case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
- for _, v := range edbMappingList {
- if v.FromEdbInfoId == edbInfo.EdbInfoId {
- edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
- }
- }
- case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ, utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 直接拼接 ,累计值同比拼接
- for _, v := range edbMappingList {
- if v.FromEdbInfoId == edbInfo.EdbInfoId {
- tmpName := ``
- if v.FromTag == `A` {
- tmpName = `拼接日期前`
- } else if v.FromTag == `B` {
- tmpName = `拼接日期后`
- }
- edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
- }
- }
- case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
- for _, v := range edbMappingList {
- //(需对上游指标+自变量,领先10天/因变量)
- if v.FromEdbInfoId == edbInfo.EdbInfoId {
- tmpName := ``
- if v.FromTag == `A` {
- tmpName = fmt.Sprintf(`自变量,领先%d天`, v.MoveValue)
- } else if v.FromTag == `B` {
- tmpName = `因变量`
- }
- edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
- }
- }
- case utils.DATA_SOURCE_CALCULATE_CORRELATION: // 滚动相关性
- for _, v := range edbMappingList {
- if v.FromEdbInfoId == edbInfo.EdbInfoId {
- edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
- }
- }
- }
- }
- if edbInfo.EdbType == 1 {
- // 基础指标的话,直接返回来源名称
- //ruleTitle = `来源于`+edbInfo.SourceName
- return
- }
- // 规则
- switch edbInfo.Source {
- case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
- ruleTitle = "=" + edbInfo.CalculateFormula
- case utils.DATA_SOURCE_CALCULATE_LJZZY, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY:
- ruleTitle = `累计转月值计算`
- case utils.DATA_SOURCE_CALCULATE_TBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ:
- ruleTitle = `同比值计算`
- case utils.DATA_SOURCE_CALCULATE_TCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ:
- ruleTitle = `同差值计算`
- case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS:
- ruleTitle = fmt.Sprintf("N数值移动均值计算(N=%s)", edbInfo.CalculateFormula)
- case utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ:
- ruleTitle = fmt.Sprintf("N数值环比值计算(N=%s)", edbInfo.CalculateFormula)
- case utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ:
- ruleTitle = fmt.Sprintf("N数值环差值计算(N=%s)", edbInfo.CalculateFormula)
- case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
- moveType := `领先`
- if edbInfo.MoveType == 2 {
- moveType = "滞后"
- }
- ruleTitle = fmt.Sprintf("时间移位计算(%s%s%s)", moveType, edbInfo.CalculateFormula, edbInfo.MoveFrequency)
- case utils.DATA_SOURCE_CALCULATE_BP, utils.DATA_SOURCE_PREDICT_CALCULATE_BP: // 变频
- childFrequency := ``
- if len(childList) > 0 {
- if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
- childFrequency = childEdbInfo.Frequency
- }
- }
- ruleTitle = fmt.Sprintf("升频计算(%s转%s)", childFrequency, edbInfo.Frequency)
- case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ: // 直接拼接
- ruleTitle = fmt.Sprintf("直接拼接计算(%s)", edbInfo.CalculateFormula)
- case utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 累计值同比拼
- ruleTitle = fmt.Sprintf("累计值同比值拼接计算(%s)", edbInfo.CalculateFormula)
- case utils.DATA_SOURCE_PYTHON:
- ruleTitle = `代码运算`
- case utils.DATA_SOURCE_CALCULATE_CJJX, utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
- ruleTitle = fmt.Sprintf("超季节性计算(N=%s,%s)", edbInfo.CalculateFormula, edbInfo.Calendar)
- case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
- var startDate, endDate string
- dateList := strings.Split(edbInfo.CalculateFormula, ",")
- if len(dateList) == 2 {
- startDate = dateList[0]
- endDate = dateList[1]
- }
- ruleTitle = fmt.Sprintf("拟合残差计算(%s至%s)", startDate, endDate)
- case utils.DATA_SOURCE_CALCULATE_ADJUST:
- ruleTitle = `数据调整`
- case utils.DATA_SOURCE_CALCULATE_NH, utils.DATA_SOURCE_PREDICT_CALCULATE_NH:
- ruleTitle = `年化计算`
- case utils.DATA_SOURCE_CALCULATE_KSZS, utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS: // 扩散指数->53
- type KszsConfig struct {
- DateType int `description:"扩散指标日期;1:全部指标日期并集;2:部分指标日期并集"`
- CheckList []string `description:"选中的数据,A,B,C"`
- }
- var config KszsConfig
- err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &config)
- if err != nil {
- return
- }
- var startDate, endDate time.Time
- childEdbInfoIdList := make([]int, 0)
- if config.DateType == 1 {
- for _, v := range childList {
- childEdbInfoIdList = append(childEdbInfoIdList, v.EdbInfoId)
- }
- } else {
- if parentEdbInfo != nil {
- edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
- if !ok {
- edbMappingList = []*models.EdbInfoCalculateMappingInfo{}
- }
- tagMap := make(map[string]int)
- for _, v := range edbMappingList {
- tagMap[v.FromTag] = v.FromEdbInfoId
- }
- for _, v := range config.CheckList {
- if tmpEdbInfoId, ok := tagMap[v]; ok {
- childEdbInfoIdList = append(childEdbInfoIdList, tmpEdbInfoId)
- }
- }
- }
- }
- for _, v := range childEdbInfoIdList {
- if childEdbInfo, ok := edbInfoMap[v]; ok {
- tmpStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, childEdbInfo.StartDate, time.Local)
- if tmpErr != nil {
- return
- }
- if startDate.IsZero() || startDate.After(tmpStartDate) {
- startDate = tmpStartDate
- }
- tmpEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, childEdbInfo.EndDate, time.Local)
- if tmpErr != nil {
- return
- }
- if endDate.IsZero() || endDate.Before(tmpEndDate) {
- endDate = tmpEndDate
- }
- }
- }
- ruleTitle = fmt.Sprintf("扩散指数计算(%s至%s)", startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
- case utils.DATA_SOURCE_STOCK_PLANT:
- ruleTitle = `来源于装置分析`
- case utils.DATA_SOURCE_CALCULATE_CORRELATION:
- type EdbCalculateFormula struct {
- BaseCalculateValue int `description:"基础计算窗口"`
- BaseCalculateUnit string `description:"基础计算频度"`
- LeadValue int `description:"领先期数"`
- LeadUnit string `description:"频度"`
- CalculateValue int `description:"计算窗口"`
- CalculateUnit string `description:"计算频度"`
- }
- var correlationConf EdbCalculateFormula
- err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &correlationConf)
- if err != nil {
- return
- }
- ruleTitle = fmt.Sprintf("滚动相关性(计算窗口%d%s,B领先A%d%s)", correlationConf.CalculateValue, correlationConf.CalculateUnit, correlationConf.LeadValue, correlationConf.LeadUnit)
- case utils.DATA_SOURCE_CALCULATE_JP, utils.DATA_SOURCE_PREDICT_CALCULATE_JP:
- childFrequency := ``
- if len(childList) > 0 {
- if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
- childFrequency = childEdbInfo.Frequency
- }
- }
- ruleTitle = fmt.Sprintf("降频计算(%s转%s,%s)", childFrequency, edbInfo.Frequency, edbInfo.CalculateFormula)
- case utils.DATA_SOURCE_CALCULATE_STANDARD_DEVIATION:
- ruleTitle = fmt.Sprintf("标准差(滚动%s期)", edbInfo.CalculateFormula)
- case utils.DATA_SOURCE_CALCULATE_PERCENTILE, utils.DATA_SOURCE_PREDICT_CALCULATE_PERCENTILE:
- type TempCalculate struct {
- CalculateValue int `description:"计算窗口"`
- CalculateUnit string `description:"计算频度"`
- }
- cf := TempCalculate{}
- if e := json.Unmarshal([]byte(edbInfo.CalculateFormula), &cf); e != nil {
- return
- }
- ruleTitle = fmt.Sprintf("百分位(时间长度%d%s)", cf.CalculateValue, cf.CalculateUnit)
- case utils.DATA_SOURCE_CALCULATE_ZSXY, utils.DATA_SOURCE_PREDICT_CALCULATE_ZSXY:
- ruleTitle = `指数修匀计算`
- }
- return
- }
- // HandleDataByLinearRegressionToList 插值法补充数据(线性方程式)
- func HandleDataByLinearRegressionToList (edbInfoDataList []*models.EdbDataList, handleDataMap map[string]float64) (dataTimeList []string,valueList []float64, err error) {
- if len(edbInfoDataList) < 2 {
- return
- }
- var startEdbInfoData *models.EdbDataList
- for _, v := range edbInfoDataList {
- handleDataMap[v.DataTime] = v.Value
- dataTimeList = append(dataTimeList, v.DataTime)
- // 第一个数据就给过滤了,给后面的试用
- if startEdbInfoData == nil {
- startEdbInfoData = v
- //startEdbInfoData.DataTime = startEdbInfoData.DataTime[:5]+ "01-01"
- continue
- }
- // 获取两条数据之间相差的天数
- startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
- currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- betweenHour := int(currDataTime.Sub(startDataTime).Hours())
- betweenDay := betweenHour / 24
- // 如果相差一天,那么过滤
- if betweenDay <= 1 {
- startEdbInfoData = v
- continue
- }
- // 生成线性方程式
- var a, b float64
- {
- coordinateData := make([]utils.Coordinate, 0)
- tmpCoordinate1 := utils.Coordinate{
- X: 1,
- Y: startEdbInfoData.Value,
- }
- coordinateData = append(coordinateData, tmpCoordinate1)
- tmpCoordinate2 := utils.Coordinate{
- X: float64(betweenDay) + 1,
- Y: v.Value,
- }
- coordinateData = append(coordinateData, tmpCoordinate2)
- a, b = utils.GetLinearResult(coordinateData)
- if math.IsNaN(a) || math.IsNaN(b) {
- err = errors.New("线性方程公式生成失败")
- return
- }
- }
- // 生成对应的值
- {
- for i := 1; i < betweenDay; i++ {
- tmpDataTime := startDataTime.AddDate(0, 0, i)
- aDecimal := decimal.NewFromFloat(a)
- xDecimal := decimal.NewFromInt(int64(i) + 1)
- bDecimal := decimal.NewFromFloat(b)
- val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
- handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
- dataTimeList = append(dataTimeList, tmpDataTime.Format(utils.FormatDate))
- valueList = append(valueList, val)
- }
- }
- startEdbInfoData = v
- }
- return
- }
- // HandleDataByLinearRegressionToList 保证生成365个数据点的线性插值法
- func HandleDataByLinearRegressionToListV2(edbInfoDataList []*models.EdbDataList, handleDataMap map[string]float64) (dataTimeList []string, valueList []float64, err error) {
- if len(edbInfoDataList) < 2 {
- return
- }
- // 确保至少有两天数据来生成线性方程
- if len(edbInfoDataList) < 2 {
- err = errors.New("至少需要两天的数据来执行线性插值")
- return
- }
- // 对数据按日期排序,确保顺序正确
- sort.Slice(edbInfoDataList, func(i, j int) bool {
- t1, _ := time.ParseInLocation(utils.FormatDate, edbInfoDataList[i].DataTime, time.Local)
- t2, _ := time.ParseInLocation(utils.FormatDate, edbInfoDataList[j].DataTime, time.Local)
- return t1.Before(t2)
- })
- startEdbInfoData := edbInfoDataList[0]
- endEdbInfoData := edbInfoDataList[len(edbInfoDataList)-1]
- // 计算起始和结束日期间实际的天数
- startDate, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
- endDate, _ := time.ParseInLocation(utils.FormatDate, endEdbInfoData.DataTime, time.Local)
- actualDays := endDate.Sub(startDate).Hours() / 24
- // 生成365个数据点,首先处理已有数据
- for _, v := range edbInfoDataList {
- handleDataMap[v.DataTime] = v.Value
- dataTimeList = append(dataTimeList, v.DataTime)
- valueList = append(valueList, v.Value)
- }
- // 如果已有数据跨越天数不足365天,则对缺失的日期进行线性插值
- if actualDays < 365 {
- // 使用已有数据点生成线性方程(这里简化处理,实际可能需更细致处理边界情况)
- var a, b float64
- coordinateData := []utils.Coordinate{
- {X: 1, Y: startEdbInfoData.Value},
- {X: float64(len(edbInfoDataList)), Y: endEdbInfoData.Value},
- }
- a, b = utils.GetLinearResult(coordinateData)
- if math.IsNaN(a) || math.IsNaN(b) {
- err = errors.New("线性方程公式生成失败")
- return
- }
- // 对剩余日期进行插值
- for i := 1; i < 365; i++ {
- day := startDate.AddDate(0, 0, i)
- if _, exists := handleDataMap[day.Format(utils.FormatDate)]; !exists {
- aDecimal := decimal.NewFromFloat(a)
- xDecimal := decimal.NewFromInt(int64(i) + 1)
- bDecimal := decimal.NewFromFloat(b)
- val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
- handleDataMap[day.Format(utils.FormatDate)] = val
- dataTimeList = append(dataTimeList, day.Format(utils.FormatDate))
- valueList = append(valueList, val)
- }
- }
- }
- return
- }
|