edb_data_calculate_percentile.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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/shopspring/decimal"
  9. "reflect"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // Percentile 百分位
  15. type Percentile struct {
  16. }
  17. // Add 添加
  18. func (obj Percentile) Add(params AddCalculateBatchParams) (edbInfo *EdbInfo, err error, errMsg string) {
  19. req := params.Req
  20. fromEdbInfo := params.FromEdbInfo
  21. edbCode := params.EdbCode
  22. o := orm.NewOrm()
  23. to, err := o.Begin()
  24. if err != nil {
  25. return
  26. }
  27. defer func() {
  28. if err != nil {
  29. _ = to.Rollback()
  30. fmt.Println(reflect.TypeOf(obj).Name(), ";Add,Err:"+err.Error())
  31. } else {
  32. _ = to.Commit()
  33. }
  34. }()
  35. edbInfo = new(EdbInfo)
  36. edbInfo.Source = obj.GetSource()
  37. edbInfo.SourceName = obj.GetSourceName()
  38. edbInfo.EdbCode = edbCode
  39. edbInfo.EdbName = req.EdbName
  40. edbInfo.EdbNameSource = req.EdbName
  41. edbInfo.Frequency = req.Frequency
  42. edbInfo.Unit = req.Unit
  43. edbInfo.ClassifyId = req.ClassifyId
  44. edbInfo.SysUserId = params.SysUserId
  45. edbInfo.SysUserRealName = params.SysUserRealName
  46. edbInfo.CreateTime = time.Now()
  47. edbInfo.ModifyTime = time.Now()
  48. edbInfo.UniqueCode = params.UniqueCode
  49. edbInfo.CalculateFormula = req.Formula
  50. edbInfo.EdbNameEn = req.EdbName
  51. edbInfo.UnitEn = req.Unit
  52. edbInfo.EdbType = obj.GetEdbType()
  53. edbInfo.Sort = GetAddEdbMaxSortByClassifyId(req.ClassifyId, utils.EDB_INFO_TYPE)
  54. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  55. if tmpErr != nil {
  56. err = tmpErr
  57. return
  58. }
  59. edbInfo.EdbInfoId = int(newEdbInfoId)
  60. //关联关系
  61. {
  62. calculateMappingItem := new(EdbInfoCalculateMapping)
  63. calculateMappingItem.CreateTime = time.Now()
  64. calculateMappingItem.ModifyTime = time.Now()
  65. calculateMappingItem.Sort = 1
  66. calculateMappingItem.EdbCode = edbCode
  67. calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
  68. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  69. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  70. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  71. calculateMappingItem.FromSource = fromEdbInfo.Source
  72. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  73. calculateMappingItem.FromTag = ""
  74. calculateMappingItem.Source = edbInfo.Source
  75. calculateMappingItem.SourceName = edbInfo.SourceName
  76. calculateMappingItem.FromSubSource = fromEdbInfo.SubSource
  77. _, err = to.Insert(calculateMappingItem)
  78. if err != nil {
  79. return
  80. }
  81. }
  82. //计算数据
  83. err, errMsg = obj.refresh(to, edbInfo, fromEdbInfo, edbInfo.EdbCode)
  84. return
  85. }
  86. // Edit 编辑
  87. func (obj Percentile) Edit(params EditCalculateBatchParams) (err error, errMsg string) {
  88. req := params.Req
  89. edbInfo := params.EdbInfo
  90. fromEdbInfo := params.FromEdbInfo
  91. o := orm.NewOrm()
  92. to, err := o.Begin()
  93. if err != nil {
  94. return
  95. }
  96. defer func() {
  97. if err != nil {
  98. _ = to.Rollback()
  99. fmt.Println(reflect.TypeOf(obj).Name(), ";Edit,Err:"+err.Error())
  100. } else {
  101. _ = to.Commit()
  102. }
  103. }()
  104. tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  105. oldEdbInfo := *edbInfo
  106. //修改指标信息
  107. edbInfo.EdbName = req.EdbName
  108. edbInfo.EdbNameSource = req.EdbName
  109. edbInfo.Frequency = req.Frequency
  110. edbInfo.Unit = req.Unit
  111. edbInfo.ClassifyId = req.ClassifyId
  112. edbInfo.CalculateFormula = req.Formula
  113. edbInfo.EdbNameEn = req.EdbNameEn
  114. edbInfo.UnitEn = req.UnitEn
  115. edbInfo.ModifyTime = time.Now()
  116. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime", "EdbNameEn", "UnitEn")
  117. if err != nil {
  118. return
  119. }
  120. var existCondition string
  121. var existPars []interface{}
  122. existCondition += " AND edb_info_id=? AND from_edb_info_id=? "
  123. existPars = append(existPars, edbInfo.EdbInfoId, req.FromEdbInfoId)
  124. //判断计算指标是否被更换
  125. count, err := GetEdbInfoCalculateCountByCondition(existCondition, existPars)
  126. if err != nil {
  127. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  128. return
  129. }
  130. if count > 0 { // 指标未被替换,无需处理逻辑
  131. // 如果相关配置更改了,那么重新计算
  132. if oldEdbInfo.CalculateFormula != edbInfo.CalculateFormula {
  133. err, errMsg = obj.refresh(to, edbInfo, fromEdbInfo, edbInfo.EdbCode)
  134. }
  135. return
  136. }
  137. //删除,计算指标关联的,基础指标的关联关系
  138. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  139. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  140. if err != nil {
  141. return
  142. }
  143. //清空原有数据
  144. sql = ` DELETE FROM ` + tableName + ` WHERE edb_info_id = ? `
  145. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  146. if err != nil {
  147. return
  148. }
  149. //关联关系
  150. {
  151. calculateMappingItem := &EdbInfoCalculateMapping{
  152. EdbInfoCalculateMappingId: 0,
  153. EdbInfoId: edbInfo.EdbInfoId,
  154. Source: obj.GetSource(),
  155. SourceName: obj.GetSourceName(),
  156. EdbCode: edbInfo.EdbCode,
  157. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  158. FromEdbCode: fromEdbInfo.EdbCode,
  159. FromEdbName: fromEdbInfo.EdbName,
  160. FromSource: fromEdbInfo.Source,
  161. FromSourceName: fromEdbInfo.SourceName,
  162. FromTag: "",
  163. Sort: 1,
  164. CreateTime: time.Now(),
  165. ModifyTime: time.Now(),
  166. FromSubSource: fromEdbInfo.SubSource,
  167. }
  168. _, err = to.Insert(calculateMappingItem)
  169. if err != nil {
  170. return
  171. }
  172. }
  173. //计算数据
  174. err, errMsg = obj.refresh(to, edbInfo, fromEdbInfo, edbInfo.EdbCode)
  175. return
  176. }
  177. // Refresh 刷新
  178. func (obj Percentile) Refresh(params RefreshParams) (err error, errMsg string) {
  179. calculateMapping, err := GetEdbInfoCalculateMappingDetail(params.EdbInfo.EdbInfoId)
  180. if err != nil {
  181. errMsg = "GetEdbInfoCalculateLjzzyDetail Err:" + err.Error()
  182. return
  183. }
  184. fromEdbInfo, err := GetEdbInfoById(calculateMapping.FromEdbInfoId)
  185. if err != nil {
  186. errMsg = "GetEdbInfoById Err:" + err.Error()
  187. return
  188. }
  189. o := orm.NewOrm()
  190. to, err := o.Begin()
  191. if err != nil {
  192. return
  193. }
  194. defer func() {
  195. if err != nil {
  196. _ = to.Rollback()
  197. fmt.Println(reflect.TypeOf(obj).Name(), ";Refresh,Err:"+err.Error())
  198. } else {
  199. _ = to.Commit()
  200. }
  201. }()
  202. // 计算数据
  203. err, errMsg = obj.refresh(to, params.EdbInfo, fromEdbInfo, params.EdbInfo.EdbCode)
  204. return
  205. }
  206. // GetSource 获取来源编码id
  207. func (obj Percentile) GetSource() int {
  208. return utils.DATA_SOURCE_CALCULATE_PERCENTILE
  209. }
  210. // GetSourceName 获取来源名称
  211. func (obj Percentile) GetSourceName() string {
  212. return utils.DATA_SOURCE_NAME_CALCULATE_PERCENTILE
  213. }
  214. // GetEdbType 获取指标类型
  215. func (obj Percentile) GetEdbType() int {
  216. return utils.CALCULATE_EDB_TYPE
  217. }
  218. // PercentileConfig 百分位的配置
  219. type PercentileConfig struct {
  220. CalculateValue int `description:"时间长度期数"`
  221. CalculateUnit string `description:"时间长度频度"`
  222. PercentType int `description:"百分位:0-数据区间(兼容历史数据); 1-数据个数;"`
  223. }
  224. // refresh 刷新
  225. func (obj Percentile) refresh(to orm.TxOrmer, edbInfo, fromEdbInfo *EdbInfo, edbCode string) (err error, errMsg string) {
  226. edbInfoId := edbInfo.EdbInfoId
  227. dataTableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  228. edbInfoIdStr := strconv.Itoa(edbInfoId)
  229. var percentileConfig PercentileConfig
  230. err = json.Unmarshal([]byte(edbInfo.CalculateFormula), &percentileConfig)
  231. if err != nil {
  232. return
  233. }
  234. // 获取百分位的指标数据
  235. fromDataList, err, errMsg := obj.getPercentileData(fromEdbInfo, percentileConfig.CalculateValue, percentileConfig.CalculateUnit, percentileConfig.PercentType)
  236. if err != nil {
  237. return
  238. }
  239. //获取指标所有数据
  240. existDataList := make([]*EdbData, 0)
  241. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  242. sql = fmt.Sprintf(sql, dataTableName)
  243. _, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
  244. if err != nil {
  245. return
  246. }
  247. existDataMap := make(map[string]string)
  248. removeDataTimeMap := make(map[string]int) //需要移除的日期数据
  249. for _, v := range existDataList {
  250. existDataMap[v.DataTime] = v.Value
  251. removeDataTimeMap[v.DataTime] = 1
  252. }
  253. needAddDateMap := make(map[time.Time]int)
  254. addSql := ` INSERT INTO ` + dataTableName + `(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  255. var isAdd bool
  256. for _, tmpData := range fromDataList {
  257. currDateStr := tmpData.DataTime
  258. currTime, tmpErr := time.ParseInLocation(utils.FormatDate, currDateStr, time.Local)
  259. if tmpErr != nil {
  260. err = tmpErr
  261. return
  262. }
  263. // 当前的实际值
  264. saveValue := decimal.NewFromFloat(tmpData.Value).Round(4).String()
  265. existVal, ok := existDataMap[currDateStr]
  266. // 如果库中已经存在该数据的话,那么就进行值的变更操作
  267. if ok {
  268. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该日期
  269. delete(removeDataTimeMap, currDateStr)
  270. if existVal != saveValue {
  271. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  272. sql = fmt.Sprintf(sql, dataTableName)
  273. _, err = to.Raw(sql, saveValue, edbInfoId, currDateStr).Exec()
  274. if err != nil {
  275. return
  276. }
  277. }
  278. continue
  279. }
  280. // 库中不存在该日期的数据
  281. timestamp := currTime.UnixNano() / 1e6
  282. timeStr := fmt.Sprintf("%d", timestamp)
  283. if _, existOk := needAddDateMap[currTime]; !existOk {
  284. addSql += GetAddSql(edbInfoIdStr, edbCode, currDateStr, timeStr, saveValue)
  285. isAdd = true
  286. }
  287. needAddDateMap[currTime] = 1
  288. }
  289. //删除已经不存在的指标数据(由于该指标当日的数据删除了)
  290. {
  291. removeDateList := make([]string, 0)
  292. for dateTime := range removeDataTimeMap {
  293. removeDateList = append(removeDateList, dateTime)
  294. }
  295. removeNum := len(removeDateList)
  296. if removeNum > 0 {
  297. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (`+utils.GetOrmInReplace(removeNum)+`) `, dataTableName)
  298. _, err = to.Raw(sql, edbInfo.EdbInfoId, removeDateList).Exec()
  299. if err != nil {
  300. fmt.Println(reflect.TypeOf(obj).Name(), " add data ;delete Err", err.Error())
  301. err = fmt.Errorf("删除不存在的指标数据失败,Err:" + err.Error())
  302. return
  303. }
  304. }
  305. }
  306. if isAdd {
  307. addSql = strings.TrimRight(addSql, ",")
  308. _, err = to.Raw(addSql).Exec()
  309. if err != nil {
  310. fmt.Println(reflect.TypeOf(obj).Name(), " add data Err", err.Error())
  311. return
  312. }
  313. }
  314. return
  315. }
  316. func (obj Percentile) calculate(edbInfoId int, date, edbInfoIdStr, edbCode, dataTableName, addSql string, val float64, dataMap, existDataMap map[string]string, to orm.TxOrmer) (newSql string, isAdd bool, err error) {
  317. newSql = addSql
  318. saveValue := utils.SubFloatToString(val, 4)
  319. //判断数据是否存在
  320. if existVal, ok := dataMap[date]; !ok {
  321. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  322. timestamp := dataTime.UnixNano() / 1e6
  323. timeStr := fmt.Sprintf("%d", timestamp)
  324. if _, existOk := existDataMap[date]; !existOk {
  325. newSql += GetAddSql(edbInfoIdStr, edbCode, date, timeStr, saveValue)
  326. isAdd = true
  327. }
  328. existDataMap[date] = date
  329. } else {
  330. if existVal != saveValue {
  331. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  332. sql = fmt.Sprintf(sql, dataTableName)
  333. _, err = to.Raw(sql, saveValue, edbInfoId, date).Exec()
  334. }
  335. }
  336. return
  337. }
  338. // GetPercentileData 获取百分位图表的指标数据
  339. func (obj Percentile) getPercentileData(fromEdbInfo *EdbInfo, calculateValue int, calculateUnit string, percentType int) (newDataList []EdbInfoSearchData, err error, errMsg string) {
  340. // 获取时间基准指标在时间区间内的值
  341. dataList := make([]*EdbInfoSearchData, 0)
  342. switch fromEdbInfo.EdbInfoType {
  343. case 0:
  344. //获取来源指标的数据
  345. dataList, err = GetEdbDataListAll(fromEdbInfo.Source, fromEdbInfo.SubSource, FindEdbDataListAllCond{
  346. EdbInfoId: fromEdbInfo.EdbInfoId,
  347. }, 1)
  348. case 1:
  349. dataList, err = GetPredictEdbDataListAllByStartDate(fromEdbInfo, 1, "")
  350. default:
  351. err = errors.New(fmt.Sprint("获取失败,指标base类型异常", fromEdbInfo.EdbInfoType))
  352. return
  353. }
  354. moveUnitDays, ok := utils.FrequencyDaysMap[calculateUnit]
  355. if !ok {
  356. errMsg = `错误的周期`
  357. err = errors.New(errMsg)
  358. return
  359. }
  360. calculateDay := calculateValue * moveUnitDays
  361. // 指标对应的所有数据
  362. newDataList = make([]EdbInfoSearchData, 0)
  363. dataMap := make(map[time.Time]float64, 0)
  364. for _, tmpData := range dataList {
  365. currDateTime, _ := time.ParseInLocation(utils.FormatDate, tmpData.DataTime, time.Local)
  366. dataMap[currDateTime] = tmpData.Value
  367. }
  368. //百分位:对所选指标滚动地取对应时间长度的数据值,取最大值Max,最小值Min,计算Max-Min,百分位=(现值-Min)/(Max-Min),Max=Min时不予计算。
  369. if percentType == utils.PercentCalculateTypeRange {
  370. for i, tmpData := range dataList {
  371. currDateTime, _ := time.ParseInLocation(utils.FormatDate, tmpData.DataTime, time.Local)
  372. maxVal := tmpData.Value
  373. minVal := tmpData.Value
  374. for k := 0; k < calculateDay; k++ {
  375. preVal, ok2 := dataMap[currDateTime.AddDate(0, 0, -k)]
  376. if ok2 {
  377. if preVal > maxVal {
  378. maxVal = preVal
  379. }
  380. if preVal < minVal {
  381. minVal = preVal
  382. }
  383. }
  384. }
  385. if maxVal == minVal {
  386. continue
  387. }
  388. tmpV := (tmpData.Value - minVal) / (maxVal - minVal) * 100
  389. tmpV, _ = decimal.NewFromFloat(tmpV).Round(4).Float64()
  390. //百分位=(现值-Min)/(Max-Min)
  391. newDataList = append(newDataList, EdbInfoSearchData{
  392. EdbDataId: i,
  393. DataTime: dataList[i].DataTime,
  394. Value: tmpV,
  395. })
  396. }
  397. }
  398. // 百分位数据个数算法
  399. // 数据区间第一个和最后一个数据点的时间和数据分别为(T1,S1)(T2,S2); N=T1到T2指标数据个数, n=小于等于S2的数据个数
  400. // 个数百分位=(n-1)/(N-1)
  401. maxDay := len(dataList) // 往前找数据的边界
  402. if percentType == utils.PercentCalculateTypeNum {
  403. for i, d := range dataList {
  404. // T2为当前日期
  405. s2 := decimal.NewFromFloat(d.Value)
  406. t2, _ := time.ParseInLocation(utils.FormatDate, d.DataTime, time.Local)
  407. // 计算N和n
  408. var bigN, tinyN int
  409. for k := 0; k < maxDay; k++ {
  410. // 往前找(时间长度)个有数据的, N理论上只有最前面几个日期<calculateDay, 后面的N=calculateDay
  411. if bigN >= calculateDay {
  412. break
  413. }
  414. preVal, preOk := dataMap[t2.AddDate(0, 0, -k)]
  415. if !preOk {
  416. continue
  417. }
  418. bigN += 1
  419. if decimal.NewFromFloat(preVal).LessThanOrEqual(s2) {
  420. tinyN += 1
  421. }
  422. }
  423. // N<=1时说明计算无效
  424. if bigN <= 1 {
  425. continue
  426. }
  427. numerator := decimal.NewFromInt(int64(tinyN - 1))
  428. denominator := decimal.NewFromInt(int64(bigN - 1))
  429. // 因为是百分位所以这里是要*100, 跟之前的算法保持同步
  430. percentVal, _ := numerator.Div(denominator).Mul(decimal.NewFromFloat(100)).Round(4).Float64()
  431. // 写进数组并判断指标最大最小值
  432. newDataList = append(newDataList, EdbInfoSearchData{
  433. EdbDataId: i,
  434. DataTime: dataList[i].DataTime,
  435. Value: percentVal,
  436. })
  437. }
  438. }
  439. return
  440. }