diff --git a/make-fake-data.js b/make-fake-data.js index 0bae6b5..54acef5 100644 --- a/make-fake-data.js +++ b/make-fake-data.js @@ -9,16 +9,27 @@ * Please see LICENSE file for your rights under this license. */ const faker = require('faker'); -const fs = require('fs'); -const path = require('path'); -const mkdirp = require('mkdirp'); +const fs = require('fs-extra'); + +function unique(generator, size) { + const data = []; + + while(data.length !== size) { + const item = generator(); + + if(!data.includes(item)) + data.push(item); + } + + return data; +} function list() { - return (new Array(10)).fill(null).map(() => faker.internet.domainName()); + return unique(faker.internet.domainName, 10); } function status() { - return {status: "enabled"}; + return { status: "enabled" }; } function pastDate() { @@ -64,6 +75,7 @@ function summary() { function topList(length, max, fakeData) { const result = []; const numbers = []; + const domains = unique(fakeData, length); for(let i = 0; i < length; i++) numbers.push(faker.random.number({ max: max })); @@ -71,12 +83,8 @@ function topList(length, max, fakeData) { numbers.sort((a, b) => parseInt(a, 10) > parseInt(b, 10) ? -1 : 1); for(let i = 0; i < length; i++) { - let domain; - - while((domain = fakeData()) in result.map(item => item.domain)) {} - result.push({ - domain: domain, + domain: domains[i], count: numbers[i] }); } @@ -104,9 +112,9 @@ function topDomains(length) { function topClients(length) { const totalQueries = faker.random.number(); - const top_clients = []; const numbers = []; + const clients = unique(faker.internet.ip, length); for(let i = 0; i < length; i++) numbers.push(faker.random.number({ max: totalQueries })); @@ -114,13 +122,9 @@ function topClients(length) { numbers.sort((a, b) => parseInt(a, 10) > parseInt(b, 10) ? -1 : 1); for(let i = 0; i < length; i++) { - let ip; - - while((ip = faker.internet.ip()) in top_clients.map(item => item.ip)) {} - top_clients.push({ name: "", - ip: ip, + ip: clients[i], count: numbers[i] }); } @@ -131,38 +135,25 @@ function topClients(length) { }; } -function forwardDestOverTime(range, totalDest) { +function clientsOverTime(range, size) { const startDate = pastDate(); - const forwardDest = (new Array(totalDest)).fill(null).map(() => faker.internet.ip()); + const clients = unique(faker.internet.ip, size) + .map(ip => ({ name: faker.internet.domainWord(), ip })); const graph = []; for(let i = 0; i < range; i++) { graph.push({ timestamp: startDate + 600 * i, - data: (new Array(totalDest)).fill(null).map(() => faker.random.number()) + data: (new Array(size)).fill(null).map(() => faker.random.number()) }); } return { "over_time": graph, - "forward_destinations": forwardDest + "clients": clients }; } -function queryTypesOverTime(range) { - const startDate = pastDate(); - const queryTypes = []; - - for(let i = 0; i < range; i++) { - queryTypes.push({ - timestamp: startDate + 600 * i, - data: [faker.random.number(), faker.random.number()] - }); - } - - return queryTypes; -} - function historyOverTime(range) { const startDate = pastDate(); const domains = []; @@ -189,48 +180,21 @@ function historyOverTime(range) { }; } -function remove(path) { - try { - fs.unlinkSync(path); - } - catch(e) { - // Ignore missing file - } -} - function write(filePath, data) { - const dir = path.dirname(filePath); - - if(!fs.existsSync(dir)) - mkdirp.sync(dir); - - fs.writeFileSync(filePath, JSON.stringify(data)); + fs.outputFileSync(filePath, JSON.stringify(data)); } console.log("Deleting old fake API data..."); - -remove("public/fakeAPI/dns/whitelist"); -remove("public/fakeAPI/dns/blacklist"); -remove("public/fakeAPI/dns/wildlist"); -remove("public/fakeAPI/dns/status"); -remove("public/fakeAPI/stats/overTime/history"); -remove("public/fakeAPI/stats/overTime/forward_destinations"); -remove("public/fakeAPI/stats/overTime/query_types"); -remove("public/fakeAPI/stats/summary"); -remove("public/fakeAPI/stats/history"); -remove("public/fakeAPI/stats/top_blocked"); -remove("public/fakeAPI/stats/top_domains"); -remove("public/fakeAPI/stats/top_clients"); +fs.emptyDirSync("public/fakeAPI/dns"); +fs.emptyDirSync("public/fakeAPI/stats"); console.log("Generating new fake API data..."); - write("public/fakeAPI/dns/whitelist", list("white")); write("public/fakeAPI/dns/blacklist", list("black")); write("public/fakeAPI/dns/wildlist", list("wild")); write("public/fakeAPI/dns/status", status()); write("public/fakeAPI/stats/overTime/history", historyOverTime(144)); -write("public/fakeAPI/stats/overTime/forward_destinations", forwardDestOverTime(144, 3)); -write("public/fakeAPI/stats/overTime/query_types", queryTypesOverTime(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/top_blocked", topBlocked(10)); diff --git a/scss/_custom.scss b/scss/_custom.scss index e9e2c9c..25545ee 100644 --- a/scss/_custom.scss +++ b/scss/_custom.scss @@ -55,4 +55,24 @@ .stat-height-lock { height: 100px; +} + +.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); +} + +.chartjs-tooltip-key { + display: inline-block; + width: 20px; + height: 10px; + margin-right: 10px; } \ No newline at end of file diff --git a/src/components/ChartTooltip.js b/src/components/ChartTooltip.js new file mode 100644 index 0000000..a4bb2fc --- /dev/null +++ b/src/components/ChartTooltip.js @@ -0,0 +1,120 @@ +/* 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 +* Chart Tooltip 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 PropTypes from 'prop-types'; + +class ChartTooltip extends Component { + state = { + tooltip: null + }; + + render() { + const { tooltip } = this.state; + + // Override the graph's options in a hacky way to avoid redrawing the entire graph. + // This lets the graph directly update the tooltip without going through the parent component. + let lastTime = Date.now(); + this.props.handler.custom = tooltip => { + // Always disable the tooltip + if(tooltip.opacity === 0) + this.setState({ tooltip }); + + // Limit how quickly we set state (50ms) + const now = Date.now(); + if(now - lastTime < 50) + return; + + lastTime = now; + this.setState({ tooltip }); + }; + + // Don't render anything if there is nothing to render + if(tooltip === null || tooltip.opacity === 0 || this.props.chart.current === null) + return null; + + // Get the graph's position data so we can offset the tooltip + const position = this.props.chart.current.chartInstance.canvas.getBoundingClientRect(); + let width = tooltip.caretX; + + // Prevent compression of the tooltip at the right edge of the screen + if(document.offsetWidth - tooltip.caretX < 400) + width = document.offsetWidth - 400; + + // Prevent tooltip disappearing behind the sidebar + if(tooltip.caretX < 100) + width = 100; + + // Tooltip CSS styling + const style = { + opacity: 1, + left: position.left + width + "px", + top: position.top + tooltip.caretY + window.scrollY + "px", + fontFamily: tooltip._bodyFontFamily, + fontSize: tooltip.bodyFontSize + "px", + fontStyle: tooltip._bodyFontStyle, + padding: tooltip.yPadding + "px " + tooltip.xPadding + "px" + }; + + // Transform and sort the tooltip data + let data = []; + if(tooltip.body) { + data = tooltip.body + .map(body => body.lines) + .map((item, i) => ({ + data: item[0], + colors: tooltip.labelColors[i] + })); + } + data.sort((a, b) => a.data.split(": ")[0].localeCompare(b.data.split(": ")[0])); + + return ( +
| {title} | +
|---|
| + + {item.data} + | +