123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- package services
- import (
- "context"
- "encoding/json"
- "eta/eta_forum_task/models"
- "eta/eta_forum_task/services/alarm_msg"
- "eta/eta_forum_task/services/eta_forum"
- "eta/eta_forum_task/utils"
- "fmt"
- "time"
- )
- func EtaForumChartUpdate(cont context.Context) (err error) {
- var tmpErr []error
- deleteCache := true
- cacheKey := "eta_forum_task:EtaForumChartUpdate"
- defer func() {
- if deleteCache {
- _ = utils.Rc.Delete(cacheKey)
- }
- stack := ""
- if err != nil {
- stack = fmt.Sprintln(stack + err.Error())
- }
- if len(tmpErr) > 0 {
- for _, v := range tmpErr {
- stack = fmt.Sprintln(stack + v.Error())
- }
- }
- if stack != "" {
- go alarm_msg.SendAlarmMsg(utils.APP_NAME_CN+"更新社区图表信息失败"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+stack, 3)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- err = fmt.Errorf("系统处理中,请稍后重试!")
- return
- }
- // 设置缓存判断处理中则不更新
- condition := " and forum_chart_info_id > 0 and source=1 "
- // 查询需要更新的图表信息总数
- total, err := models.GetChartInfoCountByCondition(condition, []interface{}{})
- if err != nil {
- return
- }
- if total == 0 {
- return
- }
- // 更新图表数据
- offset := 0
- pageSize := 100
- success := 0
- // 循环更新100个图表数据
- for i := 0; offset < total; i++ {
- // 查询需要更新的图表信息
- chartInfos, e := models.GetChartInfoListByCondition(condition, []interface{}{}, offset, pageSize)
- if e != nil {
- err = fmt.Errorf("查询需要更新的图表信息失败: %v", e)
- return
- }
- // 循环更新图表数据
- for _, chartInfo := range chartInfos {
- // 更新图表数据
- er, msg := UpdateChart(chartInfo.ChartInfoId)
- if er != nil {
- er = fmt.Errorf("图表ID %d, 更新图表数据失败: %s, %v", chartInfo.ChartInfoId, msg, er)
- tmpErr = append(tmpErr, er)
- continue
- }
- success += 1
- }
- offset = (i + 1) * pageSize
- time.Sleep(2 * time.Second)
- }
- fmt.Println("更新图表数据完成, 更新图表数据总数:", success)
- return
- }
- // UpdateChart 更新社区里的图表接口
- func UpdateChart(chartInfoId int) (err error, errMsg string) {
- // 查询图表信息
- chartInfo, err := models.GetChartInfoById(chartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "图表不存在"
- err = fmt.Errorf(errMsg)
- return
- }
- errMsg = "获取图表信息失败"
- err = fmt.Errorf("获取图表信息失败,Err:" + err.Error())
- return
- }
- if chartInfo.ForumChartInfoId <= 0 {
- return
- }
- //查询图表指标
- //获取原图表关联的指标信息列表
- chartMappingList, err := models.GetChartMappingList(chartInfoId)
- if err != nil {
- errMsg = "获取图表关联的指标信息失败"
- err = fmt.Errorf("获取图表关联的指标信息失败,Err:" + err.Error())
- return
- }
- edbIds := make([]int, 0)
- for _, v := range chartMappingList {
- edbIds = append(edbIds, v.EdbInfoId)
- }
- chartSeriesList := make([]*models.ChartSeries, 0)
- chartSeriesEdbList := make([]*models.ChartSeriesEdbMapping, 0)
- if chartInfo.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
- chartSeriesList, err = models.GetChartSeriesByChartInfoId(chartInfoId)
- if err != nil {
- errMsg = "获取图表关联的系列信息失败"
- err = fmt.Errorf("获取图表关联的系列信息失败,Err:" + err.Error())
- return
- }
- chartSeriesEdbList, err = models.GetChartSeriesEdbByChartInfoId(chartInfoId)
- if err != nil {
- errMsg = "获取图表关联的系列指标信息失败"
- err = fmt.Errorf("获取图表关联的系列指标信息失败,Err:" + err.Error())
- return
- }
- }
- var (
- edbInfoList []*models.EdbInfo
- edbMappingList []*models.EdbInfoCalculateMapping
- edbInfoDataList []*eta_forum.AddEdbDataReq
- )
- //查询指标详情
- edbInfoList, edbMappingList, edbInfoDataList, err = GetEdbListByEdbInfoId(edbIds)
- if err != nil {
- errMsg = "获取指标详情失败"
- err = fmt.Errorf("获取指标详情失败,Err:" + err.Error())
- return
- }
- req := new(eta_forum.ChartSaveLibReq)
- req.ChartInfo = chartInfo
- req.ChartInfo.ChartInfoId = chartInfo.ForumChartInfoId
- req.ChartEdbMapping = chartMappingList
- req.EdbInfoList = edbInfoList
- req.EdbInfoDataList = edbInfoDataList
- req.EdbInfoCalculateMapping = edbMappingList
- req.ChartSeries = chartSeriesList
- req.ChartSeriesEdbMapping = chartSeriesEdbList
- // 查询创建者信息
- creatorInfo, _ := models.GetSysUserById(chartInfo.SysUserId)
- if creatorInfo != nil {
- req.CreatorInfo = creatorInfo
- }
- // 添加计算指标
- reqJson, err := json.Marshal(req)
- if err != nil {
- errMsg = "参数解析异常"
- err = fmt.Errorf("参数解析异常,Err:" + err.Error())
- return
- }
- respItem, err := eta_forum.ChartUpdateLib(string(reqJson))
- if err != nil {
- errMsg = "更新失败"
- err = fmt.Errorf("上传失败,Err:" + err.Error())
- return
- }
- if respItem.Ret != 200 {
- errMsg = "更新失败"
- err = fmt.Errorf(respItem.ErrMsg)
- return
- }
- return
- }
- func GetEdbListByEdbInfoId(edbInfoIds []int) (edbInfoList []*models.EdbInfo, edbMappingList []*models.EdbInfoCalculateMapping, edbInfoDataList []*eta_forum.AddEdbDataReq, err error) {
- //查询指标信息
- //查询指标映射
- //查询所有指标数据
- //查询这个指标相关的mapping信息放到数组里,
- //将得到的指标ID信息放到数组里
- hasFindMap := make(map[int]struct{})
- edbInfoIdMap := make(map[int]struct{})
- edbMappingList = make([]*models.EdbInfoCalculateMapping, 0)
- edbMappingMap := make(map[int]struct{})
- for _, edbInfoId := range edbInfoIds {
- edbMappingList, err = traceEdbInfoByEdbInfoId(edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap)
- if err != nil {
- return
- }
- }
- // 指标信息map
- edbInfoIdList := make([]int, 0)
- for k, _ := range edbInfoIdMap {
- edbInfoIdList = append(edbInfoIdList, k)
- }
- edbInfoList, err = models.GetEdbInfoByIdList(edbInfoIdList)
- if err != nil {
- err = fmt.Errorf("traceEdbInfoByEdbInfoId GetEdbInfoByIdList err: %s", err.Error())
- return
- }
- for _, v := range edbInfoList {
- var dataList []*models.EdbDataBase
- if v.Source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
- dataList, err = models.GetEdbDataBaseMongoByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
- } else if v.Source == utils.DATA_SOURCE_THS && v.SubSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
- dataList, err = models.GetThsHfEdbDataBaseMongoByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
- } else {
- dataList, err = models.GetEdbDataBaseByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
- }
- if err != nil {
- err = fmt.Errorf("查询指标数据失败 Err: %s", err.Error())
- return
- }
- tmp := new(eta_forum.AddEdbDataReq)
- tmp.EdbCode = v.EdbCode
- tmp.EdbType = v.EdbType
- tmp.DataList = dataList
- edbInfoDataList = append(edbInfoDataList, tmp)
- }
- return
- }
- // traceEdbInfoByEdbInfoId 指标追溯
- func traceEdbInfoByEdbInfoId(edbInfoId int, hasFindMap map[int]struct{}, edbInfoIdMap map[int]struct{}, edbMappingList []*models.EdbInfoCalculateMapping, edbMappingMap map[int]struct{}) (newEdbMappingList []*models.EdbInfoCalculateMapping, err error) {
- newEdbMappingList = edbMappingList
- _, ok := hasFindMap[edbInfoId]
- if ok {
- return
- }
- if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
- edbInfoIdMap[edbInfoId] = struct{}{}
- }
- edbInfoMappingList, e := models.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
- if e != nil {
- err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", e.Error())
- return
- }
- if len(edbInfoMappingList) > 0 {
- fromEdbInfoIdList := make([]int, 0)
- for _, v := range edbInfoMappingList {
- fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
- if _, ok1 := edbInfoIdMap[v.FromEdbInfoId]; !ok1 {
- edbInfoIdMap[v.FromEdbInfoId] = struct{}{}
- }
- if _, ok2 := edbMappingMap[v.EdbInfoCalculateMappingId]; !ok2 {
- edbMappingMap[v.EdbInfoCalculateMappingId] = struct{}{}
- tmp := &models.EdbInfoCalculateMapping{
- EdbInfoCalculateMappingId: v.EdbInfoCalculateMappingId,
- EdbInfoId: v.EdbInfoId,
- Source: v.Source,
- SourceName: v.SourceName,
- EdbCode: v.EdbCode,
- FromEdbInfoId: v.FromEdbInfoId,
- FromEdbCode: v.FromEdbCode,
- FromEdbName: v.FromEdbName,
- FromSource: v.FromSource,
- FromSourceName: v.FromSourceName,
- FromTag: v.FromTag,
- Sort: v.Sort,
- CreateTime: v.CreateTime,
- ModifyTime: v.ModifyTime,
- }
- newEdbMappingList = append(newEdbMappingList, tmp)
- }
- if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
- // 查过了就不查了
- if _, ok2 := hasFindMap[v.FromEdbInfoId]; !ok2 {
- newEdbMappingList, e = traceEdbInfoByEdbInfoId(v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap)
- if e != nil {
- err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", e.Error())
- return
- }
- }
- }
- hasFindMap[v.FromEdbInfoId] = struct{}{}
- }
- }
- hasFindMap[edbInfoId] = struct{}{}
- return
- }
|