|
40 | 40 | <EditableTable
|
41 | 41 | ref="table"
|
42 | 42 | class="mb-20"
|
43 |
| - :model="formData.tableData"> |
| 43 | + :model="formData.tableData" |
| 44 | + :rules="rules"> |
44 | 45 | <EditableRow
|
45 | 46 | v-for="(item, index) in formData.tableData"
|
46 | 47 | :key="index">
|
|
61 | 62 | </EditableColumn>
|
62 | 63 | <EditableColumn
|
63 | 64 | :label="t('所属集群')"
|
64 |
| - :min-width="200"> |
| 65 | + :min-width="200" |
| 66 | + :rowspan="item.rowspan"> |
65 | 67 | <div
|
66 | 68 | v-if="item.host.master_domain"
|
67 | 69 | class="cluster-domain">
|
|
145 | 147 | shard: string;
|
146 | 148 | spec_config: MongodbInstanceModel['spec_config'];
|
147 | 149 | };
|
| 150 | + rowspan: number; |
148 | 151 | spec_id: number;
|
149 | 152 | }
|
150 | 153 |
|
|
164 | 167 | shard: '',
|
165 | 168 | spec_config: {} as MongodbInstanceModel['spec_config'],
|
166 | 169 | },
|
| 170 | + rowspan: data.rowspan || 1, |
167 | 171 | spec_id: data.spec_id || 0,
|
168 | 172 | });
|
169 | 173 |
|
| 174 | + const getClusterNodeCount = (clusterId: number) => { |
| 175 | + const countMap: Record<string, number> = {}; |
| 176 | + formData.tableData.forEach((tableItem) => { |
| 177 | + if (tableItem.host.ip && tableItem.host.cluster_id) { |
| 178 | + if (tableItem.host.cluster_id !== clusterId) { |
| 179 | + return; |
| 180 | + } |
| 181 | + const { machine_type: machineType, shard } = tableItem.host; |
| 182 | + const nodeKey = |
| 183 | + tableItem.host.cluster_type === ClusterTypes.MONGO_SHARED_CLUSTER && tableItem.host.machine_type === 'mongodb' |
| 184 | + ? shard |
| 185 | + : machineType; |
| 186 | + if (countMap[nodeKey]) { |
| 187 | + countMap[nodeKey] = countMap[nodeKey] + 1; |
| 188 | + } else { |
| 189 | + countMap[nodeKey] = 1; |
| 190 | + } |
| 191 | + } |
| 192 | + }, {}); |
| 193 | + |
| 194 | + const typeCountMap: Record<string, number[]> = { |
| 195 | + mongo_config: [], |
| 196 | + mongodb: [], |
| 197 | + mongos: [], |
| 198 | + }; |
| 199 | + Object.entries(countMap).forEach(([type, count]) => { |
| 200 | + if (type in typeCountMap) { |
| 201 | + typeCountMap[type].push(count); |
| 202 | + } else { |
| 203 | + typeCountMap.mongodb.push(count); |
| 204 | + } |
| 205 | + }); |
| 206 | + return typeCountMap; |
| 207 | + }; |
| 208 | + |
| 209 | + const rules = { |
| 210 | + 'host.ip': [ |
| 211 | + { |
| 212 | + message: '', |
| 213 | + trigger: 'change', |
| 214 | + validator: (value: string, { rowData }: { rowData: RowData }) => { |
| 215 | + const nodeCountMap = getClusterNodeCount(rowData.host.cluster_id); |
| 216 | + |
| 217 | + if (rowData.host.cluster_type === ClusterTypes.MONGO_REPLICA_SET) { |
| 218 | + if (nodeCountMap.mongodb.some((mongodbItem) => mongodbItem > 1)) { |
| 219 | + return t('同一个副本集,一次只能替换一个节点'); |
| 220 | + } |
| 221 | + return true; |
| 222 | + } |
| 223 | + if (nodeCountMap.mongo_config.some((mongoConfigItem) => mongoConfigItem > 1)) { |
| 224 | + return t('config一次只能替换一个节点'); |
| 225 | + } |
| 226 | + if (nodeCountMap.mongodb.some((mongoConfigItem) => mongoConfigItem > 1)) { |
| 227 | + return t('同一个shard,同时只能替换一个节点'); |
| 228 | + } |
| 229 | + return true; |
| 230 | + }, |
| 231 | + }, |
| 232 | + ], |
| 233 | + }; |
| 234 | + |
170 | 235 | const formData = reactive({
|
171 | 236 | clusterType: ClusterTypes.MONGO_REPLICA_SET,
|
172 | 237 | payload: createTickePayload(),
|
|
179 | 244 | useTicketDetail<Mongodb.Cutoff>(TicketTypes.MONGODB_CUTOFF, {
|
180 | 245 | onSuccess(ticketDetail) {
|
181 | 246 | const { details } = ticketDetail;
|
182 |
| - const { clusters, infos, specs } = details; |
| 247 | + const { clusters, infos } = details; |
183 | 248 | Object.assign(formData, {
|
184 |
| - payload: createTickePayload(ticketDetail), |
| 249 | + ...createTickePayload(ticketDetail), |
| 250 | + clusterType: clusters[infos[0].cluster_id].cluster_type, |
| 251 | + tableData: infos.flatMap((infoItem) => { |
| 252 | + const machineInfoList = [...infoItem.mongo_config, ...infoItem.mongodb, ...infoItem.mongos]; |
| 253 | + return machineInfoList.map((machineInfo) => |
| 254 | + createTableRow({ |
| 255 | + host: { |
| 256 | + ip: machineInfo.ip, |
| 257 | + master_domain: clusters[infoItem.cluster_id].immute_domain, |
| 258 | + } as RowData['host'], |
| 259 | + spec_id: machineInfo.spec_id, |
| 260 | + }), |
| 261 | + ); |
| 262 | + }), |
185 | 263 | });
|
186 |
| - if (infos.length > 0) { |
187 |
| - const dataList: RowData[] = []; |
188 |
| - infos.forEach((info) => { |
189 |
| - const hostList = [...info.mongo_config, ...info.mongodb, ...info.mongos]; |
190 |
| - let machineType = ''; |
191 |
| - if (info.mongo_config.length) { |
192 |
| - machineType = 'mongo_config'; |
193 |
| - } |
194 |
| - if (info.mongodb.length) { |
195 |
| - machineType = 'mongodb'; |
196 |
| - } |
197 |
| - if (info.mongos.length) { |
198 |
| - machineType = 'mongos'; |
199 |
| - } |
200 |
| - const clusterInfo = clusters[info.cluster_id]; |
201 |
| - hostList.forEach((item) => { |
202 |
| - const specInfo = specs[info.resource_spec[`${machineType}_${item.ip}`].spec_id]; |
203 |
| - dataList.push( |
204 |
| - createTableRow({ |
205 |
| - host: { |
206 |
| - bk_cloud_id: item.bk_cloud_id, |
207 |
| - bk_host_id: item.bk_host_id, |
208 |
| - cluster_id: info.cluster_id, |
209 |
| - cluster_type: clusterInfo.cluster_type, |
210 |
| - ip: item.ip, |
211 |
| - machine_type: machineType, |
212 |
| - master_domain: clusterInfo.immute_domain, |
213 |
| - related_clusters: [], |
214 |
| - shard: '', |
215 |
| - spec_config: specInfo as unknown as MongodbInstanceModel['spec_config'], |
216 |
| - }, |
217 |
| - spec_id: specInfo.id, |
218 |
| - }), |
219 |
| - ); |
220 |
| - }); |
221 |
| - }); |
222 |
| - formData.clusterType = dataList[0].host.cluster_type; |
223 |
| - formData.tableData = [...dataList]; |
224 |
| - } |
225 | 264 | },
|
226 | 265 | });
|
227 | 266 |
|
|
242 | 281 | ip_source: 'resource_pool';
|
243 | 282 | }>(TicketTypes.MONGODB_CUTOFF);
|
244 | 283 |
|
| 284 | + watch( |
| 285 | + () => formData.tableData.length, |
| 286 | + () => { |
| 287 | + sortTableByCluster(); |
| 288 | + }, |
| 289 | + ); |
| 290 | + |
| 291 | + const generateRequestParam = () => { |
| 292 | + const clusterMap: Record<string, RowData[]> = {}; |
| 293 | + formData.tableData.forEach((item) => { |
| 294 | + if (item.host.ip) { |
| 295 | + const domain = item.host.master_domain; |
| 296 | + if (!clusterMap[domain]) { |
| 297 | + clusterMap[domain] = [item]; |
| 298 | + } else { |
| 299 | + clusterMap[domain].push(item); |
| 300 | + } |
| 301 | + } |
| 302 | + }); |
| 303 | + const domains = Object.keys(clusterMap); |
| 304 | + const infos = domains.map((domain) => { |
| 305 | + const sameArr = clusterMap[domain]; |
| 306 | + const infoItem = { |
| 307 | + cluster_id: sameArr[0].host.cluster_id, |
| 308 | + mongo_config: [] as SpecHostList, |
| 309 | + mongodb: [] as SpecHostList, |
| 310 | + mongos: [] as SpecHostList, |
| 311 | + }; |
| 312 | + sameArr.forEach((item) => { |
| 313 | + const specObj = { |
| 314 | + bk_cloud_id: item.host.bk_cloud_id, |
| 315 | + bk_host_id: item.host.bk_host_id, |
| 316 | + ip: item.host.ip, |
| 317 | + spec_id: item.spec_id, |
| 318 | + }; |
| 319 | + infoItem[item.host.machine_type as keyof Omit<typeof infoItem, 'cluster_id'>].push(specObj); |
| 320 | + }); |
| 321 | + return infoItem; |
| 322 | + }); |
| 323 | + return infos; |
| 324 | + }; |
| 325 | + |
245 | 326 | const handleSubmit = async () => {
|
246 | 327 | const result = await tableRef.value!.validate();
|
247 | 328 | if (!result) {
|
248 | 329 | return;
|
249 | 330 | }
|
250 | 331 | createTicketRun({
|
251 | 332 | details: {
|
252 |
| - infos: formData.tableData.map((item) => { |
253 |
| - const infoItem = { |
254 |
| - cluster_id: item.host.cluster_id, |
255 |
| - mongo_config: [] as SpecHostList, |
256 |
| - mongodb: [] as SpecHostList, |
257 |
| - mongos: [] as SpecHostList, |
258 |
| - }; |
259 |
| - Object.assign(infoItem, { |
260 |
| - [item.host.machine_type]: [ |
261 |
| - { |
262 |
| - bk_cloud_id: item.host.bk_cloud_id, |
263 |
| - bk_host_id: item.host.bk_host_id, |
264 |
| - ip: item.host.ip, |
265 |
| - spec_id: item.spec_id, |
266 |
| - }, |
267 |
| - ], |
268 |
| - }); |
269 |
| - return infoItem; |
270 |
| - }), |
| 333 | + infos: generateRequestParam(), |
271 | 334 | ip_source: 'resource_pool',
|
272 | 335 | },
|
273 | 336 | ...formData.payload,
|
|
317 | 380 | }
|
318 | 381 | return item.host.machine_type || '';
|
319 | 382 | };
|
| 383 | + |
| 384 | + const sortTableByCluster = () => { |
| 385 | + const clusterMap: Record<string, RowData[]> = {}; |
| 386 | + const emptyRowList: RowData[] = []; |
| 387 | + formData.tableData.forEach((item) => { |
| 388 | + Object.assign(item, { rowspan: 1 }); |
| 389 | + const { master_domain: domain } = item.host; |
| 390 | + if (!domain) { |
| 391 | + emptyRowList.push(item); |
| 392 | + return; |
| 393 | + } |
| 394 | + if (!clusterMap[domain]) { |
| 395 | + clusterMap[domain] = [item]; |
| 396 | + } else { |
| 397 | + clusterMap[domain].push(item); |
| 398 | + } |
| 399 | + }); |
| 400 | + |
| 401 | + const sortedList: RowData[] = []; |
| 402 | + Object.values(clusterMap).forEach((list) => { |
| 403 | + Object.assign(list[0], { rowspan: list.length }); |
| 404 | + sortedList.push(...list); |
| 405 | + }); |
| 406 | + |
| 407 | + return [...sortedList, ...emptyRowList]; |
| 408 | + }; |
320 | 409 | </script>
|
321 | 410 | <style lang="less" scoped>
|
322 | 411 | .cluster-domain {
|
|
0 commit comments