From 3171b41ae0602adeb74151e3efb86f35ee880ba0 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Thu, 15 Mar 2018 12:52:14 -0700 Subject: [PATCH] Adjust to improved API format Signed-off-by: Mcat12 --- make-fake-data.js | 106 ++++++++++++++++---------- src/components/ForwardDestOverTime.js | 29 +++---- src/components/QueriesGraph.js | 24 ++---- src/components/QueryTypesOverTime.js | 33 ++++---- src/components/TopBlocked.js | 13 ++-- src/components/TopClients.js | 25 ++---- src/components/TopDomains.js | 13 ++-- src/utils.js | 19 +---- 8 files changed, 116 insertions(+), 146 deletions(-) diff --git a/make-fake-data.js b/make-fake-data.js index f26f8f2..0bae6b5 100644 --- a/make-fake-data.js +++ b/make-fake-data.js @@ -13,19 +13,12 @@ const fs = require('fs'); const path = require('path'); const mkdirp = require('mkdirp'); -function reply_data(data) { - return { - "data": data, - "errors": [] - } -} - function list() { - return reply_data((new Array(10)).fill(null).map(() => faker.internet.domainName())); + return (new Array(10)).fill(null).map(() => faker.internet.domainName()); } function status() { - return reply_data({"status": 1}); + return {status: "enabled"}; } function pastDate() { @@ -35,7 +28,7 @@ function pastDate() { function history(length) { const startDate = pastDate(); - return reply_data((new Array(length)).fill(null).map((_, i) => { + return (new Array(length)).fill(null).map((_, i) => { const isIPv4 = faker.random.boolean(); const isHostname = faker.random.boolean(); return [ @@ -45,7 +38,7 @@ function history(length) { isHostname ? faker.internet.domainWord() + ".local" : isIPv4 ? faker.internet.ip() : faker.internet.ipv6(), Math.floor(Math.random() * 5) + 1 ]; - })); + }); } function summary() { @@ -54,7 +47,7 @@ function summary() { const forwarded = faker.random.number({ max: total }); const clients = faker.random.number(); - return reply_data({ + return { "domains_blocked": faker.random.number(), "total_queries": total, "blocked_queries": ads, @@ -65,11 +58,11 @@ function summary() { "total_clients": clients, "unique_clients": faker.random.number({ max: clients }), ... status() - }); + }; } function topList(length, max, fakeData) { - const result = {}; + const result = []; const numbers = []; for(let i = 0; i < length; i++) @@ -80,9 +73,12 @@ function topList(length, max, fakeData) { for(let i = 0; i < length; i++) { let domain; - while((domain = fakeData()) in Object.keys(result)) {} + while((domain = fakeData()) in result.map(item => item.domain)) {} - result[domain] = numbers[i]; + result.push({ + domain: domain, + count: numbers[i] + }); } return result; @@ -91,76 +87,106 @@ function topList(length, max, fakeData) { function topBlocked(length) { const totalQueries = faker.random.number(); - return reply_data({ + return { "top_blocked": topList(length, totalQueries, faker.internet.domainName), "blocked_queries": totalQueries - }); + }; } function topDomains(length) { const totalQueries = faker.random.number(); - return reply_data({ + return { "top_domains": topList(length, totalQueries, faker.internet.domainName), "total_queries": totalQueries - }); + }; } function topClients(length) { const totalQueries = faker.random.number(); - return reply_data({ - "top_clients": topList(length, totalQueries, faker.internet.ip), - "total_queries": totalQueries - }); + const top_clients = []; + const numbers = []; + + for(let i = 0; i < length; i++) + numbers.push(faker.random.number({ max: totalQueries })); + + 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, + count: numbers[i] + }); + } + + return { + top_clients: top_clients, + total_queries: totalQueries + }; } function forwardDestOverTime(range, totalDest) { const startDate = pastDate(); - const forwardDest = topList(totalDest, faker.random.number(), faker.internet.ip); - const graph = {}; + const forwardDest = (new Array(totalDest)).fill(null).map(() => faker.internet.ip()); + const graph = []; for(let i = 0; i < range; i++) { - graph[(startDate + 600 * i).toString()] = (new Array(totalDest)).fill(null).map((_, j) => { - return faker.random.number({ max: forwardDest[Object.keys(forwardDest)[j]] }); + graph.push({ + timestamp: startDate + 600 * i, + data: (new Array(totalDest)).fill(null).map(() => faker.random.number()) }); } - return reply_data({ + return { "over_time": graph, "forward_destinations": forwardDest - }); + }; } function queryTypesOverTime(range) { const startDate = pastDate(); - const queryTypes = {}; + const queryTypes = []; for(let i = 0; i < range; i++) { - queryTypes[(startDate + 600 * i).toString()] = [faker.random.number(), faker.random.number()]; + queryTypes.push({ + timestamp: startDate + 600 * i, + data: [faker.random.number(), faker.random.number()] + }); } - return reply_data(queryTypes); + return queryTypes; } function historyOverTime(range) { const startDate = pastDate(); - const domains = {}; - const ads = {}; + const domains = []; + const ads = []; for(let i = 0; i < range; i++) { const numDomains = faker.random.number(); const numAds = faker.random.number({ max: numDomains }); - const time = (startDate + 600 * i).toString(); + const time = startDate + 600 * i; - domains[time] = numDomains; - ads[time] = numAds; + domains.push({ + timestamp: time, + count: numDomains + }); + ads.push({ + timestamp: time, + count: numAds + }); } - return reply_data({ + return { "domains_over_time": domains, "blocked_over_time": ads - }); + }; } function remove(path) { diff --git a/src/components/ForwardDestOverTime.js b/src/components/ForwardDestOverTime.js index 191e7f0..1f2d792 100644 --- a/src/components/ForwardDestOverTime.js +++ b/src/components/ForwardDestOverTime.js @@ -10,7 +10,7 @@ import React, { Component } from 'react'; import { Line } from 'react-chartjs-2'; -import { padNumber, parseObjectForGraph, api, makeCancelable, ignoreCancel } from '../utils'; +import { padNumber, api, makeCancelable, ignoreCancel } from '../utils'; export default class ForwardDestOverTime extends Component { state = { @@ -77,13 +77,10 @@ export default class ForwardDestOverTime extends Component { { repeat: this.updateGraph, interval: 10 * 60 * 1000 } ); this.updateHandler.promise.then(res => { - res.over_time = parseObjectForGraph(res.over_time); // Remove last data point as it's not yet finished - res.over_time[0].splice(-1, 1); + res.over_time.splice(-1, 1); const labels = []; - const timestamps = res.over_time[0]; - const plotdata = res.over_time[1]; const destinations = res.forward_destinations; const datasets = []; const colors = [ @@ -96,17 +93,15 @@ export default class ForwardDestOverTime extends Component { "#b0bec5" ]; - // Fill in default values + // Fill in destination information let i = 0; - for(let destination in destinations) { - if(!destinations.hasOwnProperty(destination)) continue; - + for(let destination of destinations) { datasets.push({ label: destination.split("|")[0], // 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), pointRadius: 0, pointHitRadius: 5, pointHoverRadius: 5, @@ -117,22 +112,18 @@ export default class ForwardDestOverTime extends Component { } // Fill in data & labels - for(let j in timestamps) { - if(!timestamps.hasOwnProperty(j)) continue; - - const h = parseInt(timestamps[j], 10); - const d = new Date(1000 * h); - - const sum = plotdata[j].reduce((sum, next) => sum + next); + for(let step of res.over_time) { + const date = new Date(1000 * step.timestamp); + const sum = step.data.reduce((a, b) => a + b); if (sum > 0) { for(let destination in datasets) { if(datasets.hasOwnProperty(destination)) - datasets[destination].data.push(plotdata[j][destination] / sum); + datasets[destination].data.push(step.data[destination] / sum); } } - labels.push(d); + labels.push(date); } const data = this.state.data; diff --git a/src/components/QueriesGraph.js b/src/components/QueriesGraph.js index 079a197..95fd065 100644 --- a/src/components/QueriesGraph.js +++ b/src/components/QueriesGraph.js @@ -10,7 +10,7 @@ import React, { Component } from 'react'; import { Line } from 'react-chartjs-2'; -import { padNumber, parseObjectForGraph, api, makeCancelable, ignoreCancel } from '../utils'; +import { padNumber, api, makeCancelable, ignoreCancel } from '../utils'; export default class QueriesGraph extends Component { state = { @@ -107,26 +107,14 @@ export default class QueriesGraph extends Component { updateGraph() { this.updateHandler = makeCancelable(api.getHistoryGraph(), { repeat: this.updateGraph, interval: 10 * 60 * 1000}); this.updateHandler.promise.then(res => { - res.blocked_over_time = parseObjectForGraph(res.blocked_over_time); - res.domains_over_time = parseObjectForGraph(res.domains_over_time); - // Remove last data point as it's not yet finished - res.blocked_over_time[0].splice(-1, 1); - res.blocked_over_time[1].splice(-1, 1); - res.domains_over_time[0].splice(-1, 1); - res.domains_over_time[1].splice(-1, 1); - - // Generate labels - const labels = []; - for(let i in res.blocked_over_time[0]) { - if(res.blocked_over_time[0].hasOwnProperty(i)) - labels.push(new Date(1000 * res.blocked_over_time[0][i])); - } + res.blocked_over_time.splice(-1, 1); + res.domains_over_time.splice(-1, 1); const data = this.state.data; - data.labels = labels; - data.datasets[0].data = res.domains_over_time[1]; - data.datasets[1].data = res.blocked_over_time[1]; + data.labels = res.domains_over_time.map(step => new Date(1000 * step.timestamp)); + data.datasets[0].data = res.domains_over_time.map(step => step.count); + data.datasets[1].data = res.blocked_over_time.map(step => step.count); this.setState({ data, loading: false }); }).catch(ignoreCancel); diff --git a/src/components/QueryTypesOverTime.js b/src/components/QueryTypesOverTime.js index 167e871..4aa7e2a 100644 --- a/src/components/QueryTypesOverTime.js +++ b/src/components/QueryTypesOverTime.js @@ -10,7 +10,7 @@ import React, { Component } from 'react'; import { Line } from 'react-chartjs-2'; -import { padNumber, parseObjectForGraph, api, makeCancelable, ignoreCancel } from '../utils'; +import { padNumber, api, makeCancelable, ignoreCancel } from '../utils'; export default class QueryTypesOverTime extends Component { state = { @@ -94,34 +94,27 @@ export default class QueryTypesOverTime extends Component { { repeat: this.updateGraph, interval: 10 * 60 * 1000 } ); this.updateHandler.promise.then(res => { - res = parseObjectForGraph(res); - // Remove last data point as it's not yet finished - res[0].splice(-1, 1); + res.splice(-1, 1); const labels = []; const data_A = []; const data_AAAA = []; - const timestamps = res[0]; - const plotdata = res[1]; - for(let j in timestamps) { - if(timestamps.hasOwnProperty(j)) { - const h = parseInt(timestamps[j], 10); - const d = new Date(1000 * h); + for(let step of res) { + const date = new Date(1000 * step.timestamp); - const sum = plotdata[j][0] + plotdata[j][1]; - let A = 0, AAAA = 0; + const sum = step.data.reduce((a, b) => a + b); + let A = 0, AAAA = 0; - if (sum > 0) { - A = plotdata[j][0] / sum; - AAAA = plotdata[j][1] / sum; - } - - labels.push(d); - data_A.push(A); - data_AAAA.push(AAAA); + if (sum > 0) { + A = step.data[0] / sum; + AAAA = step.data[1] / sum; } + + labels.push(date); + data_A.push(A); + data_AAAA.push(AAAA); } const data = this.state.data; diff --git a/src/components/TopBlocked.js b/src/components/TopBlocked.js index 35f776b..6497153 100644 --- a/src/components/TopBlocked.js +++ b/src/components/TopBlocked.js @@ -14,7 +14,7 @@ import { api, ignoreCancel, makeCancelable } from '../utils'; export default class TopBlocked extends Component { state = { total_blocked: 0, - top_blocked: {} + top_blocked: [] }; constructor(props) { @@ -57,16 +57,15 @@ export default class TopBlocked extends Component { Frequency { - Object.keys(this.state.top_blocked).map(item => { - const stat = this.state.top_blocked[item]; - const percentage = stat / this.state.total_blocked * 100; + this.state.top_blocked.map(item => { + const percentage = item.count / this.state.total_blocked * 100; return ( - + - {item} + {item.domain} - {stat.toLocaleString()} + {item.count.toLocaleString()}
diff --git a/src/components/TopClients.js b/src/components/TopClients.js index 08e1c98..26746cd 100644 --- a/src/components/TopClients.js +++ b/src/components/TopClients.js @@ -14,7 +14,7 @@ import { api, ignoreCancel, makeCancelable } from '../utils'; export default class TopClients extends Component { state = { total_queries: 0, - top_clients: {} + top_clients: [] }; constructor(props) { @@ -57,29 +57,16 @@ export default class TopClients extends Component { Frequency { - Object.keys(this.state.top_clients).map((item) => { - let hostname = ""; - let ipAddr = ""; - const stat = this.state.top_clients[item]; - const percentage = stat / this.state.total_queries * 100; - - // Check if we have the IP and hostname - // ex. localhost|127.0.0.1 - const parts = item.split("|"); - if(item.includes("|")) { - hostname = parts[0]; - ipAddr = parts[1]; - } - else - ipAddr = parts[0]; + this.state.top_clients.map(item => { + const percentage = item.count / this.state.total_queries * 100; return ( - + - {hostname !== "" ? hostname : ipAddr} + {item.name !== "" ? item.name : item.ip} - {stat.toLocaleString()} + {item.count.toLocaleString()}
diff --git a/src/components/TopDomains.js b/src/components/TopDomains.js index 0ea3aff..5d3bf9d 100644 --- a/src/components/TopDomains.js +++ b/src/components/TopDomains.js @@ -14,7 +14,7 @@ import { api, ignoreCancel, makeCancelable } from '../utils'; export default class TopDomains extends Component { state = { total_queries: 0, - top_domains: {} + top_domains: [] }; constructor(props) { @@ -57,16 +57,15 @@ export default class TopDomains extends Component { Frequency { - Object.keys(this.state.top_domains).map(item => { - const stat = this.state.top_domains[item]; - const percentage = stat / this.state.total_queries * 100; + this.state.top_domains.map(item => { + const percentage = item.count / this.state.total_queries * 100; return ( - + - {item} + {item.domain} - {stat.toLocaleString()} + {item.count.toLocaleString()}
diff --git a/src/utils.js b/src/utils.js index e78fae5..006e32d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -14,19 +14,6 @@ export const padNumber = (num) => { return ("00" + num).substr(-2,2); }; -export const parseObjectForGraph = (p) => { - const keys = Object.keys(p); - keys.sort((a, b) => a - b); - - const arr = [], idx = []; - for(let i = 0; i < keys.length; i++) { - arr.push(p[keys[i]]); - idx.push(keys[i]); - } - - return [idx,arr]; -}; - export const makeCancelable = (promise, { repeat = null, interval = 0 } = {}) => { let hasCanceled = false; let repeatId = null; @@ -144,10 +131,10 @@ export const api = { return data.json(); }, checkForErrors(data) { - if(!data.errors || data.errors.length > 0) { - return Promise.reject(data.errors); + if(data.error) { + return Promise.reject(data.error); } - return Promise.resolve(data.data); + return Promise.resolve(data); }, urlFor(endpoint) { let apiLocation;