forked from labring/sealos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PodDetailModal.tsx
341 lines (332 loc) · 11.1 KB
/
PodDetailModal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import React, { useState, useCallback, useRef, useEffect } from 'react';
import {
Modal,
ModalOverlay,
ModalContent,
ModalCloseButton,
ModalHeader,
ModalBody,
Box,
Flex,
Grid,
Button,
useTheme,
useDisclosure,
MenuButton
} from '@chakra-ui/react';
import { ChevronDownIcon } from '@chakra-ui/icons';
import type { PodDetailType, PodEvent } from '@/types/app';
import PodLineChart from '@/components/PodLineChart';
import { MOCK_PODS } from '@/mock/apps';
import { getPodEvents } from '@/api/app';
import { useQuery } from '@tanstack/react-query';
import { useLoading } from '@/hooks/useLoading';
import MyIcon from '@/components/Icon';
import { streamFetch } from '@/services/streamFetch';
import { useToast } from '@/hooks/useToast';
import MyMenu from '@/components/Menu';
import MyTooltip from '@/components/MyTooltip';
import styles from '../index.module.scss';
import { useTranslation } from 'next-i18next';
import { SHOW_EVENT_ANALYZE } from '@/store/static';
const Logs = ({
pod = MOCK_PODS[0],
pods = [],
podAlias,
setPodDetail,
closeFn
}: {
pod: PodDetailType;
pods: { alias: string; podName: string }[];
podAlias: string;
setPodDetail: (name: string) => void;
closeFn: () => void;
}) => {
const { t } = useTranslation();
const theme = useTheme();
const controller = useRef(new AbortController());
const { Loading } = useLoading();
const { toast } = useToast();
const [events, setEvents] = useState<PodEvent[]>([]);
const [eventAnalysesText, setEventAnalysesText] = useState('');
const { isOpen: isAnalyzing, onOpen: onStartAnalyses, onClose: onEndAnalyses } = useDisclosure();
const {
isOpen: isOpenAnalyses,
onOpen: onOpenAnalyses,
onClose: onCloseAnalyses
} = useDisclosure();
const RenderItem = useCallback(
({ label, children }: { label: string; children: React.ReactNode }) => {
return (
<Flex w={'100%'} my={5} alignItems="center">
<Box flex={'0 0 100px'} w={0}>
{label}
</Box>
<Box
flex={'1 0 0'}
w={0}
color={'myGray.600'}
userSelect={typeof children === 'string' ? 'all' : 'auto'}
>
{children}
</Box>
</Flex>
);
},
[]
);
const RenderTag = useCallback(({ children }: { children: string }) => {
return (
<MyTooltip label={children}>
<Box
py={1}
px={4}
backgroundColor={'myWhite.600'}
whiteSpace={'nowrap'}
overflow={'hidden'}
textOverflow={'ellipsis'}
color={'myGray.600'}
cursor={'default'}
border={'1px solid'}
borderColor={'myGray.100'}
>
{children}
</Box>
</MyTooltip>
);
}, []);
const { isLoading } = useQuery(['initPodEvents'], () => getPodEvents(pod.podName), {
refetchInterval: 3000,
onSuccess(res) {
setEvents(res);
}
});
useEffect(() => {
controller.current = new AbortController();
return () => {
controller.current?.abort();
};
}, []);
const onCloseAnalysesModel = useCallback(() => {
setEventAnalysesText('');
onCloseAnalyses();
controller.current?.abort();
controller.current = new AbortController();
}, [onCloseAnalyses]);
const onclickAnalyses = useCallback(async () => {
try {
onOpenAnalyses();
onStartAnalyses();
await streamFetch({
url: '/api/getPodEventsAnalyses',
data: events.map((item) => ({
reason: item.reason,
message: item.message,
count: item.count,
type: item.type,
firstTimestamp: item.firstTime,
lastTimestamp: item.lastTime
})),
abortSignal: controller.current,
onMessage: (text: string) => {
setEventAnalysesText((state) => (state += text));
}
});
} catch (err: any) {
toast({
title: typeof err === 'string' ? err : err?.message || '智能分析出错了~',
status: 'warning',
duration: 5000,
isClosable: true
});
onCloseAnalysesModel();
}
onEndAnalyses();
}, [events, onCloseAnalysesModel, onEndAnalyses, onOpenAnalyses, onStartAnalyses, toast]);
return (
<Modal isOpen={true} onClose={closeFn} size={'sm'} isCentered>
<ModalOverlay />
<ModalContent h={'90vh'} maxW={'90vw'} m={0} display={'flex'} flexDirection={'column'}>
<ModalCloseButton fontSize={16} top={6} right={6} />
<Flex p={7} alignItems={'center'}>
<Box mr={3} fontSize={'xl'} fontWeight={'bold'}>
Pod {t('Details')}
</Box>
<Box px={3}>
<MyMenu
width={240}
Button={
<MenuButton
minW={'240px'}
h={'32px'}
textAlign={'start'}
bg={'myWhite.400'}
border={theme.borders.base}
borderRadius={'md'}
>
<Flex px={4} alignItems={'center'}>
<Box flex={1}>{podAlias}</Box>
<ChevronDownIcon ml={2} />
</Flex>
</MenuButton>
}
menuList={pods.map((item) => ({
isActive: item.podName === pod.podName,
child: <Box>{item.alias}</Box>,
onClick: () => setPodDetail(item.podName)
}))}
/>
</Box>
</Flex>
<Grid gridTemplateColumns={'1fr 1fr'} gridGap={2} py={2} px={7}>
<Box>
<Box mb={3}>
CPU ({((pod.usedCpu[pod.usedCpu.length - 1] / pod.cpu) * 100).toFixed(2)}%)
</Box>
<Box h={'80px'} w={'100%'}>
<PodLineChart type={'blue'} limit={pod.cpu} data={pod.usedCpu} />
</Box>
</Box>
<Box>
<Box mb={3}>
{t('Memory')} (
{((pod.usedMemory[pod.usedMemory.length - 1] / pod.memory) * 100).toFixed(2)}
%)
</Box>
<Box h={'80px'} w={'100%'}>
<PodLineChart type={'purple'} limit={pod.memory} data={pod.usedMemory} />
</Box>
</Box>
</Grid>
<Grid py={5} flex={'1 0 0'} h={0} px={7} gridTemplateColumns={'450px 1fr'} gridGap={4}>
<Flex flexDirection={'column'} h={'100%'}>
<Box mb={4} color={'myGray.600'}>
{t('Details')}
</Box>
<Box
flex={'1 0 0'}
h={0}
backgroundColor={'myWhite.300'}
px={7}
py={3}
overflowY={'auto'}
>
<RenderItem label={t('Status')}>
<Box as="span" color={pod.status.color}>
{pod.status.label}
</Box>
</RenderItem>
<RenderItem label="Restarts">{pod.restarts}</RenderItem>
<RenderItem label="Age">{pod.age}</RenderItem>
<RenderItem label="Pod Name">{pod.podName}</RenderItem>
<RenderItem label="Controlled By">{`${pod.metadata?.ownerReferences?.[0].kind}/${pod.metadata?.ownerReferences?.[0].name}`}</RenderItem>
<RenderItem label="Labels">
<Grid gridTemplateColumns={'auto auto'} gridGap={2}>
{Object.entries(pod.metadata?.labels || {}).map(
([key, value]: [string, string]) => (
<RenderTag key={key}>{`${key}=${value}`}</RenderTag>
)
)}
</Grid>
</RenderItem>
<RenderItem label="Annotations">
{Object.entries(pod.metadata?.annotations || {}).map(
([key, value]: [string, string]) => (
<Box key={key} mb={2}>
<RenderTag>{`${key}=${value}`}</RenderTag>
</Box>
)
)}
</RenderItem>
</Box>
</Flex>
<Flex position={'relative'} flexDirection={'column'} h={'100%'}>
<Flex mb={4} alignItems={'center'}>
<Box color={'myGray.600'}>Events</Box>
{events.length > 0 && SHOW_EVENT_ANALYZE && (
<Button
ml={3}
size={'sm'}
variant={'base'}
leftIcon={<MyIcon name={'analyze'} />}
onClick={onclickAnalyses}
>
{t('Intelligent Analysis')}
</Button>
)}
</Flex>
<Box flex={'1 0 0'} h={0} overflowY={'auto'}>
{events.map((event, i) => (
<Box
key={event.id}
pl={6}
pb={6}
ml={4}
borderLeft={`2px solid ${i === events.length - 1 ? 'transparent' : '#DCE7F1'}`}
position={'relative'}
_before={{
content: '""',
position: 'absolute',
left: '-6.5px',
w: '8px',
h: '8px',
borderRadius: '8px',
backgroundColor: '#fff',
border: '2px solid',
borderColor: event.type === 'Warning' ? '#FF8492' : '#33BABB'
}}
>
<Flex lineHeight={1} mb={2} alignItems={'center'}>
<Box fontWeight={'bold'}>
{event.reason}, Last Occur: {event.lastTime}
</Box>
<Box ml={2} color={'blackAlpha.700'}>
First Seen: {event.firstTime}
</Box>
<Box ml={2} color={'blackAlpha.700'}>
count: {event.count}
</Box>
</Flex>
<Box color={'blackAlpha.700'}>{event.message}</Box>
</Box>
))}
{events.length === 0 && !isLoading && (
<Flex
alignItems={'center'}
justifyContent={'center'}
flexDirection={'column'}
h={'100%'}
>
<MyIcon name="noEvents" w={'48px'} h={'48px'} color={'transparent'} />
<Box mt={4} color={'myGray.600'}>
{t('暂无')} Events
</Box>
</Flex>
)}
</Box>
<Loading loading={isLoading} fixed={false} />
</Flex>
</Grid>
</ModalContent>
{/* analyses modal */}
<Modal isOpen={isOpenAnalyses} onClose={onCloseAnalysesModel}>
<ModalOverlay />
<ModalContent maxW={'50vw'} h={'70vh'}>
<ModalHeader>Pod {t('Intelligent Analysis')}</ModalHeader>
<ModalCloseButton />
<ModalBody
className={isAnalyzing ? styles.analysesAnimation : ''}
overflowY={'auto'}
h={'100%'}
whiteSpace={'pre-wrap'}
position={'relative'}
pb={2}
>
{eventAnalysesText}
</ModalBody>
</ModalContent>
</Modal>
</Modal>
);
};
export default Logs;