edb_info_stat.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package services
  2. import (
  3. "eta/eta_index_lib/models"
  4. "eta/eta_index_lib/models/data_stat"
  5. "eta/eta_index_lib/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // AddEdbInfoUpdateLog 添加指标编辑/刷新日志
  10. func AddEdbInfoUpdateLog(edbInfoId int, updateResult int, updateFailedReason string, sourceUpdateResult int, sourceUpdateFailedReason string, sourceUpdateTime string) (err error) {
  11. var edbInfo *models.EdbInfo
  12. if edbInfoId > 0 {
  13. // 获取指标详情
  14. edbInfo, err = models.GetEdbInfoById(edbInfoId)
  15. if err != nil {
  16. err = fmt.Errorf("指标不存在")
  17. return
  18. }
  19. log := new(data_stat.EdbInfoUpdateLog)
  20. log.EdbInfoId = edbInfo.EdbInfoId
  21. log.SourceName = edbInfo.SourceName
  22. log.Source = edbInfo.Source
  23. log.EdbCode = edbInfo.EdbCode
  24. log.EdbName = edbInfo.EdbName
  25. log.EdbNameSource = edbInfo.EdbNameSource
  26. log.Frequency = edbInfo.Frequency
  27. log.Unit = edbInfo.Unit
  28. log.StartDate = edbInfo.StartDate
  29. log.EndDate = edbInfo.EndDate
  30. log.SysUserId = edbInfo.SysUserId
  31. log.SysUserRealName = edbInfo.SysUserRealName
  32. log.UniqueCode = edbInfo.UniqueCode
  33. log.EdbCreateTime = edbInfo.CreateTime
  34. log.EdbModifyTime = edbInfo.ModifyTime
  35. log.CreateTime = time.Now()
  36. log.UpdateTime = 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.SourceUpdateResult = sourceUpdateResult
  45. log.SourceUpdateTime = sourceUpdateTime
  46. log.SourceUpdateFailedReason = sourceUpdateFailedReason
  47. _, err = data_stat.AddEdbUpdateLog(log)
  48. if err != nil {
  49. err = fmt.Errorf("新增指标更新日志失败,Err: %s", err)
  50. return
  51. }
  52. }
  53. return
  54. }
  55. // SetMysteelChemicalEdbInfoUpdateStat 定时统计钢联化工的数据源明细表
  56. func SetMysteelChemicalEdbInfoUpdateStat() (err error) {
  57. //查询钢联的所有指标信息
  58. condition := " and source = ? and edb_info_id=101838"
  59. var pars []interface{}
  60. pars = append(pars, utils.DATA_SOURCE_MYSTEEL_CHEMICAL)
  61. edbList, err := models.GetEdbInfoByCondition(condition, pars, 0)
  62. if err != nil {
  63. err = fmt.Errorf("查询钢联化工指标信息出错,err: %s", err)
  64. return
  65. }
  66. nowTime := time.Now()
  67. today := time.Now().Format(utils.FormatDate)
  68. todayT, _ := time.ParseInLocation(utils.FormatDate, today, time.Local)
  69. nextDay := time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  70. //查询当日所有钢联指标的终端更新记录
  71. updateLogList, err := data_stat.GetEdbUpdateSourceLogByCreateDate(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, today, nextDay)
  72. if err != nil {
  73. err = fmt.Errorf("查询钢联化工指标终端更新日志报错,err: %s", err)
  74. return
  75. }
  76. updateLogMap := make(map[int]*data_stat.EdbInfoUpdateLog)
  77. if len(updateLogList) > 0 {
  78. for _, v := range updateLogList {
  79. if _, ok := updateLogMap[v.EdbInfoId]; !ok {
  80. updateLogMap[v.EdbInfoId] = v
  81. }
  82. }
  83. }
  84. statCond := " and source = ? and create_time >= ? and create_time < ?"
  85. var statPars []interface{}
  86. statPars = append(statPars, utils.DATA_SOURCE_MYSTEEL_CHEMICAL, today, nextDay)
  87. //查询当日钢联所有的统计数据
  88. updateStatList, err := data_stat.GetEdbUpdateStatByCondition(statCond, statPars)
  89. if err != nil {
  90. err = fmt.Errorf("查询钢联化工数据源明细记录统计报错,err: %s", err)
  91. return
  92. }
  93. updateStatMap := make(map[int]*data_stat.EdbInfoUpdateStat)
  94. if len(updateStatList) > 0 {
  95. for _, v := range updateStatList {
  96. updateStatMap[v.EdbInfoId] = v
  97. }
  98. }
  99. logStat := new(data_stat.EdbInfoUpdateStat)
  100. //组装新增数据
  101. addList := make([]*data_stat.EdbInfoUpdateStat, 0)
  102. modifyList := make([]*data_stat.EdbInfoUpdateStat, 0)
  103. if len(edbList) > 0 {
  104. for _, v := range edbList {
  105. tmp := &data_stat.EdbInfoUpdateStat{
  106. EdbInfoId: v.EdbInfoId,
  107. SourceName: v.SourceName,
  108. Source: v.Source,
  109. EdbCode: v.EdbCode,
  110. EdbName: v.EdbName,
  111. EdbNameSource: v.EdbNameSource,
  112. Frequency: v.Frequency,
  113. Unit: v.Unit,
  114. StartDate: v.StartDate,
  115. EndDate: v.EndDate,
  116. SysUserId: v.SysUserId,
  117. SysUserRealName: v.SysUserRealName,
  118. UniqueCode: v.UniqueCode,
  119. EdbCreateTime: v.CreateTime,
  120. EdbModifyTime: v.ModifyTime,
  121. //CreateTime: v.CreateTime,
  122. LatestDate: v.LatestDate,
  123. LatestValue: v.LatestValue,
  124. TerminalCode: v.TerminalCode,
  125. DataUpdateTime: v.DataUpdateTime,
  126. ErDataUpdateDate: v.ErDataUpdateDate,
  127. /*SourceUpdateResult: v.SourceUpdateResult,
  128. SourceUpdateFailedReason: v.SourceUpdateFailedReason,
  129. SourceUpdateTime: v.SourceUpdateTime,*/
  130. ModifyTime: nowTime,
  131. //IsAdd: v.IsAdd,
  132. //NeedRefresh: v.NeedRefresh,
  133. //HasRefresh: v.HasRefresh,
  134. }
  135. // todo 判断是否需要当日更新
  136. needRefresh, _ := checkEdbInfoNeedRefresh(v.EdbInfoId, v.Frequency)
  137. tmp.NeedRefresh = needRefresh
  138. // 判断是否当日新增
  139. if v.CreateTime.After(todayT) || v.CreateTime == todayT {
  140. tmp.IsAdd = 1
  141. } else {
  142. tmp.IsAdd = 2
  143. }
  144. if up, ok := updateLogMap[v.EdbInfoId]; ok {
  145. tmp.SourceUpdateTime = up.SourceUpdateTime
  146. tmp.SourceUpdateResult = up.SourceUpdateResult
  147. tmp.SourceUpdateFailedReason = up.SourceUpdateFailedReason
  148. if up.SourceUpdateFailedReason != "" && up.SourceUpdateFailedReason != "未刷新到数据" {
  149. tmp.SourceUpdateFailedReason = "服务异常"
  150. }
  151. tmp.HasRefresh = 1
  152. } else if needRefresh == 1 {
  153. tmp.HasRefresh = 0
  154. tmp.SourceUpdateResult = 2
  155. tmp.SourceUpdateFailedReason = "服务异常"
  156. }
  157. // 判断是否需要新增还是更新
  158. if exist, ok := updateStatMap[v.EdbInfoId]; ok {
  159. tmp.Id = exist.Id
  160. modifyList = append(modifyList, tmp)
  161. } else {
  162. tmp.CreateTime = nowTime
  163. addList = append(addList, tmp)
  164. }
  165. }
  166. }
  167. //判断当日指标统计数据是否存在,如果存在则更新,不存在则新增
  168. if len(addList) > 0 {
  169. err = logStat.Add(addList)
  170. }
  171. if len(modifyList) > 0 {
  172. err = data_stat.UpdateEdbUpdateStatMulti(modifyList)
  173. }
  174. return
  175. }
  176. func checkEdbInfoNeedRefresh(edbInfoId int, frequency string) (needRefresh int, err error) {
  177. return
  178. }
  179. // SetEdbSourceStat 定时统计数据源汇总表
  180. func SetEdbSourceStat() (err error) {
  181. //查询钢联的所有指标信息
  182. nowTime := time.Now()
  183. today := time.Now().Format(utils.FormatDate)
  184. nextDay := time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  185. statCond := " and create_time >= ? and create_time < ?"
  186. var statPars []interface{}
  187. statPars = append(statPars, today, nextDay)
  188. //查询当日钢联所有的统计数据
  189. updateStatList, err := data_stat.GetEdbUpdateStatByCondition(statCond, statPars)
  190. if err != nil {
  191. err = fmt.Errorf("查询钢联化工数据源明细记录统计报错,err: %s", err)
  192. return
  193. }
  194. updateStatMap := make(map[string][]*data_stat.EdbInfoUpdateStat)
  195. if len(updateStatList) > 0 {
  196. for _, v := range updateStatList {
  197. updateStatMap[v.TerminalCode] = append(updateStatMap[v.TerminalCode], v)
  198. }
  199. }
  200. cond := " and create_time >= ? and create_time < ?"
  201. var pars []interface{}
  202. pars = append(pars, today, nextDay)
  203. //查询当日钢联所有的统计数据
  204. statList, err := data_stat.GetEdbSourceStatByCondition(cond, pars)
  205. if err != nil {
  206. err = fmt.Errorf("查询钢联化工数据源统计报错,err: %s", err)
  207. return
  208. }
  209. statMap := make(map[string]*data_stat.EdbSourceStat)
  210. if len(statList) > 0 {
  211. for _, v := range statList {
  212. statMap[v.TerminalCode] = v
  213. }
  214. }
  215. // 查询今日被删除的指标数
  216. delList, err := data_stat.GetEdbDeleteLogNumByCreateTime(today, nextDay)
  217. if err != nil {
  218. err = fmt.Errorf("查询今日被删除指标数目报错,err: %s", err)
  219. return
  220. }
  221. delMap := make(map[string]int)
  222. if len(delList) > 0 {
  223. for _, v := range delList {
  224. delMap[v.TerminalCode] = v.Num
  225. }
  226. }
  227. logStat := new(data_stat.EdbSourceStat)
  228. //组装新增数据
  229. addList := make([]*data_stat.EdbSourceStat, 0)
  230. modifyList := make([]*data_stat.EdbSourceStat, 0)
  231. for terminalCode, list := range updateStatMap {
  232. tmp := new(data_stat.EdbSourceStat)
  233. for k, v := range list {
  234. if k == 0 {
  235. tmp.SourceName = v.SourceName
  236. tmp.Source = v.Source
  237. tmp.TerminalCode = v.TerminalCode
  238. tmp.ModifyTime = nowTime
  239. }
  240. tmp.EdbNum = tmp.EdbNum + 1
  241. if v.IsAdd == 1 {
  242. tmp.EdbNewNum = tmp.EdbNewNum + 1
  243. }
  244. if v.NeedRefresh == 1 {
  245. tmp.NeedRefreshNum = tmp.NeedRefreshNum + 1
  246. }
  247. if v.HasRefresh == 1 {
  248. tmp.HasRefreshNum = tmp.HasRefreshNum + 1
  249. }
  250. if v.SourceUpdateResult == 1 {
  251. tmp.UpdateSuccessNum = tmp.UpdateSuccessNum + 1
  252. } else {
  253. tmp.UpdateFailedNum = tmp.UpdateFailedNum + 1
  254. }
  255. // todo 数据更新成功和更新失败,与刷新成功和刷新失败的含义一致
  256. tmp.DataUpdateSuccessNum = tmp.UpdateSuccessNum
  257. }
  258. // 处理今天删除的指标数量
  259. if dn, ok := delMap[terminalCode]; ok {
  260. tmp.EdbDelNum = dn
  261. }
  262. // 判断是否需要新增还是更新
  263. if exist, ok := statMap[terminalCode]; ok {
  264. tmp.Id = exist.Id
  265. modifyList = append(modifyList, tmp)
  266. } else {
  267. tmp.CreateTime = nowTime
  268. addList = append(addList, tmp)
  269. }
  270. }
  271. //判断当日指标统计数据是否存在,如果存在则更新,不存在则新增
  272. if len(addList) > 0 {
  273. err = logStat.Add(addList)
  274. }
  275. if len(modifyList) > 0 {
  276. err = data_stat.UpdateEdbSourceStatMulti(modifyList)
  277. }
  278. return
  279. }