predict_edb.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  195. fmt.Println(err)
  196. return nil, err
  197. }
  198. nanCheck := fmt.Sprintf("%0.f", calVal)
  199. if nanCheck == "NaN" || nanCheck == "+Inf" || nanCheck == "-Inf" {
  200. continue
  201. }
  202. // 移除不存在的日期
  203. delete(removeDateMap, sk)
  204. saveValue := decimal.NewFromFloat(calVal).RoundCeil(4).String() //utils.SubFloatToString(calVal, 4)
  205. existPredictEdbRuleData, ok := dataMap[sk]
  206. if !ok {
  207. dataTime, _ := time.ParseInLocation(utils.FormatDate, sk, time.Local)
  208. timestamp := dataTime.UnixNano() / 1e6
  209. if _, existOk := existDataMap[sk]; !existOk {
  210. tmpPredictEdbRuleData := &PredictEdbRuleData{
  211. //PredictEdbRuleDataId: 0,
  212. EdbInfoId: rule.EdbInfoId,
  213. ConfigId: rule.ConfigId,
  214. DataTime: sk,
  215. Value: saveValue,
  216. CreateTime: time.Now(),
  217. ModifyTime: time.Now(),
  218. DataTimestamp: timestamp,
  219. }
  220. addDataList = append(addDataList, tmpPredictEdbRuleData)
  221. }
  222. existDataMap[sk] = sk
  223. } else {
  224. existValDecimal, tmpErr := decimal.NewFromString(existPredictEdbRuleData.Value)
  225. if tmpErr != nil {
  226. err = tmpErr
  227. return nil, tmpErr
  228. }
  229. existStr := existValDecimal.String()
  230. if existStr != saveValue {
  231. existPredictEdbRuleData.Value = saveValue
  232. existPredictEdbRuleData.ModifyTime = time.Now()
  233. _, err = to.Update(existPredictEdbRuleData, "Value", "ModifyTime")
  234. if err != nil {
  235. return nil, err
  236. }
  237. }
  238. }
  239. // 计算出来的结果集
  240. resultDataList = append(resultDataList, &EdbInfoSearchData{
  241. //EdbDataId: 0,
  242. DataTime: sk,
  243. Value: calVal,
  244. })
  245. }
  246. // 添加计算出来的值入库
  247. lenAddDataList := len(addDataList)
  248. if lenAddDataList > 0 {
  249. _, err = to.InsertMulti(lenAddDataList, addDataList)
  250. if err != nil {
  251. return
  252. }
  253. }
  254. //删除多余的值
  255. lenRemoveDateList := len(removeDateMap)
  256. if lenRemoveDateList > 0 {
  257. removeDateList := make([]string, 0) //需要移除的日期
  258. for date, _ := range removeDateMap {
  259. removeDateList = append(removeDateList, date)
  260. }
  261. //如果拼接指标变更了,那么需要删除所有的指标数据
  262. sql := ` DELETE FROM predict_edb_rule_data WHERE config_id = ? and data_time in (` + utils.GetOrmInReplace(lenRemoveDateList) + `) `
  263. _, err = to.Raw(sql, rule.ConfigId, removeDateList).Exec()
  264. if err != nil {
  265. err = fmt.Errorf("删除计算失败的预测规则计算指标数据失败,Err:" + err.Error())
  266. return
  267. }
  268. }
  269. return
  270. }
  271. // RefreshCalculateByRuleByLineNh 刷新动态结果计算(线性拟合)
  272. func RefreshCalculateByRuleByLineNh(predictEdbInfo EdbInfo, predictEdbConfAndDataList []*PredictEdbConfAndData, rule PredictEdbConf) (err error, errMsg string) {
  273. o := orm.NewOrm()
  274. to, err := o.Begin()
  275. if err != nil {
  276. return
  277. }
  278. defer func() {
  279. if err != nil {
  280. to.Rollback()
  281. } else {
  282. err = to.Commit()
  283. }
  284. }()
  285. err, errMsg = CalculateByRuleByRuleLineNh(to, predictEdbInfo, predictEdbConfAndDataList, rule)
  286. return
  287. }
  288. // CalculateByRuleByRuleLineNh 一元线性拟合规则计算入库
  289. func CalculateByRuleByRuleLineNh(to orm.TxOrmer, predictEdbInfo EdbInfo, predictEdbConfAndDataList []*PredictEdbConfAndData, rule PredictEdbConf) (err error, errMsg string) {
  290. var secondDataList []*EdbInfoSearchData
  291. predictEdbInfoId := predictEdbInfo.EdbInfoId // 预测指标id
  292. // 规则
  293. var ruleConf RuleLineNhConf
  294. tmpErr := json.Unmarshal([]byte(rule.Value), &ruleConf)
  295. if tmpErr != nil {
  296. errMsg = `季节性配置信息异常`
  297. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  298. return
  299. }
  300. // 获取自身指标的数据
  301. {
  302. // 来源指标
  303. var sourceEdbInfoItem *EdbInfo
  304. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  305. err = to.Raw(sql, rule.SourceEdbInfoId).QueryRow(&sourceEdbInfoItem)
  306. if err != nil {
  307. return
  308. }
  309. predictEdbInfo.EdbInfoId = 0
  310. secondDataList, err, _ = GetPredictDataListByPredictEdbConfList(&predictEdbInfo, sourceEdbInfoItem, predictEdbConfAndDataList, 1, ``)
  311. if err != nil {
  312. return
  313. }
  314. }
  315. lenSecondData := len(secondDataList)
  316. if lenSecondData <= 0 {
  317. return
  318. }
  319. newNhccDataMap, err, errMsg := getCalculateNhccData(secondDataList, ruleConf)
  320. if err != nil {
  321. return
  322. }
  323. //将最后计算出来的结果数据处理(新增入库、编辑日期的值、删除日期)
  324. {
  325. // 获取需要预测的日期
  326. startDateStr := secondDataList[lenSecondData-1].DataTime
  327. startDate, _ := time.ParseInLocation(utils.FormatDate, startDateStr, time.Local)
  328. //endDate, _ := time.ParseInLocation(utils.FormatDate, ruleConf.EndDate, time.Local)
  329. endDate := rule.EndDate
  330. dayList := getPredictEdbDayList(startDate, endDate, predictEdbInfo.Frequency, predictEdbInfo.DataDateType)
  331. if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
  332. return
  333. }
  334. //获取该配置的所有数据
  335. dataList := make([]*PredictEdbRuleData, 0)
  336. sql := `SELECT * FROM predict_edb_rule_data WHERE config_id = ?`
  337. _, err = to.Raw(sql, rule.ConfigId).QueryRows(&dataList)
  338. if err != nil {
  339. return
  340. }
  341. dataMap := make(map[string]*PredictEdbRuleData)
  342. for _, v := range dataList {
  343. dataMap[v.DataTime] = v
  344. }
  345. //需要移除的日期
  346. removeDateList := make([]string, 0)
  347. // 已经操作过的日期
  348. existDataMap := make(map[string]string)
  349. // 添加数据
  350. addDataList := make([]*PredictEdbRuleData, 0)
  351. for _, currentDate := range dayList {
  352. // 动态拟合残差值数据
  353. currentDateStr := currentDate.Format(utils.FormatDate)
  354. val, ok := newNhccDataMap[currentDateStr]
  355. // 找不到数据,那么就移除该日期的数据
  356. if !ok {
  357. removeDateList = append(removeDateList, currentDateStr)
  358. continue
  359. }
  360. saveValue := decimal.NewFromFloat(val).RoundCeil(4).String() //utils.SubFloatToString(calVal, 4)
  361. existPredictEdbRuleData, ok := dataMap[currentDateStr]
  362. if !ok {
  363. timestamp := currentDate.UnixNano() / 1e6
  364. if _, existOk := existDataMap[currentDateStr]; !existOk {
  365. tmpPredictEdbRuleData := &PredictEdbRuleData{
  366. //PredictEdbRuleDataId: 0,
  367. EdbInfoId: predictEdbInfoId,
  368. ConfigId: rule.ConfigId,
  369. DataTime: currentDateStr,
  370. Value: saveValue,
  371. CreateTime: time.Now(),
  372. ModifyTime: time.Now(),
  373. DataTimestamp: timestamp,
  374. }
  375. addDataList = append(addDataList, tmpPredictEdbRuleData)
  376. }
  377. existDataMap[currentDateStr] = currentDateStr
  378. } else {
  379. existValDecimal, tmpErr := decimal.NewFromString(existPredictEdbRuleData.Value)
  380. if tmpErr != nil {
  381. err = tmpErr
  382. return
  383. }
  384. existStr := existValDecimal.String()
  385. if existStr != saveValue {
  386. existPredictEdbRuleData.Value = saveValue
  387. existPredictEdbRuleData.ModifyTime = time.Now()
  388. _, err = to.Update(existPredictEdbRuleData, "Value", "ModifyTime")
  389. if err != nil {
  390. return
  391. }
  392. }
  393. }
  394. }
  395. // 添加计算出来的值入库
  396. lenAddDataList := len(addDataList)
  397. if lenAddDataList > 0 {
  398. _, err = to.InsertMulti(lenAddDataList, addDataList)
  399. if err != nil {
  400. return
  401. }
  402. }
  403. //删除多余的值
  404. lenRemoveDateList := len(removeDateList)
  405. if lenRemoveDateList > 0 {
  406. //如果拼接指标变更了,那么需要删除所有的指标数据
  407. sql := ` DELETE FROM predict_edb_rule_data WHERE config_id = ? and data_time in (` + utils.GetOrmInReplace(lenRemoveDateList) + `) `
  408. _, err = to.Raw(sql, rule.ConfigId, removeDateList).Exec()
  409. if err != nil {
  410. err = fmt.Errorf("删除计算失败的预测规则计算指标数据失败,Err:" + err.Error())
  411. return
  412. }
  413. }
  414. }
  415. return
  416. }