/
index.jsx
283 lines (266 loc) · 8.82 KB
/
index.jsx
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
import React, { Component, Fragment } from 'react';
import { pipe, map, isEmpty, defaultTo, sort as rSort } from 'ramda';
import memoize from 'fast-memoize';
import { camelCase } from 'change-case/change-case';
import { withStyles } from '@material-ui/core/styles';
import TableRow from '@material-ui/core/TableRow';
import Drawer from '@material-ui/core/Drawer';
import TableCell from '@material-ui/core/TableCell';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemText from '@material-ui/core/ListItemText';
import Typography from '@material-ui/core/Typography';
import InformationVariantIcon from 'mdi-react/InformationVariantIcon';
import TableCellItem from '../TableCellItem';
import { awsProvisionerHealth } from '../../utils/prop-types';
import sort from '../../utils/sort';
import DataTable from '../DataTable';
const sorted = pipe(
rSort((a, b) =>
sort(
`${a.region}-${a.az}-${a.instanceType}`,
`${b.region}-${b.az}-${b.instanceType}`
)
),
map(({ region, az, instanceType }) => `${region}-${az}-${instanceType}`)
);
const iconSize = 18;
const or0 = defaultTo(0);
@withStyles(theme => ({
drawerContainer: {
paddingTop: theme.spacing.double,
paddingBottom: theme.spacing.double,
width: 400,
},
headline: {
paddingLeft: theme.spacing.triple,
paddingRight: theme.spacing.triple,
},
emptyText: {
marginTop: theme.spacing.unit,
},
}))
export default class AwsProvisionerHealthTable extends Component {
static propTypes = {
/** A GraphQL awsProvisionerHealth response. */
healthData: awsProvisionerHealth.isRequired,
};
state = {
sortBy: null,
sortDirection: null,
drawerOpen: false,
};
createSortedHealth = memoize(
(healthData, sortBy, sortDirection) => {
const healthSummary = {};
const { requestHealth, terminationHealth, running } = healthData;
const identifier = item =>
`${item.az}-${item.region}-${item.instanceType}`;
requestHealth.forEach(item => {
healthSummary[identifier(item)] = {
...healthSummary[identifier(item)],
...item,
};
});
terminationHealth.forEach(item => {
healthSummary[identifier(item)] = {
...healthSummary[identifier(item)],
...item,
};
});
running.forEach(item => {
healthSummary[identifier(item)] = {
...healthSummary[identifier(item)],
...item,
};
});
Object.entries(healthSummary).forEach(([key, item]) => {
healthSummary[key].healthy =
or0(item.successful) + or0(item.cleanShutdown) + or0(item.running);
healthSummary[key].unhealthy =
or0(item.failed) +
or0(item.spotKill) +
or0(item.insufficientCapacity) +
or0(item.volumeLimitExceeded) +
or0(item.missingAmi) +
or0(item.startupFailed) +
or0(item.unknownCodes) +
or0(item.noCode);
});
const sortByProperty = camelCase(sortBy);
const healthValues = Object.values(healthSummary);
if (!sortBy || isEmpty(healthValues)) {
return healthValues;
}
return healthValues.sort((a, b) => {
const firstElement =
sortDirection === 'desc' ? b[sortByProperty] : a[sortByProperty];
const secondElement =
sortDirection === 'desc' ? a[sortByProperty] : b[sortByProperty];
return sort(firstElement, secondElement);
});
},
{
serializer: ([healthData, sortBy, sortDirection]) => {
const ids = sorted(healthData.running);
return `${ids.join('-')}-${sortBy}-${sortDirection}`;
},
}
);
handleDrawerClose = () => {
this.setState({
drawerOpen: false,
drawerItem: null,
});
};
handleDrawerOpen = (healthItem, columnName) => {
this.setState({
drawerOpen: true,
drawerItem: {
...healthItem,
columnName,
},
});
};
handleHeaderClick = sortBy => {
const toggled = this.state.sortDirection === 'desc' ? 'asc' : 'desc';
const sortDirection = this.state.sortBy === sortBy ? toggled : 'desc';
this.setState({ sortBy, sortDirection });
};
render() {
const { classes, healthData } = this.props;
const { sortBy, sortDirection, drawerOpen, drawerItem } = this.state;
const sortedHealth = this.createSortedHealth(
healthData,
sortBy,
sortDirection
);
return (
<Fragment>
<DataTable
items={sortedHealth}
headers={['AZ', 'Region', 'Instance Type', 'Healthy', 'Unhealthy']}
sortByHeader={sortBy}
sortDirection={sortDirection}
onHeaderClick={this.handleHeaderClick}
noItemsMessage="Health stats not available"
renderRow={item => (
<TableRow key={`${item.az}-${item.region}-${item.instanceType}`}>
<TableCell>
<TableCellItem dense>
<ListItemText primary={item.az} />
</TableCellItem>
</TableCell>
<TableCell>
<TableCellItem dense>
<ListItemText primary={item.region} />
</TableCellItem>
</TableCell>
<TableCell>
<TableCellItem dense>
<ListItemText primary={item.instanceType} />
</TableCellItem>
</TableCell>
<TableCell>
<TableCellItem
button
dense
onClick={() => this.handleDrawerOpen(item, 'Healthy')}>
<ListItemText primary={or0(item.healthy)} />
<InformationVariantIcon size={iconSize} />
</TableCellItem>
</TableCell>
<TableCell>
<TableCellItem
button
dense
onClick={() => this.handleDrawerOpen(item, 'Unhealthy')}>
<ListItemText primary={or0(item.unhealthy)} />
<InformationVariantIcon size={iconSize} />
</TableCellItem>
</TableCell>
</TableRow>
)}
/>
<Drawer
anchor="right"
open={drawerOpen}
onClose={this.handleDrawerClose}>
<div className={classes.drawerContainer}>
<Typography variant="h5" className={classes.headline}>
{drawerItem && drawerItem.columnName}
</Typography>
{drawerItem && drawerItem.columnName === 'Healthy' && (
<List>
<ListItem>
<ListItemText
primary="Successful Requests"
secondary={or0(drawerItem.successful)}
/>
</ListItem>
<ListItem>
<ListItemText
primary="Clean Shutdown"
secondary={or0(drawerItem.cleanShutdown)}
/>
</ListItem>
<ListItem>
<ListItemText
primary="Running"
secondary={or0(drawerItem.running)}
/>
</ListItem>
</List>
)}
{drawerItem && drawerItem.columnName === 'Unhealthy' && (
<List>
<ListItem>
<ListItemText
primary="Failed Requests"
secondary={or0(drawerItem.failed)}
/>
</ListItem>
<ListItem>
<ListItemText
primary="Spot Kill"
secondary={or0(drawerItem.spotKill)}
/>
</ListItem>
<ListItem>
<ListItemText
primary="Insufficient Capacity"
secondary={or0(drawerItem.insufficientCapacity)}
/>
</ListItem>
<ListItem>
<ListItemText
primary="Volume Limit Exceeded"
secondary={or0(drawerItem.volumeLimitExceeded)}
/>
</ListItem>
<ListItem>
<ListItemText
primary="Missing AMI"
secondary={or0(drawerItem.missingAmi)}
/>
</ListItem>
<ListItem>
<ListItemText
primary="Unknown Codes"
secondary={or0(drawerItem.unknownCodes)}
/>
</ListItem>
<ListItem>
<ListItemText
primary="No Codes"
secondary={or0(drawerItem.noCode)}
/>
</ListItem>
</List>
)}
</div>
</Drawer>
</Fragment>
);
}
}