Skip to content

Instantly share code, notes, and snippets.

@Ovilia Ovilia/SleepBot.js
Last active Dec 26, 2015

Embed
What would you like to do?
Analysize Sleeping Records from SleepBot
var fs = require('fs');
// SleepBot.csv is downloaded from https://mysleepbot.com
var lines = fs.readFileSync('SleepBot.csv').toString().split('\n');
var sumToSleep = 0;
var sumToAwake = 0;
var sumSleepMin = 0;
// [[0, 1, 2, ..., 23]th hour of Monday, Tuesday, ...]
var sumSleepHourBin = [];
var sumAwakeHourBin = [];
(function() {
var arr = [];
for (var hid = 0; hid < 24; ++hid) {
arr.push(0);
}
for (var did = 0; did < 7; ++did) {
sumSleepHourBin.push(arr.slice());
sumAwakeHourBin.push(arr.slice());
}
})();
// toSleepMin and toAwakeMin for 7 days
var sumToSleepWeek = [0, 0, 0, 0, 0, 0, 0];
var sumToAwakeWeek = [0, 0, 0, 0, 0, 0, 0];
var cntToSleepWeek = [0, 0, 0, 0, 0, 0, 0];
var cntToAwakeWeek = [0, 0, 0, 0, 0, 0, 0];
// sleep less than 6 hours a day
var sleeplessWeek = [0, 0, 0, 0, 0, 0, 0];
var lastDayOfWeek = null;
var dayOfWeekBin = [0, 0, 0, 0, 0, 0, 0];
// monthly info
var lastMonth = null;
var sumToSleepLastMonth = 0;
var sumToAwakeLastMonth = 0;
var validLineCntLastMonth = 0;
var lastMonthArr = [];
var validLineCnt = 0;
for (var lid = 1, llen = lines.length; lid < llen; ++lid) {
var line = lines[lid].split('\t').filter(function (el) {
return el.length != 0
});
// empty line
if (line.length < 1) {
console.warn('Invalid line ignored:', lid, line);
continue;
}
// day of week
var date = new Date(line[0]);
// Monday is 0, Sunday is 6
var dayOfWeek = date.getDay() - 1;
if (dayOfWeek < 0) {
dayOfWeek += 7;
}
// to sleep and awake time bin
var toSleepHour = toTimeArray(line[1])[0];
var toAwakeHour = toTimeArray(line[2])[0];
++sumSleepHourBin[dayOfWeek][toSleepHour];
++sumAwakeHourBin[dayOfWeek][toAwakeHour];
// to sleep and awake time
var toSleepMin = toTimeTotalMin(line[1]);
var toAwakeMin = toTimeTotalMin(line[2]);
sumToSleep += toSleepMin;
sumToAwake += toAwakeMin;
// to sleep and awake time for each day
sumToSleepWeek[dayOfWeek] += toSleepMin;
++cntToSleepWeek[dayOfWeek];
sumToAwakeWeek[dayOfWeek] += toAwakeMin;
++cntToAwakeWeek[dayOfWeek];
// total sleep time
var sleepTime = toAwakeMin - toSleepMin;
sumSleepMin += sleepTime;
// dayOfWeek is awake day, -1 for sleep night
var nightDay = dayOfWeek - 1;
if (nightDay < 0) {
nightDay += 7;
}
// sleepless if sleep less than 6 hours a day
if (sleepTime < 6 * 60) {
++sleeplessWeek[nightDay];
}
++dayOfWeekBin[nightDay];
// last month
// a new month
var month = date.getMonth();
if (month !== lastMonth && lastMonth !== null) {
recordLastMonth(month);
}
sumToSleepLastMonth += toSleepMin;
sumToAwakeLastMonth += toAwakeMin;
++validLineCntLastMonth;
lastMonth = month;
++validLineCnt;
}
recordLastMonth(month);
for (var did = 0; did < 7; ++did) {
sumToSleepWeek[did] = cntToSleepWeek ?
sumToSleepWeek[did] / cntToSleepWeek[did] / 60 : 0;
sumToAwakeWeek[did] = cntToAwakeWeek ?
sumToAwakeWeek[did] / cntToAwakeWeek[did] / 60 : 0;
}
console.log('Valid lines:', validLineCnt);
console.log('Average sleep time is', toTimeStr(sumToSleep / validLineCnt));
console.log('Average awake time is', toTimeStr(sumToAwake / validLineCnt));
console.log('Average sleep length is', toTimeStr(sumSleepMin / validLineCnt));
console.log('SleepBin is', sumSleepHourBin);
console.log('AwakeBin is', sumAwakeHourBin);
console.log('LastMonth is', lastMonthArr);
console.log('Weekly Sleep time is', sumToSleepWeek);
console.log('Weekly Awake time is', sumToAwakeWeek);
console.log('Sleepless nights are', sleeplessWeek);
console.log('Week nights are', dayOfWeekBin);
function toInt(el) {
return parseInt(el, 10);
}
function toTimeStr(totalMin) {
if (totalMin < 0) {
// change -0:48 to be 23:12
totalMin += 24 * 60;
}
var hour = Math.floor(totalMin / 60);
var min = Math.floor(totalMin - hour * 60);
return hour + ':' + min;
}
function toTimeTotalMin(timeStr) {
var time = timeStr.split(':').map(toInt);
var totalMin = time[0] * 60 + time[1];
if (totalMin > 12 * 60) {
// change 23:12 to be -0:48
totalMin -= 24 * 60;
}
return totalMin;
}
function toTimeArray(timeStr) {
return timeStr.split(':').map(toInt);
}
function recordLastMonth(month) {
if (validLineCntLastMonth !== 0) {
lastMonthArr.push([
date.getFullYear() + '-' + (date.getMonth() + 1),
Math.floor(sumToSleepLastMonth / validLineCntLastMonth / 60 * 100) / 100,
Math.floor(sumToAwakeLastMonth / validLineCntLastMonth / 60 * 100) / 100
]);
}
sumToSleepLastMonth = 0;
sumToAwakeLastMonth = 0;
validLineCntLastMonth = 0;
}
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.