| 1 |
import React from 'react'; |
| 2 |
import Table from '@material-ui/core/Table'; |
| 3 |
import TableBody from '@material-ui/core/TableBody'; |
| 4 |
import TableCell from '@material-ui/core/TableCell'; |
| 5 |
import TableHead from '@material-ui/core/TableHead'; |
| 6 |
import TableRow from '@material-ui/core/TableRow'; |
| 7 |
import {withStyles} from '@material-ui/core/styles'; |
| 8 |
import Grid from '@material-ui/core/Grid'; |
| 9 |
import AMQPAPI from '../../AMQPApi' |
| 10 |
import Paper from '@material-ui/core/Paper'; |
| 11 |
|
| 12 |
const styles = theme => ({ |
| 13 |
tableContainer: { |
| 14 |
width: '100%', |
| 15 |
}, |
| 16 |
table: { |
| 17 |
width: '100%', |
| 18 |
}, |
| 19 |
formContainer: { |
| 20 |
width: 300, |
| 21 |
}, |
| 22 |
form: { |
| 23 |
marginLeft: theme.spacing.unit * 1, |
| 24 |
}, |
| 25 |
submit: { |
| 26 |
marginTop: theme.spacing.unit * 3, |
| 27 |
}, |
| 28 |
tableHead: { |
| 29 |
fontWeight: 'bold', |
| 30 |
} |
| 31 |
}); |
| 32 |
|
| 33 |
class Connections extends React.Component { |
| 34 |
constructor() { |
| 35 |
super(); |
| 36 |
|
| 37 |
this.state = { |
| 38 |
connections: [], |
| 39 |
}; |
| 40 |
|
| 41 |
this.loadConnections() |
| 42 |
} |
| 43 |
|
| 44 |
loadConnections = () => { |
| 45 |
AMQPAPI.get('/connections') |
| 46 |
.then(response => this.setState({connections: response.data.items ? response.data.items : []})) |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
tableItems = () => { |
| 51 |
const {classes} = this.props; |
| 52 |
let rowId = 0; |
| 53 |
|
| 54 |
return ( |
| 55 |
<Table className={classes.table}> |
| 56 |
<TableHead> |
| 57 |
<TableRow> |
| 58 |
<TableCell className={classes.tableHead}>ID</TableCell> |
| 59 |
<TableCell className={classes.tableHead}>Virtual Host</TableCell> |
| 60 |
<TableCell className={classes.tableHead}>Name</TableCell> |
| 61 |
<TableCell className={classes.tableHead}>User name</TableCell> |
| 62 |
<TableCell className={classes.tableHead}>Protocol</TableCell> |
| 63 |
<TableCell className={classes.tableHead} numeric>Channels</TableCell> |
| 64 |
<TableCell className={classes.tableHead}>From client</TableCell> |
| 65 |
<TableCell className={classes.tableHead}>To client</TableCell> |
| 66 |
</TableRow> |
| 67 |
</TableHead> |
| 68 |
<TableBody> |
| 69 |
{this.state.connections.map(row => { |
| 70 |
return ( |
| 71 |
<TableRow key={rowId++}> |
| 72 |
<TableCell>{row.id}</TableCell> |
| 73 |
<TableCell>{row.vhost}</TableCell> |
| 74 |
<TableCell>{row.addr}</TableCell> |
| 75 |
<TableCell>{row.user}</TableCell> |
| 76 |
<TableCell>{row.protocol}</TableCell> |
| 77 |
<TableCell numeric>{row.channels_count}</TableCell> |
| 78 |
<TableCell>{this.transformTraffic(row.from_client)}</TableCell> |
| 79 |
<TableCell>{this.transformTraffic(row.to_client)}</TableCell> |
| 80 |
</TableRow> |
| 81 |
); |
| 82 |
})} |
| 83 |
</TableBody> |
| 84 |
</Table> |
| 85 |
) |
| 86 |
}; |
| 87 |
|
| 88 |
transformTraffic = (trackValue) => { |
| 89 |
let value = 0; |
| 90 |
if (trackValue) { |
| 91 |
value = trackValue.value; |
| 92 |
} |
| 93 |
if (value > 1024 * 1024) { |
| 94 |
value = Math.round(value * 100 / 1024 / 1024) / 100; |
| 95 |
return value + ' MB/s'; |
| 96 |
} else if (value > 1024) { |
| 97 |
value = Math.round(value * 100 / 1024) / 100; |
| 98 |
return value + ' KB/s'; |
| 99 |
} else { |
| 100 |
return value + ' B/s'; |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
render() { |
| 105 |
setTimeout(this.loadConnections, 5000); |
| 106 |
const {classes} = this.props; |
| 107 |
|
| 108 |
return ( |
| 109 |
<Grid |
| 110 |
container |
| 111 |
direction="column" |
| 112 |
justify="flex-start" |
| 113 |
alignItems="flex-start" |
| 114 |
> |
| 115 |
<Paper className={classes.tableContainer}> |
| 116 |
{this.tableItems()} |
| 117 |
</Paper> |
| 118 |
</Grid> |
| 119 |
) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
export default withStyles(styles)(Connections); |