Move value parsing to separate lib

This commit is contained in:
Henri Bergius 2017-11-30 21:21:40 +01:00
parent ed672c7214
commit 9e6ca61dcf
2 changed files with 12 additions and 11 deletions

View file

@ -1,3 +1,5 @@
import { parseValue } from './values';
export class Timeseries { export class Timeseries {
constructor(id, endDate, days = 7, slots = 24) { constructor(id, endDate, days = 7, slots = 24) {
this.id = id; this.id = id;
@ -69,16 +71,6 @@ export class Timeseries {
return labels; return labels;
} }
parseValue(val) {
if (typeof val === 'boolean') {
if (val) {
return 1;
}
return 0;
}
return parseFloat(val);
}
flattenData(data, fromDay = 0, fromHour = 0) { flattenData(data, fromDay = 0, fromHour = 0) {
const flattened = []; const flattened = [];
data.forEach((dayData, dayIdx) => { data.forEach((dayData, dayIdx) => {
@ -100,7 +92,7 @@ export class Timeseries {
const slots = this.prepareSlots(null); const slots = this.prepareSlots(null);
data.forEach((point) => { data.forEach((point) => {
const pointDate = new Date(point.timestamp); const pointDate = new Date(point.timestamp);
const pointValue = this.parseValue(point.value); const pointValue = parseValue(point.value);
const daySlot = Math.floor((pointDate - this.startDate) / (1000 * 60 * 60 * 24)); const daySlot = Math.floor((pointDate - this.startDate) / (1000 * 60 * 60 * 24));
const timeSlot = Math.floor(pointDate.getHours() / (24 / this.slots)); const timeSlot = Math.floor(pointDate.getHours() / (24 / this.slots));
if (slots[daySlot][timeSlot] === null) { if (slots[daySlot][timeSlot] === null) {

9
lib/values.js Normal file
View file

@ -0,0 +1,9 @@
export function parseValue(val) {
if (typeof val === 'boolean') {
if (val) {
return 1;
}
return 0;
}
return parseFloat(val);
}