Last active
February 8, 2018 21:13
-
-
Save StarpTech/5f3e4547b9a3a8cbcdcd915543754898 to your computer and use it in GitHub Desktop.
Practical solution to https://github.com/onzo-com/etc-kata
This file contains hidden or 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
"use strict"; | |
const _ = require("lodash"); | |
function groupByTimeWindow(start, end) { | |
return function(o) { | |
const date = new Date(o.timestamp); | |
if (date.getUTCHours() >= start && date.getUTCHours() <= end) | |
return o.consumption_Wh; | |
return 0; | |
}; | |
} | |
module.exports.getSensorData = (data, id) => _.groupBy(data, x => x.sensor_id)[id]; | |
module.exports.getConsumptionByTimeWindow = (sensorData, start, end) => | |
_.sumBy(sensorData, groupByTimeWindow(start, end)); |
This file contains hidden or 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
"use strict"; | |
const t = require("tap"); | |
const test = t.test; | |
/** | |
* Date and time information in ISO 8601 format | |
* Electricity consumption in **Wh** | |
*/ | |
const rawData = require("./consumption_data.json"); | |
const lib = require("./"); | |
const data = lib.getSensorData(rawData, "b08c6195-8cd9-43ab-b94d-e0b887dd73d2"); | |
test("Calculate the sensor's total daily consumption in kWh", t => { | |
t.plan(1); | |
let result = lib.getConsumptionByTimeWindow(data, 0, 24) * 10e-4 | |
result = +result.toFixed(3) | |
t.strictEqual(result, 10.713); | |
}); | |
test("Calculate the sensor's average hourly consumption from 00:00 to 07:00 inclusive", t => { | |
t.plan(1); | |
let result = lib.getConsumptionByTimeWindow(data, 0, 7) / 8 * 10e-4 | |
result = +result.toFixed(6) | |
t.strictEqual(result, 0.322875); | |
}); | |
test("Calculate the sensor's average hourly consumption from 08:00 to 15:00 inclusive", t => { | |
t.plan(1); | |
let result = lib.getConsumptionByTimeWindow(data, 8, 15) / 8 * 10e-4 | |
result = +result.toFixed(5) | |
t.strictEqual(result, 0.44925); | |
}); | |
test("Calculate the sensor's average hourly consumption from 16:00 to 23:00 inclusive", t => { | |
t.plan(1); | |
let result = lib.getConsumptionByTimeWindow(data, 16, 23) / 8 * 10e-4 | |
result = +result.toFixed(3) | |
t.strictEqual(result, 0.567); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment