badguardhome/client/src/components/Dashboard/Clients.js

86 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-10-12 13:02:01 +00:00
import React, { Component } from 'react';
2018-08-30 14:25:33 +00:00
import ReactTable from 'react-table';
import PropTypes from 'prop-types';
import map from 'lodash/map';
import { Trans, withNamespaces } from 'react-i18next';
2018-08-30 14:25:33 +00:00
import Card from '../ui/Card';
2018-10-12 13:02:01 +00:00
import Cell from '../ui/Cell';
2018-08-30 14:25:33 +00:00
2019-03-20 14:04:32 +00:00
import { getPercent, getClientName } from '../../helpers/helpers';
2018-10-12 13:02:01 +00:00
import { STATUS_COLORS } from '../../helpers/constants';
class Clients extends Component {
getPercentColor = (percent) => {
if (percent > 50) {
return STATUS_COLORS.green;
} else if (percent > 10) {
return STATUS_COLORS.yellow;
}
return STATUS_COLORS.red;
}
columns = [{
Header: 'IP',
accessor: 'ip',
2019-03-20 14:04:32 +00:00
Cell: ({ value }) => {
const clientName = getClientName(this.props.clients, value);
let client;
if (clientName) {
client = <span>{clientName} <small>({value})</small></span>;
} else {
client = value;
}
return (
<div className="logs__row logs__row--overflow">
<span className="logs__text" title={value}>
{client}
</span>
</div>
);
},
2018-10-29 12:24:35 +00:00
sortMethod: (a, b) => parseInt(a.replace(/\./g, ''), 10) - parseInt(b.replace(/\./g, ''), 10),
2018-10-12 13:02:01 +00:00
}, {
2018-11-09 06:51:28 +00:00
Header: <Trans>requests_count</Trans>,
2018-10-12 13:02:01 +00:00
accessor: 'count',
Cell: ({ value }) => {
const percent = getPercent(this.props.dnsQueries, value);
const percentColor = this.getPercentColor(percent);
return (
<Cell value={value} percent={percent} color={percentColor} />
);
},
}];
render() {
const { t } = this.props;
2018-10-12 13:02:01 +00:00
return (
2018-11-09 06:51:28 +00:00
<Card title={ t('top_clients') } subtitle={ t('for_last_24_hours') } bodyType="card-table" refresh={this.props.refreshButton}>
2018-10-12 13:02:01 +00:00
<ReactTable
data={map(this.props.topClients, (value, prop) => (
{ ip: prop, count: value }
))}
columns={this.columns}
showPagination={false}
2018-11-09 06:51:28 +00:00
noDataText={ t('no_clients_found') }
2018-10-12 13:02:01 +00:00
minRows={6}
className="-striped -highlight card-table-overflow"
/>
</Card>
);
}
}
2018-08-30 14:25:33 +00:00
Clients.propTypes = {
topClients: PropTypes.object.isRequired,
2018-10-12 13:02:01 +00:00
dnsQueries: PropTypes.number.isRequired,
refreshButton: PropTypes.node.isRequired,
2019-03-20 14:04:32 +00:00
clients: PropTypes.array.isRequired,
t: PropTypes.func,
2018-08-30 14:25:33 +00:00
};
export default withNamespaces()(Clients);