edb_info.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_hub/models/data_manage"
  6. "eta/eta_hub/utils"
  7. "fmt"
  8. "strings"
  9. "time"
  10. )
  11. // TraceEdbInfoByEdbInfoId 指标追溯
  12. func TraceEdbInfoByEdbInfoId(edbInfoId int) (traceEdbInfo data_manage.TraceEdbInfoResp, err error) {
  13. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  14. if err != nil {
  15. return
  16. }
  17. edbInfoRuleMap := make(map[int]string, 0)
  18. edbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo)
  19. //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
  20. traceEdbInfo = data_manage.TraceEdbInfoResp{
  21. //EdbInfoId: edbInfo.EdbInfoId,
  22. EdbInfoId: edbInfoId,
  23. EdbInfoType: edbInfo.EdbInfoType,
  24. EdbName: edbInfo.EdbName,
  25. EdbType: edbInfo.EdbType,
  26. //Source: edbInfo.Source,
  27. UniqueCode: edbInfo.UniqueCode,
  28. ClassifyId: edbInfo.ClassifyId,
  29. EdbInfo: edbInfo,
  30. }
  31. findIdMap := make(map[int]int)
  32. findIdMap[edbInfoId] = edbInfoId
  33. existMap := make(map[int]data_manage.TraceEdbInfoResp)
  34. traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  35. edbInfoIdList := make([]int, 0)
  36. for _, v := range findIdMap {
  37. edbInfoIdList = append(edbInfoIdList, v)
  38. }
  39. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  40. if err != nil {
  41. return
  42. }
  43. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  44. for _, tmpEdbInfo := range edbInfoList {
  45. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
  46. }
  47. traceEdbInfo, err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap)
  48. return
  49. }
  50. // traceEdbInfoByEdbInfoId 指标追溯
  51. func traceEdbInfoByEdbInfoId(edbInfoId int, traceEdbInfo data_manage.TraceEdbInfoResp, edbInfoRuleMap map[int]string, findIdMap map[int]int, existMap map[int]data_manage.TraceEdbInfoResp, edbMappingMap map[int][]*data_manage.EdbInfoCalculateMappingInfo) (child []data_manage.TraceEdbInfoResp, err error) {
  52. traceEdbInfo, ok := existMap[edbInfoId]
  53. if ok {
  54. return
  55. }
  56. child = make([]data_manage.TraceEdbInfoResp, 0)
  57. edbInfoMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  58. if e != nil {
  59. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", e.Error())
  60. return
  61. }
  62. // 指标信息map
  63. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  64. if len(edbInfoMappingList) > 0 {
  65. fromEdbInfoIdList := make([]int, 0)
  66. for _, v := range edbInfoMappingList {
  67. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  68. }
  69. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(fromEdbInfoIdList)
  70. if tmpErr != nil {
  71. err = fmt.Errorf("traceEdbInfoByEdbInfoId GetEdbInfoByIdList err: %s", tmpErr.Error())
  72. return
  73. }
  74. for _, v := range edbInfoList {
  75. edbInfoMap[v.EdbInfoId] = v
  76. }
  77. }
  78. edbMappingMap[edbInfoId] = edbInfoMappingList
  79. for _, v := range edbInfoMappingList {
  80. tmpEdbInfoId := v.FromEdbInfoId
  81. tmpTraceEdbInfo := data_manage.TraceEdbInfoResp{
  82. EdbInfoId: tmpEdbInfoId,
  83. EdbInfoType: v.FromEdbInfoType,
  84. EdbType: v.FromEdbType,
  85. UniqueCode: v.FromUniqueCode,
  86. ClassifyId: v.FromClassifyId,
  87. EdbInfo: edbInfoMap[v.FromEdbInfoId],
  88. }
  89. // 计算指标/预测指标继续溯源
  90. if v.FromEdbType == 2 || v.FromEdbInfoType == 1 {
  91. // 查过了就不查了
  92. if _, ok2 := findIdMap[tmpEdbInfoId]; !ok2 {
  93. tmpTraceEdbInfo.Child, e = traceEdbInfoByEdbInfoId(tmpEdbInfoId, tmpTraceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  94. if e != nil {
  95. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", e.Error())
  96. return
  97. }
  98. }
  99. }
  100. child = append(child, tmpTraceEdbInfo)
  101. findIdMap[tmpEdbInfoId] = tmpEdbInfoId
  102. }
  103. existMap[edbInfoId] = traceEdbInfo
  104. return
  105. }
  106. func handleTraceEdbInfo(traceEdbInfoResp data_manage.TraceEdbInfoResp, parentEdbInfoId int, edbInfoMap map[int]*data_manage.EdbInfo, edbMappingMap map[int][]*data_manage.EdbInfoCalculateMappingInfo) (newTraceEdbInfoResp data_manage.TraceEdbInfoResp, err error) {
  107. edbInfo, ok := edbInfoMap[traceEdbInfoResp.EdbInfoId]
  108. if !ok {
  109. err = errors.New("指标异常")
  110. return
  111. }
  112. var parentEdbInfo *data_manage.EdbInfo
  113. if parentEdbInfoId > 0 {
  114. parentEdbInfo, ok = edbInfoMap[parentEdbInfoId]
  115. if !ok {
  116. err = errors.New("指标异常")
  117. return
  118. }
  119. }
  120. //traceEdbInfoResp.EdbName = edbInfo.EdbName
  121. traceEdbInfoResp.EdbName, traceEdbInfoResp.RuleTitle = getEdbRuleTitle(edbInfo, parentEdbInfo, traceEdbInfoResp.Child, edbInfoMap, edbMappingMap)
  122. if traceEdbInfoResp.Child != nil && len(traceEdbInfoResp.Child) > 0 {
  123. for k, v := range traceEdbInfoResp.Child {
  124. traceEdbInfoResp.Child[k], err = handleTraceEdbInfo(v, traceEdbInfoResp.EdbInfoId, edbInfoMap, edbMappingMap)
  125. if err != nil {
  126. return
  127. }
  128. }
  129. }
  130. newTraceEdbInfoResp = traceEdbInfoResp
  131. return
  132. }
  133. // getEdbRule 获取规则名称
  134. func getEdbRuleTitle(edbInfo, parentEdbInfo *data_manage.EdbInfo, childList []data_manage.TraceEdbInfoResp, edbInfoMap map[int]*data_manage.EdbInfo, edbMappingMap map[int][]*data_manage.EdbInfoCalculateMappingInfo) (edbName, ruleTitle string) {
  135. edbName = edbInfo.EdbName
  136. ruleTitle = `来源于` + edbInfo.SourceName
  137. if parentEdbInfo != nil {
  138. edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
  139. if !ok {
  140. edbMappingList = []*data_manage.EdbInfoCalculateMappingInfo{}
  141. }
  142. // 指标名称
  143. switch parentEdbInfo.Source {
  144. case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
  145. for _, v := range edbMappingList {
  146. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  147. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
  148. }
  149. }
  150. case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ, utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 直接拼接 ,累计值同比拼接
  151. for _, v := range edbMappingList {
  152. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  153. tmpName := ``
  154. if v.FromTag == `A` {
  155. tmpName = `拼接日期前`
  156. } else if v.FromTag == `B` {
  157. tmpName = `拼接日期后`
  158. }
  159. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
  160. }
  161. }
  162. case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
  163. for _, v := range edbMappingList {
  164. //(需对上游指标+自变量,领先10天/因变量)
  165. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  166. tmpName := ``
  167. if v.FromTag == `A` {
  168. tmpName = fmt.Sprintf(`自变量,领先%d天`, v.MoveValue)
  169. } else if v.FromTag == `B` {
  170. tmpName = `因变量`
  171. }
  172. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
  173. }
  174. }
  175. case utils.DATA_SOURCE_CALCULATE_CORRELATION: // 滚动相关性
  176. for _, v := range edbMappingList {
  177. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  178. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
  179. }
  180. }
  181. }
  182. }
  183. if edbInfo.EdbType == 1 {
  184. // 基础指标的话,直接返回来源名称
  185. //ruleTitle = `来源于`+edbInfo.SourceName
  186. return
  187. }
  188. // 规则
  189. switch edbInfo.Source {
  190. case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
  191. ruleTitle = "=" + edbInfo.CalculateFormula
  192. case utils.DATA_SOURCE_CALCULATE_LJZZY, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY:
  193. ruleTitle = `累计转月值计算`
  194. case utils.DATA_SOURCE_CALCULATE_TBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ:
  195. ruleTitle = `同比值计算`
  196. case utils.DATA_SOURCE_CALCULATE_TCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ:
  197. ruleTitle = `同差值计算`
  198. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS:
  199. ruleTitle = fmt.Sprintf("N数值移动均值计算(N=%s)", edbInfo.CalculateFormula)
  200. case utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ:
  201. ruleTitle = fmt.Sprintf("N数值环比值计算(N=%s)", edbInfo.CalculateFormula)
  202. case utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ:
  203. ruleTitle = fmt.Sprintf("N数值环差值计算(N=%s)", edbInfo.CalculateFormula)
  204. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
  205. moveType := `领先`
  206. if edbInfo.MoveType == 2 {
  207. moveType = "滞后"
  208. }
  209. ruleTitle = fmt.Sprintf("时间移位计算(%s%s%s)", moveType, edbInfo.CalculateFormula, edbInfo.MoveFrequency)
  210. case utils.DATA_SOURCE_CALCULATE_BP, utils.DATA_SOURCE_PREDICT_CALCULATE_BP: // 变频
  211. childFrequency := ``
  212. if len(childList) > 0 {
  213. if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
  214. childFrequency = childEdbInfo.Frequency
  215. }
  216. }
  217. ruleTitle = fmt.Sprintf("升频计算(%s转%s)", childFrequency, edbInfo.Frequency)
  218. case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ: // 直接拼接
  219. ruleTitle = fmt.Sprintf("直接拼接计算(%s)", edbInfo.CalculateFormula)
  220. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 累计值同比拼
  221. ruleTitle = fmt.Sprintf("累计值同比值拼接计算(%s)", edbInfo.CalculateFormula)
  222. case utils.DATA_SOURCE_PYTHON:
  223. ruleTitle = `代码运算`
  224. case utils.DATA_SOURCE_CALCULATE_CJJX, utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
  225. ruleTitle = fmt.Sprintf("超季节性计算(N=%s,%s)", edbInfo.CalculateFormula, edbInfo.Calendar)
  226. case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
  227. var startDate, endDate string
  228. dateList := strings.Split(edbInfo.CalculateFormula, ",")
  229. if len(dateList) == 2 {
  230. startDate = dateList[0]
  231. endDate = dateList[1]
  232. }
  233. ruleTitle = fmt.Sprintf("拟合残差计算(%s至%s)", startDate, endDate)
  234. case utils.DATA_SOURCE_CALCULATE_ADJUST:
  235. ruleTitle = `数据调整`
  236. case utils.DATA_SOURCE_CALCULATE_NH, utils.DATA_SOURCE_PREDICT_CALCULATE_NH:
  237. ruleTitle = `年化计算`
  238. case utils.DATA_SOURCE_CALCULATE_KSZS, utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS: // 扩散指数->53
  239. type KszsConfig struct {
  240. DateType int `description:"扩散指标日期;1:全部指标日期并集;2:部分指标日期并集"`
  241. CheckList []string `description:"选中的数据,A,B,C"`
  242. }
  243. var config KszsConfig
  244. err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &config)
  245. if err != nil {
  246. return
  247. }
  248. var startDate, endDate time.Time
  249. childEdbInfoIdList := make([]int, 0)
  250. if config.DateType == 1 {
  251. for _, v := range childList {
  252. childEdbInfoIdList = append(childEdbInfoIdList, v.EdbInfoId)
  253. }
  254. } else {
  255. if parentEdbInfo != nil {
  256. edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
  257. if !ok {
  258. edbMappingList = []*data_manage.EdbInfoCalculateMappingInfo{}
  259. }
  260. tagMap := make(map[string]int)
  261. for _, v := range edbMappingList {
  262. tagMap[v.FromTag] = v.FromEdbInfoId
  263. }
  264. for _, v := range config.CheckList {
  265. if tmpEdbInfoId, ok := tagMap[v]; ok {
  266. childEdbInfoIdList = append(childEdbInfoIdList, tmpEdbInfoId)
  267. }
  268. }
  269. }
  270. }
  271. for _, v := range childEdbInfoIdList {
  272. if childEdbInfo, ok := edbInfoMap[v]; ok {
  273. tmpStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, childEdbInfo.StartDate, time.Local)
  274. if tmpErr != nil {
  275. return
  276. }
  277. if startDate.IsZero() || startDate.After(tmpStartDate) {
  278. startDate = tmpStartDate
  279. }
  280. tmpEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, childEdbInfo.EndDate, time.Local)
  281. if tmpErr != nil {
  282. return
  283. }
  284. if endDate.IsZero() || endDate.Before(tmpEndDate) {
  285. endDate = tmpEndDate
  286. }
  287. }
  288. }
  289. ruleTitle = fmt.Sprintf("扩散指数计算(%s至%s)", startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  290. case utils.DATA_SOURCE_STOCK_PLANT:
  291. ruleTitle = `来源于装置分析`
  292. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  293. type EdbCalculateFormula struct {
  294. BaseCalculateValue int `description:"基础计算窗口"`
  295. BaseCalculateUnit string `description:"基础计算频度"`
  296. LeadValue int `description:"领先期数"`
  297. LeadUnit string `description:"频度"`
  298. CalculateValue int `description:"计算窗口"`
  299. CalculateUnit string `description:"计算频度"`
  300. }
  301. var correlationConf EdbCalculateFormula
  302. err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &correlationConf)
  303. if err != nil {
  304. return
  305. }
  306. ruleTitle = fmt.Sprintf("滚动相关性(计算窗口%d%s,B领先A%d%s)", correlationConf.CalculateValue, correlationConf.CalculateUnit, correlationConf.LeadValue, correlationConf.LeadUnit)
  307. case utils.DATA_SOURCE_CALCULATE_JP, utils.DATA_SOURCE_PREDICT_CALCULATE_JP:
  308. childFrequency := ``
  309. if len(childList) > 0 {
  310. if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
  311. childFrequency = childEdbInfo.Frequency
  312. }
  313. }
  314. ruleTitle = fmt.Sprintf("降频计算(%s转%s,%s)", childFrequency, edbInfo.Frequency, edbInfo.CalculateFormula)
  315. case utils.DATA_SOURCE_CALCULATE_STANDARD_DEVIATION:
  316. ruleTitle = fmt.Sprintf("标准差(滚动%s期)", edbInfo.CalculateFormula)
  317. case utils.DATA_SOURCE_CALCULATE_PERCENTILE, utils.DATA_SOURCE_PREDICT_CALCULATE_PERCENTILE:
  318. type TempCalculate struct {
  319. CalculateValue int `description:"计算窗口"`
  320. CalculateUnit string `description:"计算频度"`
  321. }
  322. cf := TempCalculate{}
  323. if e := json.Unmarshal([]byte(edbInfo.CalculateFormula), &cf); e != nil {
  324. return
  325. }
  326. ruleTitle = fmt.Sprintf("百分位(时间长度%d%s)", cf.CalculateValue, cf.CalculateUnit)
  327. case utils.DATA_SOURCE_CALCULATE_ZSXY, utils.DATA_SOURCE_PREDICT_CALCULATE_ZSXY:
  328. ruleTitle = `指数修匀计算`
  329. }
  330. return
  331. }