edb_info_stat.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package services
  2. import (
  3. "eta_gn/eta_index_lib/models"
  4. "eta_gn/eta_index_lib/models/data_stat"
  5. "eta_gn/eta_index_lib/services/alarm_msg"
  6. "eta_gn/eta_index_lib/utils"
  7. "fmt"
  8. "time"
  9. )
  10. // AddEdbInfoUpdateLog 添加指标编辑/刷新日志
  11. func AddEdbInfoUpdateLog(edbInfoId int, updateResult int, updateFailedReason string, dataUpdateResult int, dataUpdateFailedReason string, isSourceRefresh int, updateType int) (err error) {
  12. var edbInfo *models.EdbInfo
  13. if edbInfoId > 0 {
  14. // 获取指标详情
  15. edbInfo, err = models.GetEdbInfoById(edbInfoId)
  16. if err != nil {
  17. err = fmt.Errorf("指标不存在")
  18. return
  19. }
  20. log := new(data_stat.EdbInfoUpdateLog)
  21. log.EdbInfoId = edbInfo.EdbInfoId
  22. log.SourceName = edbInfo.SourceName
  23. log.Source = edbInfo.Source
  24. log.EdbCode = edbInfo.EdbCode
  25. log.EdbName = edbInfo.EdbName
  26. log.EdbNameSource = edbInfo.SourceIndexName
  27. log.Frequency = edbInfo.Frequency
  28. log.Unit = edbInfo.Unit
  29. log.StartDate = edbInfo.StartDate
  30. log.EndDate = edbInfo.EndDate
  31. log.SysUserId = edbInfo.SysUserId
  32. log.SysUserRealName = edbInfo.SysUserRealName
  33. log.UniqueCode = edbInfo.UniqueCode
  34. log.EdbCreateTime = edbInfo.CreateTime
  35. log.EdbModifyTime = edbInfo.ModifyTime
  36. log.CreateTime = time.Now()
  37. log.LatestDate = edbInfo.LatestDate
  38. log.LatestValue = edbInfo.LatestValue
  39. log.TerminalCode = edbInfo.TerminalCode
  40. log.UpdateResult = updateResult
  41. log.UpdateFailedReason = updateFailedReason
  42. log.DataUpdateTime = edbInfo.DataUpdateTime
  43. log.ErDataUpdateDate = edbInfo.ErDataUpdateDate
  44. log.DataUpdateResult = dataUpdateResult
  45. log.DataUpdateFailedReason = dataUpdateFailedReason
  46. log.IsSourceRefresh = isSourceRefresh
  47. log.UpdateType = updateType
  48. _, err = data_stat.AddEdbUpdateLog(log)
  49. if err != nil {
  50. err = fmt.Errorf("新增指标更新日志失败,Err: %s", err)
  51. return
  52. }
  53. }
  54. return
  55. }
  56. // SetEdbSourceStat 定时统计数据源汇总表
  57. func SetEdbSourceStat(needStat bool) (err error) {
  58. defer func() {
  59. if err != nil {
  60. tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新统计异常 Err: %s", err.Error())
  61. alarm_msg.SendAlarmMsg(tips, 3)
  62. }
  63. }()
  64. //查询钢联的所有指标信息
  65. nowTime := time.Now()
  66. today := time.Now().Format(utils.FormatDate)
  67. nextDay := time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  68. statCond := " and create_time >= ? and create_time < ?"
  69. var statPars []interface{}
  70. statPars = append(statPars, today, nextDay)
  71. //查询当日钢联所有的统计数据
  72. updateStatList, err := data_stat.GetEdbUpdateStatByCondition(statCond, statPars)
  73. if err != nil {
  74. err = fmt.Errorf("查询钢联化工数据源明细记录统计报错,err: %s", err)
  75. return
  76. }
  77. if !needStat && len(updateStatList) == 0 {
  78. return
  79. }
  80. updateStatMap := make(map[string][]*data_stat.EdbInfoUpdateStat)
  81. if len(updateStatList) > 0 {
  82. for _, v := range updateStatList {
  83. updateStatMap[v.TerminalCode] = append(updateStatMap[v.TerminalCode], v)
  84. }
  85. }
  86. cond := " and create_time >= ? and create_time < ?"
  87. var pars []interface{}
  88. pars = append(pars, today, nextDay)
  89. //查询当日钢联所有的统计数据
  90. statList, err := data_stat.GetEdbSourceStatByCondition(cond, pars)
  91. if err != nil {
  92. err = fmt.Errorf("查询钢联化工数据源统计报错,err: %s", err)
  93. return
  94. }
  95. statMap := make(map[string]*data_stat.EdbSourceStat)
  96. if len(statList) > 0 {
  97. for _, v := range statList {
  98. statMap[v.TerminalCode] = v
  99. }
  100. }
  101. // 查询今日被删除的指标数
  102. delList, err := data_stat.GetEdbDeleteLogNumByCreateTime(today, nextDay)
  103. if err != nil {
  104. err = fmt.Errorf("查询今日被删除指标数目报错,err: %s", err)
  105. return
  106. }
  107. delMap := make(map[string]int)
  108. if len(delList) > 0 {
  109. for _, v := range delList {
  110. delMap[v.TerminalCode] = v.Num
  111. }
  112. }
  113. logStat := new(data_stat.EdbSourceStat)
  114. //组装新增数据
  115. addList := make([]*data_stat.EdbSourceStat, 0)
  116. modifyList := make([]*data_stat.EdbSourceStat, 0)
  117. for terminalCode, list := range updateStatMap {
  118. tmp := new(data_stat.EdbSourceStat)
  119. for k, v := range list {
  120. if k == 0 {
  121. tmp.SourceName = v.SourceName
  122. tmp.Source = v.Source
  123. tmp.TerminalCode = v.TerminalCode
  124. tmp.ModifyTime = nowTime
  125. }
  126. tmp.EdbNum = tmp.EdbNum + 1
  127. if v.IsAdd == 1 {
  128. tmp.EdbNewNum = tmp.EdbNewNum + 1
  129. }
  130. if v.NeedRefresh == 1 {
  131. tmp.NeedRefreshNum = tmp.NeedRefreshNum + 1
  132. }
  133. if v.HasRefresh == 1 {
  134. tmp.HasRefreshNum = tmp.HasRefreshNum + 1
  135. }
  136. // 区分刷新成功和更新成功
  137. if v.DataUpdateResult == 1 { //处理更新结果
  138. tmp.UpdateSuccessNum = tmp.UpdateSuccessNum + 1
  139. } else if v.NeedRefresh == 1 {
  140. tmp.UpdateFailedNum = tmp.UpdateFailedNum + 1
  141. }
  142. if v.UpdateResult == 1 { //刷新结果
  143. tmp.RefreshSuccessNum = tmp.RefreshSuccessNum + 1
  144. } else if v.HasRefresh == 1 {
  145. tmp.RefreshFailedNum = tmp.RefreshFailedNum + 1
  146. }
  147. }
  148. // 处理今天删除的指标数量
  149. if dn, ok := delMap[terminalCode]; ok {
  150. tmp.EdbDelNum = dn
  151. }
  152. // 判断是否需要新增还是更新
  153. if exist, ok := statMap[terminalCode]; ok {
  154. tmp.Id = exist.Id
  155. modifyList = append(modifyList, tmp)
  156. } else {
  157. tmp.CreateTime = nowTime
  158. addList = append(addList, tmp)
  159. }
  160. if len(addList) >= 500 {
  161. err = logStat.Add(addList)
  162. if err != nil {
  163. err = fmt.Errorf("新增钢联化工统计表报错,err: %s", err)
  164. return
  165. }
  166. addList = addList[:0]
  167. }
  168. if len(modifyList) >= 500 {
  169. err = data_stat.UpdateEdbSourceStatMulti(modifyList)
  170. if err != nil {
  171. err = fmt.Errorf("更新钢联化工统计表报错,err: %s", err)
  172. return
  173. }
  174. modifyList = modifyList[:0]
  175. }
  176. }
  177. //判断当日指标统计数据是否存在,如果存在则更新,不存在则新增
  178. if len(addList) > 0 {
  179. err = logStat.Add(addList)
  180. }
  181. if len(modifyList) > 0 {
  182. err = data_stat.UpdateEdbSourceStatMulti(modifyList)
  183. }
  184. return
  185. }