@@ -87,14 +87,16 @@ func NewInstOperation(client apimachinery.ClientSetInterface, lang language.CCLa
8787
8888// ObjectWithInsts a struct include object msg and insts array
8989type ObjectWithInsts struct {
90+ AssoID int64
91+ instID int64
9092 Object metadata.Object
91- Insts [] mapstr.MapStr
93+ Inst mapstr.MapStr
9294}
9395
9496// ObjectAssoPair a struct include object msg and association
9597type ObjectAssoPair struct {
96- Object metadata.Object
97- AssocName string
98+ Object metadata.Object
99+ AssocNames [] string
98100}
99101
100102// ConditionItem subcondition
@@ -1048,7 +1050,7 @@ func (c *commonInst) findInstTopo(kit *rest.Kit, objID string, instID int64, nee
10481050
10491051 tmpResults := map [string ]* metadata.CommonInstTopo {}
10501052
1051- topoInsts , relation , err := c .getAssociatedObjectWithInsts (kit , objID , instID , needChild )
1053+ topoInsts , err := c .getAssociatedObjectWithInsts (kit , objID , instID , needChild )
10521054 if err != nil {
10531055 return 0 , nil , err
10541056 }
@@ -1066,36 +1068,27 @@ func (c *commonInst) findInstTopo(kit *rest.Kit, objID string, instID int64, nee
10661068 tmpResults [object .ObjectID ] = commonInst
10671069 }
10681070
1069- commonInst .Count = commonInst .Count + len (topoInst .Insts )
1070-
1071- for _ , inst := range topoInst .Insts {
1072-
1073- id , err := inst .Int64 (metadata .GetInstIDFieldByObjID (object .ObjectID ))
1074- if err != nil {
1075- return 0 , nil , err
1076- }
1077-
1078- name , err := inst .String (metadata .GetInstNameFieldName (object .ObjectID ))
1079- if err != nil {
1080- return 0 , nil , err
1081- }
1082-
1083- instAsst := metadata.InstNameAsst {
1084- ID : strconv .Itoa (int (id )),
1085- InstID : id ,
1086- InstName : name ,
1087- ObjectName : object .ObjectName ,
1088- ObjIcon : object .ObjIcon ,
1089- ObjID : object .ObjectID ,
1090- AssoID : relation [id ],
1091- }
1071+ name , err := topoInst .Inst .String (metadata .GetInstNameFieldName (object .ObjectID ))
1072+ if err != nil {
1073+ return 0 , nil , err
1074+ }
10921075
1093- tmpResults [object .ObjectID ].Children = append (tmpResults [object .ObjectID ].Children , instAsst )
1076+ instAsst := metadata.InstNameAsst {
1077+ ID : strconv .Itoa (int (topoInst .instID )),
1078+ InstID : topoInst .instID ,
1079+ InstName : name ,
1080+ ObjectName : object .ObjectName ,
1081+ ObjIcon : object .ObjIcon ,
1082+ ObjID : object .ObjectID ,
1083+ AssoID : topoInst .AssoID ,
10941084 }
1085+
1086+ tmpResults [object .ObjectID ].Children = append (tmpResults [object .ObjectID ].Children , instAsst )
10951087 }
10961088
10971089 results := make ([]* metadata.CommonInstTopo , 0 )
10981090 for _ , subResult := range tmpResults {
1091+ subResult .Count = len (subResult .Children )
10991092 results = append (results , subResult )
11001093 }
11011094
@@ -1443,32 +1436,27 @@ func (c *commonInst) innerHasHost(kit *rest.Kit, moduleIDs []int64) (bool, error
14431436// getAssociatedObjectWithInsts TODO
14441437// GetObjectWithInsts get object with insts, get parent or child depends on needChild
14451438func (c * commonInst ) getAssociatedObjectWithInsts (kit * rest.Kit , objID string , instID int64 , needChild bool ) (
1446- []* ObjectWithInsts , map [int64 ]int64 , error ) {
1447-
1439+ []* ObjectWithInsts , error ) {
14481440 cond := mapstr .New ()
14491441 if needChild {
14501442 cond .Set (common .BKObjIDField , objID )
14511443 } else {
14521444 cond .Set (common .BKAsstObjIDField , objID )
14531445 }
1454-
14551446 objPairs , err := c .searchAssoObjects (kit , needChild , cond )
14561447 if err != nil {
14571448 blog .Errorf ("failed to get the object(%s)'s parent, err: %v, rid: %s" , objID , err , kit .Rid )
1458- return nil , nil , err
1449+ return nil , err
14591450 }
1460-
1461- relation := make (map [int64 ]int64 )
14621451 result := make ([]* ObjectWithInsts , 0 )
14631452 for _ , objPair := range objPairs {
1464-
1453+ tempResult := make ([] * ObjectWithInsts , 0 )
14651454 queryCond := & metadata.InstAsstQueryCondition {
14661455 Cond : metadata.QueryCondition {Condition : mapstr.MapStr {
1467- common .AssociationObjAsstIDField : objPair .AssocName ,
1456+ common .AssociationObjAsstIDField : mapstr. MapStr { common . BKDBIN : objPair .AssocNames } ,
14681457 }},
14691458 ObjID : objPair .Object .ObjectID ,
14701459 }
1471-
14721460 if needChild {
14731461 queryCond .Cond .Condition .Set (common .BKInstIDField , instID )
14741462 queryCond .Cond .Condition .Set (common .BKObjIDField , objID )
@@ -1478,19 +1466,16 @@ func (c *commonInst) getAssociatedObjectWithInsts(kit *rest.Kit, objID string, i
14781466 queryCond .Cond .Condition .Set (common .BKObjIDField , objPair .Object .ObjectID )
14791467 queryCond .Cond .Condition .Set (common .BKAsstObjIDField , objID )
14801468 }
1481-
14821469 rsp , err := c .clientSet .CoreService ().Association ().ReadInstAssociation (kit .Ctx , kit .Header , queryCond )
14831470 if err != nil {
14841471 blog .Errorf ("search inst association failed , err: %v, rid: %s" , err , kit .Rid )
1485- return nil , nil , err
1472+ return nil , err
14861473 }
1487-
14881474 // found no noe inst association with this object and association info.
14891475 // which means that, this object association has not been instantiated.
14901476 if len (rsp .Info ) == 0 {
14911477 continue
14921478 }
1493-
14941479 instIDs := make ([]int64 , 0 )
14951480 for _ , item := range rsp .Info {
14961481 var instID int64
@@ -1499,30 +1484,53 @@ func (c *commonInst) getAssociatedObjectWithInsts(kit *rest.Kit, objID string, i
14991484 } else {
15001485 instID = item .InstID
15011486 }
1502- relation [instID ] = item .ID
15031487 instIDs = append (instIDs , instID )
1488+ tempResult = append (tempResult , & ObjectWithInsts {
1489+ AssoID : item .ID ,
1490+ instID : instID ,
1491+ Object : objPair .Object ,
1492+ })
15041493 }
1505-
1506- innerCond := & metadata.QueryCondition {
1507- Condition : mapstr.MapStr {objPair .Object .GetInstIDFieldName (): mapstr.MapStr {common .BKDBIN : instIDs }},
1508- Fields : []string {common .GetInstIDField (objPair .Object .ObjectID ),
1509- common .GetInstNameField (objPair .Object .ObjectID )},
1494+ rspItems , err := c .findInstsWithObjPair (kit , instIDs , objPair )
1495+ if err != nil {
1496+ return make ([]* ObjectWithInsts , 0 ), err
15101497 }
1511- if objPair .Object .IsCommon () {
1512- innerCond .Condition [common .BKObjIDField ] = objPair .Object .ObjectID
1498+ tempMap := make (map [int64 ]mapstr.MapStr )
1499+ for _ , info := range rspItems .Info {
1500+ id , err := info .Int64 (common .GetInstIDField (objPair .Object .ObjectID ))
1501+ if err != nil {
1502+ return nil , err
1503+ }
1504+ tempMap [id ] = info
15131505 }
1514-
1515- rspItems , err := c .FindInst (kit , objPair .Object .ObjectID , innerCond )
1516- if err != nil {
1517- blog .Errorf ("failed to search the insts by the condition(%#v), err: %v, rid: %s" , innerCond , err , kit .Rid )
1518- return result , nil , err
1506+ for i , inst := range tempResult {
1507+ if value , ok := tempMap [inst .instID ]; ok {
1508+ tempResult [i ].Inst = value
1509+ continue
1510+ }
1511+ return nil , fmt .Errorf ("failed to find the inst (%v) by the condition(%#v)" , inst , queryCond )
15191512 }
1520-
1521- rstObj := & ObjectWithInsts {Object : objPair .Object , Insts : rspItems .Info }
1522- result = append (result , rstObj )
1513+ result = append (result , tempResult ... )
15231514 }
1515+ return result , nil
1516+ }
15241517
1525- return result , relation , nil
1518+ func (c * commonInst ) findInstsWithObjPair (kit * rest.Kit , instIDs []int64 , objPair ObjectAssoPair ) (* metadata.InstResult , error ) {
1519+ instIDs = util .IntArrayUnique (instIDs )
1520+ innerCond := & metadata.QueryCondition {
1521+ Condition : mapstr.MapStr {objPair .Object .GetInstIDFieldName (): mapstr.MapStr {common .BKDBIN : instIDs }},
1522+ Fields : []string {common .GetInstIDField (objPair .Object .ObjectID ),
1523+ common .GetInstNameField (objPair .Object .ObjectID )},
1524+ }
1525+ if objPair .Object .IsCommon () {
1526+ innerCond .Condition [common .BKObjIDField ] = objPair .Object .ObjectID
1527+ }
1528+ rspItems , err := c .FindInst (kit , objPair .Object .ObjectID , innerCond )
1529+ if err != nil {
1530+ blog .Errorf ("failed to search the inst by the condition(%#v), err: %v, rid: %s" , innerCond , err , kit .Rid )
1531+ return nil , err
1532+ }
1533+ return rspItems , nil
15261534}
15271535
15281536func (c * commonInst ) searchAssoObjects (kit * rest.Kit , needChild bool , cond mapstr.MapStr ) ([]ObjectAssoPair ,
@@ -1544,18 +1552,22 @@ func (c *commonInst) searchAssoObjects(kit *rest.Kit, needChild bool, cond mapst
15441552 return make ([]ObjectAssoPair , 0 ), nil
15451553 }
15461554
1547- objAssoMap := make (map [string ]metadata. Association , 0 )
1555+ objAssoMap := make (map [string ][] string )
15481556 var objIDArray []string
15491557 for _ , asst := range rsp .Info {
1558+ targetID := asst .ObjectID
15501559 if needChild {
1551- objIDArray = append (objIDArray , asst .AsstObjID )
1552- objAssoMap [asst .AsstObjID ] = asst
1553- } else {
1554- objIDArray = append (objIDArray , asst .ObjectID )
1555- objAssoMap [asst .ObjectID ] = asst
1560+ targetID = asst .AsstObjID
1561+ }
1562+ objIDArray = append (objIDArray , targetID )
1563+ if len (objAssoMap [targetID ]) == 0 {
1564+ objAssoMap [targetID ] = []string {asst .AssociationName }
1565+ continue
15561566 }
1567+ objAssoMap [targetID ] = append (objAssoMap [targetID ], asst .AssociationName )
15571568 }
15581569
1570+ objIDArray = util .StrArrayUnique (objIDArray )
15591571 queryCond := & metadata.QueryCondition {
15601572 Condition : mapstr.MapStr {metadata .ModelFieldObjectID : mapstr.MapStr {common .BKDBIN : objIDArray }},
15611573 Fields : []string {common .BKObjNameField , common .BKObjIDField , common .BKObjIconField },
@@ -1571,11 +1583,11 @@ func (c *commonInst) searchAssoObjects(kit *rest.Kit, needChild bool, cond mapst
15711583 return make ([]ObjectAssoPair , 0 ), nil
15721584 }
15731585
1574- pair := make ([]ObjectAssoPair , 0 )
1586+ pair := make ([]ObjectAssoPair , 0 , len ( rspRst . Info ) )
15751587 for _ , object := range rspRst .Info {
15761588 pair = append (pair , ObjectAssoPair {
1577- Object : object ,
1578- AssocName : objAssoMap [object .ObjectID ]. AssociationName ,
1589+ Object : object ,
1590+ AssocNames : objAssoMap [object .ObjectID ],
15791591 })
15801592 }
15811593
0 commit comments