Skip to content

Instantly share code, notes, and snippets.

@sedera-tax
Created February 15, 2021 09:41
Show Gist options
  • Save sedera-tax/d36e53e88d66fc230abe76f95523eaae to your computer and use it in GitHub Desktop.
Save sedera-tax/d36e53e88d66fc230abe76f95523eaae to your computer and use it in GitHub Desktop.
function sleepTracker($heartRateLog) {
$res = [0,0,0,0,0,0];
$awake = 0;
$rem = 0;
$ls = 0;
$ds = 0;
foreach ($heartRateLog as $h) {
if ($h >= 90 && $h <= 100) {
$awake++;
}
else if ($h >= 60 && $h <= 89) {
$rem++;
}
else if ($h >= 50 && $h <= 59) {
$ls++;
}
else if ($h >= 40 && $h <= 49) {
$ds++;
}
}
$sleep = count($heartRateLog) - $awake;
$score = 100;
$text = '';
if ($sleep <= 120 || $sleep >= 600) {
$score = 40;
}
else if($sleep == 480) {
$score = 100;
}
else if ($sleep > 120 && $sleep < 360) {
//[40;60];
$score = (int) (40 + (($sleep - 120) * (60 - 40)) / (360 - 120));
}
else if ($sleep >= 360 && $sleep < 480) {
//[60;100];
$score = (int) (60 + (($sleep - 360) * (100 - 60)) / (480 - 360));
}
else if ($sleep > 480 && $sleep < 540) {
//[100;60];
$score = (int) (100 - (($sleep - 480) * (100 - 60)) / (540 - 480));
}
else if ($sleep >= 540 && $sleep < 600) {
//[60;40];
$score = (int) (60 - (($sleep - 540) * (60 - 40)) / (600 - 540));
}
if ($score < 60) {
$text = 'poor';
}
else if ($score >= 60 && $score <= 79) {
$text = 'fair';
}
else if ($score >= 80 && $score <= 94) {
$text = 'good';
}
else if ($score >= 95 && $score <= 100) {
$text = 'excellent';
}
$res[0] = convertToHoursMins($awake, '%02dh%02dm');
$res[1] = convertToHoursMins($rem, '%02dh%02dm');
$res[2] = convertToHoursMins($ls, '%02dh%02dm');
$res[3] = convertToHoursMins($ds, '%02dh%02dm');
$res[4] = convertToHoursMins($sleep, '%02dh%02dm');
$res[5] = $score . ':' . $text;
return $res;
}
function convertToHoursMins($time, $format = '%02d:%02d') {
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
return sprintf($format, $hours, $minutes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment