-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
303 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
const { readFileSync } = require('fs') | ||
const { map } = require('ramda') | ||
const { recordsLocation } = require('../util') | ||
const { logger, recordsLocation } = require('../util') | ||
|
||
const parse = (file) => JSON.parse(readFileSync(file).toString()) | ||
const records = parse(recordsLocation) | ||
try { | ||
const records = JSON.parse(readFileSync(recordsLocation).toString()) | ||
|
||
const mapRecord = (type) => map((record) => record[type], records) | ||
const downloads = mapRecord('download') | ||
const uploads = mapRecord('upload') | ||
const pings = mapRecord('ping') | ||
const mapRecord = (type) => map((record) => record[type], records) | ||
const downloads = mapRecord('download') | ||
const uploads = mapRecord('upload') | ||
const pings = mapRecord('ping') | ||
|
||
module.exports = { | ||
downloads, | ||
uploads, | ||
pings, | ||
module.exports = { | ||
downloads, | ||
uploads, | ||
pings, | ||
} | ||
} catch(error) { | ||
logger.error(`Could not parse ${recordsLocation}. Does ${recordsLocation} exists and does it contain record data?\n${error}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const test = require('tape-catch') | ||
const sinon = require('sinon') | ||
const proxyquire = require('proxyquire').noCallThru() | ||
const { includes } = require('ramda') | ||
|
||
const errorMessage = 'Could not parse' | ||
const records = | ||
'[{"ping":1,"download":4,"upload":7,"day":"24/06/2019","time":"17:50"},\n' + | ||
'{"ping":2,"download":5,"upload":8,"day":"24/06/2019","time":"18:50"},\n' + | ||
'{"ping":3,"download":6,"upload":9,"day":"29/06/2019","time":"07:30"}]' | ||
|
||
const sandbox = sinon.createSandbox() | ||
|
||
const readFileSync = sandbox.stub() | ||
const errorLogger = sandbox.spy() | ||
|
||
const teardown = () => sandbox.restore() | ||
|
||
const requireData = () => ( | ||
proxyquire('./data', { | ||
'fs': { readFileSync }, | ||
'../util': { logger: { error: errorLogger }, recordsLocation: '/path/to/records.txt' } | ||
}) | ||
) | ||
|
||
test('retrieves record data', (t) => { | ||
readFileSync.returns({ toString: () => records }) | ||
|
||
const data = requireData() | ||
|
||
t.deepEqual(data.pings, [ 1, 2, 3 ]) | ||
t.deepEqual(data.downloads, [ 4, 5, 6 ]) | ||
t.deepEqual(data.uploads, [ 7, 8, 9 ]) | ||
|
||
teardown() | ||
t.end() | ||
}) | ||
|
||
test('logs error if the file does not exist', (t) => { | ||
readFileSync.throws(errorMessage) | ||
|
||
requireData() | ||
|
||
const loggedMessage = errorLogger.args[0][0] | ||
t.ok(includes(errorMessage, loggedMessage)) | ||
|
||
teardown() | ||
t.end() | ||
}) | ||
|
||
test('logs error if there are no records in the file', (t) => { | ||
readFileSync.returns({ toString: () => '' }) | ||
|
||
requireData() | ||
|
||
const loggedMessage = errorLogger.args[0][0] | ||
t.ok(includes(errorMessage, loggedMessage)) | ||
|
||
teardown() | ||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
const test = require('tape-catch') | ||
const proxyquire = require('proxyquire').noCallThru() | ||
const { all, pluck, split } = require('ramda') | ||
|
||
const createSpeedData = (speeds) => ({ | ||
downloads: speeds, | ||
uploads: speeds, | ||
pings: speeds, | ||
}) | ||
|
||
const requireData = (speedData) => ( | ||
proxyquire('./data', { | ||
'../data': speedData, | ||
}) | ||
) | ||
|
||
test('handles one speed', (t) => { | ||
t.plan(1) | ||
|
||
const data = requireData(createSpeedData([ 1 ])) | ||
|
||
t.deepEqual(data.download, [{ key: '1.00 - 1.00', value: 1 }]) | ||
}) | ||
|
||
test('handles two speeds', (t) => { | ||
t.plan(1) | ||
|
||
const data = requireData(createSpeedData(([ 1, 2 ]))) | ||
|
||
t.deepEqual(data.download, [ | ||
{ key: '1.00 - 1.50', value: 1 }, | ||
{ key: '1.50 - 2.00', value: 1 }, | ||
]) | ||
}) | ||
|
||
test('keys have 2 decimal points', (t) => { | ||
t.plan(2) | ||
|
||
const data = requireData(createSpeedData([ 1 ])) | ||
|
||
const key = data.download[0].key | ||
const splitKey = split(' - ', key) | ||
const minDecimalsLength = split('.', splitKey[0])[1].length | ||
const maxDecimalsLength = split('.', splitKey[1])[1].length | ||
|
||
t.equal(minDecimalsLength, 2) | ||
t.equal(maxDecimalsLength, 2) | ||
}) | ||
|
||
test('keys are in the format \'number.2decimals - number.2decimals\'', (t) => { | ||
t.plan(1) | ||
|
||
const data = requireData(createSpeedData([ 1 ])) | ||
const key = data.download[0].key | ||
|
||
const format = /\d+.\d{2}\s.*\s\d+.\d{2}/ | ||
t.ok(format.test(key)) | ||
}) | ||
|
||
test('creates 5 windows', (t) => { | ||
t.plan(1) | ||
|
||
const data = requireData(createSpeedData(([ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]))) | ||
|
||
t.equal(data.download.length, 5) | ||
}) | ||
|
||
test('windows are equally spaced', (t) => { | ||
t.plan(1) | ||
|
||
const data = requireData(createSpeedData(([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]))) | ||
|
||
const keys = pluck('key', data.download) | ||
const windowSizesEqual2 = all((key) => { | ||
const splitKey = split(' - ', key) | ||
return splitKey[1] - splitKey[0] === 2 | ||
}, keys) | ||
|
||
t.ok(windowSizesEqual2) | ||
}) | ||
|
||
test('groups unequally spread data', (t) => { | ||
t.plan(1) | ||
|
||
const data = requireData(createSpeedData(([ 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]))) | ||
const values = pluck('value', data.download) | ||
|
||
t.deepEqual(values, [ 12, 2, 2, 2, 2 ]) | ||
}) | ||
|
||
test('handles windows which have no data', (t) => { | ||
t.plan(1) | ||
|
||
const data = requireData(createSpeedData(([ 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 11 ]))) | ||
const values = pluck('value', data.download) | ||
|
||
t.deepEqual(values, [ 10, 0, 0, 0, 1 ]) | ||
}) | ||
|
||
test('groups download, upload and ping data', (t) => { | ||
t.plan(1) | ||
|
||
const data = requireData(createSpeedData(([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]))) | ||
const expectedData = [ | ||
{ key: '1.00 - 3.00', value: 3 }, | ||
{ key: '3.00 - 5.00', value: 2 }, | ||
{ key: '5.00 - 7.00', value: 2 }, | ||
{ key: '7.00 - 9.00', value: 2 }, | ||
{ key: '9.00 - 11.00', value: 2 }, | ||
] | ||
|
||
t.deepEqual({ | ||
download: expectedData, | ||
upload: expectedData, | ||
ping: expectedData, | ||
}, data) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const test = require('tape-catch') | ||
const sinon = require('sinon') | ||
const proxyquire = require('proxyquire').noCallThru() | ||
|
||
const sandbox = sinon.createSandbox() | ||
|
||
const download = [{ key: '1.00 - 3.00', value: 1 }] | ||
const upload = [{ key: '1.00 - 3.00', value: 2 }] | ||
const ping = [{ key: '1.00 - 3.00', value: 3 }] | ||
const resultsDirectory = '/path/to/results/directory' | ||
const chartsDirectory = '/path/to/results/directory/charts' | ||
const downloadPath = '/path/to/results/directory/charts/download' | ||
const uploadPath = '/path/to/results/directory/charts/upload' | ||
const pingPath = '/path/to/results/directory/charts/ping' | ||
const d3nBarData = 'd3BarData' | ||
|
||
const resolve = sandbox.stub() | ||
resolve.withArgs(resultsDirectory).returns(chartsDirectory) | ||
resolve.withArgs(chartsDirectory, './download').returns(downloadPath) | ||
resolve.withArgs(chartsDirectory, './upload').returns(uploadPath) | ||
resolve.withArgs(chartsDirectory, './ping').returns(pingPath) | ||
const d3nBar = sandbox.stub() | ||
d3nBar.returns(d3nBarData) | ||
const mkdirSync = sandbox.spy() | ||
const output = sandbox.spy() | ||
|
||
const teardown = () => sandbox.restore() | ||
|
||
const generateCharts = proxyquire('./generate-charts', { | ||
'path': { resolve }, | ||
'fs': { mkdirSync }, | ||
'd3node-barchart': d3nBar, | ||
'd3node-output': output, | ||
'./data': { download, upload, ping }, | ||
}) | ||
|
||
test('creates directory to output charts into', (t) => { | ||
generateCharts({ resultsDirectory }) | ||
|
||
t.ok(mkdirSync.calledWith(chartsDirectory, { recursive: true })) | ||
|
||
teardown() | ||
t.end() | ||
}) | ||
|
||
test('creates charts', (t) => { | ||
generateCharts({ resultsDirectory }) | ||
|
||
t.ok(d3nBar.firstCall.calledWithMatch({ data: download })) | ||
t.ok(d3nBar.secondCall.calledWithMatch({ data: upload })) | ||
t.ok(d3nBar.thirdCall.calledWithMatch({ data: ping })) | ||
|
||
teardown() | ||
t.end() | ||
}) | ||
|
||
test('outputs charts', (t) => { | ||
generateCharts({ resultsDirectory }) | ||
|
||
const options = {width: 960, height: 600} | ||
t.ok(output.firstCall.calledWithMatch(downloadPath, d3nBarData, options)) | ||
t.ok(output.secondCall.calledWithMatch(uploadPath, d3nBarData, options)) | ||
t.ok(output.thirdCall.calledWithMatch(pingPath, d3nBarData, options)) | ||
|
||
teardown() | ||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
const generateCharts = require('./histogram/generateCharts') | ||
const { generateCharts } = require('./histogram') | ||
|
||
module.exports = generateCharts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const test = require('tape-catch') | ||
const sinon = require('sinon') | ||
const proxyquire = require('proxyquire').noCallThru() | ||
|
||
const sandbox = sinon.createSandbox() | ||
const generateCharts = sandbox.spy() | ||
const environmentVariable = sandbox.stub() | ||
const resultsDirectory = 'C:\\path\\to\\results\\directory' | ||
environmentVariable.withArgs('resultsDirectory').returns(resultsDirectory) | ||
|
||
const teardown = () => sandbox.restore() | ||
|
||
const requireRunGenerateCharts = () => ( | ||
proxyquire('./run-generate-charts', { | ||
'./histogram': { generateCharts }, | ||
'../util': { environmentVariable }, | ||
}) | ||
) | ||
|
||
test('generateCharts is invoked with correct arguments', (t) => { | ||
requireRunGenerateCharts() | ||
|
||
t.ok(generateCharts.calledWith({ resultsDirectory })) | ||
|
||
teardown() | ||
t.end() | ||
}) |
Oops, something went wrong.