Don't get stuck loading if there are no items

Also refactored the code into different functions and made the progress
bars look nicer.

Signed-off-by: Mcat12 <newtoncat12@yahoo.com>
This commit is contained in:
Mcat12
2018-06-14 15:49:33 -04:00
parent d2037fd519
commit 85525d2db8
3 changed files with 153 additions and 109 deletions
+50 -36
View File
@@ -14,6 +14,7 @@ import { api, ignoreCancel, makeCancelable } from '../utils';
class TopBlocked extends Component {
state = {
loading: true,
total_blocked: 0,
top_blocked: []
};
@@ -27,12 +28,59 @@ class TopBlocked extends Component {
this.updateHandler = makeCancelable(api.getTopBlocked(), { repeat: this.updateChart, interval: 10 * 1000 });
this.updateHandler.promise.then(res => {
this.setState({
loading: false,
total_blocked: res.blocked_queries,
top_blocked: res.top_blocked
});
}).catch(ignoreCancel);
}
generateTable = t => {
if(this.state.top_blocked.length === 0) {
return t("No Domains Found");
}
return (
<table className="table table-bordered">
<tbody>
<tr>
<th>{t("Domain")}</th>
<th>{t("Hits")}</th>
<th>{t("Frequency")}</th>
</tr>
{this.generateRows(t)}
</tbody>
</table>
);
};
generateRows = t => {
return this.state.top_blocked.map(item => {
const percentage = item.count / this.state.total_blocked * 100;
return (
<tr key={item.domain}>
<td>
{item.domain}
</td>
<td>
{item.count.toLocaleString()}
</td>
<td style={{"verticalAlign": "middle"}}>
<div className="progress"
title={
t("{{percent}}% of {{total}}", {
percent: percentage.toFixed(1),
total: this.state.total_blocked.toLocaleString()
})
}>
<div className="progress-bar bg-warning" style={{width: percentage + "%"}}/>
</div>
</td>
</tr>
);
});
};
componentDidMount() {
this.updateChart();
}
@@ -52,45 +100,11 @@ class TopBlocked extends Component {
</div>
<div className="card-block">
<div style={{overflowX: "auto"}}>
<table className="table table-bordered">
<tbody>
<tr>
<th>{t("Domain")}</th>
<th>{t("Hits")}</th>
<th>{t("Frequency")}</th>
</tr>
{
this.state.top_blocked.map(item => {
const percentage = item.count / this.state.total_blocked * 100;
return (
<tr key={item.domain}>
<td>
{item.domain}
</td>
<td>
{item.count.toLocaleString()}
</td>
<td>
<div className="progress progress-sm"
title={
t("{{percent}}% of {{total}}", {
percent: percentage.toFixed(1),
total: this.state.total_blocked.toLocaleString()
})
}>
<div className="progress-bar bg-warning" style={{width: percentage + "%"}}/>
</div>
</td>
</tr>
);
})
}
</tbody>
</table>
{this.generateTable(t)}
</div>
</div>
{
this.state.total_blocked === 0
this.state.loading
?
<div className="card-img-overlay" style={{background: "rgba(255,255,255,0.7)"}}>
<i className="fa fa-refresh fa-spin" style={{position: "absolute", top: "50%", left: "50%", fontSize: "30px"}}/>
+51 -37
View File
@@ -14,6 +14,7 @@ import { api, ignoreCancel, makeCancelable } from '../utils';
class TopClients extends Component {
state = {
loading: true,
total_queries: 0,
top_clients: []
};
@@ -27,12 +28,60 @@ class TopClients extends Component {
this.updateHandler = makeCancelable(api.getTopClients(), { repeat: this.updateChart, interval: 10 * 1000 });
this.updateHandler.promise.then(res => {
this.setState({
loading: false,
total_queries: res.total_queries,
top_clients: res.top_clients
});
}).catch(ignoreCancel);
}
generateTable = t => {
if(this.state.top_clients.length === 0) {
return t("No Clients Found");
}
return (
<table className="table table-bordered">
<tbody>
<tr>
<th>{t("Client")}</th>
<th>{t("Requests")}</th>
<th>{t("Frequency")}</th>
</tr>
{this.generateRows(t)}
</tbody>
</table>
);
};
generateRows = t => {
return this.state.top_clients.map(item => {
const percentage = item.count / this.state.total_queries * 100;
return (
<tr key={item.name + "|" + item.ip}>
<td>
{item.name !== "" ? item.name : item.ip}
</td>
<td>
{item.count.toLocaleString()}
</td>
<td style={{"verticalAlign": "middle"}}>
<div className="progress"
title={
t("{{percent}}% of {{total}}", {
percent: percentage.toFixed(1),
total: this.state.total_queries.toLocaleString()
})
}>
<div className="progress-bar bg-primary" style={{width: percentage + "%"}}/>
</div>
</td>
</tr>
);
});
};
componentDidMount() {
this.updateChart();
}
@@ -52,46 +101,11 @@ class TopClients extends Component {
</div>
<div className="card-block">
<div style={{overflowX: "auto"}}>
<table className="table table-bordered">
<tbody>
<tr>
<th>{t("Client")}</th>
<th>{t("Requests")}</th>
<th>{t("Frequency")}</th>
</tr>
{
this.state.top_clients.map(item => {
const percentage = item.count / this.state.total_queries * 100;
return (
<tr key={item.name + "|" + item.ip}>
<td>
{item.name !== "" ? item.name : item.ip}
</td>
<td>
{item.count.toLocaleString()}
</td>
<td>
<div className="progress progress-sm"
title={
t("{{percent}}% of {{total}}", {
percent: percentage.toFixed(1),
total: this.state.total_queries.toLocaleString()
})
}>
<div className="progress-bar bg-primary" style={{width: percentage + "%"}}/>
</div>
</td>
</tr>
);
})
}
</tbody>
</table>
{this.generateTable(t)}
</div>
</div>
{
this.state.total_queries === 0
this.state.loading
?
<div className="card-img-overlay" style={{background: "rgba(255,255,255,0.7)"}}>
<i className="fa fa-refresh fa-spin" style={{position: "absolute", top: "50%", left: "50%", fontSize: "30px"}}/>
+52 -36
View File
@@ -14,6 +14,7 @@ import { api, ignoreCancel, makeCancelable } from '../utils';
class TopDomains extends Component {
state = {
loading: true,
total_queries: 0,
top_domains: []
};
@@ -27,12 +28,61 @@ class TopDomains extends Component {
this.updateHandler = makeCancelable(api.getTopDomains(), { repeat: this.updateChart, interval: 10 * 1000 });
this.updateHandler.promise.then(res => {
this.setState({
loading: false,
total_queries: res.total_queries,
top_domains: res.top_domains
});
}).catch(ignoreCancel);
}
generateTable = t => {
if(this.state.top_domains.length === 0) {
return t("No Domains Found");
}
return (
<table className="table table-bordered">
<tbody>
<tr>
<th>{t("Domain")}</th>
<th>{t("Hits")}</th>
<th>{t("Frequency")}</th>
</tr>
{
this.generateRows(t)
}
</tbody>
</table>
);
};
generateRows = t => {
return this.state.top_domains.map(item => {
const percentage = item.count / this.state.total_queries * 100;
return (
<tr key={item.domain}>
<td>
{item.domain}
</td>
<td>
{item.count.toLocaleString()}
</td>
<td style={{"verticalAlign": "middle"}}>
<div className="progress"
title={
t("{{percent}}% of {{total}}", {
percent: percentage.toFixed(1),
total: this.state.total_queries.toLocaleString()
})
}>
<div className="progress-bar bg-success" style={{width: percentage + "%"}}/>
</div>
</td>
</tr>
);
});
};
componentDidMount() {
this.updateChart();
}
@@ -52,45 +102,11 @@ class TopDomains extends Component {
</div>
<div className="card-block">
<div style={{overflowX: "auto"}}>
<table className="table table-bordered">
<tbody>
<tr>
<th>{t("Domain")}</th>
<th>{t("Hits")}</th>
<th>{t("Frequency")}</th>
</tr>
{
this.state.top_domains.map(item => {
const percentage = item.count / this.state.total_queries * 100;
return (
<tr key={item.domain}>
<td>
{item.domain}
</td>
<td>
{item.count.toLocaleString()}
</td>
<td>
<div className="progress progress-sm"
title={
t("{{percent}}% of {{total}}", {
percent: percentage.toFixed(1),
total: this.state.total_queries.toLocaleString()
})
}>
<div className="progress-bar bg-success" style={{width: percentage + "%"}}/>
</div>
</td>
</tr>
);
})
}
</tbody>
</table>
{this.generateTable(t)}
</div>
</div>
{
this.state.total_queries === 0
this.state.loading
?
<div className="card-img-overlay" style={{background: "rgba(255,255,255,0.7)"}}>
<i className="fa fa-refresh fa-spin" style={{position: "absolute", top: "50%", left: "50%", fontSize: "30px"}}/>