Skip to content

Instantly share code, notes, and snippets.

@StarpTech StarpTech/index.js
Last active Feb 8, 2018

Embed
What would you like to do?
Practical solution to https://github.com/onzo-com/etc-kata
"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));
"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
You can’t perform that action at this time.