<el-select v-model="form.nbdconfig.threads.type"…
- User
<div v-if="form.nbdconfig.threads.type === 2"> <el-input type="number" v-model.number="form.nbdconfig.threads.threadscount" :min="1" :max="8" :placeholder="$t('set.configuration.advanced.threads.placeholder')" @keydown.enter.prevent="" @change="validateThreadCount" /> </div> cpuCount: { type: Number as PropType<number> }const form = ref({ nbdconfig: { threads: { threadscount: 0, type: 0 } } })
const threadsOptions = computed(() => [ { label: t('auto').toString(), value: 0 }, { label: t('max').toString(), value: 1 }, { label: t('num').toString(), value: 2 } ]) const validateThreadCount = (value: number) => { if (value > props.cpuCount) { form.value.nbdconfig.threads.threadscount = props.cpuCount } }нужно доработать возможности выбора
type 2 - явное указание количества потоков (от 1 до количества процессоров props.cpuCount) type 0 auto - определяем сами на основе загрузки cpu props.cpuCount - берём 50% от idle, но от 1 до 8 type 1 max - определяем сами на основе количества процессоров props.cpuCount, но не более 8 По умолчанию auto
- Assistant
- User
на русском
- Assistant
- User
const updateThreadsCount = (type) => { switch (type) { case 0: // Авто form.value.nbdconfig.threads.threadscount = Math.max(1, Math.min(8, Math.floor(props.cpuCount * 0.5))); break; case 1: // Макс form.value.nbdconfig.threads.threadscount = Math.min(8, props.cpuCount); break; case 2: // Число if (form.value.nbdconfig.threads.threadscount > props.cpuCount) { form.value.nbdconfig.threads.threadscount = props.cpuCount; } break; default: break; } };
перепиши по другому
- Assistant
Конечно! Давайте перепишем функцию
updateThreadsCount, чтобы она оставалась читаемой, понятной и функциональной. Мы также можем улучшить читаемость кода, вынеся расчеты в отдельные функции, если это необходимо.const calculateAutoThreads = (cpuCount) => { return Math.max(1, Math.min(8, Math.floor(cpuCount * 0.5))); }; const calculateMaxThreads = (cpuCount) => { return Math.min(8, cpuCount); }; const updateThreadsCount = (type) => { let updatedCount = form.value.nbdconfig.threads.threadscount; if (type === 0) { // Авто updatedCount = calculateAutoThreads(props.cpuCount); } else if (type === 1) { // Макс updatedCount = calculateMaxThreads(props.cpuCount); } else if (type === 2) { // Число updatedCount = Math.min(form.value.nbdconfig.threads.threadscount, props.cpuCount); } form.value.nbdconfig.threads.threadscount = updatedCount; };Объяснение изменений:
- User
<div v-if="form.nbdconfig.threads.type === 2"> <el-input v-model.number="form.nbdconfig.threads.threadscount" type="number" :min="1" :max="8" :placeholder="$t('set.configuration.advanced.threads.placeholder')" @keydown.enter.prevent="" @change="validateThreadCount" /> </div> cpuCount: { type: Number as PropType<number> } const form = ref({ nbdconfig: { threads: { threadscount: 0, type: 0 } } }) const threadsOptions = computed(() => [ { label: t('set.configuration.advanced.threads.auto').toString(), value: 0 }, { label: t('set.configuration.advanced.threads.max').toString(), value: 1 }, { label: t('set.configuration.advanced.threads.num').toString(), value: 2 } ]) const calculateAutoThreads = (cpuCount: number) => { return Math.max(1, Math.min(8, Math.floor(cpuCount * 0.5))) } const calculateMaxThreads = (cpuCount: number) => { return Math.min(8, cpuCount) } const updateThreadsCount = (type: number) => { let updatedCount = form.value.nbdconfig.threads.threadscount if (type === 0) { // auto updatedCount = calculateAutoThreads(props.cpuCount) } else if (type === 1) { // max updatedCount = calculateMaxThreads(props.cpuCount) } else if (type === 2) { // num updatedCount = Math.min(form.value.nbdconfig.threads.threadscount, props.cpuCount) } form.value.nbdconfig.threads.threadscount = updatedCount } // Отслеживание изменений типа и обновление количества потоков соответственно watch(() => form.value.nbdconfig.threads.type, (newType: number) => { updateThreadsCount(newType) }) const validateThreadCount = (value: number) => { if (value > props.cpuCount) { form.value.nbdconfig.threads.threadscount = props.cpuCount // the number of threads cannot be greater than the number of CPUs } } - Assistant