Move value parsing to separate lib
This commit is contained in:
parent
ed672c7214
commit
9e6ca61dcf
2 changed files with 12 additions and 11 deletions
|
@ -1,3 +1,5 @@
|
|||
import { parseValue } from './values';
|
||||
|
||||
export class Timeseries {
|
||||
constructor(id, endDate, days = 7, slots = 24) {
|
||||
this.id = id;
|
||||
|
@ -69,16 +71,6 @@ export class Timeseries {
|
|||
return labels;
|
||||
}
|
||||
|
||||
parseValue(val) {
|
||||
if (typeof val === 'boolean') {
|
||||
if (val) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return parseFloat(val);
|
||||
}
|
||||
|
||||
flattenData(data, fromDay = 0, fromHour = 0) {
|
||||
const flattened = [];
|
||||
data.forEach((dayData, dayIdx) => {
|
||||
|
@ -100,7 +92,7 @@ export class Timeseries {
|
|||
const slots = this.prepareSlots(null);
|
||||
data.forEach((point) => {
|
||||
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 timeSlot = Math.floor(pointDate.getHours() / (24 / this.slots));
|
||||
if (slots[daySlot][timeSlot] === null) {
|
||||
|
|
9
lib/values.js
Normal file
9
lib/values.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
export function parseValue(val) {
|
||||
if (typeof val === 'boolean') {
|
||||
if (val) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return parseFloat(val);
|
||||
}
|
Loading…
Reference in a new issue