Merge pull request #50 from pi-hole/feature/doughnut-charts

Doughnut Charts
This commit is contained in:
Mark Drobnak
2018-06-17 12:51:23 -04:00
committed by GitHub
17 changed files with 401 additions and 91 deletions
+47
View File
@@ -72,6 +72,51 @@ function summary() {
};
}
function queryTypes() {
const data = [];
const total = faker.random.number();
const names = [
"A (IPv4)",
"AAAA (IPv6)",
"ANY",
"SRV",
"SOA",
"PTR",
"TXT"
];
const step = total / names.length;
for(const name of names) {
data.push({
name,
percent: step / total
});
}
return data;
}
function forwardDestinations(length) {
const totalQueries = faker.random.number();
const destinations = [];
const numbers = [];
const names = unique(faker.internet.domainName, length);
const ipAddrs = unique(faker.internet.ip, length);
for(let i = 0; i < length; i++)
numbers.push(faker.random.number({ max: totalQueries }));
for(let i = 0; i < length; i++) {
destinations.push({
name: names[i],
ip: ipAddrs[i],
percent: numbers[i] / totalQueries
});
}
return destinations;
}
function topList(length, max, fakeData) {
const result = [];
const numbers = [];
@@ -197,6 +242,8 @@ write("public/fakeAPI/stats/overTime/history", historyOverTime(144));
write("public/fakeAPI/stats/overTime/clients", clientsOverTime(144, 5));
write("public/fakeAPI/stats/summary", summary());
write("public/fakeAPI/stats/history", history(5000));
write("public/fakeAPI/stats/query_types", queryTypes());
write("public/fakeAPI/stats/forward_destinations", forwardDestinations(3));
write("public/fakeAPI/stats/top_blocked", topBlocked(10));
write("public/fakeAPI/stats/top_domains", topDomains(10));
write("public/fakeAPI/stats/top_clients", topClients(10));
+2 -1
View File
@@ -18,5 +18,6 @@
"Client activity from {{from}} to {{to}}": "Client activity from {{from}} to {{to}}",
"{{percent}}% of {{total}}": "{{percent}}% of {{total}}",
"No Clients Found": "No Clients Found",
"No Domains Found": "No Domains Found"
"No Domains Found": "No Domains Found",
"Queries Answered By Destination": "Queries Answered By Destination"
}
+3 -1
View File
@@ -16,5 +16,7 @@
"Total Queries": "Peticiones totales",
"Blocked Queries": "Bloqueadas",
"Client activity from {{from}} to {{to}}": "Actividad del cliente {{from}} a {{to}}",
"{{percent}}% of {{total}}": "{{percent}}% de {{total}}"
"{{percent}}% of {{total}}": "{{percent}}% de {{total}}",
"No Clients Found": "No se han encontrado clientes",
"No Domains Found": "No se han encontrado Dominios"
}
-15
View File
@@ -1,15 +0,0 @@
{
"Status": "toestand",
"Donate": "schenken",
"Domain": "Domein",
"Hits": "Hits",
"Frequency": "Frequentie",
"Client": "Cliënt",
"Requests": "verzoeken",
"Add": "Toevoegen",
"Time": "Tijd",
"Type": "Type",
"Action": "Actie",
"Enabled": "Activeren",
"Disabled": "Deactiveren"
}
-17
View File
@@ -1,17 +0,0 @@
{
"Domains On Blocklist": "Domeinen op blacklist",
"Query Types": "Query Types",
"Forward Destinations": "forward bestemmingen",
"Top Permitted Domains": "Top Domeinen",
"Top Blocked Domains": "Top Adverteerders",
"Top Clients": "Top Cliënten",
"Total Queries ({{count}} clients)": "",
"Total Queries ({{count}} clients)_plural": "",
"Queries Blocked": "Geblokkeerde querys",
"Percent Blocked": "Procent geblokkeerd",
"Queries Over Last 24 Hours": "Queries over de laatste 24 uur",
"Queries Answered By": "Querys beantword door",
"Queries from {{from}} to {{to}}": "Querys van naar",
"Total Queries": "Totaal aantal querys",
"Blocked Queries": "Geblokkeerde querys"
}
-5
View File
@@ -1,5 +0,0 @@
{
"if you found this useful": "Als u dit nuttig vond",
"Core Version": "Pi-hole Version",
"Web Version": "Web Interface Version"
}
-12
View File
@@ -1,12 +0,0 @@
{
"Adding {{domain}}...": "Het toevoegen aan de",
"Successfully added {{domain}}": "Toegevoegd aan de lijst",
"Failed to add {{domain}}": "Verzuimde",
"Note: Whitelisting a subdomain of a wildcard blocked domain is not possible.": "Opgelet: Het whitelisten van een subdomein of wildcard van een geblokkeerd domein is niet mogelijk.",
"Note: Only the domain and subdomains of the blocked domain will be blocked.": "Opgelost: Alleen het domein en subdomein van het domein zullen worden geblokkeerd.",
"Add a domain (example.com or sub.example.com)": "Voeg domein toe (example.com of sub.example.com)",
"{{domain}} is already added": "is reeds toegevoegd",
"Failed to remove {{domain}}": "Mislukt om te verwijderen",
"There are no domains in this list": "Er zijn geen domeinen in deze lijst",
"Input a regular expression": "Voer een reguliere expressie in"
}
-10
View File
@@ -1,10 +0,0 @@
{
"Query Log": "Query Log",
"Whitelist": "whitelist",
"Blacklist": "blacklist",
"Dashboard": "Dashboard",
"Exact": "Exact",
"Login": "Login",
"Logout": "Uitloggen",
"Regex": "Reguliere expressie"
}
-7
View File
@@ -1,7 +0,0 @@
{
"Password": "Wachtwoord",
"Sign in to start your session": "Log in om uw sessie te starten",
"Log in": "Log in",
"Forgot Password": "Wachtwoord vergeten",
"Wrong Password!": "Foutief wachtwoord!"
}
-6
View File
@@ -1,6 +0,0 @@
{
"Blocked": "Geblokkeerd",
"Allowed (forwarded)": "Toegestaan (doorgestuurd)",
"Blocked (regex)": "Geblokkeerd (reguliere expressie)",
"Blocked (blacklist)": "Geblokkeerd (blacklist)"
}
+40 -16
View File
@@ -58,27 +58,51 @@
}
.chartjs-tooltip {
opacity: 1;
position: absolute;
background: rgba(0, 0, 0, .7);
color: white;
border-radius: 3px;
-webkit-transition: all .1s ease;
transition: all .1s ease;
pointer-events: none;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
opacity: 1;
position: absolute;
background: rgba(0, 0, 0, .7);
color: white;
border-radius: 3px;
-webkit-transition: all .1s ease;
transition: all .1s ease;
pointer-events: none;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}
.chartjs-tooltip-key {
display: inline-block;
width: 20px;
height: 10px;
margin-right: 10px;
display: inline-block;
width: 20px;
height: 10px;
margin-right: 10px;
}
.chart-legend {
height: 250px;
overflow: auto;
margin: auto;
list-style-type: none;
}
.chart-legend li {
display: inherit;
cursor: pointer;
text-align: left;
}
.chart-legend li span {
width: 20px;
height: 12px;
margin-right: 5px;
top: 5px;
}
.strike {
text-decoration: line-through !important;
}
.background-image {
background: url("../img/backgroundLight.png") repeat fixed;
background: url("../img/backgroundLight.png") repeat fixed;
}
.container-toggler {
@@ -118,7 +142,7 @@
position: fixed;
top: 0;
z-index: 1020;
width: 1120px;
width: 1120px;
}
.boxcontainer .app-body {
margin-top: 55px;
+1 -1
View File
@@ -56,7 +56,7 @@ class ClientsGraph extends Component {
// If we ran out of colors, make a random one
backgroundColor: i < colors.length
? colors[i]
: '#' + parseInt("" + Math.random() * 0xffffff, 10).toString(16),
: '#' + parseInt("" + Math.random() * 0xffffff, 10).toString(16).padStart(6, "0"),
pointRadius: 0,
pointHitRadius: 5,
pointHoverRadius: 5,
+164
View File
@@ -0,0 +1,164 @@
/* Pi-hole: A black hole for Internet advertisements
* (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* Web Interface
* Forward Destinations Chart component
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
import React, { Component } from 'react';
import { Doughnut } from 'react-chartjs-2';
import { translate } from 'react-i18next';
import { makeCancelable, api, ignoreCancel } from "../utils";
class ForwardDestinationsChart extends Component {
state = {
loading: true,
data: [],
colors: [],
labels: []
};
constructor(props) {
super(props);
this.chartRef = React.createRef();
}
updateChart = () => {
this.updateHandler = makeCancelable(
api.getForwardDestinations(),
{ repeat: this.updateChart, interval: 10 * 60 * 1000 }
);
this.updateHandler.promise.then(forwardDestinations => {
const colors = [
"#20a8d8",
"#f86c6b",
"#4dbd74",
"#f8cb00",
"#263238",
"#63c2de",
"#b0bec5"
];
const data = [];
const labels = [];
const usedColors = [];
// Fill in dataset metadata
let i = 0;
for(let destination of forwardDestinations) {
data.push(destination.percent);
labels.push(destination.name.length !== 0 ? destination.name : destination.ip);
usedColors.push(
// If we ran out of colors, make a random one
i < colors.length
? colors[i]
: '#' + parseInt("" + Math.random() * 0xffffff, 10).toString(16).padStart(6, "0")
);
i++;
}
this.setState({
loading: false,
data,
colors: usedColors,
labels
})
}).catch(ignoreCancel);
};
handleClick = (e, index) => {
// Hide the destination by clicking on the internal legend item
const chart = this.chartRef.current.chartInstance;
chart.legend.options.onClick.call(chart, e, chart.legend.legendItems[index]);
// Cause an update so the external legend gets updated
this.forceUpdate();
};
componentDidMount() {
this.updateChart();
}
componentWillUnmount() {
this.updateHandler.cancel();
}
render() {
const { t } = this.props;
const options = {
legend: { display: false },
tooltips: {
enabled: true,
callbacks: {
title: () => t("Queries Answered By Destination"),
label: (tooltipItems, data) => {
const dataset = data.datasets[tooltipItems.datasetIndex];
const label = data.labels[tooltipItems.index];
return label + ": " + dataset.data[tooltipItems.index].toFixed(1) + "%";
}
}
},
maintainAspectRatio: false
};
// Get the metadata for the items, so we know if they are hidden or not.
// If the chart has not been created yet, make some fake metadata.
const meta = this.chartRef.current !== null
? this.chartRef.current.chartInstance.getDatasetMeta(0).data
: this.state.data.map(() => ({ hidden: false }));
return (
<div className="card">
<div className="card-header">
{t("Queries Answered By")}
</div>
<div className="card-block">
<div className="float-left" style={{ width: "67%" }}>
<Doughnut width={100} height={250} options={options} ref={this.chartRef}
data={{
datasets: [{
data: this.state.data,
backgroundColor: this.state.colors
}],
labels: this.state.labels
}}/>
</div>
<div className="float-right" style={{ width: "33%" }}>
<ul className="chart-legend" style={{ height: "250px" }}>
{
this.state.labels
// Zip label and color together
.map((label, i) => [label, this.state.colors[i]])
// Create the list items
.map(([label, color], i) => (
<li key={i}
className={meta[i] && meta[i].hidden ? "strike" : ""}
onClick={e => this.handleClick(e, i)}>
<span style={{backgroundColor: color}}/>
{label}
</li>
))
}
</ul>
</div>
</div>
{
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" }}/>
</div>
:
null
}
</div>
);
}
}
export default translate("dashboard")(ForwardDestinationsChart);
+124
View File
@@ -0,0 +1,124 @@
/* Pi-hole: A black hole for Internet advertisements
* (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* Web Interface
* Query Types Chart component
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
import React, { Component } from 'react';
import { Doughnut } from 'react-chartjs-2';
import { translate } from 'react-i18next';
import { makeCancelable, api, ignoreCancel } from "../utils";
class QueryTypesChart extends Component {
state = {
loading: true,
data: [],
colors: [],
labels: []
};
updateChart = () => {
this.updateHandler = makeCancelable(api.getQueryTypes(), { repeat: this.updateChart, interval: 10 * 60 * 1000 });
this.updateHandler.promise.then(queryTypes => {
const colors = [
"#20a8d8",
"#f86c6b",
"#4dbd74",
"#f8cb00",
"#263238",
"#63c2de",
"#b0bec5"
];
const data = [];
const labels = [];
const usedColors = [];
// Fill in dataset metadata
let i = 0;
for(let queryType of queryTypes) {
data.push(queryType.percent);
labels.push(queryType.name);
usedColors.push(
// If we ran out of colors, make a random one
i < colors.length
? colors[i]
: '#' + parseInt("" + Math.random() * 0xffffff, 10).toString(16)
);
i++;
}
this.setState({
loading: false,
data,
colors: usedColors,
labels
})
}).catch(ignoreCancel);
};
componentDidMount() {
this.updateChart();
}
componentWillUnmount() {
this.updateHandler.cancel();
}
render() {
const { t } = this.props;
const options = {
legend: {
display: true,
position: "right"
},
tooltips: {
enabled: true,
callbacks: {
title: () => t("Query Types"),
label: (tooltipItems, data) => {
const dataset = data.datasets[tooltipItems.datasetIndex];
const label = data.labels[tooltipItems.index];
return label + ": " + dataset.data[tooltipItems.index].toFixed(1) + "%";
}
}
},
maintainAspectRatio: false
};
return (
<div className="card">
<div className="card-header">
{t("Query Types")}
</div>
<div className="card-block">
<Doughnut width={100} height={250} options={options}
data={{
datasets: [{
data: this.state.data,
backgroundColor: this.state.colors
}],
labels: this.state.labels
}}/>
</div>
{
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" }}/>
</div>
:
null
}
</div>
);
}
}
export default translate("dashboard")(QueryTypesChart);
+4
View File
@@ -23,6 +23,10 @@ class StatusBadge extends Component {
this.updateStatus();
}
componentWillUnmount() {
this.updateHandler.cancel();
}
render() {
const { t } = this.props;
+6
View File
@@ -75,6 +75,12 @@ export const api = {
getClientsGraph() {
return api.get("stats/overTime/clients")
},
getQueryTypes() {
return api.get("stats/query_types")
},
getForwardDestinations() {
return api.get("stats/forward_destinations")
},
getTopDomains() {
return api.get("stats/top_domains");
},
+10
View File
@@ -12,6 +12,8 @@ import React, { Fragment } from 'react';
import SummaryStats from '../components/SummaryStats';
import QueriesGraph from '../components/QueriesGraph';
import ClientsGraph from '../components/ClientsGraph';
import QueryTypesChart from "../components/QueryTypesChart";
import ForwardDestinationsChart from "../components/ForwardDestinationsChart";
import TopDomains from '../components/TopDomains';
import TopBlocked from '../components/TopBlocked';
import TopClients from '../components/TopClients';
@@ -35,6 +37,14 @@ export default () => (
<ClientsGraph/>
</div>
</div>
<div className="row">
<div className="col-md-6">
<QueryTypesChart/>
</div>
<div className="col-md-6">
<ForwardDestinationsChart/>
</div>
</div>
<div className="row">
<div className="col-md-6 col-xl-4">
<TopDomains/>