@@ -53,8 +53,6 @@ func (md *MongoDatabase) FindExadataInstance(rackID string) (*model.OracleExadat
5353
5454 pipeline := bson.A {
5555 bson.D {{Key : "$match" , Value : bson.D {{Key : "rackID" , Value : rackID }}}},
56- bson.D {{Key : "$unwind" , Value : "$components" }},
57- bson.D {{Key : "$unwind" , Value : "$components.vms" }},
5856 bson.D {
5957 {Key : "$lookup" ,
6058 Value : bson.D {
@@ -68,112 +66,135 @@ func (md *MongoDatabase) FindExadataInstance(rackID string) (*model.OracleExadat
6866 bson.D {
6967 {Key : "$set" ,
7068 Value : bson.D {
71- {Key : "components.vms.clusterName " ,
69+ {Key : "components" ,
7270 Value : bson.D {
73- {Key : "$cond " ,
71+ {Key : "$map " ,
7472 Value : bson.D {
75- {Key : "if" ,
76- Value : bson.D {
77- {Key : "$eq" ,
78- Value : bson.A {
79- bson.D {{Key : "$size" , Value : "$matchedDocument" }},
80- 1 ,
81- },
82- },
83- },
84- },
85- {Key : "then" ,
73+ {Key : "input" , Value : "$components" },
74+ {Key : "as" , Value : "component" },
75+ {Key : "in" ,
8676 Value : bson.D {
87- {Key : "$arrayElemAt " ,
77+ {Key : "$mergeObjects " ,
8878 Value : bson.A {
89- "$matchedDocument.clustername" ,
90- 0 ,
79+ "$$component" ,
80+ bson.D {
81+ {Key : "vms" ,
82+ Value : bson.D {
83+ {Key : "$map" ,
84+ Value : bson.D {
85+ {Key : "input" , Value : "$$component.vms" },
86+ {Key : "as" , Value : "vm" },
87+ {Key : "in" ,
88+ Value : bson.D {
89+ {Key : "$mergeObjects" ,
90+ Value : bson.A {
91+ "$$vm" ,
92+ bson.D {
93+ {Key : "clusterName" ,
94+ Value : bson.D {
95+ {Key : "$let" ,
96+ Value : bson.D {
97+ {Key : "vars" ,
98+ Value : bson.D {
99+ {Key : "matchedDocument" ,
100+ Value : bson.D {
101+ {Key : "$filter" ,
102+ Value : bson.D {
103+ {Key : "input" , Value : "$matchedDocument" },
104+ {Key : "as" , Value : "match" },
105+ {Key : "cond" ,
106+ Value : bson.D {
107+ {Key : "$and" ,
108+ Value : bson.A {
109+ bson.D {
110+ {Key : "$eq" ,
111+ Value : bson.A {
112+ "$$match.instancerackid" ,
113+ "$rackID" ,
114+ },
115+ },
116+ },
117+ bson.D {
118+ {Key : "$eq" ,
119+ Value : bson.A {
120+ "$$match.vmname" ,
121+ "$$vm.name" ,
122+ },
123+ },
124+ },
125+ bson.D {
126+ {Key : "$eq" ,
127+ Value : bson.A {
128+ "$$match.hostid" ,
129+ "$$component.hostID" ,
130+ },
131+ },
132+ },
133+ },
134+ },
135+ },
136+ },
137+ },
138+ },
139+ },
140+ },
141+ },
142+ },
143+ {Key : "in" ,
144+ Value : bson.D {
145+ {Key : "$cond" ,
146+ Value : bson.D {
147+ {Key : "if" ,
148+ Value : bson.D {
149+ {Key : "$gt" ,
150+ Value : bson.A {
151+ bson.D {{Key : "$size" , Value : "$$matchedDocument" }},
152+ 0 ,
153+ },
154+ },
155+ },
156+ },
157+ {Key : "then" ,
158+ Value : bson.D {
159+ {Key : "$arrayElemAt" ,
160+ Value : bson.A {
161+ "$$matchedDocument.clustername" ,
162+ 0 ,
163+ },
164+ },
165+ },
166+ },
167+ {Key : "else" , Value : "" },
168+ },
169+ },
170+ },
171+ },
172+ },
173+ },
174+ },
175+ },
176+ },
177+ },
178+ },
179+ },
180+ },
181+ },
182+ },
183+ },
184+ },
185+ },
91186 },
92187 },
93188 },
94189 },
95- {Key : "else" , Value : "" },
96- },
97- },
98- },
99- },
100- },
101- },
102- },
103- bson.D {
104- {Key : "$group" ,
105- Value : bson.D {
106- {Key : "_id" ,
107- Value : bson.D {
108- {Key : "_id" , Value : "$_id" },
109- {Key : "hostname" , Value : "$hostname" },
110- {Key : "environment" , Value : "$environment" },
111- {Key : "location" , Value : "$location" },
112- {Key : "rackID" , Value : "$rackID" },
113- },
114- },
115- {Key : "components" ,
116- Value : bson.D {
117- {Key : "$push" ,
118- Value : bson.D {
119- {Key : "rackID" , Value : "$components.rackID" },
120- {Key : "hostType" , Value : "$components.hostType" },
121- {Key : "hostname" , Value : "$components.hostname" },
122- {Key : "hostID" , Value : "$components.hostID" },
123- {Key : "cpuEnabled" , Value : "$components.cpuEnabled" },
124- {Key : "totalCPU" , Value : "$components.totalCPU" },
125- {Key : "memory" , Value : "$components.memory" },
126- {Key : "imageVersion" , Value : "$components.imageVersion" },
127- {Key : "kernel" , Value : "$components.kernel" },
128- {Key : "model" , Value : "$components.model" },
129- {Key : "fanUsed" , Value : "$components.fanUsed" },
130- {Key : "fanTotal" , Value : "$components.fanTotal" },
131- {Key : "psuUsed" , Value : "$components.psuUsed" },
132- {Key : "psuTotal" , Value : "$components.psuTotal" },
133- {Key : "msStatus" , Value : "$components.msStatus" },
134- {Key : "rsStatus" , Value : "$components.rsStatus" },
135- {Key : "cellServiceStatus" , Value : "$components.cellServiceStatus" },
136- {Key : "swVersion" , Value : "$components.swVersion" },
137- {Key : "vms" ,
138- Value : bson.A {
139- bson.D {
140- {Key : "type" , Value : "$components.vms.type" },
141- {Key : "physicalHost" , Value : "$components.vms.physicalHost" },
142- {Key : "status" , Value : "$components.vms.status" },
143- {Key : "name" , Value : "$components.vms.name" },
144- {Key : "cpuCurrent" , Value : "$components.vms.cpuCurrent" },
145- {Key : "cpuRestart" , Value : "$components.vms.cpuRestart" },
146- {Key : "ramCurrent" , Value : "$components.vms.ramCurrent" },
147- {Key : "ramRestart" , Value : "$components.vms.ramRestart" },
148- {Key : "cpuOnline" , Value : "$components.vms.cpuOnline" },
149- {Key : "cpuMaxUsable" , Value : "$components.vms.cpuMaxUsable" },
150- {Key : "ramOnline" , Value : "$components.vms.ramOnline" },
151- {Key : "ramMaxUsable" , Value : "$components.vms.ramMaxUsable" },
152- {Key : "clusterName" , Value : "$components.vms.clusterName" },
153- },
154- },
155- },
156- {Key : "storageCells" , Value : "$components.storageCells" },
157- {Key : "clusterNames" , Value : "$components.clusterNames" },
158190 },
159191 },
160192 },
161193 },
162194 },
163195 },
164196 },
165- bson.D {
166- {Key : "$project" ,
167- Value : bson.D {
168- {Key : "_id" , Value : "$_id._id" },
169- {Key : "hostname" , Value : "$_id.hostname" },
170- {Key : "environment" , Value : "$_id.environment" },
171- {Key : "location" , Value : "$_id.location" },
172- {Key : "rackID" , Value : "$_id.rackID" },
173- {Key : "components" , Value : "$components" },
174- },
175- },
176- },
197+ bson.D {{Key : "$unset" , Value : "matchedDocument" }},
177198 }
178199
179200 cur , err := md .Client .Database (md .Config .Mongodb .DBName ).Collection (exadataCollection ).Aggregate (ctx , pipeline )
0 commit comments