Skip to content

Commit e4c61ff

Browse files
authored
fix(components): resolve several problems of the cluster creation (#112)
1 parent b180442 commit e4c61ff

File tree

5 files changed

+147
-42
lines changed

5 files changed

+147
-42
lines changed

src/components/CreateClusterPanel/SimpleForm.tsx

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ProductsKnowledge,
1010
RegionKnowledge,
1111
useComponents,
12+
useParamGroups,
1213
useProducts,
1314
useVendorsAndRegions,
1415
} from '@/components/CreateClusterPanel/helpers'
@@ -76,6 +77,9 @@ export function SimpleForm({
7677
arch
7778
)
7879

80+
// Get parameter groups with productId, productVersion
81+
const { paramGroups } = useParamGroups(productId, productVersion)
82+
7983
const setVendor = useCallback(
8084
(vendorId?: string) => {
8185
if (!vendorAndRegions) return
@@ -131,6 +135,15 @@ export function SimpleForm({
131135
setProduct()
132136
}, [products])
133137

138+
// set a default value of the parameterGroupID item
139+
useEffect(() => {
140+
if (paramGroups?.length) {
141+
form.setFieldsValue({
142+
parameterGroupID: paramGroups[0].id,
143+
})
144+
}
145+
}, [paramGroups, form])
146+
134147
const { t, i18n } = useI18n()
135148

136149
const vendorSelector = useMemo(
@@ -173,9 +186,10 @@ export function SimpleForm({
173186
type={productId}
174187
arch={arch}
175188
products={products}
189+
paramGroups={paramGroups}
176190
/>
177191
),
178-
[productId, arch, vendorId, region, products, i18n.language]
192+
[productId, arch, vendorId, region, products, paramGroups, i18n.language]
179193
)
180194

181195
const nodeOptions = useMemo(
@@ -210,6 +224,7 @@ export function SimpleForm({
210224
<Form
211225
layout="horizontal"
212226
hideRequiredMark
227+
scrollToFirstError
213228
colon={false}
214229
form={form}
215230
name="create"
@@ -243,6 +258,7 @@ function BasicOptions({
243258
type,
244259
arch,
245260
products,
261+
paramGroups,
246262
}: {
247263
t: TFunction<''>
248264
onSelectProduct: (type: string) => void
@@ -252,6 +268,7 @@ function BasicOptions({
252268
type: string
253269
arch: string
254270
products: ProductsKnowledge
271+
paramGroups: { id: string; name: string }[]
255272
}) {
256273
return (
257274
<Card title={t('basic.title')}>
@@ -269,6 +286,21 @@ function BasicOptions({
269286
))}
270287
</Select>
271288
</Form.Item>
289+
<Form.Item
290+
name="cpuArchitecture"
291+
label={t('basic.fields.arch')}
292+
rules={[{ required: true }]}
293+
initialValue={arch}
294+
>
295+
<Radio.Group onChange={(e) => onSelectArch(e.target.value)}>
296+
{!!type &&
297+
products.products[type]?._archs.map((a) => (
298+
<Radio.Button value={a} key={a}>
299+
{a}
300+
</Radio.Button>
301+
))}
302+
</Radio.Group>
303+
</Form.Item>
272304
<Form.Item
273305
label={t('basic.fields.version')}
274306
name="clusterVersion"
@@ -285,19 +317,19 @@ function BasicOptions({
285317
</Select>
286318
</Form.Item>
287319
<Form.Item
288-
name="cpuArchitecture"
289-
label={t('basic.fields.arch')}
290-
rules={[{ required: true }]}
291-
initialValue={arch}
320+
label={t('basic.fields.paramGroup')}
321+
name="parameterGroupID"
322+
rules={[
323+
{ required: true, message: t('basic.rules.paramGroup.require') },
324+
]}
292325
>
293-
<Radio.Group onChange={(e) => onSelectArch(e.target.value)}>
294-
{!!type &&
295-
products.products[type]?._archs.map((a) => (
296-
<Radio.Button value={a} key={a}>
297-
{a}
298-
</Radio.Button>
299-
))}
300-
</Radio.Group>
326+
<Select>
327+
{paramGroups.map((item) => (
328+
<Select.Option value={item.id} key={item.id}>
329+
{item.name}
330+
</Select.Option>
331+
))}
332+
</Select>
301333
</Form.Item>
302334
</Card>
303335
)
@@ -354,7 +386,7 @@ function ComponentOptions({
354386
label={t('component.fields.copies')}
355387
>
356388
<Select>
357-
{[1, 2, 3, 4, 5].map((count) => (
389+
{[1, 3, 5, 7].map((count) => (
358390
<Select.Option key={count} value={count}>
359391
{count}
360392
</Select.Option>
@@ -365,15 +397,14 @@ function ComponentOptions({
365397
<Row
366398
gutter={20}
367399
style={{
368-
lineHeight: '12px',
369400
fontSize: 16,
370401
}}
371402
>
372403
<Col span={8}>{t('component.fields.zone')}</Col>
373404
<Col span={8}>{t('component.fields.spec')}</Col>
374405
<Col span={8}>{t('component.fields.amount')}</Col>
375406
</Row>
376-
<Divider style={{ margin: '16px 0' }} />
407+
<Divider style={{ margin: '12px 0' }} />
377408
{component.zones.map((zone, i) => {
378409
if (!zone) return undefined
379410
return (
@@ -475,6 +506,9 @@ function ClusterOptions() {
475506
dropdownStyle={{ display: 'none' }}
476507
/>
477508
</Form.Item>
509+
<Form.Item label={t('cluster.fields.user')}>
510+
<span>root</span>
511+
</Form.Item>
478512
<Form.Item
479513
name="dbPassword"
480514
label={t('cluster.fields.password')}

src/components/CreateClusterPanel/helpers.ts

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { ProductStatus, RequestClusterCreate } from '@/api/model'
1+
import {
2+
ParamGroupDBType,
3+
ProductStatus,
4+
RequestClusterCreate,
5+
} from '@/api/model'
26
import { useMemo } from 'react'
37
import { message } from 'antd'
48
import { TFunction } from 'react-i18next'
@@ -7,6 +11,7 @@ import {
711
useQueryProducts,
812
useQueryZones,
913
} from '@/api/hooks/platform'
14+
import { useQueryParamGroupList } from '@/api/hooks/param-group'
1015
import { mapObj } from '@/utils/obj'
1116

1217
export function processCreateRequest(
@@ -64,6 +69,19 @@ export function processCreateRequest(
6469
)
6570
return false
6671
}
72+
73+
// FIXME: remove hard-coded validation for TiKV
74+
if (
75+
node.componentType === 'TiKV' &&
76+
node.totalNodeCount! < value.copies!
77+
) {
78+
message.error(
79+
t('create.validation.storage.instanceLimit', {
80+
name: comp.name,
81+
})
82+
)
83+
return false
84+
}
6785
}
6886
}
6987
return true
@@ -115,7 +133,7 @@ export function useProducts(vendorId?: string, regionId?: string) {
115133
)
116134
return useMemo<ProductsKnowledge>(() => {
117135
const rawRegions = data?.data.data?.products // region => product => arch => version
118-
if (!rawRegions || !regionId)
136+
if (!rawRegions || !regionId || !rawRegions[regionId])
119137
return {
120138
_products: [],
121139
products: {},
@@ -199,6 +217,47 @@ export function useComponents(
199217
}, [data, vendorId, regionId])
200218
}
201219

220+
/**
221+
* Hook for getting parameter groups for the cluster
222+
* @param productType database type. e.g. TiDB
223+
* @param productVersion database version. e.g. v5.2.2
224+
*/
225+
export function useParamGroups(productType?: string, productVersion?: string) {
226+
const dbTypeHashmap: { [k: string]: ParamGroupDBType } = {
227+
TiDB: ParamGroupDBType.tidb,
228+
DM: ParamGroupDBType.dm,
229+
}
230+
const dbType = productType ? dbTypeHashmap[productType] : undefined
231+
232+
const dbVersion = productVersion?.match(/v\d+\.\d+/)?.[0]
233+
234+
const { data: response, isLoading } = useQueryParamGroupList(
235+
{
236+
dbType,
237+
dbVersion,
238+
},
239+
{
240+
enabled: !!dbType && !!dbVersion,
241+
refetchOnWindowFocus: false,
242+
}
243+
)
244+
const data = response?.data.data
245+
246+
const ret = useMemo(
247+
() =>
248+
data?.map((el) => ({
249+
id: el.paramGroupId!,
250+
name: el.name!,
251+
})) || [],
252+
[data]
253+
)
254+
255+
return {
256+
paramGroups: ret,
257+
isLoading,
258+
}
259+
}
260+
202261
export type VendorKnowledge = {
203262
id: string
204263
name: string

src/components/CreateClusterPanel/index.module.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414

1515
:global(.ant-form-item-label) {
16-
width: 120px;
16+
width: 200px;
1717
}
1818

1919
:global(.ant-card), :global(.ant-collapse) {

src/components/CreateClusterPanel/translations/en.yaml

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
basic:
2-
title: Basic Options
2+
title: Database Basic Information
33
fields:
4-
type: Cluster Type
5-
arch: Arch
6-
version: Version
4+
type: Database Type
5+
arch: CPU Architecture
6+
version: Database Version
7+
paramGroup: Parameter Group
78
rules:
89
type:
9-
require: Please select the cluster type
10+
require: Please select a database type
1011
version:
11-
require: Please select the cluster version
12+
require: Please select a database version
13+
paramGroup:
14+
require: Please select a parameter group
1215
component:
1316
title: '{{ name }} Options'
1417
optional: Optional
1518
allocator:
1619
title: Quick Allocate
1720
items: '{{ count }} Nodes'
1821
fields:
19-
copies: Copies
22+
copies: Number of Storage Replicas
2023
nodes: Nodes
2124
zone: Available Zone
22-
spec: Spec
23-
amount: Amount
25+
spec: Specification Code
26+
amount: Number of Instances
2427
cluster:
25-
title: Cluster Options
28+
title: Cluster Basic Information
2629
fields:
2730
name: Cluster Name
28-
tags: Cluster Tags
29-
password: DB Password
31+
tags: Cluster Labels
32+
user: Database User
33+
password: Database Password
3034
exclusive: Exclusive
3135
tooltip:
3236
name: Upper and lower case letters, numbers and hyphens, starting with a letter, and with length between 8-32
@@ -51,6 +55,8 @@ create:
5155
miss: 'The {{ name }} node cannot be missing!'
5256
minZone: '{{ name }} requires at least {{ count }} nodes'
5357
maxZone: '{{ name }} requires no more than {{ count }} nodes'
58+
storage:
59+
instanceLimit: 'The number of {{ name }} instance cannot be less then the number of storage replicas'
5460
name: Create Cluster
5561

5662
message:
@@ -85,8 +91,8 @@ preview:
8591
columns:
8692
component: Component
8793
zone: Zone
88-
spec: Spec
89-
amount: Amount
94+
spec: Specification Code
95+
amount: Number of Instances
9096
status: Status
9197
status:
9298
normal: Normal

src/components/CreateClusterPanel/translations/zh.yaml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
basic:
2-
title: 基础配置
2+
title: 数据库基础信息
33
fields:
4-
type: 集群类型
5-
arch: 体系架构
6-
version: 集群版本
4+
type: 数据库类型
5+
arch: CPU 体系架构
6+
version: 数据库版本
7+
paramGroup: 参数组
78
rules:
89
type:
9-
require: 请选择集群类型
10+
require: 请选择数据库类型
1011
version:
11-
require: 请选择集群版本
12+
require: 请选择数据库版本
13+
paramGroup:
14+
require: 请选择参数组
1215
component:
1316
title: '{{ name }} 配置'
1417
optional: 可选
1518
allocator:
1619
title: 快速分配
1720
items: '{{ count }} 节点'
1821
fields:
19-
copies: 副本数
22+
copies: 存储副本数
2023
nodes: 节点
2124
zone: 可用区
22-
spec: 规格
25+
spec: 规格代码
2326
amount: 数量
2427
cluster:
25-
title: 集群信息
28+
title: 集群基础信息
2629
fields:
2730
name: 集群名称
2831
tags: 集群标签
32+
user: 数据库用户
2933
password: 数据库密码
3034
exclusive: 独占部署
3135
tooltip:
@@ -51,6 +55,8 @@ create:
5155
miss: '不能缺失 {{ name }} 节点!'
5256
minZone: '{{ name }} 至少需要 {{ count }} 个节点!'
5357
maxZone: '{{ name }} 至多拥有 {{ count }} 个节点!'
58+
storage:
59+
instanceLimit: '{{ name }} 实例数不能少于存储副本数'
5460
name: 创建集群
5561

5662
message:

0 commit comments

Comments
 (0)