chart_info.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta/eta_forum_task/models"
  6. "eta/eta_forum_task/services/alarm_msg"
  7. "eta/eta_forum_task/services/eta_forum"
  8. "eta/eta_forum_task/utils"
  9. "fmt"
  10. "time"
  11. )
  12. func EtaForumChartUpdate(cont context.Context) (err error) {
  13. var tmpErr []error
  14. deleteCache := true
  15. cacheKey := "eta_forum_task:EtaForumChartUpdate"
  16. defer func() {
  17. if deleteCache {
  18. _ = utils.Rc.Delete(cacheKey)
  19. }
  20. stack := ""
  21. if err != nil {
  22. stack = fmt.Sprintln(stack + err.Error())
  23. }
  24. if len(tmpErr) > 0 {
  25. for _, v := range tmpErr {
  26. stack = fmt.Sprintln(stack + v.Error())
  27. }
  28. }
  29. if stack != "" {
  30. go alarm_msg.SendAlarmMsg(utils.APP_NAME_CN+"更新社区图表信息失败"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+stack, 3)
  31. }
  32. }()
  33. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  34. deleteCache = false
  35. err = fmt.Errorf("系统处理中,请稍后重试!")
  36. return
  37. }
  38. // 设置缓存判断处理中则不更新
  39. condition := " and forum_chart_info_id > 0 and source=1 "
  40. // 查询需要更新的图表信息总数
  41. total, err := models.GetChartInfoCountByCondition(condition, []interface{}{})
  42. if err != nil {
  43. return
  44. }
  45. if total == 0 {
  46. return
  47. }
  48. // 更新图表数据
  49. offset := 0
  50. pageSize := 100
  51. success := 0
  52. // 循环更新100个图表数据
  53. for i := 0; offset < total; i++ {
  54. // 查询需要更新的图表信息
  55. chartInfos, e := models.GetChartInfoListByCondition(condition, []interface{}{}, offset, pageSize)
  56. if e != nil {
  57. err = fmt.Errorf("查询需要更新的图表信息失败: %v", e)
  58. return
  59. }
  60. // 循环更新图表数据
  61. for _, chartInfo := range chartInfos {
  62. // 更新图表数据
  63. er, msg := UpdateChart(chartInfo.ChartInfoId)
  64. if er != nil {
  65. er = fmt.Errorf("图表ID %d, 更新图表数据失败: %s, %v", chartInfo.ChartInfoId, msg, er)
  66. tmpErr = append(tmpErr, er)
  67. continue
  68. }
  69. success += 1
  70. }
  71. offset = (i + 1) * pageSize
  72. time.Sleep(2 * time.Second)
  73. }
  74. fmt.Println("更新图表数据完成, 更新图表数据总数:", success)
  75. return
  76. }
  77. // UpdateChart 更新社区里的图表接口
  78. func UpdateChart(chartInfoId int) (err error, errMsg string) {
  79. // 查询图表信息
  80. chartInfo, err := models.GetChartInfoById(chartInfoId)
  81. if err != nil {
  82. if err.Error() == utils.ErrNoRow() {
  83. errMsg = "图表不存在"
  84. err = fmt.Errorf(errMsg)
  85. return
  86. }
  87. errMsg = "获取图表信息失败"
  88. err = fmt.Errorf("获取图表信息失败,Err:" + err.Error())
  89. return
  90. }
  91. if chartInfo.ForumChartInfoId <= 0 {
  92. return
  93. }
  94. //查询图表指标
  95. //获取原图表关联的指标信息列表
  96. chartMappingList, err := models.GetChartMappingList(chartInfoId)
  97. if err != nil {
  98. errMsg = "获取图表关联的指标信息失败"
  99. err = fmt.Errorf("获取图表关联的指标信息失败,Err:" + err.Error())
  100. return
  101. }
  102. edbIds := make([]int, 0)
  103. for _, v := range chartMappingList {
  104. edbIds = append(edbIds, v.EdbInfoId)
  105. }
  106. chartSeriesList := make([]*models.ChartSeries, 0)
  107. chartSeriesEdbList := make([]*models.ChartSeriesEdbMapping, 0)
  108. if chartInfo.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
  109. chartSeriesList, err = models.GetChartSeriesByChartInfoId(chartInfoId)
  110. if err != nil {
  111. errMsg = "获取图表关联的系列信息失败"
  112. err = fmt.Errorf("获取图表关联的系列信息失败,Err:" + err.Error())
  113. return
  114. }
  115. chartSeriesEdbList, err = models.GetChartSeriesEdbByChartInfoId(chartInfoId)
  116. if err != nil {
  117. errMsg = "获取图表关联的系列指标信息失败"
  118. err = fmt.Errorf("获取图表关联的系列指标信息失败,Err:" + err.Error())
  119. return
  120. }
  121. }
  122. var (
  123. edbInfoList []*models.EdbInfo
  124. edbMappingList []*models.EdbInfoCalculateMapping
  125. edbInfoDataList []*eta_forum.AddEdbDataReq
  126. )
  127. //查询指标详情
  128. edbInfoList, edbMappingList, edbInfoDataList, err = GetEdbListByEdbInfoId(edbIds)
  129. if err != nil {
  130. errMsg = "获取指标详情失败"
  131. err = fmt.Errorf("获取指标详情失败,Err:" + err.Error())
  132. return
  133. }
  134. req := new(eta_forum.ChartSaveLibReq)
  135. req.ChartInfo = chartInfo
  136. req.ChartInfo.ChartInfoId = chartInfo.ForumChartInfoId
  137. req.ChartEdbMapping = chartMappingList
  138. req.EdbInfoList = edbInfoList
  139. req.EdbInfoDataList = edbInfoDataList
  140. req.EdbInfoCalculateMapping = edbMappingList
  141. req.ChartSeries = chartSeriesList
  142. req.ChartSeriesEdbMapping = chartSeriesEdbList
  143. // 查询创建者信息
  144. creatorInfo, _ := models.GetSysUserById(chartInfo.SysUserId)
  145. if creatorInfo != nil {
  146. req.CreatorInfo = creatorInfo
  147. }
  148. // 添加计算指标
  149. reqJson, err := json.Marshal(req)
  150. if err != nil {
  151. errMsg = "参数解析异常"
  152. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  153. return
  154. }
  155. respItem, err := eta_forum.ChartUpdateLib(string(reqJson))
  156. if err != nil {
  157. errMsg = "更新失败"
  158. err = fmt.Errorf("上传失败,Err:" + err.Error())
  159. return
  160. }
  161. if respItem.Ret != 200 {
  162. errMsg = "更新失败"
  163. err = fmt.Errorf(respItem.ErrMsg)
  164. return
  165. }
  166. return
  167. }
  168. func GetEdbListByEdbInfoId(edbInfoIds []int) (edbInfoList []*models.EdbInfo, edbMappingList []*models.EdbInfoCalculateMapping, edbInfoDataList []*eta_forum.AddEdbDataReq, err error) {
  169. //查询指标信息
  170. //查询指标映射
  171. //查询所有指标数据
  172. //查询这个指标相关的mapping信息放到数组里,
  173. //将得到的指标ID信息放到数组里
  174. hasFindMap := make(map[int]struct{})
  175. edbInfoIdMap := make(map[int]struct{})
  176. edbMappingList = make([]*models.EdbInfoCalculateMapping, 0)
  177. edbMappingMap := make(map[int]struct{})
  178. for _, edbInfoId := range edbInfoIds {
  179. edbMappingList, err = traceEdbInfoByEdbInfoId(edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap)
  180. if err != nil {
  181. return
  182. }
  183. }
  184. // 指标信息map
  185. edbInfoIdList := make([]int, 0)
  186. for k, _ := range edbInfoIdMap {
  187. edbInfoIdList = append(edbInfoIdList, k)
  188. }
  189. edbInfoList, err = models.GetEdbInfoByIdList(edbInfoIdList)
  190. if err != nil {
  191. err = fmt.Errorf("traceEdbInfoByEdbInfoId GetEdbInfoByIdList err: %s", err.Error())
  192. return
  193. }
  194. for _, v := range edbInfoList {
  195. var dataList []*models.EdbDataBase
  196. if v.Source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  197. dataList, err = models.GetEdbDataBaseMongoByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
  198. } else if v.Source == utils.DATA_SOURCE_THS && v.SubSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  199. dataList, err = models.GetThsHfEdbDataBaseMongoByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
  200. } else {
  201. dataList, err = models.GetEdbDataBaseByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
  202. }
  203. if err != nil {
  204. err = fmt.Errorf("查询指标数据失败 Err: %s", err.Error())
  205. return
  206. }
  207. tmp := new(eta_forum.AddEdbDataReq)
  208. tmp.EdbCode = v.EdbCode
  209. tmp.EdbType = v.EdbType
  210. tmp.DataList = dataList
  211. edbInfoDataList = append(edbInfoDataList, tmp)
  212. }
  213. return
  214. }
  215. // traceEdbInfoByEdbInfoId 指标追溯
  216. func traceEdbInfoByEdbInfoId(edbInfoId int, hasFindMap map[int]struct{}, edbInfoIdMap map[int]struct{}, edbMappingList []*models.EdbInfoCalculateMapping, edbMappingMap map[int]struct{}) (newEdbMappingList []*models.EdbInfoCalculateMapping, err error) {
  217. newEdbMappingList = edbMappingList
  218. _, ok := hasFindMap[edbInfoId]
  219. if ok {
  220. return
  221. }
  222. if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
  223. edbInfoIdMap[edbInfoId] = struct{}{}
  224. }
  225. edbInfoMappingList, e := models.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  226. if e != nil {
  227. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", e.Error())
  228. return
  229. }
  230. if len(edbInfoMappingList) > 0 {
  231. fromEdbInfoIdList := make([]int, 0)
  232. for _, v := range edbInfoMappingList {
  233. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  234. if _, ok1 := edbInfoIdMap[v.FromEdbInfoId]; !ok1 {
  235. edbInfoIdMap[v.FromEdbInfoId] = struct{}{}
  236. }
  237. if _, ok2 := edbMappingMap[v.EdbInfoCalculateMappingId]; !ok2 {
  238. edbMappingMap[v.EdbInfoCalculateMappingId] = struct{}{}
  239. tmp := &models.EdbInfoCalculateMapping{
  240. EdbInfoCalculateMappingId: v.EdbInfoCalculateMappingId,
  241. EdbInfoId: v.EdbInfoId,
  242. Source: v.Source,
  243. SourceName: v.SourceName,
  244. EdbCode: v.EdbCode,
  245. FromEdbInfoId: v.FromEdbInfoId,
  246. FromEdbCode: v.FromEdbCode,
  247. FromEdbName: v.FromEdbName,
  248. FromSource: v.FromSource,
  249. FromSourceName: v.FromSourceName,
  250. FromTag: v.FromTag,
  251. Sort: v.Sort,
  252. CreateTime: v.CreateTime,
  253. ModifyTime: v.ModifyTime,
  254. }
  255. newEdbMappingList = append(newEdbMappingList, tmp)
  256. }
  257. if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
  258. // 查过了就不查了
  259. if _, ok2 := hasFindMap[v.FromEdbInfoId]; !ok2 {
  260. newEdbMappingList, e = traceEdbInfoByEdbInfoId(v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap)
  261. if e != nil {
  262. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", e.Error())
  263. return
  264. }
  265. }
  266. }
  267. hasFindMap[v.FromEdbInfoId] = struct{}{}
  268. }
  269. }
  270. hasFindMap[edbInfoId] = struct{}{}
  271. return
  272. }