forked from kubernetes-sigs/headlamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverview.tsx
More file actions
208 lines (195 loc) · 6.51 KB
/
Overview.tsx
File metadata and controls
208 lines (195 loc) · 6.51 KB
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
import { FormControlLabel, Switch } from '@mui/material';
import Grid from '@mui/material/Grid';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { useLocation } from 'react-router';
import Event from '../../lib/k8s/event';
import Node from '../../lib/k8s/node';
import Pod from '../../lib/k8s/pod';
import { useFilterFunc } from '../../lib/util';
import { OverviewChart } from '../../redux/overviewChartsSlice';
import { useTypedSelector } from '../../redux/reducers/reducers';
import { DateLabel, Link, PageGrid, StatusLabel } from '../common';
import ResourceListView from '../common/Resource/ResourceListView';
import { SectionBox } from '../common/SectionBox';
import ShowHideLabel from '../common/ShowHideLabel';
import { LightTooltip } from '../common/Tooltip';
import {
CpuCircularChart,
MemoryCircularChart,
NodesStatusCircleChart,
PodsStatusCircleChart,
} from './Charts';
import { ClusterGroupErrorMessage } from './ClusterGroupErrorMessage';
export default function Overview() {
const { t } = useTranslation(['translation']);
const [pods] = Pod.useList();
const [nodes] = Node.useList();
const [nodeMetrics, metricsError] = Node.useMetrics();
const chartProcessors = useTypedSelector(state => state.overviewCharts.processors);
const noMetrics = metricsError?.status === 404;
const noPermissions = metricsError?.status === 403;
// Process the default charts through any registered processors
const defaultCharts: OverviewChart[] = [
{
id: 'cpu',
component: () => (
<CpuCircularChart items={nodes} itemsMetrics={nodeMetrics} noMetrics={noMetrics} />
),
},
{
id: 'memory',
component: () => (
<MemoryCircularChart items={nodes} itemsMetrics={nodeMetrics} noMetrics={noMetrics} />
),
},
{
id: 'pods',
component: () => <PodsStatusCircleChart items={pods} />,
},
{
id: 'nodes',
component: () => <NodesStatusCircleChart items={nodes} />,
},
];
const charts = chartProcessors.reduce(
(currentCharts, p) => p.processor(currentCharts),
defaultCharts
);
return (
<PageGrid>
<SectionBox title={t('translation|Overview')} py={2} mt={[4, 0, 0]}>
{noPermissions ? (
<ClusterGroupErrorMessage errors={[metricsError]} />
) : (
<Grid container justifyContent="flex-start" alignItems="stretch" spacing={4}>
{charts.map(chart => (
<Grid key={chart.id} item xs sx={{ maxWidth: '300px' }}>
<chart.component />
</Grid>
))}
</Grid>
)}
</SectionBox>
<EventsSection />
</PageGrid>
);
}
function EventsSection() {
const EVENT_WARNING_SWITCH_FILTER_STORAGE_KEY = 'EVENT_WARNING_SWITCH_FILTER_STORAGE_KEY';
const EVENT_WARNING_SWITCH_DEFAULT = true;
const { t } = useTranslation(['translation', 'glossary']);
const location = useLocation();
const queryParams = new URLSearchParams(location.search);
const eventsFilter = queryParams.get('eventsFilter');
const filterFunc = useFilterFunc<Event>(['.jsonData.involvedObject.kind']);
const [isWarningEventSwitchChecked, setIsWarningEventSwitchChecked] = React.useState(
Boolean(
JSON.parse(
localStorage.getItem(EVENT_WARNING_SWITCH_FILTER_STORAGE_KEY) ||
EVENT_WARNING_SWITCH_DEFAULT.toString()
)
)
);
const { items: events, errors: eventsErrors } = Event.useList({ limit: Event.maxLimit });
const warningActionFilterFunc = (event: Event, search?: string) => {
if (!filterFunc(event, search)) {
return false;
}
if (isWarningEventSwitchChecked) {
return event.jsonData.type === 'Warning';
}
// Return true because if we reach this point, it means we're only filtering by
// the default filterFunc (and its result was 'true').
return true;
};
const numWarnings = React.useMemo(
() => events?.filter(e => e.type === 'Warning').length ?? '?',
[events]
);
function makeStatusLabel(event: Event) {
return (
<StatusLabel
status={event.type === 'Normal' ? '' : 'warning'}
sx={theme => ({
[theme.breakpoints.up('md')]: {
display: 'unset',
},
})}
>
{event.reason}
</StatusLabel>
);
}
function makeObjectLink(event: Event) {
const obj = event.involvedObjectInstance;
if (!!obj) {
return <Link kubeObject={obj} />;
}
return event.involvedObject.name;
}
return (
<ResourceListView
title={t('glossary|Events')}
headerProps={{
noNamespaceFilter: false,
titleSideActions: [
<FormControlLabel
checked={isWarningEventSwitchChecked}
label={t('Only warnings ({{ numWarnings }})', { numWarnings })}
control={<Switch color="primary" />}
onChange={(event, checked) => {
localStorage.setItem(EVENT_WARNING_SWITCH_FILTER_STORAGE_KEY, checked.toString());
setIsWarningEventSwitchChecked(checked);
}}
/>,
],
}}
defaultGlobalFilter={eventsFilter ?? undefined}
data={events}
errors={eventsErrors}
columns={[
{
label: t('Type'),
getValue: event => event.involvedObject.kind,
},
{
label: t('Name'),
getValue: event => event.involvedObjectInstance?.getName() ?? event.involvedObject.name,
render: event => makeObjectLink(event),
gridTemplate: 1.5,
},
'namespace',
'cluster',
{
label: t('Reason'),
getValue: event => event.reason,
render: event => (
<LightTooltip title={event.reason} interactive>
{makeStatusLabel(event)}
</LightTooltip>
),
},
{
label: t('Message'),
getValue: event => event.message ?? '',
render: event => (
<ShowHideLabel labelId={event.metadata?.uid || ''}>{event.message || ''}</ShowHideLabel>
),
gridTemplate: 1.5,
},
{
id: 'last-seen',
label: t('Last Seen'),
gridTemplate: 'min-content',
cellProps: { align: 'right' },
getValue: event => -new Date(event.lastOccurrence).getTime(),
render: event => <DateLabel date={event.lastOccurrence} format="mini" />,
},
]}
filterFunction={warningActionFilterFunc}
defaultSortingColumn={{ id: 'last-seen', desc: false }}
id="headlamp-cluster.overview.events"
/>
);
}