123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931 |
- package services
- import (
- "encoding/json"
- "eta/eta_task/models/data_manage"
- "eta/eta_task/models/data_manage/cross_variety"
- "eta/eta_task/models/fe_calendar"
- "eta/eta_task/models/sandbox"
- "eta/eta_task/services/alarm_msg"
- "eta/eta_task/utils"
- "fmt"
- "time"
- )
- func InitEdbRelation() {
- //InitChartEdbRelation()
- //InitChartCrossVariety()
- //initCalendarIndicatorRelation()
- initSandBoxEdbRelation()
- }
- func InitChartEdbRelation() {
- fmt.Println("开始处理图表中的指标引用")
- var err error
- var addNum int
- defer func() {
- if err != nil {
- msg := fmt.Sprintf("初始化指标在图表中的引用失败 InitChartEdbRelation err: %v", err)
- utils.FileLog.Info(msg)
- fmt.Println(msg)
- go alarm_msg.SendAlarmMsg(msg, 3)
- }
- }()
- //查询chart_edb_mapping 表
- total, err := data_manage.GetChartEdbMappingTotal()
- if err != nil {
- err = fmt.Errorf("查询图表关联指标失败 err: %v", err)
- return
- }
- if total == 0 {
- return
- }
- //分页查询,每次处理500条记录
- pageSize := 500
- totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
- addList := make([]*data_manage.EdbInfoRelation, 0)
- //查询图表列表
- for i := 0; i < totalPage; i += 1 {
- startSize := i * pageSize
- list, e := data_manage.GetChartEdbMappingList(startSize, pageSize)
- if e != nil {
- err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
- return
- }
- if len(list) == 0 {
- break
- }
- edbInfoIds := make([]int, 0)
- for _, v := range list {
- edbInfoIds = append(edbInfoIds, v.EdbInfoId)
- }
- // 查询指标信息表
- edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
- if e != nil {
- err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
- return
- }
- if len(edbInfoList) == 0 {
- continue
- }
- // 查询计算指标信息,并且建立关联关系
- // 查询间接引用的指标信息
- calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
- if e != nil {
- err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
- return
- }
- // 查询指标间接引用
- edbInfoMap := make(map[int]*data_manage.EdbInfo)
- for _, v := range edbInfoList {
- edbInfoMap[v.EdbInfoId] = v
- }
- // 筛选有用的图表
- finalList := make([]*data_manage.ChartEdbMapping, 0)
- chartIds := make([]int, 0)
- for _, v := range list {
- if _, ok2 := edbInfoMap[v.EdbInfoId]; !ok2 {
- continue
- }
- finalList = append(finalList, v)
- chartIds = append(chartIds, v.ChartInfoId)
- }
- if len(chartIds) == 0 {
- continue
- }
- // 查询图表信息
- chartInfoList, e := data_manage.GetChartInfoByChartInfoIds(chartIds)
- if e != nil {
- err = fmt.Errorf("查询图表信息列表失败 Err:%s", e)
- return
- }
- chartInfoMap := make(map[int]*data_manage.ChartInfo)
- for _, v := range chartInfoList {
- chartInfoMap[v.ChartInfoId] = v
- }
- //查询引用关系列表,
- chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(chartIds, utils.EDB_RELATION_CHART)
- if e != nil {
- err = fmt.Errorf("查询图表引用关系列表失败 Err:%s", e)
- return
- }
- existRelationMap := make(map[string]struct{})
- for _, v := range chartEdbRelationList {
- name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
- existRelationMap[name] = struct{}{}
- }
- for _, v := range finalList {
- nowTime := time.Now()
- name := fmt.Sprintf("%d-%d", v.ChartInfoId, v.EdbInfoId)
- if _, ok := existRelationMap[name]; !ok {
- //查询图表信息
- chartInfo, ok1 := chartInfoMap[v.ChartInfoId]
- if !ok1 {
- continue
- }
- edbInfo, ok2 := edbInfoMap[v.EdbInfoId]
- if !ok2 {
- continue
- }
- //todo 引用时间
- tmp := &data_manage.EdbInfoRelation{
- ReferObjectId: v.ChartInfoId,
- ReferObjectType: utils.EDB_RELATION_CHART,
- ReferObjectSubType: chartInfo.Source,
- EdbInfoId: v.EdbInfoId,
- EdbName: edbInfo.EdbName,
- Source: edbInfo.Source,
- EdbCode: edbInfo.EdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- }
- addList = append(addList, tmp)
- existRelationMap[name] = struct{}{}
- // 添加间接引用记录
- if edbInfo.EdbType == 2 {
- childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
- if !ok1 {
- continue
- }
- for _, childEdbMappingId := range childEdbMappingIds {
- childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
- if !ok2 {
- continue
- }
- childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
- if !ok2 {
- continue
- }
- tmp1 := &data_manage.EdbInfoRelation{
- ReferObjectId: v.ChartInfoId,
- ReferObjectType: utils.EDB_RELATION_CHART,
- ReferObjectSubType: chartInfo.Source,
- EdbInfoId: childEdb.EdbInfoId,
- EdbName: childEdb.EdbName,
- Source: childEdb.Source,
- EdbCode: childEdb.EdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- RelationType: 1,
- RootEdbInfoId: edbInfo.EdbInfoId,
- ChildEdbInfoId: childEdbMapping.EdbInfoId,
- }
- addList = append(addList, tmp1)
- // todo 防止重复
- }
- }
- if len(addList) > pageSize {
- err = data_manage.AddEdbInfoRelationMulti(addList)
- if err != nil {
- err = fmt.Errorf("新增引用记录失败 Err:%s", err)
- return
- }
- addNum += len(addList)
- addList = make([]*data_manage.EdbInfoRelation, 0)
- }
- }
- }
- }
- //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
- if len(addList) > 0 {
- err = data_manage.AddEdbInfoRelationMulti(addList)
- if err != nil {
- err = fmt.Errorf("新增引用记录失败 Err:%s", err)
- return
- }
- addNum += len(addList)
- }
- fmt.Printf("图表指标引用记录处理完成, 新增%d条记录\n", addNum)
- return
- }
- // InitChartCrossVariety 处理特殊图表,跨品种分析图表
- func InitChartCrossVariety() {
- fmt.Println("开始跨品种分析图表中的指标引用")
- var addNum int
- var err error
- defer func() {
- if err != nil {
- msg := fmt.Sprintf("初始化指标在跨品种分析图表中的引用失败 InitChartCrossVariety err: %v", err)
- utils.FileLog.Info(msg)
- fmt.Println(msg)
- go alarm_msg.SendAlarmMsg(msg, 3)
- }
- }()
- total, err := cross_variety.GetChartInfoCrossVarietyTotal()
- if err != nil {
- err = fmt.Errorf("查询图表关联指标失败 err: %v", err)
- return
- }
- if total == 0 {
- return
- }
- //分页查询,每次处理500条记录
- pageSize := 500
- totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
- addList := make([]*data_manage.EdbInfoRelation, 0)
- //查询图表列表
- for i := 0; i < totalPage; i += 1 {
- startSize := i * pageSize
- list, e := cross_variety.GetChartInfoCrossVarietyList(startSize, pageSize)
- if e != nil {
- err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
- return
- }
- if len(list) == 0 {
- break
- }
- chartIds := make([]int, 0)
- tagIds := make([]int, 0)
- tagIdsMap := make(map[int]struct{})
- tagChartMap := make(map[int][]*cross_variety.ChartInfoCrossVariety)
- for _, v := range list {
- if _, ok := tagIdsMap[v.ChartXTagId]; !ok {
- tagIds = append(tagIds, v.ChartXTagId)
- tagIdsMap[v.ChartXTagId] = struct{}{}
- }
- if _, ok := tagIdsMap[v.ChartYTagId]; !ok {
- tagIds = append(tagIds, v.ChartYTagId)
- tagIdsMap[v.ChartYTagId] = struct{}{}
- }
- if chartCross, ok := tagChartMap[v.ChartXTagId]; ok {
- chartCross = append(chartCross, v)
- tagChartMap[v.ChartXTagId] = chartCross
- } else {
- chartCross = make([]*cross_variety.ChartInfoCrossVariety, 0)
- chartCross = append(chartCross, v)
- tagChartMap[v.ChartXTagId] = chartCross
- }
- if chartCross, ok := tagChartMap[v.ChartYTagId]; ok {
- chartCross = append(chartCross, v)
- tagChartMap[v.ChartYTagId] = chartCross
- } else {
- chartCross = make([]*cross_variety.ChartInfoCrossVariety, 0)
- chartCross = append(chartCross, v)
- tagChartMap[v.ChartYTagId] = chartCross
- }
- }
- chartTagVarietyList, e := cross_variety.GetChartTagVarietyEdbInfoIdsByTagIds(tagIds)
- if e != nil {
- err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
- return
- }
- edbInfoIds := make([]int, 0)
- chartTagVarietyMap := make(map[int][]*cross_variety.ChartTagVariety)
- for _, v := range chartTagVarietyList {
- if tagList, ok := chartTagVarietyMap[v.EdbInfoId]; ok {
- tagList = append(tagList, v)
- chartTagVarietyMap[v.EdbInfoId] = tagList
- } else {
- tagList = make([]*cross_variety.ChartTagVariety, 0)
- tagList = append(tagList, v)
- chartTagVarietyMap[v.EdbInfoId] = tagList
- }
- edbInfoIds = append(edbInfoIds, v.EdbInfoId)
- }
- // 查询指标信息表
- edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
- if e != nil {
- err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
- return
- }
- if len(edbInfoList) == 0 {
- continue
- }
- // 查询计算指标信息,并且建立关联关系
- // 查询间接引用的指标信息
- calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
- if e != nil {
- err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
- return
- }
- edbInfoMap := make(map[int]*data_manage.EdbInfo)
- chartInfoCrossMap := make(map[int]struct{})
- chartInfoCrossList := make([]*cross_variety.ChartInfoCrossVariety, 0)
- edbCrossMap := make(map[int][]*cross_variety.ChartInfoCrossVariety)
- for _, v := range edbInfoList {
- edbInfoMap[v.EdbInfoId] = v
- if tagList, ok := chartTagVarietyMap[v.EdbInfoId]; ok {
- for _, tag := range tagList {
- if chartCross, ok2 := tagChartMap[tag.ChartTagId]; ok2 {
- for _, crossItem := range chartCross {
- if _, ok3 := chartInfoCrossMap[crossItem.ChartInfoId]; !ok3 {
- chartInfoCrossMap[crossItem.ChartInfoId] = struct{}{}
- chartInfoCrossList = append(chartInfoCrossList, crossItem)
- chartIds = append(chartIds, crossItem.ChartInfoId)
- }
- }
- }
- }
- }
- edbCrossMap[v.EdbInfoId] = chartInfoCrossList
- chartInfoCrossMap = make(map[int]struct{})
- chartInfoCrossList = make([]*cross_variety.ChartInfoCrossVariety, 0)
- }
- /*// 查询图表信息
- chartInfoList, e := data_manage.GetChartInfoByChartInfoIds(chartIds)
- if e != nil {
- err = fmt.Errorf("查询图表信息列表失败 Err:%s", e)
- return
- }
- chartInfoMap := make(map[int]*data_manage.ChartInfo)
- for _, v := range chartInfoList {
- chartInfoMap[v.ChartInfoId] = v
- }
- */
- //查询引用关系列表,
- chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(chartIds, utils.EDB_RELATION_CHART)
- if e != nil {
- err = fmt.Errorf("查询图表引用关系列表失败 Err:%s", e)
- return
- }
- existRelationMap := make(map[string]struct{})
- for _, v := range chartEdbRelationList {
- name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
- existRelationMap[name] = struct{}{}
- }
- for edbInfoId, chartCrossList := range edbCrossMap {
- nowTime := time.Now()
- for _, item := range chartCrossList {
- name := fmt.Sprintf("%d-%d", item.ChartInfoId, edbInfoId)
- if _, ok1 := existRelationMap[name]; !ok1 {
- edbInfo, ok2 := edbInfoMap[edbInfoId]
- if !ok2 {
- continue
- }
- //todo 引用时间
- tmp := &data_manage.EdbInfoRelation{
- ReferObjectId: item.ChartInfoId,
- ReferObjectType: utils.EDB_RELATION_CHART,
- ReferObjectSubType: utils.CHART_SOURCE_CROSS_HEDGING,
- EdbInfoId: edbInfoId,
- EdbName: edbInfo.EdbName,
- Source: edbInfo.Source,
- EdbCode: edbInfo.EdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: item.CreateTime,
- }
- addList = append(addList, tmp)
- existRelationMap[name] = struct{}{}
- // 添加间接引用记录
- if edbInfo.EdbType == 2 {
- childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
- if !ok1 {
- continue
- }
- for _, childEdbMappingId := range childEdbMappingIds {
- childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
- if !ok2 {
- continue
- }
- childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
- if !ok2 {
- continue
- }
- tmp1 := &data_manage.EdbInfoRelation{
- ReferObjectId: item.ChartInfoId,
- ReferObjectType: utils.EDB_RELATION_CHART,
- ReferObjectSubType: utils.CHART_SOURCE_CROSS_HEDGING,
- EdbInfoId: childEdb.EdbInfoId,
- EdbName: childEdb.EdbName,
- Source: childEdb.Source,
- EdbCode: childEdb.EdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: item.CreateTime,
- RelationType: 1,
- RootEdbInfoId: edbInfo.EdbInfoId,
- ChildEdbInfoId: childEdbMapping.EdbInfoId,
- }
- addList = append(addList, tmp1)
- // todo 防止重复
- }
- }
- if len(addList) > pageSize {
- err = data_manage.AddEdbInfoRelationMulti(addList)
- if err != nil {
- err = fmt.Errorf("新增引用记录失败 Err:%s", err)
- return
- }
- addNum += len(addList)
- addList = make([]*data_manage.EdbInfoRelation, 0)
- }
- }
- }
- }
- }
- //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
- if len(addList) > 0 {
- err = data_manage.AddEdbInfoRelationMulti(addList)
- if err != nil {
- err = fmt.Errorf("新增引用记录失败 Err:%s", err)
- return
- }
- addNum += len(addList)
- }
- fmt.Printf("跨品种分析图表指标引用记录处理完成, 新增%d条记录\n", addNum)
- return
- }
- // 初始化事件日历中的指标引用
- func initCalendarIndicatorRelation() {
- fmt.Println("开始处理事件日历中的指标引用")
- var addNum int
- var err error
- defer func() {
- if err != nil {
- msg := fmt.Sprintf("初始化指标在事件日历中的引用失败 initCalendarIndicatorRelation err: %v", err)
- utils.FileLog.Info(msg)
- fmt.Println(msg)
- go alarm_msg.SendAlarmMsg(msg, 3)
- }
- }()
- //查询chart_edb_mapping 表
- obj := new(fe_calendar.FeCalendarMatter)
- condition := " AND edb_info_id > 0"
- total, err := obj.GetCountByCondition(condition, []interface{}{})
- if err != nil {
- err = fmt.Errorf("查询事件日历关联指标失败 err: %v", err)
- return
- }
- if total == 0 {
- return
- }
- //分页查询,每次处理500条记录
- pageSize := 500
- totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
- addList := make([]*data_manage.EdbInfoRelation, 0)
- //查询图表列表
- for i := 0; i < totalPage; i += 1 {
- startSize := i * pageSize
- list, e := obj.GetPageItemsByCondition(condition, []interface{}{}, []string{}, "", startSize, pageSize)
- if e != nil {
- err = fmt.Errorf("查询事件日历关联指标列表失败 Err:%s", e)
- return
- }
- if len(list) == 0 {
- break
- }
- edbInfoIds := make([]int, 0)
- edbInfoMatterMap := make(map[int][]*fe_calendar.FeCalendarMatter)
- for _, v := range list {
- edbInfoIds = append(edbInfoIds, v.EdbInfoId)
- items, ok := edbInfoMatterMap[v.EdbInfoId]
- if ok {
- items = append(items, v)
- edbInfoMatterMap[v.EdbInfoId] = items
- } else {
- items = make([]*fe_calendar.FeCalendarMatter, 0)
- items = append(items, v)
- edbInfoMatterMap[v.EdbInfoId] = items
- }
- }
- // 查询指标信息表
- edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
- if e != nil {
- err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
- return
- }
- if len(edbInfoList) == 0 {
- continue
- }
- // 查询计算指标信息,并且建立关联关系
- // 查询间接引用的指标信息
- calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
- if e != nil {
- err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
- return
- }
- edbInfoMap := make(map[int]*data_manage.EdbInfo)
- matterIds := make([]int, 0)
- for _, v := range edbInfoList {
- edbInfoMap[v.EdbInfoId] = v
- items, ok := edbInfoMatterMap[v.EdbInfoId]
- if ok {
- for _, item := range items {
- matterIds = append(matterIds, item.FeCalendarMatterId)
- }
- }
- }
- //查询引用关系列表,
- chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(matterIds, utils.EDB_RELATION_CALENDAR)
- if e != nil {
- err = fmt.Errorf("查询图表引用关系列表失败 Err:%s", e)
- return
- }
- existRelationMap := make(map[string]struct{})
- for _, v := range chartEdbRelationList {
- name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
- existRelationMap[name] = struct{}{}
- }
- for edbInfoId, edbInfo := range edbInfoMap {
- nowTime := time.Now()
- items, ok := edbInfoMatterMap[edbInfoId]
- if ok {
- for _, v := range items {
- name := fmt.Sprintf("%d-%d", v.FeCalendarMatterId, v.EdbInfoId)
- if _, ok1 := existRelationMap[name]; !ok1 {
- //todo 引用时间
- tmp := &data_manage.EdbInfoRelation{
- ReferObjectId: v.FeCalendarMatterId,
- ReferObjectType: utils.EDB_RELATION_CALENDAR,
- EdbInfoId: v.EdbInfoId,
- EdbName: edbInfo.EdbName,
- Source: edbInfo.Source,
- EdbCode: edbInfo.EdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- }
- addList = append(addList, tmp)
- existRelationMap[name] = struct{}{}
- // 添加间接引用记录
- if edbInfo.EdbType == 2 {
- childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
- if !ok1 {
- continue
- }
- for _, childEdbMappingId := range childEdbMappingIds {
- childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
- if !ok2 {
- continue
- }
- childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
- if !ok2 {
- continue
- }
- tmp1 := &data_manage.EdbInfoRelation{
- ReferObjectId: v.FeCalendarMatterId,
- ReferObjectType: utils.EDB_RELATION_CALENDAR,
- EdbInfoId: childEdb.EdbInfoId,
- EdbName: childEdb.EdbName,
- Source: childEdb.Source,
- EdbCode: childEdb.EdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- RelationType: 1,
- RootEdbInfoId: edbInfo.EdbInfoId,
- ChildEdbInfoId: childEdbMapping.EdbInfoId,
- }
- addList = append(addList, tmp1)
- // todo 防止重复
- }
- }
- if len(addList) > pageSize {
- err = data_manage.AddEdbInfoRelationMulti(addList)
- if err != nil {
- err = fmt.Errorf("新增引用记录失败 Err:%s", err)
- return
- }
- addNum += len(addList)
- addList = make([]*data_manage.EdbInfoRelation, 0)
- }
- }
- }
- }
- }
- }
- //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
- if len(addList) > 0 {
- err = data_manage.AddEdbInfoRelationMulti(addList)
- if err != nil {
- err = fmt.Errorf("新增引用记录失败 Err:%s", err)
- return
- }
- addNum += len(addList)
- }
- fmt.Printf("事件日历指标引用记录处理完成, 新增%d条记录\n", addNum)
- return
- }
- // 处理逻辑图中的指标引用
- func initSandBoxEdbRelation() {
- fmt.Println("开始处理逻辑图中的指标引用")
- var err error
- var addNum int
- defer func() {
- if err != nil {
- msg := fmt.Sprintf("初始化指标在逻辑图中的引用失败 initSandBoxEdbRelation err: %v", err)
- utils.FileLog.Info(msg)
- fmt.Println(msg)
- go alarm_msg.SendAlarmMsg(msg, 3)
- }
- }()
- condition := " AND is_delete = 0"
- total, err := sandbox.GetSandboxListCountByCondition(condition, []interface{}{})
- if err != nil {
- err = fmt.Errorf("查询逻辑图总数失败 err: %v", err)
- return
- }
- if total == 0 {
- return
- }
- //分页查询,每次处理500条记录
- pageSize := 100
- totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
- addList := make([]*data_manage.EdbInfoRelation, 0)
- //查询图表列表
- for i := 0; i < totalPage; i += 1 {
- startSize := i * pageSize
- list, e := sandbox.GetSandboxListByCondition(condition, []interface{}{}, startSize, pageSize)
- if e != nil {
- err = fmt.Errorf("查询逻辑图列表失败 Err:%s", e)
- return
- }
- if len(list) == 0 {
- break
- }
- edbInfoIds := make([]int, 0)
- edbSandboxMap := make(map[int][]*sandbox.Sandbox)
- for _, v := range list {
- if v.Content == "" {
- continue
- }
- edbInfoIdsTmp, e := getSandBoxEdbIdsByContent(v.Content)
- if e != nil {
- continue
- //err = fmt.Errorf("查询逻辑图关联的指标Id失败 Err:%s", e)
- //return
- }
- for _, edbId := range edbInfoIdsTmp {
- edbInfoIds = append(edbInfoIds, edbId)
- edbSandboxMap[edbId] = append(edbSandboxMap[edbId], v)
- }
- }
- if len(edbInfoIds) <= 0 {
- continue
- }
- // 查询指标信息表
- edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
- if e != nil {
- err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
- return
- }
- if len(edbInfoList) == 0 {
- continue
- }
- // 查询计算指标信息,并且建立关联关系
- // 查询间接引用的指标信息
- calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
- if e != nil {
- err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
- return
- }
- edbInfoMap := make(map[int]*data_manage.EdbInfo)
- sandboxIds := make([]int, 0)
- for _, v := range edbInfoList {
- edbInfoMap[v.EdbInfoId] = v
- if items, ok := edbSandboxMap[v.EdbInfoId]; ok {
- for _, item := range items {
- sandboxIds = append(sandboxIds, item.SandboxId)
- }
- }
- }
- //查询引用关系列表,
- chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(sandboxIds, utils.EDB_RELATION_SANDBOX)
- if e != nil {
- err = fmt.Errorf("查询逻辑图引用关系列表失败 Err:%s", e)
- return
- }
- existRelationMap := make(map[string]struct{})
- for _, v := range chartEdbRelationList {
- name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
- existRelationMap[name] = struct{}{}
- }
- for edbInfoId, sandboxList := range edbSandboxMap {
- nowTime := time.Now()
- for _, v := range sandboxList {
- name := fmt.Sprintf("%d-%d", v.SandboxId, edbInfoId)
- if _, ok := existRelationMap[name]; !ok {
- edbInfo, ok2 := edbInfoMap[edbInfoId]
- if !ok2 {
- continue
- }
- //todo 引用时间
- tmp := &data_manage.EdbInfoRelation{
- ReferObjectId: v.SandboxId,
- ReferObjectType: utils.EDB_RELATION_SANDBOX,
- EdbInfoId: edbInfoId,
- EdbName: edbInfo.EdbName,
- Source: edbInfo.Source,
- EdbCode: edbInfo.EdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- }
- addList = append(addList, tmp)
- existRelationMap[name] = struct{}{}
- // 添加间接引用记录
- if edbInfo.EdbType == 2 {
- childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
- if !ok1 {
- continue
- }
- for _, childEdbMappingId := range childEdbMappingIds {
- childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
- if !ok2 {
- continue
- }
- childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
- if !ok2 {
- continue
- }
- tmp1 := &data_manage.EdbInfoRelation{
- ReferObjectId: v.SandboxId,
- ReferObjectType: utils.EDB_RELATION_SANDBOX,
- EdbInfoId: childEdb.EdbInfoId,
- EdbName: childEdb.EdbName,
- Source: childEdb.Source,
- EdbCode: childEdb.EdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- RelationType: 1,
- RootEdbInfoId: edbInfo.EdbInfoId,
- ChildEdbInfoId: childEdbMapping.EdbInfoId,
- }
- addList = append(addList, tmp1)
- // todo 防止重复
- }
- }
- if len(addList) > pageSize {
- err = data_manage.AddEdbInfoRelationMulti(addList)
- if err != nil {
- err = fmt.Errorf("新增引用记录失败 Err:%s", err)
- return
- }
- addNum += len(addList)
- addList = make([]*data_manage.EdbInfoRelation, 0)
- }
- }
- }
- }
- }
- //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
- if len(addList) > 0 {
- err = data_manage.AddEdbInfoRelationMulti(addList)
- if err != nil {
- err = fmt.Errorf("新增引用记录失败 Err:%s", err)
- return
- }
- addNum += len(addList)
- }
- fmt.Printf("逻辑图指标引用记录处理完成, 新增%d条记录\n", addNum)
- return
- }
- func getSandBoxEdbIdsByContent(content string) (edbInfoIds []int, err error) {
- var contentInfo sandbox.ContentDataStruct
- err = json.Unmarshal([]byte(content), &contentInfo)
- if err != nil {
- err = fmt.Errorf("json.Unmarshal err:%s", err.Error())
- return
- }
- // 遍历所有节点
- for _, node := range contentInfo.Cells {
- if node.Data == nil {
- continue
- }
- for _, v := range node.Data.LinkData {
- if v.Type == 1 {
- edbInfoIds = append(edbInfoIds, v.Id)
- }
- }
- }
- return
- }
- func GetEdbListByEdbInfoId(edbInfoList []*data_manage.EdbInfo) (edbInfoMap map[int]*data_manage.EdbInfo, edbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping, edbInfoMappingRootIdsMap map[int][]int, err error) {
- //查询指标信息
- //查询指标映射
- //查询所有指标数据
- //查询这个指标相关的mapping信息放到数组里,
- //将得到的指标ID信息放到数组里
- hasFindMap := make(map[int]struct{})
- edbInfoIdMap := make(map[int]struct{})
- edbMappingList := make([]*data_manage.EdbInfoCalculateMapping, 0)
- edbInfoMappingRootIdsMap = make(map[int][]int, 0)
- edbMappingMap := make(map[int]struct{})
- for _, edbInfo := range edbInfoList {
- if edbInfo.EdbType == 2 {
- edbInfoId := edbInfo.EdbInfoId
- edbMappingList, err = getCalculateEdbInfoByEdbInfoId(edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId)
- if err != nil {
- err = fmt.Errorf(" GetCalculateEdbInfoByEdbInfoId err: %s", err.Error())
- return
- }
- }
- }
- if len(edbMappingList) == 0 {
- return
- }
- // 查询指标信息
- // 指标信息map
- edbInfoIdList := make([]int, 0)
- for k, _ := range edbInfoIdMap {
- edbInfoIdList = append(edbInfoIdList, k)
- }
- edbInfoMap = make(map[int]*data_manage.EdbInfo)
- edbMappingListMap = make(map[int]*data_manage.EdbInfoCalculateMapping)
- if len(edbInfoIdList) > 0 {
- edbInfoList, err = data_manage.GetEdbInfoByIdList(edbInfoIdList)
- if err != nil {
- err = fmt.Errorf(" GetEdbInfoByIdList err: %s", err.Error())
- return
- }
- for _, v := range edbInfoList {
- edbInfoMap[v.EdbInfoId] = v
- }
- }
- if len(edbMappingList) > 0 {
- for _, v := range edbMappingList {
- edbMappingListMap[v.EdbInfoCalculateMappingId] = v
- }
- }
- return
- }
- // getCalculateEdbInfoByEdbInfoId 计算指标追溯
- func getCalculateEdbInfoByEdbInfoId(edbInfoId int, hasFindMap map[int]struct{}, edbInfoIdMap map[int]struct{}, edbMappingList []*data_manage.EdbInfoCalculateMapping, edbMappingMap map[int]struct{}, edbInfoMappingRootIdsMap map[int][]int, rootEdbInfoId int) (newEdbMappingList []*data_manage.EdbInfoCalculateMapping, err error) {
- newEdbMappingList = edbMappingList
- _, ok := hasFindMap[edbInfoId]
- if ok {
- return
- }
- if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
- edbInfoIdMap[edbInfoId] = struct{}{}
- }
- edbInfoMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
- if e != nil {
- err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", e.Error())
- return
- }
- if len(edbInfoMappingList) > 0 {
- fromEdbInfoIdList := make([]int, 0)
- edbInfoMappingIdList := make([]int, 0)
- for _, v := range edbInfoMappingList {
- fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
- edbInfoMappingIdList = append(edbInfoMappingIdList, v.EdbInfoCalculateMappingId)
- if _, ok1 := edbInfoIdMap[v.FromEdbInfoId]; !ok1 {
- edbInfoIdMap[v.FromEdbInfoId] = struct{}{}
- }
- if _, ok2 := edbMappingMap[v.EdbInfoCalculateMappingId]; !ok2 {
- edbMappingMap[v.EdbInfoCalculateMappingId] = struct{}{}
- tmp := &data_manage.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 = getCalculateEdbInfoByEdbInfoId(v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, rootEdbInfoId)
- if e != nil {
- err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", e.Error())
- return
- }
- }
- }
- hasFindMap[v.FromEdbInfoId] = struct{}{}
- }
- edbInfoMappingRootIdsMap[rootEdbInfoId] = append(edbInfoMappingRootIdsMap[rootEdbInfoId], edbInfoMappingIdList...)
- }
- hasFindMap[edbInfoId] = struct{}{}
- return
- }
|