-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: set the max limit and display ui of resource numbers (#1918)
* refactoring resource number UI of routing list page * set the max value of the resource slider in the service launcher using config.toml * fix typo * display resource type icon in the service detail * fix eslint * display fixed number for memory * fix: condition to determine whether to display the GPU slider * change `somthing.slot` to `somthing.device`, improve type definitions * use `toFixed(2)` to display FGPU number * add test code for iSizeToSize and update test snapshot * display shmem on Endpoint detail * fix: add shmem unit to the body of `/services` POST request
- Loading branch information
Showing
7 changed files
with
295 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { iSizeToSize } from '../helper'; | ||
import Flex from './Flex'; | ||
import { Tooltip, Typography, theme } from 'antd'; | ||
import React, { ReactElement } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
export type ResourceTypeKey = | ||
| 'cpu' | ||
| 'mem' | ||
| 'cuda.device' | ||
| 'cuda.shares' | ||
| 'rocm.device' | ||
| 'tpu.device' | ||
| 'ipu.device' | ||
| 'atom.device' | ||
| 'warboy.device'; | ||
|
||
export type ResourceOpts = { | ||
shmem: number; | ||
}; | ||
interface Props { | ||
type: ResourceTypeKey; | ||
extra?: ReactElement; | ||
opts?: ResourceOpts; | ||
value: string; | ||
} | ||
|
||
type ResourceTypeInfo<V> = { | ||
[key in ResourceTypeKey]: V; | ||
}; | ||
const ResourceNumber: React.FC<Props> = ({ | ||
type, | ||
value: amount, | ||
extra, | ||
opts, | ||
}) => { | ||
const { t } = useTranslation(); | ||
const { token } = theme.useToken(); | ||
const units: ResourceTypeInfo<string> = { | ||
cpu: t('session.core'), | ||
mem: 'GiB', | ||
'cuda.device': 'GPU', | ||
'cuda.shares': 'FGPU', | ||
'rocm.device': 'GPU', | ||
'tpu.device': 'TPU', | ||
'ipu.device': 'IPU', | ||
'atom.device': 'ATOM', | ||
'warboy.device': 'Warboy', | ||
}; | ||
|
||
return ( | ||
<Flex direction="row" gap="xxs"> | ||
<ResourceTypeIcon type={type} /> | ||
<Typography.Text> | ||
{units[type] === 'GiB' | ||
? iSizeToSize(amount + 'b', 'g', 2).numberFixed | ||
: units[type] === 'FGPU' | ||
? parseFloat(amount).toFixed(2) | ||
: amount} | ||
</Typography.Text> | ||
<Typography.Text type="secondary">{units[type]}</Typography.Text> | ||
{type === 'mem' && opts?.shmem && ( | ||
<Typography.Text | ||
type="secondary" | ||
style={{ fontSize: token.fontSizeSM }} | ||
> | ||
(SHM: {iSizeToSize(opts.shmem + 'b', 'g', 2).numberFixed}GiB) | ||
</Typography.Text> | ||
)} | ||
{extra} | ||
</Flex> | ||
); | ||
}; | ||
|
||
const MWCIconWrap: React.FC<{ size?: number; children: string }> = ({ | ||
size = 16, | ||
children, | ||
}) => { | ||
return ( | ||
// @ts-ignore | ||
<mwc-icon | ||
style={{ | ||
'--mdc-icon-size': `${size + 2}px`, | ||
width: size, | ||
height: size, | ||
}} | ||
> | ||
{children} | ||
{/* @ts-ignore */} | ||
</mwc-icon> | ||
); | ||
}; | ||
interface AccTypeIconProps | ||
extends Omit<React.ImgHTMLAttributes<HTMLImageElement>, 'src'> { | ||
type: ResourceTypeKey; | ||
showIcon?: boolean; | ||
showUnit?: boolean; | ||
showTooltip?: boolean; | ||
size?: number; | ||
} | ||
export const ResourceTypeIcon: React.FC<AccTypeIconProps> = ({ | ||
type, | ||
size = 16, | ||
showIcon = true, | ||
showUnit = true, | ||
showTooltip = true, | ||
...props | ||
}) => { | ||
const { t } = useTranslation(); | ||
|
||
const resourceTypeIconSrcMap: ResourceTypeInfo< | ||
[ReactElement | string, string] | ||
> = { | ||
cpu: [ | ||
<MWCIconWrap size={size}>developer_board</MWCIconWrap>, | ||
t('session.core'), | ||
], | ||
mem: [<MWCIconWrap size={size}>memory</MWCIconWrap>, 'GiB'], | ||
'cuda.device': ['/resources/icons/file_type_cuda.svg', 'GPU'], | ||
'cuda.shares': ['/resources/icons/file_type_cuda.svg', 'FGPU'], | ||
'rocm.device': ['/resources/icons/ROCm.png', 'GPU'], | ||
'tpu.device': [<MWCIconWrap size={size}>view_module</MWCIconWrap>, 'TPU'], | ||
'ipu.device': [<MWCIconWrap size={size}>view_module</MWCIconWrap>, 'IPU'], | ||
'atom.device': ['/resources/icons/rebel.svg', 'ATOM'], | ||
'warboy.device': ['/resources/icons/furiosa.svg', 'Warboy'], | ||
}; | ||
|
||
return ( | ||
<Tooltip | ||
title={ | ||
showTooltip ? `${type} (${resourceTypeIconSrcMap[type][1]})` : undefined | ||
} | ||
> | ||
{typeof resourceTypeIconSrcMap[type]?.[0] === 'string' ? ( | ||
<img | ||
{...props} | ||
style={{ | ||
height: size, | ||
...(props.style || {}), | ||
}} | ||
// @ts-ignore | ||
src={resourceTypeIconSrcMap[type]?.[0] || ''} | ||
alt={type} | ||
/> | ||
) : ( | ||
<div style={{ width: 16, height: 16 }}> | ||
{resourceTypeIconSrcMap[type]?.[0]} | ||
</div> | ||
)} | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
export default ResourceNumber; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { iSizeToSize } from './index'; | ||
|
||
describe('iSizeToSize', () => { | ||
it('should convert iSize to Size with default fixed value', () => { | ||
const sizeWithUnit = '1K'; | ||
const targetSizeUnit = 'B'; | ||
const result = iSizeToSize(sizeWithUnit, targetSizeUnit); | ||
expect(result).toEqual({ | ||
number: 1024, | ||
numberFixed: '1024.00', | ||
unit: 'B', | ||
numberUnit: '1024.00B', | ||
}); | ||
}); | ||
|
||
it('should convert iSize to Size with fixed value of 0', () => { | ||
const sizeWithUnit = '1K'; | ||
const targetSizeUnit = 'B'; | ||
const fixed = 0; | ||
const result = iSizeToSize(sizeWithUnit, targetSizeUnit, fixed); | ||
expect(result).toEqual({ | ||
number: 1024, | ||
numberFixed: '1024', | ||
unit: 'B', | ||
numberUnit: '1024B', | ||
}); | ||
}); | ||
|
||
it('should convert iSize to Size with targetSizeUnit of "k"', () => { | ||
const sizeWithUnit = '1M'; | ||
const targetSizeUnit = 'k'; | ||
const result = iSizeToSize(sizeWithUnit, targetSizeUnit); | ||
expect(result).toEqual({ | ||
number: 1024, | ||
numberFixed: '1024.00', | ||
unit: 'k', | ||
numberUnit: '1024.00k', | ||
}); | ||
}); | ||
|
||
it('should convert iSize to Size with targetSizeUnit of "t"', () => { | ||
const sizeWithUnit = '1P'; | ||
const targetSizeUnit = 't'; | ||
const result = iSizeToSize(sizeWithUnit, targetSizeUnit); | ||
expect(result).toEqual({ | ||
number: 0.0009765625, | ||
numberFixed: '0.00', | ||
unit: 't', | ||
numberUnit: '0.00t', | ||
}); | ||
}); | ||
|
||
it('should throw an error if size format is invalid', () => { | ||
const sizeWithUnit = 'invalid'; | ||
expect(() => iSizeToSize(sizeWithUnit)).toThrow('Invalid size format'); | ||
}); | ||
|
||
it('should use default targetSizeUnit and fixed values if not provided', () => { | ||
const sizeWithUnit = '1K'; | ||
const result = iSizeToSize(sizeWithUnit); | ||
expect(result).toEqual({ | ||
number: 1, | ||
numberFixed: '1.00', | ||
unit: 'K', | ||
numberUnit: '1.00K', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.