predict_edb.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_index_lib/utils"
  6. "fmt"
  7. "github.com/beego/beego/v2/client/orm"
  8. "github.com/dengsgo/math-engine/engine"
  9. "github.com/shopspring/decimal"
  10. "strings"
  11. "time"
  12. )
  13. // CalculateRule 预测指标 规则 计算
  14. type CalculateRule struct {
  15. EdbInfoId int `description:"指标id"`
  16. ConfigId int `description:"配置id"`
  17. TrendsCalculateMappingList []*PredictEdbConfCalculateMapping
  18. EdbInfoList []*EdbInfo
  19. EdbInfoIdBytes []string
  20. Formula string
  21. RuleType int `description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值,9:动态环差"`
  22. EndDate string `description:"截止日期"`
  23. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  24. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  25. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  26. }
  27. // RefreshCalculateByRuleBy9 刷新计算
  28. func RefreshCalculateByRuleBy9(rule CalculateRule) (resultDataList []*EdbInfoSearchData, err error) {
  29. o := orm.NewOrm()
  30. to, err := o.Begin()
  31. if err != nil {
  32. return
  33. }
  34. defer func() {
  35. if err != nil {
  36. to.Rollback()
  37. } else {
  38. err = to.Commit()
  39. }
  40. }()
  41. resultDataList, err = CalculateByRuleBy9(to, rule)
  42. return
  43. }
  44. // CalculateByRuleBy9 动态环差规则计算入库
  45. func CalculateByRuleBy9(to orm.TxOrmer, rule CalculateRule) (resultDataList []*EdbInfoSearchData, err error) {
  46. realSaveDataMap := make(map[string]map[int]float64)
  47. saveDataMap := make(map[string]map[int]float64)
  48. // 最小的结束日期 , 最晚的数据开始日期
  49. var minLatestDate, maxStartDate time.Time
  50. dateList := make([]string, 0) // 第一个指标的日期数据
  51. formulaStr := strings.ToUpper(rule.Formula)
  52. // 获取关联指标数据
  53. for edbInfoIndex, v := range rule.EdbInfoList {
  54. dataList, tmpErr := GetPredictEdbDataListAll(v, 1)
  55. if tmpErr != nil {
  56. err = tmpErr
  57. return
  58. }
  59. //lenData := len(dataList)
  60. for _, dv := range dataList {
  61. // 现有实际数据
  62. if val, ok := realSaveDataMap[dv.DataTime]; ok {
  63. if _, ok := val[v.EdbInfoId]; !ok {
  64. val[v.EdbInfoId] = dv.Value
  65. }
  66. } else {
  67. temp := make(map[int]float64)
  68. temp[v.EdbInfoId] = dv.Value
  69. realSaveDataMap[dv.DataTime] = temp
  70. }
  71. // 待处理的数据
  72. if val, ok := saveDataMap[dv.DataTime]; ok {
  73. if _, ok := val[v.EdbInfoId]; !ok {
  74. val[v.EdbInfoId] = dv.Value
  75. }
  76. } else {
  77. temp := make(map[int]float64)
  78. temp[v.EdbInfoId] = dv.Value
  79. saveDataMap[dv.DataTime] = temp
  80. }
  81. // 以第一个指标的日期作为基准日期
  82. if edbInfoIndex == 0 {
  83. dateList = append(dateList, dv.DataTime)
  84. tmpDate, _ := time.ParseInLocation(utils.FormatDate, dv.DataTime, time.Local)
  85. if minLatestDate.IsZero() || tmpDate.After(minLatestDate) {
  86. minLatestDate = tmpDate
  87. }
  88. if maxStartDate.IsZero() || tmpDate.Before(maxStartDate) {
  89. maxStartDate = tmpDate
  90. }
  91. }
  92. }
  93. /*if lenData > 0 {
  94. tmpLatestDate, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-1].DataTime, time.Local)
  95. if minLatestDate.IsZero() || minLatestDate.After(tmpLatestDate) {
  96. minLatestDate = tmpLatestDate
  97. }
  98. tmpStartDate, _ := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  99. if maxStartDate.IsZero() || maxStartDate.Before(tmpStartDate) {
  100. maxStartDate = tmpStartDate
  101. }
  102. }*/
  103. }
  104. // todo 数据处理,将日期内不全的数据做填补
  105. HandleDateSaveDataMap(dateList, maxStartDate, minLatestDate, realSaveDataMap, saveDataMap, rule.EdbInfoList, rule.EmptyType)
  106. // 添加数据
  107. addDataList := make([]*PredictEdbRuleData, 0)
  108. // 计算规则
  109. formulaDateSlice, formulaDateMap, err := utils.HandleFormulaJson(formulaStr, minLatestDate)
  110. if err != nil {
  111. return
  112. }
  113. //获取指标所有数据
  114. dataList := make([]*PredictEdbRuleData, 0)
  115. sql := `SELECT * FROM predict_edb_rule_data WHERE config_id = ?`
  116. _, err = to.Raw(sql, rule.ConfigId).QueryRows(&dataList)
  117. if err != nil {
  118. return
  119. }
  120. dataMap := make(map[string]*PredictEdbRuleData)
  121. removeDateMap := make(map[string]*PredictEdbRuleData) //需要移除的日期
  122. for _, v := range dataList {
  123. dataMap[v.DataTime] = v
  124. removeDateMap[v.DataTime] = v
  125. }
  126. existDataMap := make(map[string]string)
  127. // 判断是否特殊处理max和min函数
  128. maxDealFlag := false
  129. if rule.EmptyType == 4 && rule.MaxEmptyType == 2 {
  130. maxDealFlag = true
  131. }
  132. for sk, sv := range saveDataMap {
  133. // 当空值处理类型选择了不计算时,只要有一个指标在某个日期没有值(即空值),则计算指标在该日期没有值
  134. if rule.EmptyType == 1 {
  135. if len(sv) != len(rule.EdbInfoList) {
  136. continue
  137. }
  138. }
  139. //fmt.Println(sk, sv)
  140. // 根据时间范围,选择对应的公式
  141. formulaMap := make(map[string]string)
  142. formulaStr = ""
  143. for _, fv := range formulaDateSlice {
  144. if sk < fv {
  145. if f, ok := formulaDateMap[fv]; ok {
  146. formulaStr = f
  147. formulaMap, err = utils.CheckFormula(formulaStr)
  148. if err != nil {
  149. err = fmt.Errorf("公式错误,请重新填写")
  150. return
  151. }
  152. }
  153. break
  154. }
  155. }
  156. if formulaStr == "" {
  157. continue
  158. }
  159. svMax := make(map[int]float64)
  160. if maxDealFlag {
  161. // 特殊处理max和min函数,如果原本的值为空,则选择空值参与运算
  162. if svMaxData, ok := realSaveDataMap[sk]; ok {
  163. svMax = svMaxData
  164. }
  165. }
  166. formulaStr = strings.ToUpper(formulaStr)
  167. //fmt.Println(sk, sv)
  168. formulaFormStr := ReplaceFormula(rule.EdbInfoList, sv, svMax, formulaMap, formulaStr, rule.EdbInfoIdBytes, maxDealFlag)
  169. //计算公式异常,那么就移除该指标
  170. if formulaFormStr == "" {
  171. continue
  172. }
  173. //utils.FileLog.Info(fmt.Sprintf("formulaFormStr:%s", formulaFormStr))
  174. //expression := formula.NewExpression(formulaFormStr)
  175. //calResult, tmpErr := expression.Evaluate()
  176. //if tmpErr != nil {
  177. // // 分母为0的报错
  178. // if strings.Contains(tmpErr.Error(), "divide by zero") {
  179. // continue
  180. // }
  181. // err = errors.New("计算失败:Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  182. // //fmt.Println(err)
  183. // return
  184. //}
  185. //calVal, tmpErr := calResult.Float64()
  186. //if tmpErr != nil {
  187. // err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  188. // //fmt.Println(err)
  189. // return
  190. //}
  191. calVal, err := engine.ParseAndExec(formulaFormStr)
  192. //calVal, err := calResult.Float64()
  193. if err != nil {
  194. // 分母为0的报错,忽略该循环
  195. if utils.IsDivideZero(err) {
  196. //removeDateList = append(removeDateList, sk)
  197. continue
  198. }
  199. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  200. fmt.Println(err)
  201. return nil, err
  202. }
  203. nanCheck := fmt.Sprintf("%0.f", calVal)
  204. if nanCheck == "NaN" || nanCheck == "+Inf" || nanCheck == "-Inf" {
  205. continue
  206. }
  207. // 移除不存在的日期
  208. delete(removeDateMap, sk)
  209. saveValue := decimal.NewFromFloat(calVal).Round(4).String() //utils.SubFloatToString(calVal, 4)
  210. existPredictEdbRuleData, ok := dataMap[sk]
  211. if !ok {
  212. dataTime, _ := time.ParseInLocation(utils.FormatDate, sk, time.Local)
  213. timestamp := dataTime.UnixNano() / 1e6
  214. if _, existOk := existDataMap[sk]; !existOk {
  215. tmpPredictEdbRuleData := &PredictEdbRuleData{
  216. //PredictEdbRuleDataId: 0,
  217. EdbInfoId: rule.EdbInfoId,
  218. ConfigId: rule.ConfigId,
  219. DataTime: sk,
  220. Value: saveValue,
  221. CreateTime: time.Now(),
  222. ModifyTime: time.Now(),
  223. DataTimestamp: timestamp,
  224. }
  225. addDataList = append(addDataList, tmpPredictEdbRuleData)
  226. }
  227. existDataMap[sk] = sk
  228. } else {
  229. existValDecimal, tmpErr := decimal.NewFromString(existPredictEdbRuleData.Value)
  230. if tmpErr != nil {
  231. err = tmpErr
  232. return nil, tmpErr
  233. }
  234. existStr := existValDecimal.String()
  235. if existStr != saveValue {
  236. existPredictEdbRuleData.Value = saveValue
  237. existPredictEdbRuleData.ModifyTime = time.Now()
  238. _, err = to.Update(existPredictEdbRuleData, "Value", "ModifyTime")
  239. if err != nil {
  240. return nil, err
  241. }
  242. }
  243. }
  244. // 计算出来的结果集
  245. resultDataList = append(resultDataList, &EdbInfoSearchData{
  246. //EdbDataId: 0,
  247. DataTime: sk,
  248. Value: calVal,
  249. })
  250. }
  251. // 添加计算出来的值入库
  252. lenAddDataList := len(addDataList)
  253. if lenAddDataList > 0 {
  254. _, err = to.InsertMulti(lenAddDataList, addDataList)
  255. if err != nil {
  256. return
  257. }
  258. }
  259. //删除多余的值
  260. lenRemoveDateList := len(removeDateMap)
  261. if lenRemoveDateList > 0 {
  262. removeDateList := make([]string, 0) //需要移除的日期
  263. for date, _ := range removeDateMap {
  264. removeDateList = append(removeDateList, date)
  265. }
  266. //如果拼接指标变更了,那么需要删除所有的指标数据
  267. sql := ` DELETE FROM predict_edb_rule_data WHERE config_id = ? and data_time in (` + utils.GetOrmInReplace(lenRemoveDateList) + `) `
  268. _, err = to.Raw(sql, rule.ConfigId, removeDateList).Exec()
  269. if err != nil {
  270. err = fmt.Errorf("删除计算失败的预测规则计算指标数据失败,Err:" + err.Error())
  271. return
  272. }
  273. }
  274. return
  275. }
  276. // RefreshCalculateByRuleByLineNh 刷新动态结果计算(线性拟合)
  277. func RefreshCalculateByRuleByLineNh(predictEdbInfo EdbInfo, predictEdbConfAndDataList []*PredictEdbConfAndData, rule PredictEdbConf) (err error, errMsg string) {
  278. o := orm.NewOrm()
  279. to, err := o.Begin()
  280. if err != nil {
  281. return
  282. }
  283. defer func() {
  284. if err != nil {
  285. to.Rollback()
  286. } else {
  287. err = to.Commit()
  288. }
  289. }()
  290. err, errMsg = CalculateByRuleByRuleLineNh(to, predictEdbInfo, predictEdbConfAndDataList, rule)
  291. return
  292. }
  293. // CalculateByRuleByRuleLineNh 一元线性拟合规则计算入库
  294. func CalculateByRuleByRuleLineNh(to orm.TxOrmer, predictEdbInfo EdbInfo, predictEdbConfAndDataList []*PredictEdbConfAndData, rule PredictEdbConf) (err error, errMsg string) {
  295. var secondDataList []*EdbInfoSearchData
  296. predictEdbInfoId := predictEdbInfo.EdbInfoId // 预测指标id
  297. // 规则
  298. var ruleConf RuleLineNhConf
  299. tmpErr := json.Unmarshal([]byte(rule.Value), &ruleConf)
  300. if tmpErr != nil {
  301. errMsg = `季节性配置信息异常`
  302. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  303. return
  304. }
  305. // 获取自身指标的数据
  306. {
  307. // 来源指标
  308. var sourceEdbInfoItem *EdbInfo
  309. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  310. err = to.Raw(sql, rule.SourceEdbInfoId).QueryRow(&sourceEdbInfoItem)
  311. if err != nil {
  312. return
  313. }
  314. predictEdbInfo.EdbInfoId = 0
  315. secondDataList, err, _ = GetPredictDataListByPredictEdbConfList(&predictEdbInfo, sourceEdbInfoItem, predictEdbConfAndDataList, 1, ``)
  316. if err != nil {
  317. return
  318. }
  319. }
  320. lenSecondData := len(secondDataList)
  321. if lenSecondData <= 0 {
  322. return
  323. }
  324. newNhccDataMap, err, errMsg := getCalculateNhccData(secondDataList, ruleConf)
  325. if err != nil {
  326. return
  327. }
  328. //将最后计算出来的结果数据处理(新增入库、编辑日期的值、删除日期)
  329. {
  330. // 获取需要预测的日期
  331. startDateStr := secondDataList[lenSecondData-1].DataTime
  332. startDate, _ := time.ParseInLocation(utils.FormatDate, startDateStr, time.Local)
  333. //endDate, _ := time.ParseInLocation(utils.FormatDate, ruleConf.EndDate, time.Local)
  334. endDate := rule.EndDate
  335. dayList := getPredictEdbDayList(startDate, endDate, predictEdbInfo.Frequency, predictEdbInfo.DataDateType)
  336. if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
  337. return
  338. }
  339. //获取该配置的所有数据
  340. dataList := make([]*PredictEdbRuleData, 0)
  341. sql := `SELECT * FROM predict_edb_rule_data WHERE config_id = ?`
  342. _, err = to.Raw(sql, rule.ConfigId).QueryRows(&dataList)
  343. if err != nil {
  344. return
  345. }
  346. dataMap := make(map[string]*PredictEdbRuleData)
  347. for _, v := range dataList {
  348. dataMap[v.DataTime] = v
  349. }
  350. //需要移除的日期
  351. removeDateList := make([]string, 0)
  352. // 已经操作过的日期
  353. existDataMap := make(map[string]string)
  354. // 添加数据
  355. addDataList := make([]*PredictEdbRuleData, 0)
  356. for _, currentDate := range dayList {
  357. // 动态拟合残差值数据
  358. currentDateStr := currentDate.Format(utils.FormatDate)
  359. val, ok := newNhccDataMap[currentDateStr]
  360. // 找不到数据,那么就移除该日期的数据
  361. if !ok {
  362. removeDateList = append(removeDateList, currentDateStr)
  363. continue
  364. }
  365. saveValue := decimal.NewFromFloat(val).Round(4).String() //utils.SubFloatToString(calVal, 4)
  366. existPredictEdbRuleData, ok := dataMap[currentDateStr]
  367. if !ok {
  368. timestamp := currentDate.UnixNano() / 1e6
  369. if _, existOk := existDataMap[currentDateStr]; !existOk {
  370. tmpPredictEdbRuleData := &PredictEdbRuleData{
  371. //PredictEdbRuleDataId: 0,
  372. EdbInfoId: predictEdbInfoId,
  373. ConfigId: rule.ConfigId,
  374. DataTime: currentDateStr,
  375. Value: saveValue,
  376. CreateTime: time.Now(),
  377. ModifyTime: time.Now(),
  378. DataTimestamp: timestamp,
  379. }
  380. addDataList = append(addDataList, tmpPredictEdbRuleData)
  381. }
  382. existDataMap[currentDateStr] = currentDateStr
  383. } else {
  384. existValDecimal, tmpErr := decimal.NewFromString(existPredictEdbRuleData.Value)
  385. if tmpErr != nil {
  386. err = tmpErr
  387. return
  388. }
  389. existStr := existValDecimal.String()
  390. if existStr != saveValue {
  391. existPredictEdbRuleData.Value = saveValue
  392. existPredictEdbRuleData.ModifyTime = time.Now()
  393. _, err = to.Update(existPredictEdbRuleData, "Value", "ModifyTime")
  394. if err != nil {
  395. return
  396. }
  397. }
  398. }
  399. }
  400. // 添加计算出来的值入库
  401. lenAddDataList := len(addDataList)
  402. if lenAddDataList > 0 {
  403. _, err = to.InsertMulti(lenAddDataList, addDataList)
  404. if err != nil {
  405. return
  406. }
  407. }
  408. //删除多余的值
  409. lenRemoveDateList := len(removeDateList)
  410. if lenRemoveDateList > 0 {
  411. //如果拼接指标变更了,那么需要删除所有的指标数据
  412. sql := ` DELETE FROM predict_edb_rule_data WHERE config_id = ? and data_time in (` + utils.GetOrmInReplace(lenRemoveDateList) + `) `
  413. _, err = to.Raw(sql, rule.ConfigId, removeDateList).Exec()
  414. if err != nil {
  415. err = fmt.Errorf("删除计算失败的预测规则计算指标数据失败,Err:" + err.Error())
  416. return
  417. }
  418. }
  419. }
  420. return
  421. }