Skip to content

Commit

Permalink
fix(frontend): tendbcluster迁移主从单据克隆bug #7767
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 23294
  • Loading branch information
JustaCattt authored and jinquantianxia committed Nov 8, 2024
1 parent b30b3aa commit 1f24484
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
:key="item.rowKey"
ref="rowRefs"
:data="item"
:inputed-ips="inputedIps"
:removeable="tableData.length < 2"
@add="(payload: Array<IDataRow>) => handleAppend(index, payload)"
@clone="(payload: IDataRow) => handleClone(index, payload)"
Expand Down Expand Up @@ -129,6 +130,7 @@
const selected = shallowRef({ TendbClusterHost: [] } as InstanceSelectorValues<IValue>);
const inputedIps = computed(() => tableData.value.map((item) => item.clusterData.ip));
const totalNum = computed(() => tableData.value.filter((item) => Boolean(item.clusterData.ip)).length);
// ip 是否已存在表格的映射表
Expand Down Expand Up @@ -222,6 +224,11 @@
masterInstanceList: spiderMachineItem.related_instances,
});
ipMemo[ip] = true;
Object.keys(ipMemo).forEach((ip) => {
if (ipMemo[ip]) {
selected.value.TendbClusterHost.push({ ip } as IValue);
}
});
};
// 追加一个集群
Expand Down Expand Up @@ -264,33 +271,17 @@
const handleSubmit = async () => {
try {
isSubmitting.value = true;
const rowDataList = await Promise.all(rowRefs.value!.map((item) => item.getValue()));
const infos = await Promise.all(rowRefs.value!.map((item) => item.getValue()));
const params = {
bk_biz_id: currentBizId,
ticket_type: TicketTypes.TENDBCLUSTER_MIGRATE_CLUSTER,
remark: formData.remark,
details: {
...formData,
ip_source: 'manual_input',
infos: tableData.value.map((row) => {
const { clusterData } = row;
const [{ oldSlave, newInstaceList }] = rowDataList.filter((item) => item.oldMasterIp === clusterData.ip);
return {
cluster_id: clusterData.clusterId,
new_master: newInstaceList[0],
new_slave: newInstaceList[1],
old_master: {
ip: clusterData.ip,
bk_cloud_id: clusterData.cloudId,
bk_host_id: clusterData.hostId,
bk_biz_id: currentBizId,
},
old_slave: oldSlave,
};
}),
infos,
},
};
await createTicket(params).then((data) => {
window.changeConfirm = false;
router.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
</div>
</template>

<script lang="ts">
const hostsMemo: { [key: string]: Record<string, boolean> } = {};
</script>
<script setup lang="ts">
import { useI18n } from 'vue-i18n';
Expand All @@ -36,33 +33,40 @@
import TableEditInput from '@components/render-table/columns/input/index.vue';
import { random } from '@utils';
interface Props {
ip?: string;
inputedIps?: string[];
}
interface Emits {
(e: 'inputFinish', value: string): void;
}
interface Exposes {
getValue: () => Promise<string>;
getValue: () => Promise<{
cluster_id: number;
old_master: {
bk_biz_id: number;
bk_cloud_id: number;
bk_host_id: number;
ip: string;
};
}>;
}
const props = withDefaults(defineProps<Props>(), {
ip: '',
inputedIps: () => [],
});
const emits = defineEmits<Emits>();
const instanceKey = `render_host_${random()}`;
hostsMemo[instanceKey] = {};
const emits = defineEmits<Emits>();
const { currentBizId } = useGlobalBizs();
const { t } = useI18n();
const localValue = ref(props.ip);
const editRef = ref();
const hostInfo = ref<ServiceReturnType<typeof checkMysqlInstances>[number]>();
const rules = [
{
Expand All @@ -80,45 +84,27 @@
instance_addresses: [value],
});
if (data.length > 0) {
[hostInfo.value] = data;
localValue.value = data[0].ip;
emits('inputFinish', value);
return true;
}
return false;
},
message: t('目标主机不存在'),
},
{
validator: () => {
const currentClusterSelectMap = hostsMemo[instanceKey];
const otherClusterMemoMap = { ...hostsMemo };
delete otherClusterMemoMap[instanceKey];
const otherClusterIdMap = Object.values(otherClusterMemoMap).reduce(
(result, item) => ({
...result,
...item,
}),
{} as Record<string, boolean>,
);
const currentSelectClusterIdList = Object.keys(currentClusterSelectMap);
for (let i = 0; i < currentSelectClusterIdList.length; i++) {
if (otherClusterIdMap[currentSelectClusterIdList[i]]) {
return false;
}
}
return true;
},
validator: (value: string) => props.inputedIps.filter((item) => item === value).length < 2,
message: t('目标主机重复'),
},
];
// 同步外部值
watch(
() => props.ip,
(newIp) => {
if (newIp) {
localValue.value = newIp;
() => {
if (props.ip) {
localValue.value = props.ip;
}
},
{
Expand All @@ -129,30 +115,30 @@
watch(
localValue,
() => {
if (localValue.value) {
hostsMemo[instanceKey][localValue.value] = true;
}
setTimeout(() => {
editRef.value!.getValue();
});
},
{
immediate: true,
},
);
const handleInputFinish = (value: string) => {
hostsMemo[instanceKey][localValue.value] = true;
emits('inputFinish', value);
localValue.value = value;
};
onBeforeUnmount(() => {
delete hostsMemo[instanceKey];
});
defineExpose<Exposes>({
getValue() {
return editRef.value
.getValue()
.then(() => localValue.value)
.catch(() => Promise.reject(localValue.value));
return editRef.value!.getValue().then(() => ({
cluster_id: hostInfo.value?.cluster_id,
old_master: {
ip: hostInfo.value?.ip,
bk_cloud_id: hostInfo.value?.bk_cloud_id,
bk_host_id: hostInfo.value?.bk_host_id,
bk_biz_id: currentBizId,
},
}));
},
});
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,33 @@
</template>

<script lang="ts">
import { checkHost, getHostTopoInfos } from '@services/source/ipchooser';
import type { HostInfo } from '@services/types';
import type { HostItem, IDataRow } from './Row.vue';
const singleHostSelectMemo: { [key: string]: Record<string, boolean> } = {};
type HostTopoInfo = ServiceReturnType<typeof getHostTopoInfos>['hosts_topo_info'][number];
interface Props {
clusterData: IDataRow['clusterData'];
newHostList: IDataRow['newHostList'];
}
interface Exposes {
getValue: () => Promise<{
new_master: HostItem;
new_slave: HostItem;
}>;
}
</script>

<script setup lang="ts">
import _ from 'lodash';
import tippy, { type Instance, type SingleTarget } from 'tippy.js';
import { useI18n } from 'vue-i18n';
import { checkHost, getHostTopoInfos } from '@services/source/ipchooser';
import type { HostInfo } from '@services/types';
import { useGlobalBizs } from '@stores';
import { OSTypes } from '@common/const';
Expand All @@ -145,26 +161,6 @@
import { random } from '@utils';
import type { IDataRow } from './Row.vue';
type HostTopoInfo = ServiceReturnType<typeof getHostTopoInfos>['hosts_topo_info'][number];
interface Props {
clusterData: IDataRow['clusterData'];
newHostList: IDataRow['newHostList'];
}
interface Exposes {
getValue: () => Promise<
{
ip: string;
bk_cloud_id: number;
bk_host_id: number;
bk_biz_id: number;
}[]
>;
}
const props = defineProps<Props>();
const genHostKey = (hostData: HostTopoInfo) => `#${hostData.bk_cloud_id}#${hostData.ip}`;
Expand Down Expand Up @@ -303,7 +299,12 @@
watch(
() => props.newHostList,
() => {
localValue.value = props.newHostList.join(',');
if (props.newHostList.length > 0) {
localValue.value = props.newHostList.join(',');
setTimeout(() => {
inputRef.value!.getValue();
});
}
},
{
immediate: true,
Expand Down Expand Up @@ -351,6 +352,9 @@
const handleHostChange = (hostList: HostInfo[]) => {
localHostList.value = hostList;
localValue.value = hostList.map((hostItem) => hostItem.ip).join(',');
setTimeout(() => {
inputRef.value!.getValue();
});
};
const handleConflictHostChange = (hostData: HostTopoInfo, checked: boolean) => {
Expand Down Expand Up @@ -381,10 +385,10 @@
bk_host_id: hostItem.host_id,
bk_biz_id: hostItem.biz.id,
}));
return inputRef
.value!.getValue()
.then(() => Promise.resolve(hostList))
.catch(() => Promise.reject(hostList));
return inputRef.value!.getValue().then(() => ({
new_master: hostList[0],
new_slave: hostList[1],
}));
},
});
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
(e: 'change', value: string): void;
}
interface Expose {
interface Exposes {
getValue: () => Promise<{
ip: string;
bk_cloud_id: number;
bk_host_id: number;
bk_biz_id: number;
old_slave: {
bk_biz_id: number;
bk_cloud_id: number;
bk_host_id: number;
ip: string;
};
}>;
}
Expand Down Expand Up @@ -94,18 +96,16 @@
},
);
defineExpose<Expose>({
defineExpose<Exposes>({
getValue() {
const slave = slaveInfo.value;
if (slave) {
return Promise.resolve({
ip: slave.ip,
bk_cloud_id: slave.bk_cloud_id,
bk_host_id: slave.bk_host_id,
bk_biz_id: slave.bk_biz_id,
});
}
return textRef.value!.getValue();
return textRef.value!.getValue().then(() => ({
old_slave: {
ip: slaveInfo.value?.ip,
bk_cloud_id: slaveInfo.value?.bk_cloud_id,
bk_host_id: slaveInfo.value?.bk_host_id,
bk_biz_id: slaveInfo.value?.bk_biz_id,
},
}));
},
});
</script>
Loading

0 comments on commit 1f24484

Please sign in to comment.