12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370 |
- package services
- import (
- "encoding/json"
- "eta/eta_task/models/data_manage"
- "eta/eta_task/models/data_manage/cross_variety"
- "eta/eta_task/models/data_manage/excel"
- "eta/eta_task/models/fe_calendar"
- "eta/eta_task/models/sandbox"
- "eta/eta_task/services/alarm_msg"
- "eta/eta_task/utils"
- "fmt"
- "time"
- )
- 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
- }
- // 查询计算指标信息,并且建立关联关系
- // 查询间接引用的指标信息
- calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList, false)
- 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
- }
- if chartInfo.Source == utils.CHART_SOURCE_CROSS_HEDGING { //过滤掉跨品种分析的图表
- continue
- }
- edbInfo, ok2 := edbInfoMap[v.EdbInfoId]
- if !ok2 {
- continue
- }
- // 去掉预测指标
- if edbInfo.EdbInfoType == 1 {
- continue
- }
- 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,
- }
- tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
- addList = append(addList, tmp)
- existRelationMap[name] = struct{}{}
- // 添加间接引用记录
- if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
- childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
- if !ok1 {
- continue
- }
- for _, childEdbMappingId := range childEdbMappingIds {
- childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
- if !ok2 {
- continue
- }
- name1 := fmt.Sprintf("%d-%d", v.ChartInfoId, childEdbMapping.FromEdbInfoId)
- if _, ok2 := existRelationMap[name1]; ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
- continue
- }
- tmp1 := &data_manage.EdbInfoRelation{
- ReferObjectId: v.ChartInfoId,
- ReferObjectType: utils.EDB_RELATION_CHART,
- ReferObjectSubType: chartInfo.Source,
- EdbInfoId: childEdbMapping.FromEdbInfoId,
- EdbName: childEdbMapping.FromEdbName,
- Source: childEdbMapping.FromSource,
- EdbCode: childEdbMapping.FromEdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- RelationType: 1,
- RootEdbInfoId: edbInfo.EdbInfoId,
- ChildEdbInfoId: childEdbMapping.EdbInfoId,
- RelationCode: tmp.RelationCode,
- }
- 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)
- chartIdMap := make(map[int]struct{})
- tagIds := make([]int, 0)
- tagIdsMap := make(map[int]struct{})
- tagChartMap := make(map[int][]*cross_variety.ChartInfoCrossVariety)
- for _, v := range list {
- if _, ok := chartIdMap[v.ChartInfoId]; !ok {
- chartIdMap[v.ChartInfoId] = struct{}{}
- chartIds = append(chartIds, v.ChartInfoId)
- }
- 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
- }
- }
- chartInfoMap := make(map[int]*data_manage.ChartInfo)
- if len(chartIds) > 0 {
- // 查询图表信息
- chartInfoList, e := data_manage.GetChartInfoByChartInfoIds(chartIds)
- if e != nil {
- err = fmt.Errorf("查询图表信息列表失败 Err:%s", e)
- return
- }
- for _, v := range chartInfoList {
- chartInfoMap[v.ChartInfoId] = v
- }
- }
- 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
- }
- // 查询计算指标信息,并且建立关联关系
- // 查询间接引用的指标信息
- calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList, false)
- 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 {
- chartInfo, chartOk := chartInfoMap[crossItem.ChartInfoId]
- if !chartOk { //表示图表不存在
- continue
- }
- // 查询真正有用的指标ID
- var config cross_variety.ChartConfigReq
- e := json.Unmarshal([]byte(chartInfo.ExtraConfig), &config)
- if e != nil {
- continue
- }
- if utils.InArrayByInt(config.VarietyList, tag.ChartVarietyId) {
- chartInfoCrossMap[crossItem.ChartInfoId] = struct{}{}
- chartInfoCrossList = append(chartInfoCrossList, crossItem)
- }
- }
- }
- }
- }
- }
- edbCrossMap[v.EdbInfoId] = chartInfoCrossList
- chartInfoCrossMap = make(map[int]struct{})
- chartInfoCrossList = make([]*cross_variety.ChartInfoCrossVariety, 0)
- }
- //查询引用关系列表,
- 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
- }
- // 去掉预测指标
- if edbInfo.EdbInfoType == 1 {
- continue
- }
- 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,
- }
- tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
- addList = append(addList, tmp)
- existRelationMap[name] = struct{}{}
- // 添加间接引用记录
- if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
- childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
- if !ok1 {
- continue
- }
- for _, childEdbMappingId := range childEdbMappingIds {
- childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
- if !ok2 {
- continue
- }
- name1 := fmt.Sprintf("%d-%d", item.ChartInfoId, childEdbMapping.FromEdbInfoId)
- if _, ok2 := existRelationMap[name1]; ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
- continue
- }
- tmp1 := &data_manage.EdbInfoRelation{
- ReferObjectId: item.ChartInfoId,
- ReferObjectType: utils.EDB_RELATION_CHART,
- ReferObjectSubType: utils.CHART_SOURCE_CROSS_HEDGING,
- EdbInfoId: childEdbMapping.FromEdbInfoId,
- EdbName: childEdbMapping.FromEdbName,
- Source: childEdbMapping.FromSource,
- EdbCode: childEdbMapping.FromEdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: item.CreateTime,
- RelationType: 1,
- RootEdbInfoId: edbInfo.EdbInfoId,
- ChildEdbInfoId: childEdbMapping.EdbInfoId,
- RelationCode: tmp.RelationCode,
- }
- 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
- }
- // 查询计算指标信息,并且建立关联关系
- // 查询间接引用的指标信息
- calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList, false)
- 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 {
- // 去掉预测指标
- if edbInfo.EdbInfoType == 1 {
- continue
- }
- 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,
- }
- tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
- addList = append(addList, tmp)
- existRelationMap[name] = struct{}{}
- // 添加间接引用记录
- if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
- childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
- if !ok1 {
- continue
- }
- for _, childEdbMappingId := range childEdbMappingIds {
- childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
- if !ok2 {
- continue
- }
- name1 := fmt.Sprintf("%d-%d", v.FeCalendarMatterId, childEdbMapping.FromEdbInfoId)
- if _, ok2 := existRelationMap[name1]; ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
- continue
- }
- tmp1 := &data_manage.EdbInfoRelation{
- ReferObjectId: v.FeCalendarMatterId,
- ReferObjectType: utils.EDB_RELATION_CALENDAR,
- EdbInfoId: childEdbMapping.FromEdbInfoId,
- EdbName: childEdbMapping.FromEdbName,
- Source: childEdbMapping.FromSource,
- EdbCode: childEdbMapping.FromEdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- RelationType: 1,
- RootEdbInfoId: edbInfo.EdbInfoId,
- ChildEdbInfoId: childEdbMapping.EdbInfoId,
- RelationCode: tmp.RelationCode,
- }
- 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 InitExcelEdbRelation() {
- 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)
- }
- }()
- //查询表格指标绑定表
- sources := []int{utils.TIME_TABLE, utils.MIXED_TABLE, utils.BALANCE_TABLE}
- total, err := excel.GetExcelEdbMappingTotalBySource(sources)
- if err != nil {
- err = fmt.Errorf("查询表格关联指标失败 err: %v", err)
- return
- }
- if total == 0 {
- return
- }
- //分页查询,每次处理100条记录
- 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 := excel.GetExcelEdbMappingListBySource(sources, 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
- }
- // 查询计算指标信息,并且建立关联关系
- // 查询间接引用的指标信息
- calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList, false)
- 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
- }
- // 筛选有用的表格
- excelIds := make([]int, 0)
- for _, v := range list {
- excelIds = append(excelIds, v.ExcelInfoId)
- }
- //查询引用关系列表,
- chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(excelIds, utils.EDB_RELATION_TABLE)
- 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 list {
- nowTime := time.Now()
- name := fmt.Sprintf("%d-%d", v.ExcelInfoId, v.EdbInfoId)
- if _, ok := existRelationMap[name]; !ok {
- edbInfo, ok2 := edbInfoMap[v.EdbInfoId]
- if !ok2 {
- continue
- }
- // 去掉预测指标
- if edbInfo.EdbInfoType == 1 {
- continue
- }
- tmp := &data_manage.EdbInfoRelation{
- ReferObjectId: v.ExcelInfoId,
- ReferObjectType: utils.EDB_RELATION_TABLE,
- ReferObjectSubType: v.Source,
- EdbInfoId: v.EdbInfoId,
- EdbName: edbInfo.EdbName,
- Source: edbInfo.Source,
- EdbCode: edbInfo.EdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- }
- tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
- addList = append(addList, tmp)
- existRelationMap[name] = struct{}{}
- // 添加间接引用记录
- if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
- childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
- if !ok1 {
- continue
- }
- for _, childEdbMappingId := range childEdbMappingIds {
- childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
- if !ok2 {
- continue
- }
- name1 := fmt.Sprintf("%d-%d", v.ExcelInfoId, childEdbMapping.FromEdbInfoId)
- if _, ok2 := existRelationMap[name1]; ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
- continue
- }
- tmp1 := &data_manage.EdbInfoRelation{
- ReferObjectId: v.ExcelInfoId,
- ReferObjectType: utils.EDB_RELATION_TABLE,
- ReferObjectSubType: v.Source,
- EdbInfoId: childEdbMapping.FromEdbInfoId,
- EdbName: childEdbMapping.FromEdbName,
- Source: childEdbMapping.FromSource,
- EdbCode: childEdbMapping.FromEdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- RelationType: 1,
- RootEdbInfoId: edbInfo.EdbInfoId,
- ChildEdbInfoId: childEdbMapping.EdbInfoId,
- RelationCode: tmp.RelationCode,
- }
- 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
- }
- // 查询计算指标信息,并且建立关联关系
- // 查询间接引用的指标信息
- calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList, false)
- 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
- }
- // 去掉预测指标
- if edbInfo.EdbInfoType == 1 {
- continue
- }
- 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,
- }
- tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
- addList = append(addList, tmp)
- existRelationMap[name] = struct{}{}
- // 添加间接引用记录
- if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
- childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
- if !ok1 {
- continue
- }
- for _, childEdbMappingId := range childEdbMappingIds {
- childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
- if !ok2 {
- continue
- }
- name1 := fmt.Sprintf("%d-%d", v.SandboxId, childEdbMapping.FromEdbInfoId)
- if _, ok2 := existRelationMap[name1]; ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
- continue
- }
- tmp1 := &data_manage.EdbInfoRelation{
- ReferObjectId: v.SandboxId,
- ReferObjectType: utils.EDB_RELATION_SANDBOX,
- EdbInfoId: childEdbMapping.FromEdbInfoId,
- EdbName: childEdbMapping.FromEdbName,
- Source: childEdbMapping.FromSource,
- EdbCode: childEdbMapping.FromEdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- RelationType: 1,
- RootEdbInfoId: edbInfo.EdbInfoId,
- ChildEdbInfoId: childEdbMapping.EdbInfoId,
- RelationCode: tmp.RelationCode,
- }
- 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, needPredict bool) (edbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping, edbInfoMappingRootIdsMap map[int][]int, err error) {
- if len(edbInfoList) == 0 {
- return
- }
- edbInfoIds := make([]int, 0)
- for _, v := range edbInfoList {
- if (v.EdbType == 2 && v.EdbInfoType == 0) || (v.EdbType == 1 && v.EdbInfoType == 1 && needPredict) { //普通计算指标,或者是基础预测指标
- edbInfoIds = append(edbInfoIds, v.EdbInfoId)
- }
- }
- if len(edbInfoIds) == 0 {
- return
- }
- //查询指标信息
- allEdbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo, 0)
- allMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoIds(edbInfoIds)
- if e != nil {
- err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoIds err: %s", e.Error())
- return
- }
- for _, v := range allMappingList {
- if _, ok := allEdbMappingMap[v.EdbInfoId]; !ok {
- allEdbMappingMap[v.EdbInfoId] = make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
- }
- allEdbMappingMap[v.EdbInfoId] = append(allEdbMappingMap[v.EdbInfoId], v)
- }
- //查询指标映射
- //查询所有指标数据
- //查询这个指标相关的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 && edbInfo.EdbInfoType == 0) || (edbInfo.EdbType == 1 && edbInfo.EdbInfoType == 1 && needPredict) {
- edbInfoId := edbInfo.EdbInfoId
- edbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, 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)
- }
- edbMappingListMap = make(map[int]*data_manage.EdbInfoCalculateMapping)
- if len(edbMappingList) > 0 {
- for _, v := range edbMappingList {
- edbMappingListMap[v.EdbInfoCalculateMappingId] = v
- }
- }
- return
- }
- // getCalculateEdbInfoByEdbInfoId 计算指标追溯
- func getCalculateEdbInfoByEdbInfoId(allEdbMappingMap map[int][]*data_manage.EdbInfoCalculateMappingInfo, 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 := make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
- edbInfoMappingList, ok = allEdbMappingMap[edbInfoId]
- if !ok {
- edbInfoMappingList, err = data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
- if err != nil {
- err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", err.Error())
- return
- }
- }
- hasFindMap[edbInfoId] = struct{}{}
- 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, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, rootEdbInfoId)
- if err != nil {
- err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", err.Error())
- return
- }
- }
- }
- hasFindMap[v.FromEdbInfoId] = struct{}{}
- }
- edbInfoMappingRootIdsMap[rootEdbInfoId] = append(edbInfoMappingRootIdsMap[rootEdbInfoId], edbInfoMappingIdList...)
- }
- return
- }
- // GetCalculateEdbByFromEdbInfo 找到依赖于该基础指标的所有计算指标
- func GetCalculateEdbByFromEdbInfo(edbInfoIds []int, calculateEdbIds []int, hasFind map[int]struct{}) (newCalculateEdbIds []int, err error) {
- if len(edbInfoIds) == 0 {
- return
- }
- newCalculateEdbIds = calculateEdbIds
- newEdbInfoIds := make([]int, 0)
- for _, v := range edbInfoIds {
- if _, ok := hasFind[v]; ok {
- continue
- }
- newEdbInfoIds = append(newEdbInfoIds, v)
- }
- if len(newEdbInfoIds) == 0 {
- return
- }
- var condition string
- var pars []interface{}
- // 关联指标
- condition += ` AND b.from_edb_info_id in (` + utils.GetOrmInReplace(len(newEdbInfoIds)) + `)`
- pars = append(pars, newEdbInfoIds)
- //获取关联图表列表
- list, err := data_manage.GetRelationEdbInfoListMappingByCondition(condition, pars)
- if err != nil && err.Error() != utils.ErrNoRow() {
- err = fmt.Errorf("获取关联指标信息失败,Err:%s", err.Error())
- return
- }
- calculateEdbIdsTmp := make([]int, 0)
- for _, mapping := range list {
- if mapping.EdbType == 2 && mapping.EdbInfoType == 0 { // 如果指标库里的计算指标,则加入,否则继续找
- newCalculateEdbIds = append(newCalculateEdbIds, mapping.EdbInfoId)
- calculateEdbIdsTmp = append(calculateEdbIdsTmp, mapping.EdbInfoId)
- }
- }
- for _, v := range newEdbInfoIds {
- hasFind[v] = struct{}{}
- }
- if len(calculateEdbIdsTmp) > 0 {
- newCalculateEdbIds, err = GetCalculateEdbByFromEdbInfo(calculateEdbIdsTmp, newCalculateEdbIds, hasFind)
- if err != nil {
- return
- }
- }
- return
- }
- // 初始化预测指标中的指标引用
- func InitPredictEdbRelation() {
- fmt.Println("开始处理预测指标中的指标引用")
- var err error
- var addNum int
- defer func() {
- if err != nil {
- msg := fmt.Sprintf("初始化指标在预测指标中的引用失败 InitPredictEdbRelation err: %v", err)
- utils.FileLog.Info(msg)
- fmt.Println(msg)
- go alarm_msg.SendAlarmMsg(msg, 3)
- }
- }()
- //查询基础预测指标总数
- condition := ` AND source = 30 `
- var pars []interface{}
- totalTmp, err := data_manage.GetEdbInfoCalculateMappingCountByCondition(condition, pars)
- if err != nil {
- err = fmt.Errorf("查询表格关联指标失败 err: %v", err)
- return
- }
- total := int(totalTmp)
- if total == 0 {
- return
- }
- //分页查询,每次处理100条记录
- 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 := data_manage.GetEdbInfoCalculateMappingPageByCondition(condition, pars, 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.FromEdbInfoId)
- }
- // 查询指标信息表
- edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
- if e != nil {
- err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
- return
- }
- if len(edbInfoList) == 0 {
- continue
- }
- // 查询计算指标信息,并且建立关联关系
- // 查询间接引用的指标信息
- calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList, true)
- 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
- }
- // 筛选有用的表格
- predictIds := make([]int, 0)
- for _, v := range list {
- predictIds = append(predictIds, v.EdbInfoId)
- }
- //查询引用关系列表,
- edbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(predictIds, utils.EDB_RELATION_PREDICT_EDB)
- if e != nil {
- err = fmt.Errorf("查询表格引用关系列表失败 Err:%s", e)
- return
- }
- existRelationMap := make(map[string]struct{})
- for _, v := range edbRelationList {
- name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
- existRelationMap[name] = struct{}{}
- }
- for _, v := range list {
- nowTime := time.Now()
- name := fmt.Sprintf("%d-%d", v.EdbInfoId, v.FromEdbInfoId)
- if _, ok := existRelationMap[name]; !ok {
- edbInfo, ok2 := edbInfoMap[v.FromEdbInfoId]
- if !ok2 {
- continue
- }
- tmp := &data_manage.EdbInfoRelation{
- ReferObjectId: v.EdbInfoId,
- ReferObjectType: utils.EDB_RELATION_PREDICT_EDB,
- ReferObjectSubType: 1,
- EdbInfoId: v.FromEdbInfoId,
- EdbName: edbInfo.EdbName,
- Source: edbInfo.Source,
- EdbCode: edbInfo.EdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- }
- tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
- addList = append(addList, tmp)
- existRelationMap[name] = struct{}{}
- // 添加间接引用记录
- if (edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0) || (edbInfo.EdbType == 1 && edbInfo.EdbInfoType == 1) {
- childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
- if !ok1 {
- continue
- }
- for _, childEdbMappingId := range childEdbMappingIds {
- childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
- if !ok2 {
- continue
- }
- name1 := fmt.Sprintf("%d-%d", v.EdbInfoId, childEdbMapping.FromEdbInfoId)
- if _, ok2 := existRelationMap[name1]; ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
- continue
- }
- tmp1 := &data_manage.EdbInfoRelation{
- ReferObjectId: v.EdbInfoId,
- ReferObjectType: utils.EDB_RELATION_PREDICT_EDB,
- ReferObjectSubType: 1,
- EdbInfoId: childEdbMapping.FromEdbInfoId,
- EdbName: childEdbMapping.FromEdbName,
- Source: childEdbMapping.FromSource,
- EdbCode: childEdbMapping.FromEdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: v.CreateTime,
- RelationType: 1,
- RootEdbInfoId: edbInfo.EdbInfoId,
- ChildEdbInfoId: childEdbMapping.EdbInfoId,
- RelationCode: tmp.RelationCode,
- }
- 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
- }
|