edb_relation.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_task/models/data_manage"
  5. "eta/eta_task/models/data_manage/cross_variety"
  6. "eta/eta_task/models/fe_calendar"
  7. "eta/eta_task/models/sandbox"
  8. "eta/eta_task/services/alarm_msg"
  9. "eta/eta_task/utils"
  10. "fmt"
  11. "time"
  12. )
  13. func InitEdbRelation() {
  14. //InitChartEdbRelation()
  15. //InitChartCrossVariety()
  16. //initCalendarIndicatorRelation()
  17. initSandBoxEdbRelation()
  18. }
  19. func InitChartEdbRelation() {
  20. fmt.Println("开始处理图表中的指标引用")
  21. var err error
  22. var addNum int
  23. defer func() {
  24. if err != nil {
  25. msg := fmt.Sprintf("初始化指标在图表中的引用失败 InitChartEdbRelation err: %v", err)
  26. utils.FileLog.Info(msg)
  27. fmt.Println(msg)
  28. go alarm_msg.SendAlarmMsg(msg, 3)
  29. }
  30. }()
  31. //查询chart_edb_mapping 表
  32. total, err := data_manage.GetChartEdbMappingTotal()
  33. if err != nil {
  34. err = fmt.Errorf("查询图表关联指标失败 err: %v", err)
  35. return
  36. }
  37. if total == 0 {
  38. return
  39. }
  40. //分页查询,每次处理500条记录
  41. pageSize := 500
  42. totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
  43. addList := make([]*data_manage.EdbInfoRelation, 0)
  44. //查询图表列表
  45. for i := 0; i < totalPage; i += 1 {
  46. startSize := i * pageSize
  47. list, e := data_manage.GetChartEdbMappingList(startSize, pageSize)
  48. if e != nil {
  49. err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
  50. return
  51. }
  52. if len(list) == 0 {
  53. break
  54. }
  55. edbInfoIds := make([]int, 0)
  56. for _, v := range list {
  57. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  58. }
  59. // 查询指标信息表
  60. edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  61. if e != nil {
  62. err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
  63. return
  64. }
  65. if len(edbInfoList) == 0 {
  66. continue
  67. }
  68. // 查询计算指标信息,并且建立关联关系
  69. // 查询间接引用的指标信息
  70. calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
  71. if e != nil {
  72. err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
  73. return
  74. }
  75. // 查询指标间接引用
  76. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  77. for _, v := range edbInfoList {
  78. edbInfoMap[v.EdbInfoId] = v
  79. }
  80. // 筛选有用的图表
  81. finalList := make([]*data_manage.ChartEdbMapping, 0)
  82. chartIds := make([]int, 0)
  83. for _, v := range list {
  84. if _, ok2 := edbInfoMap[v.EdbInfoId]; !ok2 {
  85. continue
  86. }
  87. finalList = append(finalList, v)
  88. chartIds = append(chartIds, v.ChartInfoId)
  89. }
  90. if len(chartIds) == 0 {
  91. continue
  92. }
  93. // 查询图表信息
  94. chartInfoList, e := data_manage.GetChartInfoByChartInfoIds(chartIds)
  95. if e != nil {
  96. err = fmt.Errorf("查询图表信息列表失败 Err:%s", e)
  97. return
  98. }
  99. chartInfoMap := make(map[int]*data_manage.ChartInfo)
  100. for _, v := range chartInfoList {
  101. chartInfoMap[v.ChartInfoId] = v
  102. }
  103. //查询引用关系列表,
  104. chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(chartIds, utils.EDB_RELATION_CHART)
  105. if e != nil {
  106. err = fmt.Errorf("查询图表引用关系列表失败 Err:%s", e)
  107. return
  108. }
  109. existRelationMap := make(map[string]struct{})
  110. for _, v := range chartEdbRelationList {
  111. name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
  112. existRelationMap[name] = struct{}{}
  113. }
  114. for _, v := range finalList {
  115. nowTime := time.Now()
  116. name := fmt.Sprintf("%d-%d", v.ChartInfoId, v.EdbInfoId)
  117. if _, ok := existRelationMap[name]; !ok {
  118. //查询图表信息
  119. chartInfo, ok1 := chartInfoMap[v.ChartInfoId]
  120. if !ok1 {
  121. continue
  122. }
  123. edbInfo, ok2 := edbInfoMap[v.EdbInfoId]
  124. if !ok2 {
  125. continue
  126. }
  127. //todo 引用时间
  128. tmp := &data_manage.EdbInfoRelation{
  129. ReferObjectId: v.ChartInfoId,
  130. ReferObjectType: utils.EDB_RELATION_CHART,
  131. ReferObjectSubType: chartInfo.Source,
  132. EdbInfoId: v.EdbInfoId,
  133. EdbName: edbInfo.EdbName,
  134. Source: edbInfo.Source,
  135. EdbCode: edbInfo.EdbCode,
  136. CreateTime: nowTime,
  137. ModifyTime: nowTime,
  138. RelationTime: v.CreateTime,
  139. }
  140. addList = append(addList, tmp)
  141. existRelationMap[name] = struct{}{}
  142. // 添加间接引用记录
  143. if edbInfo.EdbType == 2 {
  144. childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  145. if !ok1 {
  146. continue
  147. }
  148. for _, childEdbMappingId := range childEdbMappingIds {
  149. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  150. if !ok2 {
  151. continue
  152. }
  153. childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
  154. if !ok2 {
  155. continue
  156. }
  157. tmp1 := &data_manage.EdbInfoRelation{
  158. ReferObjectId: v.ChartInfoId,
  159. ReferObjectType: utils.EDB_RELATION_CHART,
  160. ReferObjectSubType: chartInfo.Source,
  161. EdbInfoId: childEdb.EdbInfoId,
  162. EdbName: childEdb.EdbName,
  163. Source: childEdb.Source,
  164. EdbCode: childEdb.EdbCode,
  165. CreateTime: nowTime,
  166. ModifyTime: nowTime,
  167. RelationTime: v.CreateTime,
  168. RelationType: 1,
  169. RootEdbInfoId: edbInfo.EdbInfoId,
  170. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  171. }
  172. addList = append(addList, tmp1)
  173. // todo 防止重复
  174. }
  175. }
  176. if len(addList) > pageSize {
  177. err = data_manage.AddEdbInfoRelationMulti(addList)
  178. if err != nil {
  179. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  180. return
  181. }
  182. addNum += len(addList)
  183. addList = make([]*data_manage.EdbInfoRelation, 0)
  184. }
  185. }
  186. }
  187. }
  188. //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
  189. if len(addList) > 0 {
  190. err = data_manage.AddEdbInfoRelationMulti(addList)
  191. if err != nil {
  192. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  193. return
  194. }
  195. addNum += len(addList)
  196. }
  197. fmt.Printf("图表指标引用记录处理完成, 新增%d条记录\n", addNum)
  198. return
  199. }
  200. // InitChartCrossVariety 处理特殊图表,跨品种分析图表
  201. func InitChartCrossVariety() {
  202. fmt.Println("开始跨品种分析图表中的指标引用")
  203. var addNum int
  204. var err error
  205. defer func() {
  206. if err != nil {
  207. msg := fmt.Sprintf("初始化指标在跨品种分析图表中的引用失败 InitChartCrossVariety err: %v", err)
  208. utils.FileLog.Info(msg)
  209. fmt.Println(msg)
  210. go alarm_msg.SendAlarmMsg(msg, 3)
  211. }
  212. }()
  213. total, err := cross_variety.GetChartInfoCrossVarietyTotal()
  214. if err != nil {
  215. err = fmt.Errorf("查询图表关联指标失败 err: %v", err)
  216. return
  217. }
  218. if total == 0 {
  219. return
  220. }
  221. //分页查询,每次处理500条记录
  222. pageSize := 500
  223. totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
  224. addList := make([]*data_manage.EdbInfoRelation, 0)
  225. //查询图表列表
  226. for i := 0; i < totalPage; i += 1 {
  227. startSize := i * pageSize
  228. list, e := cross_variety.GetChartInfoCrossVarietyList(startSize, pageSize)
  229. if e != nil {
  230. err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
  231. return
  232. }
  233. if len(list) == 0 {
  234. break
  235. }
  236. chartIds := make([]int, 0)
  237. tagIds := make([]int, 0)
  238. tagIdsMap := make(map[int]struct{})
  239. tagChartMap := make(map[int][]*cross_variety.ChartInfoCrossVariety)
  240. for _, v := range list {
  241. if _, ok := tagIdsMap[v.ChartXTagId]; !ok {
  242. tagIds = append(tagIds, v.ChartXTagId)
  243. tagIdsMap[v.ChartXTagId] = struct{}{}
  244. }
  245. if _, ok := tagIdsMap[v.ChartYTagId]; !ok {
  246. tagIds = append(tagIds, v.ChartYTagId)
  247. tagIdsMap[v.ChartYTagId] = struct{}{}
  248. }
  249. if chartCross, ok := tagChartMap[v.ChartXTagId]; ok {
  250. chartCross = append(chartCross, v)
  251. tagChartMap[v.ChartXTagId] = chartCross
  252. } else {
  253. chartCross = make([]*cross_variety.ChartInfoCrossVariety, 0)
  254. chartCross = append(chartCross, v)
  255. tagChartMap[v.ChartXTagId] = chartCross
  256. }
  257. if chartCross, ok := tagChartMap[v.ChartYTagId]; ok {
  258. chartCross = append(chartCross, v)
  259. tagChartMap[v.ChartYTagId] = chartCross
  260. } else {
  261. chartCross = make([]*cross_variety.ChartInfoCrossVariety, 0)
  262. chartCross = append(chartCross, v)
  263. tagChartMap[v.ChartYTagId] = chartCross
  264. }
  265. }
  266. chartTagVarietyList, e := cross_variety.GetChartTagVarietyEdbInfoIdsByTagIds(tagIds)
  267. if e != nil {
  268. err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
  269. return
  270. }
  271. edbInfoIds := make([]int, 0)
  272. chartTagVarietyMap := make(map[int][]*cross_variety.ChartTagVariety)
  273. for _, v := range chartTagVarietyList {
  274. if tagList, ok := chartTagVarietyMap[v.EdbInfoId]; ok {
  275. tagList = append(tagList, v)
  276. chartTagVarietyMap[v.EdbInfoId] = tagList
  277. } else {
  278. tagList = make([]*cross_variety.ChartTagVariety, 0)
  279. tagList = append(tagList, v)
  280. chartTagVarietyMap[v.EdbInfoId] = tagList
  281. }
  282. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  283. }
  284. // 查询指标信息表
  285. edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  286. if e != nil {
  287. err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
  288. return
  289. }
  290. if len(edbInfoList) == 0 {
  291. continue
  292. }
  293. // 查询计算指标信息,并且建立关联关系
  294. // 查询间接引用的指标信息
  295. calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
  296. if e != nil {
  297. err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
  298. return
  299. }
  300. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  301. chartInfoCrossMap := make(map[int]struct{})
  302. chartInfoCrossList := make([]*cross_variety.ChartInfoCrossVariety, 0)
  303. edbCrossMap := make(map[int][]*cross_variety.ChartInfoCrossVariety)
  304. for _, v := range edbInfoList {
  305. edbInfoMap[v.EdbInfoId] = v
  306. if tagList, ok := chartTagVarietyMap[v.EdbInfoId]; ok {
  307. for _, tag := range tagList {
  308. if chartCross, ok2 := tagChartMap[tag.ChartTagId]; ok2 {
  309. for _, crossItem := range chartCross {
  310. if _, ok3 := chartInfoCrossMap[crossItem.ChartInfoId]; !ok3 {
  311. chartInfoCrossMap[crossItem.ChartInfoId] = struct{}{}
  312. chartInfoCrossList = append(chartInfoCrossList, crossItem)
  313. chartIds = append(chartIds, crossItem.ChartInfoId)
  314. }
  315. }
  316. }
  317. }
  318. }
  319. edbCrossMap[v.EdbInfoId] = chartInfoCrossList
  320. chartInfoCrossMap = make(map[int]struct{})
  321. chartInfoCrossList = make([]*cross_variety.ChartInfoCrossVariety, 0)
  322. }
  323. /*// 查询图表信息
  324. chartInfoList, e := data_manage.GetChartInfoByChartInfoIds(chartIds)
  325. if e != nil {
  326. err = fmt.Errorf("查询图表信息列表失败 Err:%s", e)
  327. return
  328. }
  329. chartInfoMap := make(map[int]*data_manage.ChartInfo)
  330. for _, v := range chartInfoList {
  331. chartInfoMap[v.ChartInfoId] = v
  332. }
  333. */
  334. //查询引用关系列表,
  335. chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(chartIds, utils.EDB_RELATION_CHART)
  336. if e != nil {
  337. err = fmt.Errorf("查询图表引用关系列表失败 Err:%s", e)
  338. return
  339. }
  340. existRelationMap := make(map[string]struct{})
  341. for _, v := range chartEdbRelationList {
  342. name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
  343. existRelationMap[name] = struct{}{}
  344. }
  345. for edbInfoId, chartCrossList := range edbCrossMap {
  346. nowTime := time.Now()
  347. for _, item := range chartCrossList {
  348. name := fmt.Sprintf("%d-%d", item.ChartInfoId, edbInfoId)
  349. if _, ok1 := existRelationMap[name]; !ok1 {
  350. edbInfo, ok2 := edbInfoMap[edbInfoId]
  351. if !ok2 {
  352. continue
  353. }
  354. //todo 引用时间
  355. tmp := &data_manage.EdbInfoRelation{
  356. ReferObjectId: item.ChartInfoId,
  357. ReferObjectType: utils.EDB_RELATION_CHART,
  358. ReferObjectSubType: utils.CHART_SOURCE_CROSS_HEDGING,
  359. EdbInfoId: edbInfoId,
  360. EdbName: edbInfo.EdbName,
  361. Source: edbInfo.Source,
  362. EdbCode: edbInfo.EdbCode,
  363. CreateTime: nowTime,
  364. ModifyTime: nowTime,
  365. RelationTime: item.CreateTime,
  366. }
  367. addList = append(addList, tmp)
  368. existRelationMap[name] = struct{}{}
  369. // 添加间接引用记录
  370. if edbInfo.EdbType == 2 {
  371. childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  372. if !ok1 {
  373. continue
  374. }
  375. for _, childEdbMappingId := range childEdbMappingIds {
  376. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  377. if !ok2 {
  378. continue
  379. }
  380. childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
  381. if !ok2 {
  382. continue
  383. }
  384. tmp1 := &data_manage.EdbInfoRelation{
  385. ReferObjectId: item.ChartInfoId,
  386. ReferObjectType: utils.EDB_RELATION_CHART,
  387. ReferObjectSubType: utils.CHART_SOURCE_CROSS_HEDGING,
  388. EdbInfoId: childEdb.EdbInfoId,
  389. EdbName: childEdb.EdbName,
  390. Source: childEdb.Source,
  391. EdbCode: childEdb.EdbCode,
  392. CreateTime: nowTime,
  393. ModifyTime: nowTime,
  394. RelationTime: item.CreateTime,
  395. RelationType: 1,
  396. RootEdbInfoId: edbInfo.EdbInfoId,
  397. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  398. }
  399. addList = append(addList, tmp1)
  400. // todo 防止重复
  401. }
  402. }
  403. if len(addList) > pageSize {
  404. err = data_manage.AddEdbInfoRelationMulti(addList)
  405. if err != nil {
  406. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  407. return
  408. }
  409. addNum += len(addList)
  410. addList = make([]*data_manage.EdbInfoRelation, 0)
  411. }
  412. }
  413. }
  414. }
  415. }
  416. //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
  417. if len(addList) > 0 {
  418. err = data_manage.AddEdbInfoRelationMulti(addList)
  419. if err != nil {
  420. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  421. return
  422. }
  423. addNum += len(addList)
  424. }
  425. fmt.Printf("跨品种分析图表指标引用记录处理完成, 新增%d条记录\n", addNum)
  426. return
  427. }
  428. // 初始化事件日历中的指标引用
  429. func initCalendarIndicatorRelation() {
  430. fmt.Println("开始处理事件日历中的指标引用")
  431. var addNum int
  432. var err error
  433. defer func() {
  434. if err != nil {
  435. msg := fmt.Sprintf("初始化指标在事件日历中的引用失败 initCalendarIndicatorRelation err: %v", err)
  436. utils.FileLog.Info(msg)
  437. fmt.Println(msg)
  438. go alarm_msg.SendAlarmMsg(msg, 3)
  439. }
  440. }()
  441. //查询chart_edb_mapping 表
  442. obj := new(fe_calendar.FeCalendarMatter)
  443. condition := " AND edb_info_id > 0"
  444. total, err := obj.GetCountByCondition(condition, []interface{}{})
  445. if err != nil {
  446. err = fmt.Errorf("查询事件日历关联指标失败 err: %v", err)
  447. return
  448. }
  449. if total == 0 {
  450. return
  451. }
  452. //分页查询,每次处理500条记录
  453. pageSize := 500
  454. totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
  455. addList := make([]*data_manage.EdbInfoRelation, 0)
  456. //查询图表列表
  457. for i := 0; i < totalPage; i += 1 {
  458. startSize := i * pageSize
  459. list, e := obj.GetPageItemsByCondition(condition, []interface{}{}, []string{}, "", startSize, pageSize)
  460. if e != nil {
  461. err = fmt.Errorf("查询事件日历关联指标列表失败 Err:%s", e)
  462. return
  463. }
  464. if len(list) == 0 {
  465. break
  466. }
  467. edbInfoIds := make([]int, 0)
  468. edbInfoMatterMap := make(map[int][]*fe_calendar.FeCalendarMatter)
  469. for _, v := range list {
  470. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  471. items, ok := edbInfoMatterMap[v.EdbInfoId]
  472. if ok {
  473. items = append(items, v)
  474. edbInfoMatterMap[v.EdbInfoId] = items
  475. } else {
  476. items = make([]*fe_calendar.FeCalendarMatter, 0)
  477. items = append(items, v)
  478. edbInfoMatterMap[v.EdbInfoId] = items
  479. }
  480. }
  481. // 查询指标信息表
  482. edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  483. if e != nil {
  484. err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
  485. return
  486. }
  487. if len(edbInfoList) == 0 {
  488. continue
  489. }
  490. // 查询计算指标信息,并且建立关联关系
  491. // 查询间接引用的指标信息
  492. calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
  493. if e != nil {
  494. err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
  495. return
  496. }
  497. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  498. matterIds := make([]int, 0)
  499. for _, v := range edbInfoList {
  500. edbInfoMap[v.EdbInfoId] = v
  501. items, ok := edbInfoMatterMap[v.EdbInfoId]
  502. if ok {
  503. for _, item := range items {
  504. matterIds = append(matterIds, item.FeCalendarMatterId)
  505. }
  506. }
  507. }
  508. //查询引用关系列表,
  509. chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(matterIds, utils.EDB_RELATION_CALENDAR)
  510. if e != nil {
  511. err = fmt.Errorf("查询图表引用关系列表失败 Err:%s", e)
  512. return
  513. }
  514. existRelationMap := make(map[string]struct{})
  515. for _, v := range chartEdbRelationList {
  516. name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
  517. existRelationMap[name] = struct{}{}
  518. }
  519. for edbInfoId, edbInfo := range edbInfoMap {
  520. nowTime := time.Now()
  521. items, ok := edbInfoMatterMap[edbInfoId]
  522. if ok {
  523. for _, v := range items {
  524. name := fmt.Sprintf("%d-%d", v.FeCalendarMatterId, v.EdbInfoId)
  525. if _, ok1 := existRelationMap[name]; !ok1 {
  526. //todo 引用时间
  527. tmp := &data_manage.EdbInfoRelation{
  528. ReferObjectId: v.FeCalendarMatterId,
  529. ReferObjectType: utils.EDB_RELATION_CALENDAR,
  530. EdbInfoId: v.EdbInfoId,
  531. EdbName: edbInfo.EdbName,
  532. Source: edbInfo.Source,
  533. EdbCode: edbInfo.EdbCode,
  534. CreateTime: nowTime,
  535. ModifyTime: nowTime,
  536. RelationTime: v.CreateTime,
  537. }
  538. addList = append(addList, tmp)
  539. existRelationMap[name] = struct{}{}
  540. // 添加间接引用记录
  541. if edbInfo.EdbType == 2 {
  542. childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  543. if !ok1 {
  544. continue
  545. }
  546. for _, childEdbMappingId := range childEdbMappingIds {
  547. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  548. if !ok2 {
  549. continue
  550. }
  551. childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
  552. if !ok2 {
  553. continue
  554. }
  555. tmp1 := &data_manage.EdbInfoRelation{
  556. ReferObjectId: v.FeCalendarMatterId,
  557. ReferObjectType: utils.EDB_RELATION_CALENDAR,
  558. EdbInfoId: childEdb.EdbInfoId,
  559. EdbName: childEdb.EdbName,
  560. Source: childEdb.Source,
  561. EdbCode: childEdb.EdbCode,
  562. CreateTime: nowTime,
  563. ModifyTime: nowTime,
  564. RelationTime: v.CreateTime,
  565. RelationType: 1,
  566. RootEdbInfoId: edbInfo.EdbInfoId,
  567. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  568. }
  569. addList = append(addList, tmp1)
  570. // todo 防止重复
  571. }
  572. }
  573. if len(addList) > pageSize {
  574. err = data_manage.AddEdbInfoRelationMulti(addList)
  575. if err != nil {
  576. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  577. return
  578. }
  579. addNum += len(addList)
  580. addList = make([]*data_manage.EdbInfoRelation, 0)
  581. }
  582. }
  583. }
  584. }
  585. }
  586. }
  587. //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
  588. if len(addList) > 0 {
  589. err = data_manage.AddEdbInfoRelationMulti(addList)
  590. if err != nil {
  591. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  592. return
  593. }
  594. addNum += len(addList)
  595. }
  596. fmt.Printf("事件日历指标引用记录处理完成, 新增%d条记录\n", addNum)
  597. return
  598. }
  599. // 处理逻辑图中的指标引用
  600. func initSandBoxEdbRelation() {
  601. fmt.Println("开始处理逻辑图中的指标引用")
  602. var err error
  603. var addNum int
  604. defer func() {
  605. if err != nil {
  606. msg := fmt.Sprintf("初始化指标在逻辑图中的引用失败 initSandBoxEdbRelation err: %v", err)
  607. utils.FileLog.Info(msg)
  608. fmt.Println(msg)
  609. go alarm_msg.SendAlarmMsg(msg, 3)
  610. }
  611. }()
  612. condition := " AND is_delete = 0"
  613. total, err := sandbox.GetSandboxListCountByCondition(condition, []interface{}{})
  614. if err != nil {
  615. err = fmt.Errorf("查询逻辑图总数失败 err: %v", err)
  616. return
  617. }
  618. if total == 0 {
  619. return
  620. }
  621. //分页查询,每次处理500条记录
  622. pageSize := 100
  623. totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
  624. addList := make([]*data_manage.EdbInfoRelation, 0)
  625. //查询图表列表
  626. for i := 0; i < totalPage; i += 1 {
  627. startSize := i * pageSize
  628. list, e := sandbox.GetSandboxListByCondition(condition, []interface{}{}, startSize, pageSize)
  629. if e != nil {
  630. err = fmt.Errorf("查询逻辑图列表失败 Err:%s", e)
  631. return
  632. }
  633. if len(list) == 0 {
  634. break
  635. }
  636. edbInfoIds := make([]int, 0)
  637. edbSandboxMap := make(map[int][]*sandbox.Sandbox)
  638. for _, v := range list {
  639. if v.Content == "" {
  640. continue
  641. }
  642. edbInfoIdsTmp, e := getSandBoxEdbIdsByContent(v.Content)
  643. if e != nil {
  644. continue
  645. //err = fmt.Errorf("查询逻辑图关联的指标Id失败 Err:%s", e)
  646. //return
  647. }
  648. for _, edbId := range edbInfoIdsTmp {
  649. edbInfoIds = append(edbInfoIds, edbId)
  650. edbSandboxMap[edbId] = append(edbSandboxMap[edbId], v)
  651. }
  652. }
  653. if len(edbInfoIds) <= 0 {
  654. continue
  655. }
  656. // 查询指标信息表
  657. edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  658. if e != nil {
  659. err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
  660. return
  661. }
  662. if len(edbInfoList) == 0 {
  663. continue
  664. }
  665. // 查询计算指标信息,并且建立关联关系
  666. // 查询间接引用的指标信息
  667. calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
  668. if e != nil {
  669. err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
  670. return
  671. }
  672. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  673. sandboxIds := make([]int, 0)
  674. for _, v := range edbInfoList {
  675. edbInfoMap[v.EdbInfoId] = v
  676. if items, ok := edbSandboxMap[v.EdbInfoId]; ok {
  677. for _, item := range items {
  678. sandboxIds = append(sandboxIds, item.SandboxId)
  679. }
  680. }
  681. }
  682. //查询引用关系列表,
  683. chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(sandboxIds, utils.EDB_RELATION_SANDBOX)
  684. if e != nil {
  685. err = fmt.Errorf("查询逻辑图引用关系列表失败 Err:%s", e)
  686. return
  687. }
  688. existRelationMap := make(map[string]struct{})
  689. for _, v := range chartEdbRelationList {
  690. name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
  691. existRelationMap[name] = struct{}{}
  692. }
  693. for edbInfoId, sandboxList := range edbSandboxMap {
  694. nowTime := time.Now()
  695. for _, v := range sandboxList {
  696. name := fmt.Sprintf("%d-%d", v.SandboxId, edbInfoId)
  697. if _, ok := existRelationMap[name]; !ok {
  698. edbInfo, ok2 := edbInfoMap[edbInfoId]
  699. if !ok2 {
  700. continue
  701. }
  702. //todo 引用时间
  703. tmp := &data_manage.EdbInfoRelation{
  704. ReferObjectId: v.SandboxId,
  705. ReferObjectType: utils.EDB_RELATION_SANDBOX,
  706. EdbInfoId: edbInfoId,
  707. EdbName: edbInfo.EdbName,
  708. Source: edbInfo.Source,
  709. EdbCode: edbInfo.EdbCode,
  710. CreateTime: nowTime,
  711. ModifyTime: nowTime,
  712. RelationTime: v.CreateTime,
  713. }
  714. addList = append(addList, tmp)
  715. existRelationMap[name] = struct{}{}
  716. // 添加间接引用记录
  717. if edbInfo.EdbType == 2 {
  718. childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  719. if !ok1 {
  720. continue
  721. }
  722. for _, childEdbMappingId := range childEdbMappingIds {
  723. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  724. if !ok2 {
  725. continue
  726. }
  727. childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
  728. if !ok2 {
  729. continue
  730. }
  731. tmp1 := &data_manage.EdbInfoRelation{
  732. ReferObjectId: v.SandboxId,
  733. ReferObjectType: utils.EDB_RELATION_SANDBOX,
  734. EdbInfoId: childEdb.EdbInfoId,
  735. EdbName: childEdb.EdbName,
  736. Source: childEdb.Source,
  737. EdbCode: childEdb.EdbCode,
  738. CreateTime: nowTime,
  739. ModifyTime: nowTime,
  740. RelationTime: v.CreateTime,
  741. RelationType: 1,
  742. RootEdbInfoId: edbInfo.EdbInfoId,
  743. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  744. }
  745. addList = append(addList, tmp1)
  746. // todo 防止重复
  747. }
  748. }
  749. if len(addList) > pageSize {
  750. err = data_manage.AddEdbInfoRelationMulti(addList)
  751. if err != nil {
  752. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  753. return
  754. }
  755. addNum += len(addList)
  756. addList = make([]*data_manage.EdbInfoRelation, 0)
  757. }
  758. }
  759. }
  760. }
  761. }
  762. //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
  763. if len(addList) > 0 {
  764. err = data_manage.AddEdbInfoRelationMulti(addList)
  765. if err != nil {
  766. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  767. return
  768. }
  769. addNum += len(addList)
  770. }
  771. fmt.Printf("逻辑图指标引用记录处理完成, 新增%d条记录\n", addNum)
  772. return
  773. }
  774. func getSandBoxEdbIdsByContent(content string) (edbInfoIds []int, err error) {
  775. var contentInfo sandbox.ContentDataStruct
  776. err = json.Unmarshal([]byte(content), &contentInfo)
  777. if err != nil {
  778. err = fmt.Errorf("json.Unmarshal err:%s", err.Error())
  779. return
  780. }
  781. // 遍历所有节点
  782. for _, node := range contentInfo.Cells {
  783. if node.Data == nil {
  784. continue
  785. }
  786. for _, v := range node.Data.LinkData {
  787. if v.Type == 1 {
  788. edbInfoIds = append(edbInfoIds, v.Id)
  789. }
  790. }
  791. }
  792. return
  793. }
  794. func GetEdbListByEdbInfoId(edbInfoList []*data_manage.EdbInfo) (edbInfoMap map[int]*data_manage.EdbInfo, edbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping, edbInfoMappingRootIdsMap map[int][]int, err error) {
  795. //查询指标信息
  796. //查询指标映射
  797. //查询所有指标数据
  798. //查询这个指标相关的mapping信息放到数组里,
  799. //将得到的指标ID信息放到数组里
  800. hasFindMap := make(map[int]struct{})
  801. edbInfoIdMap := make(map[int]struct{})
  802. edbMappingList := make([]*data_manage.EdbInfoCalculateMapping, 0)
  803. edbInfoMappingRootIdsMap = make(map[int][]int, 0)
  804. edbMappingMap := make(map[int]struct{})
  805. for _, edbInfo := range edbInfoList {
  806. if edbInfo.EdbType == 2 {
  807. edbInfoId := edbInfo.EdbInfoId
  808. edbMappingList, err = getCalculateEdbInfoByEdbInfoId(edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId)
  809. if err != nil {
  810. err = fmt.Errorf(" GetCalculateEdbInfoByEdbInfoId err: %s", err.Error())
  811. return
  812. }
  813. }
  814. }
  815. if len(edbMappingList) == 0 {
  816. return
  817. }
  818. // 查询指标信息
  819. // 指标信息map
  820. edbInfoIdList := make([]int, 0)
  821. for k, _ := range edbInfoIdMap {
  822. edbInfoIdList = append(edbInfoIdList, k)
  823. }
  824. edbInfoMap = make(map[int]*data_manage.EdbInfo)
  825. edbMappingListMap = make(map[int]*data_manage.EdbInfoCalculateMapping)
  826. if len(edbInfoIdList) > 0 {
  827. edbInfoList, err = data_manage.GetEdbInfoByIdList(edbInfoIdList)
  828. if err != nil {
  829. err = fmt.Errorf(" GetEdbInfoByIdList err: %s", err.Error())
  830. return
  831. }
  832. for _, v := range edbInfoList {
  833. edbInfoMap[v.EdbInfoId] = v
  834. }
  835. }
  836. if len(edbMappingList) > 0 {
  837. for _, v := range edbMappingList {
  838. edbMappingListMap[v.EdbInfoCalculateMappingId] = v
  839. }
  840. }
  841. return
  842. }
  843. // getCalculateEdbInfoByEdbInfoId 计算指标追溯
  844. func getCalculateEdbInfoByEdbInfoId(edbInfoId int, hasFindMap map[int]struct{}, edbInfoIdMap map[int]struct{}, edbMappingList []*data_manage.EdbInfoCalculateMapping, edbMappingMap map[int]struct{}, edbInfoMappingRootIdsMap map[int][]int, rootEdbInfoId int) (newEdbMappingList []*data_manage.EdbInfoCalculateMapping, err error) {
  845. newEdbMappingList = edbMappingList
  846. _, ok := hasFindMap[edbInfoId]
  847. if ok {
  848. return
  849. }
  850. if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
  851. edbInfoIdMap[edbInfoId] = struct{}{}
  852. }
  853. edbInfoMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  854. if e != nil {
  855. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", e.Error())
  856. return
  857. }
  858. if len(edbInfoMappingList) > 0 {
  859. fromEdbInfoIdList := make([]int, 0)
  860. edbInfoMappingIdList := make([]int, 0)
  861. for _, v := range edbInfoMappingList {
  862. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  863. edbInfoMappingIdList = append(edbInfoMappingIdList, v.EdbInfoCalculateMappingId)
  864. if _, ok1 := edbInfoIdMap[v.FromEdbInfoId]; !ok1 {
  865. edbInfoIdMap[v.FromEdbInfoId] = struct{}{}
  866. }
  867. if _, ok2 := edbMappingMap[v.EdbInfoCalculateMappingId]; !ok2 {
  868. edbMappingMap[v.EdbInfoCalculateMappingId] = struct{}{}
  869. tmp := &data_manage.EdbInfoCalculateMapping{
  870. EdbInfoCalculateMappingId: v.EdbInfoCalculateMappingId,
  871. EdbInfoId: v.EdbInfoId,
  872. Source: v.Source,
  873. SourceName: v.SourceName,
  874. EdbCode: v.EdbCode,
  875. FromEdbInfoId: v.FromEdbInfoId,
  876. FromEdbCode: v.FromEdbCode,
  877. FromEdbName: v.FromEdbName,
  878. FromSource: v.FromSource,
  879. FromSourceName: v.FromSourceName,
  880. FromTag: v.FromTag,
  881. Sort: v.Sort,
  882. CreateTime: v.CreateTime,
  883. ModifyTime: v.ModifyTime,
  884. }
  885. newEdbMappingList = append(newEdbMappingList, tmp)
  886. }
  887. if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
  888. // 查过了就不查了
  889. if _, ok2 := hasFindMap[v.FromEdbInfoId]; !ok2 {
  890. newEdbMappingList, e = getCalculateEdbInfoByEdbInfoId(v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, rootEdbInfoId)
  891. if e != nil {
  892. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", e.Error())
  893. return
  894. }
  895. }
  896. }
  897. hasFindMap[v.FromEdbInfoId] = struct{}{}
  898. }
  899. edbInfoMappingRootIdsMap[rootEdbInfoId] = append(edbInfoMappingRootIdsMap[rootEdbInfoId], edbInfoMappingIdList...)
  900. }
  901. hasFindMap[edbInfoId] = struct{}{}
  902. return
  903. }