123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- package services
- import (
- "eta_gn/eta_index_lib/models"
- "eta_gn/eta_index_lib/models/data_stat"
- "eta_gn/eta_index_lib/services/alarm_msg"
- "eta_gn/eta_index_lib/utils"
- "fmt"
- "time"
- )
- // AddEdbInfoUpdateLog 添加指标编辑/刷新日志
- func AddEdbInfoUpdateLog(edbInfoId int, updateResult int, updateFailedReason string, dataUpdateResult int, dataUpdateFailedReason string, isSourceRefresh int, updateType int) (err error) {
- var edbInfo *models.EdbInfo
- if edbInfoId > 0 {
- // 获取指标详情
- edbInfo, err = models.GetEdbInfoById(edbInfoId)
- if err != nil {
- err = fmt.Errorf("指标不存在")
- return
- }
- log := new(data_stat.EdbInfoUpdateLog)
- log.EdbInfoId = edbInfo.EdbInfoId
- log.SourceName = edbInfo.SourceName
- log.Source = edbInfo.Source
- log.EdbCode = edbInfo.EdbCode
- log.EdbName = edbInfo.EdbName
- log.EdbNameSource = edbInfo.SourceIndexName
- log.Frequency = edbInfo.Frequency
- log.Unit = edbInfo.Unit
- log.StartDate = edbInfo.StartDate
- log.EndDate = edbInfo.EndDate
- log.SysUserId = edbInfo.SysUserId
- log.SysUserRealName = edbInfo.SysUserRealName
- log.UniqueCode = edbInfo.UniqueCode
- log.EdbCreateTime = edbInfo.CreateTime
- log.EdbModifyTime = edbInfo.ModifyTime
- log.CreateTime = time.Now()
- log.LatestDate = edbInfo.LatestDate
- log.LatestValue = edbInfo.LatestValue
- log.TerminalCode = edbInfo.TerminalCode
- log.UpdateResult = updateResult
- log.UpdateFailedReason = updateFailedReason
- log.DataUpdateTime = edbInfo.DataUpdateTime
- log.ErDataUpdateDate = edbInfo.ErDataUpdateDate
- log.DataUpdateResult = dataUpdateResult
- log.DataUpdateFailedReason = dataUpdateFailedReason
- log.IsSourceRefresh = isSourceRefresh
- log.UpdateType = updateType
- _, err = data_stat.AddEdbUpdateLog(log)
- if err != nil {
- err = fmt.Errorf("新增指标更新日志失败,Err: %s", err)
- return
- }
- }
- return
- }
- // SetEdbSourceStat 定时统计数据源汇总表
- func SetEdbSourceStat(needStat bool) (err error) {
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新统计异常 Err: %s", err.Error())
- alarm_msg.SendAlarmMsg(tips, 3)
- }
- }()
- //查询钢联的所有指标信息
- nowTime := time.Now()
- today := time.Now().Format(utils.FormatDate)
- nextDay := time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
- statCond := " and create_time >= ? and create_time < ?"
- var statPars []interface{}
- statPars = append(statPars, today, nextDay)
- //查询当日钢联所有的统计数据
- updateStatList, err := data_stat.GetEdbUpdateStatByCondition(statCond, statPars)
- if err != nil {
- err = fmt.Errorf("查询钢联化工数据源明细记录统计报错,err: %s", err)
- return
- }
- if !needStat && len(updateStatList) == 0 {
- return
- }
- updateStatMap := make(map[string][]*data_stat.EdbInfoUpdateStat)
- if len(updateStatList) > 0 {
- for _, v := range updateStatList {
- updateStatMap[v.TerminalCode] = append(updateStatMap[v.TerminalCode], v)
- }
- }
- cond := " and create_time >= ? and create_time < ?"
- var pars []interface{}
- pars = append(pars, today, nextDay)
- //查询当日钢联所有的统计数据
- statList, err := data_stat.GetEdbSourceStatByCondition(cond, pars)
- if err != nil {
- err = fmt.Errorf("查询钢联化工数据源统计报错,err: %s", err)
- return
- }
- statMap := make(map[string]*data_stat.EdbSourceStat)
- if len(statList) > 0 {
- for _, v := range statList {
- statMap[v.TerminalCode] = v
- }
- }
- // 查询今日被删除的指标数
- delList, err := data_stat.GetEdbDeleteLogNumByCreateTime(today, nextDay)
- if err != nil {
- err = fmt.Errorf("查询今日被删除指标数目报错,err: %s", err)
- return
- }
- delMap := make(map[string]int)
- if len(delList) > 0 {
- for _, v := range delList {
- delMap[v.TerminalCode] = v.Num
- }
- }
- logStat := new(data_stat.EdbSourceStat)
- //组装新增数据
- addList := make([]*data_stat.EdbSourceStat, 0)
- modifyList := make([]*data_stat.EdbSourceStat, 0)
- for terminalCode, list := range updateStatMap {
- tmp := new(data_stat.EdbSourceStat)
- for k, v := range list {
- if k == 0 {
- tmp.SourceName = v.SourceName
- tmp.Source = v.Source
- tmp.TerminalCode = v.TerminalCode
- tmp.ModifyTime = nowTime
- }
- tmp.EdbNum = tmp.EdbNum + 1
- if v.IsAdd == 1 {
- tmp.EdbNewNum = tmp.EdbNewNum + 1
- }
- if v.NeedRefresh == 1 {
- tmp.NeedRefreshNum = tmp.NeedRefreshNum + 1
- }
- if v.HasRefresh == 1 {
- tmp.HasRefreshNum = tmp.HasRefreshNum + 1
- }
- // 区分刷新成功和更新成功
- if v.DataUpdateResult == 1 { //处理更新结果
- tmp.UpdateSuccessNum = tmp.UpdateSuccessNum + 1
- } else if v.NeedRefresh == 1 {
- tmp.UpdateFailedNum = tmp.UpdateFailedNum + 1
- }
- if v.UpdateResult == 1 { //刷新结果
- tmp.RefreshSuccessNum = tmp.RefreshSuccessNum + 1
- } else if v.HasRefresh == 1 {
- tmp.RefreshFailedNum = tmp.RefreshFailedNum + 1
- }
- }
- // 处理今天删除的指标数量
- if dn, ok := delMap[terminalCode]; ok {
- tmp.EdbDelNum = dn
- }
- // 判断是否需要新增还是更新
- if exist, ok := statMap[terminalCode]; ok {
- tmp.Id = exist.Id
- modifyList = append(modifyList, tmp)
- } else {
- tmp.CreateTime = nowTime
- addList = append(addList, tmp)
- }
- if len(addList) >= 500 {
- err = logStat.Add(addList)
- if err != nil {
- err = fmt.Errorf("新增钢联化工统计表报错,err: %s", err)
- return
- }
- addList = addList[:0]
- }
- if len(modifyList) >= 500 {
- err = data_stat.UpdateEdbSourceStatMulti(modifyList)
- if err != nil {
- err = fmt.Errorf("更新钢联化工统计表报错,err: %s", err)
- return
- }
- modifyList = modifyList[:0]
- }
- }
- //判断当日指标统计数据是否存在,如果存在则更新,不存在则新增
- if len(addList) > 0 {
- err = logStat.Add(addList)
- }
- if len(modifyList) > 0 {
- err = data_stat.UpdateEdbSourceStatMulti(modifyList)
- }
- return
- }
|