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

104 lines
3.1 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 { 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;
};
2018-10-12 13:02:01 +00:00
columns = [
{
Header: 'IP',
accessor: 'ip',
Cell: ({ value }) => {
const clientName =
getClientName(this.props.clients, value) ||
getClientName(this.props.autoClients, value);
let client;
2019-03-20 14:04:32 +00:00
if (clientName) {
client = (
<span>
{clientName} <small>({value})</small>
</span>
);
} else {
client = value;
}
2019-03-20 14:04:32 +00:00
return (
<div className="logs__row logs__row--overflow">
<span className="logs__text" title={value}>
{client}
</span>
</div>
);
},
sortMethod: (a, b) =>
parseInt(a.replace(/\./g, ''), 10) - parseInt(b.replace(/\./g, ''), 10),
2019-03-20 14:04:32 +00:00
},
{
Header: <Trans>requests_count</Trans>,
accessor: 'count',
Cell: ({ value }) => {
const percent = getPercent(this.props.dnsQueries, value);
const percentColor = this.getPercentColor(percent);
2018-10-12 13:02:01 +00:00
return <Cell value={value} percent={percent} color={percentColor} />;
},
2018-10-12 13:02:01 +00:00
},
];
2018-10-12 13:02:01 +00:00
render() {
const {
t, refreshButton, topClients, subtitle,
} = this.props;
2018-10-12 13:02:01 +00:00
return (
<Card
title={t('top_clients')}
subtitle={subtitle}
bodyType="card-table"
refresh={refreshButton}
>
2018-10-12 13:02:01 +00:00
<ReactTable
data={topClients.map(item => ({
ip: item.name,
count: item.count,
}))}
2018-10-12 13:02:01 +00:00
columns={this.columns}
showPagination={false}
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.array.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,
2019-05-23 11:14:22 +00:00
autoClients: PropTypes.array.isRequired,
subtitle: PropTypes.string.isRequired,
t: PropTypes.func.isRequired,
2018-08-30 14:25:33 +00:00
};
export default withNamespaces()(Clients);