Spaces:
Running
อัปเดตเว็บแอป HTML5 หนึ่งหน้า (มือถือเป็นหลัก) ดังนี้:
Browse filesคงโครงสร้างแท็บ: Today / Recovery / Sleep / Training / Trends และโมดอล Journal / Settings / Report
ใช้ mock data + localStorage เก็บสถานะ
ใส่ สูตรคำนวณจริงจัง สำหรับ Recovery, Sleep Coach, Strain Target, และ Baseline modeling
ทำให้ Journal (Behaviour) มีผลต่อเมตริกวันถัดไปแบบมีน้ำหนัก, ครึ่งอายุ (half-life) และการทับซ้อนผล (stacking)
เพิ่ม หน้าตั้งค่า Sensitivity และ Waterfall contribution ให้ผู้ใช้เห็นว่าปัจจัยไหนเพิ่ม/ลดคะแนน
1) โมเดล Baseline & Z-score
สร้างฟังก์ชันใหม่ใน JS (ทั้งหมด inline):
computeBaselines(history, windowShort=21, windowLong=60)
คำนวณ baseline แบบ EWMA และ percentile band สำหรับแต่ละเมตริก: hrv_ms, rhr_bpm, sleep_duration_h, resp_rate, skin_temp_delta, strain
EWMA: ewma_t = α * x_t + (1-α) * ewma_{t-1} โดย α = 2/(N+1) และทำทั้งหน้าต่างสั้น/ยาว
เก็บ zScore = (x - baseline_long.mean) / baseline_long.std (ป้องกันหารศูนย์ด้วย ε=1e-6)
ส่งออก { baselineShort, baselineLong, zScores, bands }
ใช้ guard: clip ค่า z ที่ ±3.5 และกำหนดช่วงเมตริกเชิงสรีรวิทยา เช่น HRV 20–180 ms, RHR 35–120 bpm, Resp 8–28 rpm, SkinTempΔ −1.5–+1.5 °C
2) สูตรคำนวณ Recovery Score (0–100)
เพิ่ม computeRecoveryScore(state):
สร้างสเกลย่อย (ก่อน behaviour):
hrv_component = mapZPositive(z.hrv_ms) * w_hrv
rhr_component = mapZNegative(z.rhr_bpm) * w_rhr
sleep_component = clamp( sleep_perf_pct/100, 0, 1 ) * w_sleep
resp_component = mapZNegative(abs(z.resp_rate)) * w_resp
skin_component = mapZNegative(abs(z.skin_temp_delta)) * w_skin
yesterday_strain_component = mapRange(strain_yday, 0..21 → 1..0) * w_strainLoad
illness_flag_component = (ill ? 0 : 1) * w_ill (ill = ป่วย/เจ็บคอ/ไข้ จาก Journal)
รวมคะแนนก่อนปรับพฤติกรรม:
raw = normalize01( hrv + rhr + sleep + resp + skin + strainLoad + ill )
จากนั้น Behaviour Adjustments (ข้อ 3) จะเปลี่ยนเมตริกอินพุต ก่อนสรุปรวม เพื่อให้ Waterfall อธิบายผลกระทบได้
Post-processing:
recovery = round( clamp( 100 * (raw ** γ_curve), 0, 100) )
ให้เพิ่ม Sensitivity slider ใน Settings (0.5–2.0) ที่แปลงเป็น γ_curve และสเกลน้ำหนัก w_* อัตโนมัติ
Helper mapping
mapZPositive(z) = clamp( sigmoid( z ), 0, 1 )
mapZNegative(z) = clamp( sigmoid( -z ), 0, 1 )
sigmoid(x)=1/(1+e^{-k*x}) ตั้ง k=0.9 (ปรับได้จาก Settings “Aggressiveness”)
normalize01(sum) = หารด้วยผลรวมของน้ำหนักทั้งหมด (ป้องกันหารศูนย์)
น้ำหนักเริ่มต้น (ปรับได้ใน Settings):
w_hrv=0.28, w_rhr=0.22, w_sleep=0.22, w_resp=0.06, w_skin=0.06, w_strainLoad=0.10, w_ill=0.06
3) Behaviour Engine (Journal → Metric Effects)
เพิ่มระบบ “ผลของพฤติกรรม” ที่กระทบเมตริกของ วันนี้/วันถัดไป ด้วยครึ่งอายุและบทลงโทษ/รางวัล
สคีมา Journal (อัปเดต):
{
"id": "caffeine_late",
"label_th": "คาเฟอีนหลังบ่าย 2",
"category": "Stimulants",
"intensity": 0, // 0-3 จาก UI
"effect": [
{"target":"sleep_need_h","type":"add","value":0.4,"per_intensity":true,"half_life_days":1},
{"target":"sleep_efficiency_pct","type":"mul","value":0.96,"per_intensity":true,"half_life_days":1},
{"target":"hrv_ms","type":"mul","value":0.98,"per_intensity":true,"half_life_days":1}
],
"applies": {"time_window":"today_evening", "sex":"any"}
}
กำหนด แผนที่ผลกระทบ (mapping) สำหรับพฤติกรรมสำคัญ (ตัวอย่างชุดเริ่มต้น ~30 รายการ; อนุญาตให้ขยายได้ใน Settings):
Alcohol (low/med/high) → ↑ sleep_need, ↓ sleep_eff, ↑ rhr, ↓ hrv (half-life 1–2d ตามปริมาณ)
Late caffeine → ↑ sleep_need, ↓ sleep_eff, ↓ hrv (1d)
Heavy meal late → ↓ sleep_eff (1d)
Sauna/Cold plunge → ↑ hrv เล็กน้อย, ↓ rhr เล็กน้อย (0.5–1d)
Intense travel/Jet lag → ↑ sleep_need, ↑ resp, ↑ skin_tempΔ (2–3d)
Illness symptoms → ↑ rhr, ↓ hrv, ↓ recovery cap, ↑ resp, ↑ skin_tempΔ (2–5d)
Strength day / HIIT → ↑ strain capacity วันนี้, ↑ sleep_need คืนนี้ (1d)
Meditation/Stretch → ↑ hrv เล็กน้อย (0.5–1d)
Menstruation phases (ถ้าเลือกเพศหญิง) → ปรับ sleep_need/strain_cap เป็นรอบเดือน (half-life ตาม phase)
ฟังก์ชันใหม่:
applyBehaviorEffects(journalActive, metricsToday)
รวมผล type: add/mul ต่อ input metrics: sleep_need_h, sleep_efficiency_pct, hrv_ms, rhr_bpm, resp_rate, skin_temp_delta, strain_capacity
บันทึก contribution รายตัวไว้ใน state.contributions เพื่อใช้กับ Waterfall
decayBehaviorEffects(prevCarryOver)
ใช้ value * 0.5^(days/half_life) กับทุกเอฟเฟกต์ที่ carry-over คงค้าง
persistBehaviorState()
เก็บผลคงค้าง (carry-over) ไว้ใน localStorage.behaviourCarry
ลำดับคำนวณประจำวัน (Initialization):
โหลด historical & carry-over
decayBehaviorEffects() → ได้ผลลัพธ์คงค้าง t-1
รวมกับ journal วันนี้ ผ่าน applyBehaviorEffects() → ได้ metrics ที่ปรับแล้ว
computeBaselines() + computeRecoveryScore()
เรนเดอร์ทุกแท็บ + อัปเดตกราฟ
4) Sleep Coach (จำลองจริงจัง)
เพิ่ม computeSleepCoach(state):
sleep_need = target_base(7.5h) + k_strain * mapRange(strain_today,0..21→0..1) + k_debt * sleep_debt
sleep_window = preferred_window(user_pref) และ chronotype_offset (0 = none, ถ้าผู้ใช้เปิด)
ให้ คาเฟอีน/แอลกอฮอล์/เดินทาง เพิ่ม sleep_need และลด sleep_efficiency_pct ตาม mapping
คืนค่า: {recommended_bedtime, recommended_waketime, rationale: [{factor, delta_h}]}
อัปเดตหน้าสลีปให้แสดง stacked area sleep stages และ Sleep Debt ที่คำนวณจาก need - actual แบบ rolling 14 วัน
5) Strain Target & Training Guard
เพิ่ม computeStrainTarget(state):
strain_cap = 21 * clamp( 0.5 + 0.5*mapZPositive(z.hrv_ms) - 0.5*mapZPositive(z.rhr_bpm) + behaviour_bonus, 0.2, 1.0 )
ถ้า recovery < threshold_low (เช่น 33) → แสดง Guard แนะนำฝึกเบา
ถ้ามี illness active → ลด cap เพิ่มเติม และเปลี่ยน UI เป็น “Rest suggested”
Training live (mock) ให้ Strain สดไม่เกิน strain_cap (bar แสดง cap)
6) Waterfall Contributions (Recovery)
ในแท็บ Recovery เพิ่มกราฟ “Why is my recovery X?” แบบ waterfall bar (Chart.js):
แกนตั้ง: คะแนนส่วนเพิ่ม (−/+)
แกนนอน: HRV, RHR, Sleep, Resp, Skin, Yesterday Strain, Illness, Behaviours (รวม), Residual
คลิกแท่ง Behaviours เปิดโมดอลแสดงรายการย่อยและค่าส่งผล (เช่น “Alcohol high: −8.2”, “Sauna: +1.1”)
7) Settings ใหม่ (Sensitivity & Model)
สไลเดอร์: Sensitivity (0.5–2.0), Aggressiveness k (0.5–1.5), Weights editor (ตารางแก้ w_*)
เช็คบ็อกซ์: Enable chronotype, Show long-term bands, Clip z at ±3.0
ปุ่ม Calibrate baselines (14d/30d): สุ่มสร้างข้อมูล 14/30 วันใหม่เพื่อตั้ง baseline และล้าง debt/carry-over
8) โครงสร้างข้อมูล & ตัวอย่าง
ปรับ mockDB ให้มี:
{
"profile": {"name":"Demo User","age":32,"gender":"unspecified","chronotype":"none"},
"today": {"date":"YYYY-MM-DD", "strain":12.8, "hrv_ms":82, "rhr_bpm":53, "resp_rate":14.4, "skin_temp_delta":0.0,
"sleep_actual_h":7.1, "sleep_efficiency_pct":92, "ill": false},
"carryOver": { "effects": [] }, // บันทึกผลคงค้างของ behaviours
"journal_logs": [{ "date":"YYYY-MM-DD", "items":[{"id":"alcohol_high","intensity":2}, {"id":"sauna","intensity":1}] }],
"history": { "days": [ /* 90 รายการ: {date, hrv_ms, rhr_bpm, strain, sleep_actual_h, resp_rate, skin_temp_delta, recovery} */ ] },
"behaviour_catalog": [ /* 30–50 รายการตาม mapping */ ]
}
กำหนด generator สร้าง 90 วันย้อนหลัง ให้สัมพันธ์กัน: HRV ต่ำ ↔ RHR สูง, Strain �
- index.html +1056 -109
|
@@ -348,14 +348,13 @@
|
|
| 348 |
<h2 class="text-2xl font-bold mb-1" data-translate="recovery">การฟื้นตัว</h2>
|
| 349 |
<p class="text-sm opacity-75" data-translate="recovery-subtitle">ติดตามสถานะการฟื้นตัวของคุณ</p>
|
| 350 |
</div>
|
| 351 |
-
|
| 352 |
-
<!-- Recovery Score Card -->
|
| 353 |
<div class="card mb-4">
|
| 354 |
<div class="text-center py-4">
|
| 355 |
-
<div class="text-5xl font-bold text-green-400 mb-2">74</div>
|
| 356 |
<div class="text-lg font-medium mb-3" data-translate="recovery-score">คะแนนการฟื้นตัว</div>
|
| 357 |
<div class="w-full bg-gray-700 rounded-full h-4 mb-2">
|
| 358 |
-
<div class="bg-gradient-to-r from-green-500 to-green-400 h-4 rounded-full" style="width: 74%"></div>
|
| 359 |
</div>
|
| 360 |
<div class="flex justify-between text-xs opacity-75">
|
| 361 |
<span data-translate="poor">แย่</span>
|
|
@@ -366,7 +365,15 @@
|
|
| 366 |
</div>
|
| 367 |
</div>
|
| 368 |
|
| 369 |
-
<!--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
<div class="card mb-4">
|
| 371 |
<h3 class="font-semibold mb-3">HRV (7 วัน)</h3>
|
| 372 |
<canvas id="hrv-chart" height="150"></canvas>
|
|
@@ -408,8 +415,7 @@
|
|
| 408 |
<h2 class="text-2xl font-bold mb-1" data-translate="sleep">การนอนหลับ</h2>
|
| 409 |
<p class="text-sm opacity-75" data-translate="sleep-subtitle">วิเคราะห์คุณภาพการนอนหลับของคุณ</p>
|
| 410 |
</div>
|
| 411 |
-
|
| 412 |
-
<!-- Sleep Coach Card -->
|
| 413 |
<div class="card mb-4">
|
| 414 |
<h3 class="font-semibold mb-3 flex items-center gap-2">
|
| 415 |
<i data-feather="moon" class="w-4 h-4 text-blue-400"></i>
|
|
@@ -419,23 +425,25 @@
|
|
| 419 |
<div class="text-center p-3 rounded-lg bg-blue-500/20">
|
| 420 |
<i data-feather="log-in" class="w-5 h-5 mx-auto mb-1 text-blue-400"></i>
|
| 421 |
<div class="text-xs opacity-75 mb-1" data-translate="bedtime">เวลาเข้านอน</div>
|
| 422 |
-
<div class="font-semibold">22:45</div>
|
| 423 |
</div>
|
| 424 |
<div class="text-center p-3 rounded-lg bg-yellow-500/20">
|
| 425 |
<i data-feather="log-out" class="w-5 h-5 mx-auto mb-1 text-yellow-400"></i>
|
| 426 |
<div class="text-xs opacity-75 mb-1" data-translate="wake-time">ตื่นนอน</div>
|
| 427 |
-
<div class="font-semibold">06:30</div>
|
| 428 |
</div>
|
| 429 |
</div>
|
| 430 |
<div class="mt-3 p-3 rounded-lg bg-green-500/20">
|
| 431 |
<div class="flex items-center gap-2 text-green-400">
|
| 432 |
<i data-feather="target" class="w-4 h-4"></i>
|
| 433 |
-
<span class="text-sm"
|
| 434 |
</div>
|
| 435 |
</div>
|
|
|
|
|
|
|
|
|
|
| 436 |
</div>
|
| 437 |
-
|
| 438 |
-
<!-- Sleep Stages -->
|
| 439 |
<div class="card mb-4">
|
| 440 |
<h3 class="font-semibold mb-3" data-translate="sleep-stages">ระยะการนอนหลับ</h3>
|
| 441 |
<canvas id="sleep-stages-chart" height="150"></canvas>
|
|
@@ -472,14 +480,24 @@
|
|
| 472 |
<h2 class="text-2xl font-bold mb-1" data-translate="training">การฝึก</h2>
|
| 473 |
<p class="text-sm opacity-75" data-translate="training-subtitle">บันทึกและติดตามการออกกำลังกาย</p>
|
| 474 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
|
| 476 |
-
<!-- Start Workout Button -->
|
| 477 |
<button onclick="startWorkout()" class="w-full card bg-gradient-to-r from-red-500 to-orange-500 text-white font-semibold py-4 flex items-center justify-center gap-2 hover:opacity-90 transition mb-4">
|
| 478 |
<i data-feather="play-circle" class="w-6 h-6"></i>
|
| 479 |
<span data-translate="start-workout">เริ่ม Live Workout</span>
|
| 480 |
</button>
|
| 481 |
-
|
| 482 |
-
<!-- Sport Selection -->
|
| 483 |
<div class="card mb-4">
|
| 484 |
<h3 class="font-semibold mb-3" data-translate="select-sport">เลือกชนิดกีฬา</h3>
|
| 485 |
<div class="grid grid-cols-4 gap-2">
|
|
@@ -649,20 +667,20 @@
|
|
| 649 |
<h4 class="font-semibold mb-2" data-translate="nutrition">โภชนาการ</h4>
|
| 650 |
<div class="grid grid-cols-2 gap-2">
|
| 651 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 652 |
-
<input type="checkbox" class="form-checkbox text-primary">
|
| 653 |
-
<span class="text-sm">
|
| 654 |
</label>
|
| 655 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 656 |
-
<input type="checkbox" class="form-checkbox text-primary">
|
| 657 |
-
<span class="text-sm">
|
| 658 |
</label>
|
| 659 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 660 |
-
<input type="checkbox" class="form-checkbox text-primary">
|
| 661 |
-
<span class="text-sm">
|
| 662 |
</label>
|
| 663 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 664 |
-
<input type="checkbox" class="form-checkbox text-primary">
|
| 665 |
-
<span class="text-sm">
|
| 666 |
</label>
|
| 667 |
</div>
|
| 668 |
</div>
|
|
@@ -670,20 +688,20 @@
|
|
| 670 |
<h4 class="font-semibold mb-2" data-translate="lifestyle">ไลฟ์สไตล์</h4>
|
| 671 |
<div class="grid grid-cols-2 gap-2">
|
| 672 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 673 |
-
<input type="checkbox" class="form-checkbox text-primary">
|
| 674 |
-
<span class="text-sm">
|
| 675 |
</label>
|
| 676 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 677 |
-
<input type="checkbox" class="form-checkbox text-primary">
|
| 678 |
-
<span class="text-sm">
|
| 679 |
</label>
|
| 680 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 681 |
-
<input type="checkbox" class="form-checkbox text-primary">
|
| 682 |
-
<span class="text-sm">
|
| 683 |
</label>
|
| 684 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 685 |
-
<input type="checkbox" class="form-checkbox text-primary">
|
| 686 |
-
<span class="text-sm">
|
| 687 |
</label>
|
| 688 |
</div>
|
| 689 |
</div>
|
|
@@ -691,24 +709,24 @@
|
|
| 691 |
<h4 class="font-semibold mb-2" data-translate="recovery-tools">เครื่องมือฟื้นตัว</h4>
|
| 692 |
<div class="grid grid-cols-2 gap-2">
|
| 693 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 694 |
-
<input type="checkbox" class="form-checkbox text-primary">
|
| 695 |
-
<span class="text-sm">
|
| 696 |
</label>
|
| 697 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 698 |
-
<input type="checkbox" class="form-checkbox text-primary">
|
| 699 |
-
<span class="text-sm">
|
| 700 |
</label>
|
| 701 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 702 |
-
<input type="checkbox" class="form-checkbox text-primary">
|
| 703 |
-
<span class="text-sm">
|
| 704 |
</label>
|
| 705 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 706 |
-
<input type="checkbox" class="form-checkbox text-primary">
|
| 707 |
-
<span class="text-sm">
|
| 708 |
</label>
|
| 709 |
</div>
|
| 710 |
</div>
|
| 711 |
-
|
| 712 |
<label class="block">
|
| 713 |
<span class="font-semibold mb-2 block" data-translate="mood">อารมณ์</span>
|
| 714 |
<select class="w-full p-2 rounded-lg bg-gray-700/50 border border-gray-600">
|
|
@@ -726,10 +744,9 @@
|
|
| 726 |
</div>
|
| 727 |
</div>
|
| 728 |
</div>
|
| 729 |
-
|
| 730 |
-
<!-- Settings Modal -->
|
| 731 |
<div id="settings-modal" class="modal">
|
| 732 |
-
<div class="modal-content slide-up">
|
| 733 |
<div class="p-6">
|
| 734 |
<div class="flex justify-between items-center mb-4">
|
| 735 |
<h3 class="text-xl font-bold" data-translate="settings">การตั้งค่า</h3>
|
|
@@ -737,54 +754,187 @@
|
|
| 737 |
<i data-feather="x" class="w-5 h-5"></i>
|
| 738 |
</button>
|
| 739 |
</div>
|
| 740 |
-
<div class="space-y-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 741 |
<div>
|
| 742 |
-
<
|
| 743 |
-
<
|
| 744 |
-
<
|
| 745 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 746 |
</select>
|
| 747 |
</div>
|
|
|
|
|
|
|
| 748 |
<div>
|
| 749 |
-
<
|
| 750 |
-
<
|
| 751 |
-
<
|
| 752 |
-
|
| 753 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 754 |
</div>
|
|
|
|
|
|
|
| 755 |
<div>
|
| 756 |
-
<
|
| 757 |
-
|
| 758 |
-
<
|
| 759 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 760 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 761 |
</div>
|
| 762 |
</div>
|
| 763 |
</div>
|
| 764 |
</div>
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
const mockData = {
|
| 769 |
profile: {
|
| 770 |
name: "Demo User",
|
| 771 |
age: 32,
|
| 772 |
-
gender: "unspecified"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 773 |
},
|
| 774 |
today: {
|
| 775 |
date: "2025-11-11",
|
| 776 |
-
strain:
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 784 |
},
|
| 785 |
sleep: {
|
| 786 |
-
|
| 787 |
-
|
|
|
|
|
|
|
|
|
|
| 788 |
stages: [
|
| 789 |
{t: "22:50", stage: "Light", duration: 50},
|
| 790 |
{t: "23:40", stage: "Deep", duration: 90},
|
|
@@ -794,17 +944,584 @@
|
|
| 794 |
{t: "05:40", stage: "REM", duration: 50},
|
| 795 |
{t: "06:30", stage: "Awake", duration: 0}
|
| 796 |
],
|
| 797 |
-
|
| 798 |
-
}
|
| 799 |
-
recovery_history: generateRecoveryHistory(30),
|
| 800 |
-
strain_history: generateStrainHistory(30),
|
| 801 |
-
workouts: generateWorkouts(10),
|
| 802 |
-
journal_logs: [],
|
| 803 |
-
hrv_data: generateHRVData(14),
|
| 804 |
-
rhr_data: generateRHRData(14)
|
| 805 |
};
|
| 806 |
|
| 807 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 808 |
const translations = {
|
| 809 |
th: {
|
| 810 |
"app-title": "VitalTrack Pro",
|
|
@@ -992,18 +1709,17 @@
|
|
| 992 |
}
|
| 993 |
return data;
|
| 994 |
}
|
| 995 |
-
|
| 996 |
// Initialize app
|
| 997 |
function initApp() {
|
| 998 |
feather.replace();
|
| 999 |
-
|
|
|
|
| 1000 |
updateDateTime();
|
| 1001 |
setInterval(updateDateTime, 60000);
|
| 1002 |
setInterval(updateLiveHR, 1000);
|
| 1003 |
renderWorkoutList();
|
| 1004 |
}
|
| 1005 |
-
|
| 1006 |
-
// Update date/time
|
| 1007 |
function updateDateTime() {
|
| 1008 |
const now = new Date();
|
| 1009 |
const options = { year: 'numeric', month: 'long', day: 'numeric' };
|
|
@@ -1065,10 +1781,31 @@
|
|
| 1065 |
}
|
| 1066 |
});
|
| 1067 |
}
|
| 1068 |
-
|
| 1069 |
// Modal functions
|
| 1070 |
function openJournalModal() {
|
| 1071 |
-
document.getElementById('journal-modal')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1072 |
}
|
| 1073 |
|
| 1074 |
function closeJournalModal() {
|
|
@@ -1077,32 +1814,120 @@
|
|
| 1077 |
|
| 1078 |
function openSettingsModal() {
|
| 1079 |
document.getElementById('settings-modal').classList.add('active');
|
|
|
|
| 1080 |
}
|
| 1081 |
|
| 1082 |
function closeSettingsModal() {
|
| 1083 |
document.getElementById('settings-modal').classList.remove('active');
|
| 1084 |
}
|
| 1085 |
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
| 1090 |
-
|
| 1091 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1092 |
};
|
| 1093 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1094 |
document.querySelectorAll('#journal-modal input[type="checkbox"]:checked').forEach(checkbox => {
|
| 1095 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1096 |
});
|
| 1097 |
|
| 1098 |
-
|
| 1099 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1100 |
|
| 1101 |
-
//
|
|
|
|
|
|
|
| 1102 |
showToast('Journal saved successfully!');
|
| 1103 |
}
|
| 1104 |
-
|
| 1105 |
-
// Workout functions
|
| 1106 |
function startWorkout() {
|
| 1107 |
document.getElementById('live-workout').classList.remove('hidden');
|
| 1108 |
workoutStartTime = Date.now();
|
|
@@ -1438,17 +2263,132 @@
|
|
| 1438 |
// Redraw charts with new period
|
| 1439 |
drawTrendsCharts();
|
| 1440 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1441 |
|
| 1442 |
// Update live HR
|
| 1443 |
function updateLiveHR() {
|
| 1444 |
-
const hr =
|
| 1445 |
document.getElementById('current-hr').textContent = hr;
|
| 1446 |
-
|
|
|
|
|
|
|
| 1447 |
}
|
| 1448 |
-
|
| 1449 |
// Export data
|
| 1450 |
function exportData() {
|
| 1451 |
-
const dataStr = JSON.stringify(
|
| 1452 |
const dataBlob = new Blob([dataStr], {type: 'application/json'});
|
| 1453 |
const url = URL.createObjectURL(dataBlob);
|
| 1454 |
const link = document.createElement('a');
|
|
@@ -1460,12 +2400,12 @@
|
|
| 1460 |
|
| 1461 |
// Reset data
|
| 1462 |
function resetData() {
|
| 1463 |
-
if (confirm('Are you sure you want to reset all
|
|
|
|
| 1464 |
location.reload();
|
| 1465 |
}
|
| 1466 |
}
|
| 1467 |
-
|
| 1468 |
-
// Toast notification
|
| 1469 |
function showToast(message) {
|
| 1470 |
const toast = document.createElement('div');
|
| 1471 |
toast.className = 'fixed top-20 left-1/2 transform -translate-x-1/2 bg-green-500 text-white px-4 py-2 rounded-lg shadow-lg z-50';
|
|
@@ -1491,14 +2431,21 @@
|
|
| 1491 |
toggleLanguage();
|
| 1492 |
}
|
| 1493 |
}
|
| 1494 |
-
|
| 1495 |
// Initialize on load
|
| 1496 |
document.addEventListener('DOMContentLoaded', () => {
|
| 1497 |
loadPreferences();
|
| 1498 |
initApp();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1499 |
});
|
| 1500 |
-
|
| 1501 |
-
// Handle resize
|
| 1502 |
window.addEventListener('resize', () => {
|
| 1503 |
drawHRChart();
|
| 1504 |
if (document.getElementById('tab-recovery').classList.contains('active')) {
|
|
|
|
| 348 |
<h2 class="text-2xl font-bold mb-1" data-translate="recovery">การฟื้นตัว</h2>
|
| 349 |
<p class="text-sm opacity-75" data-translate="recovery-subtitle">ติดตามสถานะการฟื้นตัวของคุณ</p>
|
| 350 |
</div>
|
| 351 |
+
<!-- Enhanced Recovery Score Card -->
|
|
|
|
| 352 |
<div class="card mb-4">
|
| 353 |
<div class="text-center py-4">
|
| 354 |
+
<div class="text-5xl font-bold text-green-400 mb-2" id="recovery-score-display">74</div>
|
| 355 |
<div class="text-lg font-medium mb-3" data-translate="recovery-score">คะแนนการฟื้นตัว</div>
|
| 356 |
<div class="w-full bg-gray-700 rounded-full h-4 mb-2">
|
| 357 |
+
<div id="recovery-bar" class="bg-gradient-to-r from-green-500 to-green-400 h-4 rounded-full transition-all duration-500" style="width: 74%"></div>
|
| 358 |
</div>
|
| 359 |
<div class="flex justify-between text-xs opacity-75">
|
| 360 |
<span data-translate="poor">แย่</span>
|
|
|
|
| 365 |
</div>
|
| 366 |
</div>
|
| 367 |
|
| 368 |
+
<!-- Waterfall Chart -->
|
| 369 |
+
<div class="card mb-4">
|
| 370 |
+
<h3 class="font-semibold mb-3">Why is my recovery <span id="recovery-waterfall-value">74</span>?</h3>
|
| 371 |
+
<canvas id="waterfall-chart" height="200"></canvas>
|
| 372 |
+
<div class="mt-2 text-xs opacity-75">
|
| 373 |
+
Click behaviors to see detailed breakdown
|
| 374 |
+
</div>
|
| 375 |
+
</div>
|
| 376 |
+
<!-- HRV Chart -->
|
| 377 |
<div class="card mb-4">
|
| 378 |
<h3 class="font-semibold mb-3">HRV (7 วัน)</h3>
|
| 379 |
<canvas id="hrv-chart" height="150"></canvas>
|
|
|
|
| 415 |
<h2 class="text-2xl font-bold mb-1" data-translate="sleep">การนอนหลับ</h2>
|
| 416 |
<p class="text-sm opacity-75" data-translate="sleep-subtitle">วิเคราะห์คุณภาพการนอนหลับของคุณ</p>
|
| 417 |
</div>
|
| 418 |
+
<!-- Enhanced Sleep Coach Card -->
|
|
|
|
| 419 |
<div class="card mb-4">
|
| 420 |
<h3 class="font-semibold mb-3 flex items-center gap-2">
|
| 421 |
<i data-feather="moon" class="w-4 h-4 text-blue-400"></i>
|
|
|
|
| 425 |
<div class="text-center p-3 rounded-lg bg-blue-500/20">
|
| 426 |
<i data-feather="log-in" class="w-5 h-5 mx-auto mb-1 text-blue-400"></i>
|
| 427 |
<div class="text-xs opacity-75 mb-1" data-translate="bedtime">เวลาเข้านอน</div>
|
| 428 |
+
<div class="font-semibold" id="sleep-coach-bedtime">22:45</div>
|
| 429 |
</div>
|
| 430 |
<div class="text-center p-3 rounded-lg bg-yellow-500/20">
|
| 431 |
<i data-feather="log-out" class="w-5 h-5 mx-auto mb-1 text-yellow-400"></i>
|
| 432 |
<div class="text-xs opacity-75 mb-1" data-translate="wake-time">ตื่นนอน</div>
|
| 433 |
+
<div class="font-semibold" id="sleep-coach-waketime">06:30</div>
|
| 434 |
</div>
|
| 435 |
</div>
|
| 436 |
<div class="mt-3 p-3 rounded-lg bg-green-500/20">
|
| 437 |
<div class="flex items-center gap-2 text-green-400">
|
| 438 |
<i data-feather="target" class="w-4 h-4"></i>
|
| 439 |
+
<span class="text-sm">เป้าหมาย: <span id="sleep-coach-need">7.5</span> ชั่วโมง</span>
|
| 440 |
</div>
|
| 441 |
</div>
|
| 442 |
+
<div class="mt-3 p-3 rounded-lg bg-gray-700/30" id="sleep-rationale">
|
| 443 |
+
<!-- Rationale will be populated here -->
|
| 444 |
+
</div>
|
| 445 |
</div>
|
| 446 |
+
<!-- Sleep Stages -->
|
|
|
|
| 447 |
<div class="card mb-4">
|
| 448 |
<h3 class="font-semibold mb-3" data-translate="sleep-stages">ระยะการนอนหลับ</h3>
|
| 449 |
<canvas id="sleep-stages-chart" height="150"></canvas>
|
|
|
|
| 480 |
<h2 class="text-2xl font-bold mb-1" data-translate="training">การฝึก</h2>
|
| 481 |
<p class="text-sm opacity-75" data-translate="training-subtitle">บันทึกและติดตามการออกกำลังกาย</p>
|
| 482 |
</div>
|
| 483 |
+
<!-- Enhanced Training with Target Display -->
|
| 484 |
+
<div class="card mb-4 bg-gradient-to-r from-orange-500/10 to-red-500/10">
|
| 485 |
+
<div class="flex justify-between items-center">
|
| 486 |
+
<div>
|
| 487 |
+
<h3 class="font-semibold">Today's Target</h3>
|
| 488 |
+
<div class="text-2xl font-bold text-orange-400" id="strain-target-display">Target: 15</div>
|
| 489 |
+
</div>
|
| 490 |
+
<div id="guard-status" class="text-sm">
|
| 491 |
+
<!-- Guard status will be populated here -->
|
| 492 |
+
</div>
|
| 493 |
+
</div>
|
| 494 |
+
</div>
|
| 495 |
|
|
|
|
| 496 |
<button onclick="startWorkout()" class="w-full card bg-gradient-to-r from-red-500 to-orange-500 text-white font-semibold py-4 flex items-center justify-center gap-2 hover:opacity-90 transition mb-4">
|
| 497 |
<i data-feather="play-circle" class="w-6 h-6"></i>
|
| 498 |
<span data-translate="start-workout">เริ่ม Live Workout</span>
|
| 499 |
</button>
|
| 500 |
+
<!-- Sport Selection -->
|
|
|
|
| 501 |
<div class="card mb-4">
|
| 502 |
<h3 class="font-semibold mb-3" data-translate="select-sport">เลือกชนิดกีฬา</h3>
|
| 503 |
<div class="grid grid-cols-4 gap-2">
|
|
|
|
| 667 |
<h4 class="font-semibold mb-2" data-translate="nutrition">โภชนาการ</h4>
|
| 668 |
<div class="grid grid-cols-2 gap-2">
|
| 669 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 670 |
+
<input type="checkbox" class="form-checkbox text-primary" data-behaviour="caffeine_late">
|
| 671 |
+
<span class="text-sm">คาเฟอีนหลังบ่าย</span>
|
| 672 |
</label>
|
| 673 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 674 |
+
<input type="checkbox" class="form-checkbox text-primary" data-behaviour="alcohol_low">
|
| 675 |
+
<span class="text-sm">แอลกอฮอล์ (น้อย)</span>
|
| 676 |
</label>
|
| 677 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 678 |
+
<input type="checkbox" class="form-checkbox text-primary" data-behaviour="alcohol_high">
|
| 679 |
+
<span class="text-sm">แอลกอฮอล์ (มาก)</span>
|
| 680 |
</label>
|
| 681 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 682 |
+
<input type="checkbox" class="form-checkbox text-primary" data-behaviour="late_meal">
|
| 683 |
+
<span class="text-sm">อาหารมื้อดึก</span>
|
| 684 |
</label>
|
| 685 |
</div>
|
| 686 |
</div>
|
|
|
|
| 688 |
<h4 class="font-semibold mb-2" data-translate="lifestyle">ไลฟ์สไตล์</h4>
|
| 689 |
<div class="grid grid-cols-2 gap-2">
|
| 690 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 691 |
+
<input type="checkbox" class="form-checkbox text-primary" data-behaviour="travel_jetlag">
|
| 692 |
+
<span class="text-sm">เดินทาง/Jet Lag</span>
|
| 693 |
</label>
|
| 694 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 695 |
+
<input type="checkbox" class="form-checkbox text-primary" data-behaviour="illness">
|
| 696 |
+
<span class="text-sm">ป่วย/ไข้/เจ็บคอ</span>
|
| 697 |
</label>
|
| 698 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 699 |
+
<input type="checkbox" class="form-checkbox text-primary" data-behaviour="stress_high">
|
| 700 |
+
<span class="text-sm">เครียดสูง</span>
|
| 701 |
</label>
|
| 702 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 703 |
+
<input type="checkbox" class="form-checkbox text-primary" data-behaviour="meditation">
|
| 704 |
+
<span class="text-sm">นั่งสมาธิ</span>
|
| 705 |
</label>
|
| 706 |
</div>
|
| 707 |
</div>
|
|
|
|
| 709 |
<h4 class="font-semibold mb-2" data-translate="recovery-tools">เครื่องมือฟื้นตัว</h4>
|
| 710 |
<div class="grid grid-cols-2 gap-2">
|
| 711 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 712 |
+
<input type="checkbox" class="form-checkbox text-primary" data-behaviour="sauna">
|
| 713 |
+
<span class="text-sm">ซาวน่า</span>
|
| 714 |
</label>
|
| 715 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 716 |
+
<input type="checkbox" class="form-checkbox text-primary" data-behaviour="cold_plunge">
|
| 717 |
+
<span class="text-sm">อาบน้ำเย็น</span>
|
| 718 |
</label>
|
| 719 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 720 |
+
<input type="checkbox" class="form-checkbox text-primary" data-behaviour="stretching">
|
| 721 |
+
<span class="text-sm">ยืดเส้นยืดสาย</span>
|
| 722 |
</label>
|
| 723 |
<label class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-700/30 cursor-pointer">
|
| 724 |
+
<input type="checkbox" class="form-checkbox text-primary" data-behaviour="strength_training">
|
| 725 |
+
<span class="text-sm">การฝึกแรง</span>
|
| 726 |
</label>
|
| 727 |
</div>
|
| 728 |
</div>
|
| 729 |
+
<div>
|
| 730 |
<label class="block">
|
| 731 |
<span class="font-semibold mb-2 block" data-translate="mood">อารมณ์</span>
|
| 732 |
<select class="w-full p-2 rounded-lg bg-gray-700/50 border border-gray-600">
|
|
|
|
| 744 |
</div>
|
| 745 |
</div>
|
| 746 |
</div>
|
| 747 |
+
<!-- Enhanced Settings Modal -->
|
|
|
|
| 748 |
<div id="settings-modal" class="modal">
|
| 749 |
+
<div class="modal-content slide-up max-h-[90vh] overflow-y-auto">
|
| 750 |
<div class="p-6">
|
| 751 |
<div class="flex justify-between items-center mb-4">
|
| 752 |
<h3 class="text-xl font-bold" data-translate="settings">การตั้งค่า</h3>
|
|
|
|
| 754 |
<i data-feather="x" class="w-5 h-5"></i>
|
| 755 |
</button>
|
| 756 |
</div>
|
| 757 |
+
<div class="space-y-6">
|
| 758 |
+
<!-- Model Sensitivity -->
|
| 759 |
+
<div>
|
| 760 |
+
<h4 class="font-semibold mb-3">Model Sensitivity</h4>
|
| 761 |
+
<div class="space-y-3">
|
| 762 |
+
<div>
|
| 763 |
+
<label class="block text-sm mb-1">Sensitivity (0.5-2.0)</label>
|
| 764 |
+
<div class="flex items-center gap-2">
|
| 765 |
+
<input type="range" id="sensitivity-slider" min="0.5" max="2.0" step="0.1" value="1.0"
|
| 766 |
+
class="flex-1" oninput="document.getElementById('sensitivity-value').textContent = this.value">
|
| 767 |
+
<span id="sensitivity-value" class="w-12 text-right">1.0</span>
|
| 768 |
+
</div>
|
| 769 |
+
<div class="text-xs opacity-75">Lower = more sensitive to changes</div>
|
| 770 |
+
</div>
|
| 771 |
+
<div>
|
| 772 |
+
<label class="block text-sm mb-1">Aggressiveness (0.5-1.5)</label>
|
| 773 |
+
<div class="flex items-center gap-2">
|
| 774 |
+
<input type="range" id="aggressiveness-slider" min="0.5" max="1.5" step="0.05" value="0.9"
|
| 775 |
+
class="flex-1" oninput="document.getElementById('aggressiveness-value').textContent = this.value">
|
| 776 |
+
<span id="aggressiveness-value" class="w-12 text-right">0.90</span>
|
| 777 |
+
</div>
|
| 778 |
+
<div class="text-xs opacity-75">Higher = stronger penalties/bonuses</div>
|
| 779 |
+
</div>
|
| 780 |
+
</div>
|
| 781 |
+
</div>
|
| 782 |
+
|
| 783 |
+
<!-- Recovery Weights -->
|
| 784 |
<div>
|
| 785 |
+
<h4 class="font-semibold mb-3">Recovery Weights</h4>
|
| 786 |
+
<div class="grid grid-cols-2 gap-2 text-sm">
|
| 787 |
+
<div>
|
| 788 |
+
<label class="block mb-1">HRV</label>
|
| 789 |
+
<input type="number" id="weight-w_hrv" step="0.01" min="0" max="1" class="w-full p-1 rounded bg-gray-700/50 border border-gray-600">
|
| 790 |
+
</div>
|
| 791 |
+
<div>
|
| 792 |
+
<label class="block mb-1">RHR</label>
|
| 793 |
+
<input type="number" id="weight-w_rhr" step="0.01" min="0" max="1" class="w-full p-1 rounded bg-gray-700/50 border border-gray-600">
|
| 794 |
+
</div>
|
| 795 |
+
<div>
|
| 796 |
+
<label class="block mb-1">Sleep</label>
|
| 797 |
+
<input type="number" id="weight-w_sleep" step="0.01" min="0" max="1" class="w-full p-1 rounded bg-gray-700/50 border border-gray-600">
|
| 798 |
+
</div>
|
| 799 |
+
<div>
|
| 800 |
+
<label class="block mb-1">Respiration</label>
|
| 801 |
+
<input type="number" id="weight-w_resp" step="0.01" min="0" max="1" class="w-full p-1 rounded bg-gray-700/50 border border-gray-600">
|
| 802 |
+
</div>
|
| 803 |
+
<div>
|
| 804 |
+
<label class="block mb-1">Skin Temp</label>
|
| 805 |
+
<input type="number" id="weight-w_skin" step="0.01" min="0" max="1" class="w-full p-1 rounded bg-gray-700/50 border border-gray-600">
|
| 806 |
+
</div>
|
| 807 |
+
<div>
|
| 808 |
+
<label class="block mb-1">Strain Load</label>
|
| 809 |
+
<input type="number" id="weight-w_strainLoad" step="0.01" min="0" max="1" class="w-full p-1 rounded bg-gray-700/50 border border-gray-600">
|
| 810 |
+
</div>
|
| 811 |
+
<div>
|
| 812 |
+
<label class="block mb-1">Illness</label>
|
| 813 |
+
<input type="number" id="weight-w_ill" step="0.01" min="0" max="1" class="w-full p-1 rounded bg-gray-700/50 border border-gray-600">
|
| 814 |
+
</div>
|
| 815 |
+
</div>
|
| 816 |
+
</div>
|
| 817 |
+
|
| 818 |
+
<!-- Chronotype -->
|
| 819 |
+
<div>
|
| 820 |
+
<h4 class="font-semibold mb-3">Chronotype</h4>
|
| 821 |
+
<select id="chronotype-select" class="w-full p-2 rounded-lg bg-gray-700/50 border border-gray-600">
|
| 822 |
+
<option value="none">Neutral</option>
|
| 823 |
+
<option value="early_bird">Early Bird (05:30)</option>
|
| 824 |
+
<option value="morning">Morning (06:00)</option>
|
| 825 |
+
<option value="neutral">Neutral (06:30)</option>
|
| 826 |
+
<option value="evening">Evening (07:00)</option>
|
| 827 |
+
<option value="night_owl">Night Owl (08:00)</option>
|
| 828 |
</select>
|
| 829 |
</div>
|
| 830 |
+
|
| 831 |
+
<!-- Calibration -->
|
| 832 |
<div>
|
| 833 |
+
<h4 class="font-semibold mb-3">Calibration</h4>
|
| 834 |
+
<div class="space-y-2">
|
| 835 |
+
<div>
|
| 836 |
+
<label class="block text-sm mb-1">Generate Historical Data</label>
|
| 837 |
+
<div class="flex gap-2">
|
| 838 |
+
<select id="calibrate-days" class="flex-1 p-2 rounded-lg bg-gray-700/50 border border-gray-600">
|
| 839 |
+
<option value="14">14 days</option>
|
| 840 |
+
<option value="30">30 days</option>
|
| 841 |
+
<option value="60">60 days</option>
|
| 842 |
+
<option value="90">90 days</option>
|
| 843 |
+
</select>
|
| 844 |
+
<button onclick="calibrateBaselines()" class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition">
|
| 845 |
+
Calibrate
|
| 846 |
+
</button>
|
| 847 |
+
</div>
|
| 848 |
+
</div>
|
| 849 |
+
</div>
|
| 850 |
</div>
|
| 851 |
+
|
| 852 |
+
<!-- Data Management -->
|
| 853 |
<div>
|
| 854 |
+
<h4 class="font-semibold mb-3">Data Management</h4>
|
| 855 |
+
<div class="space-y-2">
|
| 856 |
+
<button onclick="exportData()" class="w-full p-2 rounded-lg bg-gray-700/50 border border-gray-600 text-left hover:bg-gray-700/70 transition">
|
| 857 |
+
<i data-feather="download" class="inline w-4 h-4 mr-2"></i>
|
| 858 |
+
Export Data
|
| 859 |
+
</button>
|
| 860 |
+
<button onclick="resetData()" class="w-full p-2 rounded-lg bg-red-500/20 border border-red-500 text-red-400 hover:bg-red-500/30 transition">
|
| 861 |
+
<i data-feather="trash-2" class="inline w-4 h-4 mr-2"></i>
|
| 862 |
+
Reset All Data
|
| 863 |
+
</button>
|
| 864 |
+
</div>
|
| 865 |
</div>
|
| 866 |
+
|
| 867 |
+
<!-- Save Button -->
|
| 868 |
+
<button onclick="saveSettings()" class="w-full bg-primary text-white font-semibold py-3 rounded-lg hover:bg-primary-dark transition">
|
| 869 |
+
Save Settings
|
| 870 |
+
</button>
|
| 871 |
</div>
|
| 872 |
</div>
|
| 873 |
</div>
|
| 874 |
</div>
|
| 875 |
+
<script>
|
| 876 |
+
// Enhanced Data Structure with Behavior System
|
| 877 |
+
const appState = {
|
|
|
|
| 878 |
profile: {
|
| 879 |
name: "Demo User",
|
| 880 |
age: 32,
|
| 881 |
+
gender: "unspecified",
|
| 882 |
+
chronotype: "none"
|
| 883 |
+
},
|
| 884 |
+
settings: {
|
| 885 |
+
sensitivity: 1.0,
|
| 886 |
+
aggressiveness: 0.9,
|
| 887 |
+
weights: {
|
| 888 |
+
w_hrv: 0.28,
|
| 889 |
+
w_rhr: 0.22,
|
| 890 |
+
w_sleep: 0.22,
|
| 891 |
+
w_resp: 0.06,
|
| 892 |
+
w_skin: 0.06,
|
| 893 |
+
w_strainLoad: 0.10,
|
| 894 |
+
w_ill: 0.06
|
| 895 |
+
},
|
| 896 |
+
clipZ: 3.5,
|
| 897 |
+
enableChronotype: false
|
| 898 |
},
|
| 899 |
today: {
|
| 900 |
date: "2025-11-11",
|
| 901 |
+
strain: 12.8,
|
| 902 |
+
hrv_ms: 82,
|
| 903 |
+
rhr_bpm: 53,
|
| 904 |
+
resp_rate: 14.4,
|
| 905 |
+
skin_temp_delta: 0.0,
|
| 906 |
+
sleep_actual_h: 7.1,
|
| 907 |
+
sleep_efficiency_pct: 92,
|
| 908 |
+
sleep_need_h: 7.5,
|
| 909 |
+
ill: false,
|
| 910 |
+
recovery: 0,
|
| 911 |
+
strain_target: 21
|
| 912 |
+
},
|
| 913 |
+
carryOver: {
|
| 914 |
+
effects: []
|
| 915 |
+
},
|
| 916 |
+
journal_logs: [],
|
| 917 |
+
history: {
|
| 918 |
+
days: generateRealisticHistory(90)
|
| 919 |
+
},
|
| 920 |
+
behaviour_catalog: getBehaviourCatalog(),
|
| 921 |
+
contributions: {
|
| 922 |
+
hrv: 0,
|
| 923 |
+
rhr: 0,
|
| 924 |
+
sleep: 0,
|
| 925 |
+
resp: 0,
|
| 926 |
+
skin: 0,
|
| 927 |
+
strainLoad: 0,
|
| 928 |
+
ill: 0,
|
| 929 |
+
behaviours: 0,
|
| 930 |
+
residual: 0
|
| 931 |
},
|
| 932 |
sleep: {
|
| 933 |
+
coach: {
|
| 934 |
+
recommended_bedtime: "22:45",
|
| 935 |
+
recommended_waketime: "06:30",
|
| 936 |
+
rationale: []
|
| 937 |
+
},
|
| 938 |
stages: [
|
| 939 |
{t: "22:50", stage: "Light", duration: 50},
|
| 940 |
{t: "23:40", stage: "Deep", duration: 90},
|
|
|
|
| 944 |
{t: "05:40", stage: "REM", duration: 50},
|
| 945 |
{t: "06:30", stage: "Awake", duration: 0}
|
| 946 |
],
|
| 947 |
+
debt_14d: 0
|
| 948 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 949 |
};
|
| 950 |
|
| 951 |
+
// Behaviour Catalog with Effects
|
| 952 |
+
function getBehaviourCatalog() {
|
| 953 |
+
return [
|
| 954 |
+
{
|
| 955 |
+
id: "caffeine_late",
|
| 956 |
+
label_th: "คาเฟอีนหลังบ่าย",
|
| 957 |
+
category: "Stimulants",
|
| 958 |
+
effect: [
|
| 959 |
+
{target: "sleep_need_h", type: "add", value: 0.4, per_intensity: true, half_life_days: 1},
|
| 960 |
+
{target: "sleep_efficiency_pct", type: "mul", value: 0.96, per_intensity: true, half_life_days: 1},
|
| 961 |
+
{target: "hrv_ms", type: "mul", value: 0.98, per_intensity: true, half_life_days: 1}
|
| 962 |
+
],
|
| 963 |
+
applies: {time_window: "today_evening", sex: "any"}
|
| 964 |
+
},
|
| 965 |
+
{
|
| 966 |
+
id: "alcohol_low",
|
| 967 |
+
label_th: "แอลกอฮอล์ (น้อย)",
|
| 968 |
+
category: "Substances",
|
| 969 |
+
effect: [
|
| 970 |
+
{target: "sleep_need_h", type: "add", value: 0.2, per_intensity: true, half_life_days: 1},
|
| 971 |
+
{target: "sleep_efficiency_pct", type: "mul", value: 0.98, per_intensity: true, half_life_days: 1},
|
| 972 |
+
{target: "hrv_ms", type: "mul", value: 0.96, per_intensity: true, half_life_days: 2},
|
| 973 |
+
{target: "rhr_bpm", type: "add", value: 2, per_intensity: true, half_life_days: 1}
|
| 974 |
+
],
|
| 975 |
+
applies: {time_window: "today_evening", sex: "any"}
|
| 976 |
+
},
|
| 977 |
+
{
|
| 978 |
+
id: "alcohol_high",
|
| 979 |
+
label_th: "แอลกอฮอล์ (มาก)",
|
| 980 |
+
category: "Substances",
|
| 981 |
+
effect: [
|
| 982 |
+
{target: "sleep_need_h", type: "add", value: 0.8, per_intensity: true, half_life_days: 2},
|
| 983 |
+
{target: "sleep_efficiency_pct", type: "mul", value: 0.92, per_intensity: true, half_life_days: 2},
|
| 984 |
+
{target: "hrv_ms", type: "mul", value: 0.90, per_intensity: true, half_life_days: 3},
|
| 985 |
+
{target: "rhr_bpm", type: "add", value: 5, per_intensity: true, half_life_days: 2}
|
| 986 |
+
],
|
| 987 |
+
applies: {time_window: "today_evening", sex: "any"}
|
| 988 |
+
},
|
| 989 |
+
{
|
| 990 |
+
id: "late_meal",
|
| 991 |
+
label_th: "อาหารมื้อดึก",
|
| 992 |
+
category: "Nutrition",
|
| 993 |
+
effect: [
|
| 994 |
+
{target: "sleep_efficiency_pct", type: "mul", value: 0.97, per_intensity: true, half_life_days: 1}
|
| 995 |
+
],
|
| 996 |
+
applies: {time_window: "today_evening", sex: "any"}
|
| 997 |
+
},
|
| 998 |
+
{
|
| 999 |
+
id: "sauna",
|
| 1000 |
+
label_th: "ซาวน่า",
|
| 1001 |
+
category: "Recovery",
|
| 1002 |
+
effect: [
|
| 1003 |
+
{target: "hrv_ms", type: "mul", value: 1.03, per_intensity: true, half_life_days: 0.5},
|
| 1004 |
+
{target: "rhr_bpm", type: "add", value: -1, per_intensity: true, half_life_days: 0.5}
|
| 1005 |
+
],
|
| 1006 |
+
applies: {time_window: "today_any", sex: "any"}
|
| 1007 |
+
},
|
| 1008 |
+
{
|
| 1009 |
+
id: "cold_plunge",
|
| 1010 |
+
label_th: "อาบน้ำเย็น",
|
| 1011 |
+
category: "Recovery",
|
| 1012 |
+
effect: [
|
| 1013 |
+
{target: "hrv_ms", type: "mul", value: 1.02, per_intensity: true, half_life_days: 0.5},
|
| 1014 |
+
{target: "rhr_bpm", type: "add", value: -0.5, per_intensity: true, half_life_days: 0.5}
|
| 1015 |
+
],
|
| 1016 |
+
applies: {time_window: "today_any", sex: "any"}
|
| 1017 |
+
},
|
| 1018 |
+
{
|
| 1019 |
+
id: "travel_jetlag",
|
| 1020 |
+
label_th: "เดินทาง/Jet Lag",
|
| 1021 |
+
category: "Stress",
|
| 1022 |
+
effect: [
|
| 1023 |
+
{target: "sleep_need_h", type: "add", value: 0.6, per_intensity: true, half_life_days: 3},
|
| 1024 |
+
{target: "resp_rate", type: "add", value: 1.5, per_intensity: true, half_life_days: 2},
|
| 1025 |
+
{target: "skin_temp_delta", type: "add", value: 0.2, per_intensity: true, half_life_days: 2}
|
| 1026 |
+
],
|
| 1027 |
+
applies: {time_window: "today_any", sex: "any"}
|
| 1028 |
+
},
|
| 1029 |
+
{
|
| 1030 |
+
id: "illness",
|
| 1031 |
+
label_th: "ป่วย/ไข้/เจ็บคอ",
|
| 1032 |
+
category: "Health",
|
| 1033 |
+
effect: [
|
| 1034 |
+
{target: "rhr_bpm", type: "add", value: 8, per_intensity: true, half_life_days: 3},
|
| 1035 |
+
{target: "hrv_ms", type: "mul", value: 0.85, per_intensity: true, half_life_days: 4},
|
| 1036 |
+
{target: "resp_rate", type: "add", value: 3, per_intensity: true, half_life_days: 3},
|
| 1037 |
+
{target: "skin_temp_delta", type: "add", value: 0.3, per_intensity: true, half_life_days: 2}
|
| 1038 |
+
],
|
| 1039 |
+
applies: {time_window: "today_any", sex: "any"}
|
| 1040 |
+
},
|
| 1041 |
+
{
|
| 1042 |
+
id: "strength_training",
|
| 1043 |
+
label_th: "การฝึกแรง",
|
| 1044 |
+
category: "Training",
|
| 1045 |
+
effect: [
|
| 1046 |
+
{target: "sleep_need_h", type: "add", value: 0.3, per_intensity: true, half_life_days: 1}
|
| 1047 |
+
],
|
| 1048 |
+
applies: {time_window: "today_any", sex: "any"}
|
| 1049 |
+
},
|
| 1050 |
+
{
|
| 1051 |
+
id: "hiit",
|
| 1052 |
+
label_th: "HIIT",
|
| 1053 |
+
category: "Training",
|
| 1054 |
+
effect: [
|
| 1055 |
+
{target: "sleep_need_h", type: "add", value: 0.4, per_intensity: true, half_life_days: 1}
|
| 1056 |
+
],
|
| 1057 |
+
applies: {time_window: "today_any", sex: "any"}
|
| 1058 |
+
},
|
| 1059 |
+
{
|
| 1060 |
+
id: "meditation",
|
| 1061 |
+
label_th: "นั่งสมาธิ",
|
| 1062 |
+
category: "Recovery",
|
| 1063 |
+
effect: [
|
| 1064 |
+
{target: "hrv_ms", type: "mul", value: 1.01, per_intensity: true, half_life_days: 1}
|
| 1065 |
+
],
|
| 1066 |
+
applies: {time_window: "today_any", sex: "any"}
|
| 1067 |
+
},
|
| 1068 |
+
{
|
| 1069 |
+
id: "stretching",
|
| 1070 |
+
label_th: "ยืดเส้นยืดสาย",
|
| 1071 |
+
category: "Recovery",
|
| 1072 |
+
effect: [
|
| 1073 |
+
{target: "hrv_ms", type: "mul", value: 1.005, per_intensity: true, half_life_days: 0.5}
|
| 1074 |
+
],
|
| 1075 |
+
applies: {time_window: "today_any", sex: "any"}
|
| 1076 |
+
},
|
| 1077 |
+
{
|
| 1078 |
+
id: "stress_high",
|
| 1079 |
+
label_th: "เครียดสูง",
|
| 1080 |
+
category: "Stress",
|
| 1081 |
+
effect: [
|
| 1082 |
+
{target: "rhr_bpm", type: "add", value: 3, per_intensity: true, half_life_days: 1},
|
| 1083 |
+
{target: "hrv_ms", type: "mul", value: 0.95, per_intensity: true, half_life_days: 1}
|
| 1084 |
+
],
|
| 1085 |
+
applies: {time_window: "today_any", sex: "any"}
|
| 1086 |
+
}
|
| 1087 |
+
];
|
| 1088 |
+
}
|
| 1089 |
+
|
| 1090 |
+
// Generate realistic historical data with correlations
|
| 1091 |
+
function generateRealisticHistory(days) {
|
| 1092 |
+
const history = [];
|
| 1093 |
+
let baseHRV = 75;
|
| 1094 |
+
let baseRHR = 55;
|
| 1095 |
+
let baseResp = 14;
|
| 1096 |
+
let baseSkinTemp = 0;
|
| 1097 |
+
let strainAccumulator = 0;
|
| 1098 |
+
|
| 1099 |
+
for (let i = days - 1; i >= 0; i--) {
|
| 1100 |
+
const date = new Date();
|
| 1101 |
+
date.setDate(date.getDate() - i);
|
| 1102 |
+
|
| 1103 |
+
// Simulate correlations
|
| 1104 |
+
const strainVariation = (Math.random() - 0.5) * 10;
|
| 1105 |
+
strainAccumulator = Math.max(0, strainAccumulator + strainVariation - 5);
|
| 1106 |
+
const strain = Math.max(0, 5 + strainAccumulator + Math.random() * 8);
|
| 1107 |
+
|
| 1108 |
+
// HRV inversely correlated with strain and RHR
|
| 1109 |
+
const hrv = Math.max(20, Math.min(180,
|
| 1110 |
+
baseHRV - strain * 1.5 + (Math.random() - 0.5) * 15));
|
| 1111 |
+
|
| 1112 |
+
// RHR positively correlated with strain
|
| 1113 |
+
const rhr = Math.max(35, Math.min(120,
|
| 1114 |
+
baseRHR + strain * 0.8 + (Math.random() - 0.5) * 8));
|
| 1115 |
+
|
| 1116 |
+
// Recovery inversely correlated with yesterday's strain
|
| 1117 |
+
const recovery = Math.max(0, Math.min(100,
|
| 1118 |
+
85 - strain * 2 + (Math.random() - 0.5) * 10));
|
| 1119 |
+
|
| 1120 |
+
// Sleep needs increase with strain
|
| 1121 |
+
const sleepNeed = Math.max(6, 7.5 + strain * 0.1);
|
| 1122 |
+
const sleepActual = sleepNeed + (Math.random() - 0.5) * 2;
|
| 1123 |
+
const sleepEff = Math.max(60, Math.min(100, 90 + (Math.random() - 0.5) * 15));
|
| 1124 |
+
|
| 1125 |
+
history.push({
|
| 1126 |
+
date: date.toISOString().split('T')[0],
|
| 1127 |
+
hrv_ms: hrv,
|
| 1128 |
+
rhr_bpm: rhr,
|
| 1129 |
+
strain: strain,
|
| 1130 |
+
sleep_actual_h: sleepActual,
|
| 1131 |
+
sleep_need_h: sleepNeed,
|
| 1132 |
+
sleep_efficiency_pct: sleepEff,
|
| 1133 |
+
resp_rate: baseResp + (Math.random() - 0.5) * 3,
|
| 1134 |
+
skin_temp_delta: baseSkinTemp + (Math.random() - 0.5) * 0.3,
|
| 1135 |
+
recovery: recovery
|
| 1136 |
+
});
|
| 1137 |
+
|
| 1138 |
+
// Update baselines slightly over time
|
| 1139 |
+
baseHRV += (Math.random() - 0.5) * 2;
|
| 1140 |
+
baseRHR += (Math.random() - 0.5) * 1;
|
| 1141 |
+
}
|
| 1142 |
+
|
| 1143 |
+
return history;
|
| 1144 |
+
}
|
| 1145 |
+
|
| 1146 |
+
// Baseline & Z-score Computation
|
| 1147 |
+
function computeBaselines(history, windowShort = 21, windowLong = 60) {
|
| 1148 |
+
const recent = history.slice(-windowLong);
|
| 1149 |
+
const short = history.slice(-windowShort);
|
| 1150 |
+
|
| 1151 |
+
if (recent.length === 0) {
|
| 1152 |
+
return {
|
| 1153 |
+
baselineShort: {},
|
| 1154 |
+
baselineLong: {},
|
| 1155 |
+
zScores: {},
|
| 1156 |
+
bands: {}
|
| 1157 |
+
};
|
| 1158 |
+
}
|
| 1159 |
+
|
| 1160 |
+
const metrics = ['hrv_ms', 'rhr_bpm', 'sleep_actual_h', 'resp_rate', 'skin_temp_delta', 'strain'];
|
| 1161 |
+
const result = {
|
| 1162 |
+
baselineShort: {},
|
| 1163 |
+
baselineLong: {},
|
| 1164 |
+
zScores: {},
|
| 1165 |
+
bands: {}
|
| 1166 |
+
};
|
| 1167 |
+
|
| 1168 |
+
metrics.forEach(metric => {
|
| 1169 |
+
// Compute EWMA for long window
|
| 1170 |
+
const alphaLong = 2 / (windowLong + 1);
|
| 1171 |
+
let ewmaLong = recent[0][metric];
|
| 1172 |
+
for (let i = 1; i < recent.length; i++) {
|
| 1173 |
+
ewmaLong = alphaLong * recent[i][metric] + (1 - alphaLong) * ewmaLong;
|
| 1174 |
+
}
|
| 1175 |
+
|
| 1176 |
+
// Compute EWMA for short window
|
| 1177 |
+
const alphaShort = 2 / (windowShort + 1);
|
| 1178 |
+
let ewmaShort = short[0][metric];
|
| 1179 |
+
for (let i = 1; i < short.length; i++) {
|
| 1180 |
+
ewmaShort = alphaShort * short[i][metric] + (1 - alphaShort) * ewmaShort;
|
| 1181 |
+
}
|
| 1182 |
+
|
| 1183 |
+
// Compute stats
|
| 1184 |
+
const values = recent.map(d => d[metric]);
|
| 1185 |
+
const mean = values.reduce((a, b) => a + b, 0) / values.length;
|
| 1186 |
+
const variance = values.reduce((a, b) => a + Math.pow(b - mean, 2), 0) / values.length;
|
| 1187 |
+
const std = Math.sqrt(variance) || 1e-6;
|
| 1188 |
+
|
| 1189 |
+
result.baselineShort[metric] = ewmaShort;
|
| 1190 |
+
result.baselineLong[metric] = mean;
|
| 1191 |
+
result.baselineLong[metric + '_std'] = std;
|
| 1192 |
+
result.bands[metric] = {
|
| 1193 |
+
lower: mean - std * 1.96,
|
| 1194 |
+
upper: mean + std * 1.96
|
| 1195 |
+
};
|
| 1196 |
+
});
|
| 1197 |
+
|
| 1198 |
+
return result;
|
| 1199 |
+
}
|
| 1200 |
+
|
| 1201 |
+
// Apply behavior effects with carry-over
|
| 1202 |
+
function applyBehaviorEffects(journalActive, metricsToday) {
|
| 1203 |
+
const adjusted = {...metricsToday};
|
| 1204 |
+
const contributions = [];
|
| 1205 |
+
|
| 1206 |
+
journalActive.forEach(entry => {
|
| 1207 |
+
const behaviour = appState.behaviour_catalog.find(b => b.id === entry.id);
|
| 1208 |
+
if (!behaviour) return;
|
| 1209 |
+
|
| 1210 |
+
behaviour.effect.forEach(effect => {
|
| 1211 |
+
let value = effect.value;
|
| 1212 |
+
if (effect.per_intensity) {
|
| 1213 |
+
value *= (entry.intensity || 1);
|
| 1214 |
+
}
|
| 1215 |
+
|
| 1216 |
+
const oldValue = adjusted[effect.target];
|
| 1217 |
+
|
| 1218 |
+
if (effect.type === 'add') {
|
| 1219 |
+
adjusted[effect.target] = (adjusted[effect.target] || 0) + value;
|
| 1220 |
+
} else if (effect.type === 'mul') {
|
| 1221 |
+
adjusted[effect.target] = (adjusted[effect.target] || 1) * value;
|
| 1222 |
+
}
|
| 1223 |
+
|
| 1224 |
+
// Store for waterfall
|
| 1225 |
+
contributions.push({
|
| 1226 |
+
behaviour: behaviour.label_th,
|
| 1227 |
+
target: effect.target,
|
| 1228 |
+
delta: adjusted[effect.target] - oldValue,
|
| 1229 |
+
half_life: effect.half_life_days
|
| 1230 |
+
});
|
| 1231 |
+
});
|
| 1232 |
+
});
|
| 1233 |
+
|
| 1234 |
+
return { adjusted, contributions };
|
| 1235 |
+
}
|
| 1236 |
+
|
| 1237 |
+
// Decay carry-over effects
|
| 1238 |
+
function decayBehaviorEffects(carryOver) {
|
| 1239 |
+
const today = new Date().toISOString().split('T')[0];
|
| 1240 |
+
const decayed = carryOver.effects.filter(effect => {
|
| 1241 |
+
const daysDiff = Math.floor((new Date(today) - new Date(effect.applied_date)) / (1000 * 60 * 60 * 24));
|
| 1242 |
+
if (daysDiff > effect.half_life_days * 3) return false; // Remove after 3 half-lives
|
| 1243 |
+
|
| 1244 |
+
const decayFactor = Math.pow(0.5, daysDiff / effect.half_life_days);
|
| 1245 |
+
effect.value *= decayFactor;
|
| 1246 |
+
return true;
|
| 1247 |
+
});
|
| 1248 |
+
|
| 1249 |
+
return { effects: decayed };
|
| 1250 |
+
}
|
| 1251 |
+
|
| 1252 |
+
// Recovery Score Calculation
|
| 1253 |
+
function computeRecoveryScore(state, baselines, zScores) {
|
| 1254 |
+
const w = appState.settings.weights;
|
| 1255 |
+
const settings = appState.settings;
|
| 1256 |
+
|
| 1257 |
+
// Helper functions
|
| 1258 |
+
const sigmoid = (x, k = settings.aggressiveness) => 1 / (1 + Math.exp(-k * x));
|
| 1259 |
+
const mapZPositive = (z) => Math.max(0, Math.min(1, sigmoid(z)));
|
| 1260 |
+
const mapZNegative = (z) => Math.max(0, Math.min(1, sigmoid(-z)));
|
| 1261 |
+
const mapRange = (value, fromMin, fromMax, toMin, toMax) => {
|
| 1262 |
+
const clamped = Math.max(fromMin, Math.min(fromMax, value));
|
| 1263 |
+
return toMin + ((clamped - fromMin) / (fromMax - fromMin)) * (toMax - toMin);
|
| 1264 |
+
};
|
| 1265 |
+
const normalize01 = (sum, weights) => sum / (weights.reduce((a, b) => a + b, 0) || 1);
|
| 1266 |
+
|
| 1267 |
+
// Calculate components
|
| 1268 |
+
const hrv_component = mapZPositive(zScores.hrv_ms) * w.w_hrv;
|
| 1269 |
+
const rhr_component = mapZNegative(zScores.rhr_bpm) * w.w_rhr;
|
| 1270 |
+
const sleep_component = Math.max(0, Math.min(1, state.today.sleep_efficiency_pct / 100)) * w.w_sleep;
|
| 1271 |
+
const resp_component = mapZNegative(Math.abs(zScores.resp_rate)) * w.w_resp;
|
| 1272 |
+
const skin_component = mapZNegative(Math.abs(zScores.skin_temp_delta)) * w.w_skin;
|
| 1273 |
+
|
| 1274 |
+
// Get yesterday's strain
|
| 1275 |
+
const yesterdayStrain = state.history.days[state.history.days.length - 2]?.strain || 10;
|
| 1276 |
+
const yesterday_strain_component = mapRange(yesterdayStrain, 0, 21, 1, 0) * w.w_strainLoad;
|
| 1277 |
+
|
| 1278 |
+
const illness_flag_component = (state.today.ill ? 0 : 1) * w.w_ill;
|
| 1279 |
+
|
| 1280 |
+
// Combine and normalize
|
| 1281 |
+
const raw = normalize01(
|
| 1282 |
+
hrv_component + rhr_component + sleep_component + resp_component +
|
| 1283 |
+
skin_component + yesterday_strain_component + illness_flag_component,
|
| 1284 |
+
Object.values(w)
|
| 1285 |
+
);
|
| 1286 |
+
|
| 1287 |
+
// Apply sensitivity curve
|
| 1288 |
+
const gamma_curve = 1 / settings.sensitivity;
|
| 1289 |
+
const recovery = Math.round(Math.max(0, Math.min(100, 100 * Math.pow(raw, gamma_curve))));
|
| 1290 |
+
|
| 1291 |
+
// Store contributions for waterfall
|
| 1292 |
+
state.contributions = {
|
| 1293 |
+
hrv: (hrv_component / Object.values(w).reduce((a, b) => a + b, 0)) * 100 - 50,
|
| 1294 |
+
rhr: (rhr_component / Object.values(w).reduce((a, b) => a + b, 0)) * 100 - 50,
|
| 1295 |
+
sleep: (sleep_component / Object.values(w).reduce((a, b) => a + b, 0)) * 100 - 50,
|
| 1296 |
+
resp: (resp_component / Object.values(w).reduce((a, b) => a + b, 0)) * 100 - 50,
|
| 1297 |
+
skin: (skin_component / Object.values(w).reduce((a, b) => a + b, 0)) * 100 - 50,
|
| 1298 |
+
strainLoad: (yesterday_strain_component / Object.values(w).reduce((a, b) => a + b, 0)) * 100 - 50,
|
| 1299 |
+
ill: (illness_flag_component / Object.values(w).reduce((a, b) => a + b, 0)) * 100 - 50,
|
| 1300 |
+
behaviours: 0, // Will be filled by behavior effects
|
| 1301 |
+
residual: 0
|
| 1302 |
+
};
|
| 1303 |
+
|
| 1304 |
+
return recovery;
|
| 1305 |
+
}
|
| 1306 |
+
|
| 1307 |
+
// Sleep Coach Calculation
|
| 1308 |
+
function computeSleepCoach(state) {
|
| 1309 |
+
const baseSleep = 7.5;
|
| 1310 |
+
const strain = state.today.strain;
|
| 1311 |
+
const history = state.history.days.slice(-14);
|
| 1312 |
+
const avgSleep = history.length > 0 ?
|
| 1313 |
+
history.reduce((sum, d) => sum + d.sleep_actual_h, 0) / history.length : baseSleep;
|
| 1314 |
+
|
| 1315 |
+
// Calculate sleep debt
|
| 1316 |
+
const debt = Math.max(0, (baseSleep - avgSleep) * 0.5);
|
| 1317 |
+
state.sleep.debt_14d = debt;
|
| 1318 |
+
|
| 1319 |
+
// Base need + strain adjustment + debt
|
| 1320 |
+
const strainMultiplier = mapRange(strain, 0, 21, 1, 1.3);
|
| 1321 |
+
const sleepNeed = baseSleep * strainMultiplier + debt;
|
| 1322 |
+
|
| 1323 |
+
// Apply behavior effects
|
| 1324 |
+
let adjustedNeed = sleepNeed;
|
| 1325 |
+
let efficiency = 95;
|
| 1326 |
+
const rationale = [];
|
| 1327 |
+
|
| 1328 |
+
// Get today's journal
|
| 1329 |
+
const todayJournal = state.journal_logs.filter(log =>
|
| 1330 |
+
log.date === state.today.date
|
| 1331 |
+
).flatMap(log => log.items);
|
| 1332 |
+
|
| 1333 |
+
todayJournal.forEach(entry => {
|
| 1334 |
+
const behaviour = appState.behaviour_catalog.find(b => b.id === entry.id);
|
| 1335 |
+
if (!behaviour) return;
|
| 1336 |
+
|
| 1337 |
+
behaviour.effect.forEach(effect => {
|
| 1338 |
+
if (effect.target === 'sleep_need_h') {
|
| 1339 |
+
const addValue = effect.value * (entry.intensity || 1);
|
| 1340 |
+
adjustedNeed += addValue;
|
| 1341 |
+
rationale.push(`${behaviour.label_th}: +${addValue.toFixed(1)}h`);
|
| 1342 |
+
} else if (effect.target === 'sleep_efficiency_pct') {
|
| 1343 |
+
efficiency *= effect.value;
|
| 1344 |
+
rationale.push(`${behaviour.label_th}: ประสิทธิภาพ ${((effect.value - 1) * 100).toFixed(0)}%`);
|
| 1345 |
+
}
|
| 1346 |
+
});
|
| 1347 |
+
});
|
| 1348 |
+
|
| 1349 |
+
// Calculate bedtime based on chronotype
|
| 1350 |
+
const targetWaketime = appState.settings.enableChronotype && state.profile.chronotype !== "none" ?
|
| 1351 |
+
getChronotypeWaketime(state.profile.chronotype) : "06:30";
|
| 1352 |
+
|
| 1353 |
+
const sleepHours = adjustedNeed * (efficiency / 100);
|
| 1354 |
+
const bedtime = calculateBedtime(targetWaketime, sleepHours);
|
| 1355 |
+
|
| 1356 |
+
state.sleep.coach = {
|
| 1357 |
+
recommended_bedtime: bedtime,
|
| 1358 |
+
recommended_waketime: targetWaketime,
|
| 1359 |
+
rationale: rationale,
|
| 1360 |
+
sleep_need_h: adjustedNeed,
|
| 1361 |
+
efficiency_pct: efficiency
|
| 1362 |
+
};
|
| 1363 |
+
|
| 1364 |
+
return state.sleep.coach;
|
| 1365 |
+
}
|
| 1366 |
+
|
| 1367 |
+
// Strain Target Calculation
|
| 1368 |
+
function computeStrainTarget(state, baselines, zScores) {
|
| 1369 |
+
const hrvFactor = mapZPositive(zScores.hrv_ms);
|
| 1370 |
+
const rhrFactor = mapZPositive(zScores.rhr_bpm);
|
| 1371 |
+
const recovery = state.today.recovery;
|
| 1372 |
+
|
| 1373 |
+
// Base capacity based on physiological readiness
|
| 1374 |
+
let capacity = 21 * Math.max(0.2, Math.min(1.0,
|
| 1375 |
+
0.5 + 0.5 * hrvFactor - 0.5 * rhrFactor
|
| 1376 |
+
));
|
| 1377 |
+
|
| 1378 |
+
// Apply behavior bonuses
|
| 1379 |
+
const todayJournal = state.journal_logs.filter(log =>
|
| 1380 |
+
log.date === state.today.date
|
| 1381 |
+
).flatMap(log => log.items);
|
| 1382 |
+
|
| 1383 |
+
let behaviorBonus = 0;
|
| 1384 |
+
todayJournal.forEach(entry => {
|
| 1385 |
+
if (entry.id === 'sauna' || entry.id === 'cold_plunge') {
|
| 1386 |
+
behaviorBonus += 0.05 * (entry.intensity || 1);
|
| 1387 |
+
} else if (entry.id === 'meditation' || entry.id === 'stretching') {
|
| 1388 |
+
behaviorBonus += 0.02 * (entry.intensity || 1);
|
| 1389 |
+
} else if (entry.id === 'illness') {
|
| 1390 |
+
capacity *= 0.5;
|
| 1391 |
+
}
|
| 1392 |
+
});
|
| 1393 |
+
|
| 1394 |
+
capacity *= (1 + behaviorBonus);
|
| 1395 |
+
|
| 1396 |
+
// Apply recovery guard
|
| 1397 |
+
if (recovery < 33) {
|
| 1398 |
+
capacity = Math.min(capacity, 6); // Rest only
|
| 1399 |
+
} else if (recovery < 50) {
|
| 1400 |
+
capacity = Math.min(capacity, 10); // Light only
|
| 1401 |
+
} else if (recovery < 67) {
|
| 1402 |
+
capacity = Math.min(capacity, 15); // Moderate max
|
| 1403 |
+
}
|
| 1404 |
+
|
| 1405 |
+
state.today.strain_target = Math.round(capacity);
|
| 1406 |
+
return state.today.strain_target;
|
| 1407 |
+
}
|
| 1408 |
+
|
| 1409 |
+
// Helper functions
|
| 1410 |
+
function mapRange(value, fromMin, fromMax, toMin, toMax) {
|
| 1411 |
+
const clamped = Math.max(fromMin, Math.min(fromMax, value));
|
| 1412 |
+
return toMin + ((clamped - fromMin) / (fromMax - fromMin)) * (toMax - toMin);
|
| 1413 |
+
}
|
| 1414 |
+
|
| 1415 |
+
function getChronotypeWaketime(chronotype) {
|
| 1416 |
+
const times = {
|
| 1417 |
+
"early_bird": "05:30",
|
| 1418 |
+
"morning": "06:00",
|
| 1419 |
+
"neutral": "06:30",
|
| 1420 |
+
"evening": "07:00",
|
| 1421 |
+
"night_owl": "08:00"
|
| 1422 |
+
};
|
| 1423 |
+
return times[chronotype] || "06:30";
|
| 1424 |
+
}
|
| 1425 |
+
|
| 1426 |
+
function calculateBedtime(waketime, sleepHours) {
|
| 1427 |
+
const [hours, minutes] = waketime.split(':').map(Number);
|
| 1428 |
+
const totalMinutes = hours * 60 + minutes;
|
| 1429 |
+
const bedtimeMinutes = (totalMinutes - Math.round(sleepHours * 60) + 24 * 60) % (24 * 60);
|
| 1430 |
+
const bedHours = Math.floor(bedtimeMinutes / 60);
|
| 1431 |
+
const bedMins = bedtimeMinutes % 60;
|
| 1432 |
+
return `${bedHours.toString().padStart(2, '0')}:${bedMins.toString().padStart(2, '0')}`;
|
| 1433 |
+
}
|
| 1434 |
+
|
| 1435 |
+
// Main calculation sequence
|
| 1436 |
+
function runDailyCalculations() {
|
| 1437 |
+
// Load state from localStorage
|
| 1438 |
+
loadStateFromStorage();
|
| 1439 |
+
|
| 1440 |
+
// Step 1: Decay carry-over effects
|
| 1441 |
+
appState.carryOver = decayBehaviorEffects(appState.carryOver);
|
| 1442 |
+
|
| 1443 |
+
// Step 2: Apply today's behaviors
|
| 1444 |
+
const todayJournal = appState.journal_logs.filter(log =>
|
| 1445 |
+
log.date === appState.today.date
|
| 1446 |
+
).flatMap(log => log.items);
|
| 1447 |
+
|
| 1448 |
+
const behaviorResult = applyBehaviorEffects(todayJournal, appState.today);
|
| 1449 |
+
const adjustedMetrics = behaviorResult.adjusted;
|
| 1450 |
+
|
| 1451 |
+
// Step 3: Compute baselines and Z-scores
|
| 1452 |
+
const baselines = computeBaselines(appState.history.days);
|
| 1453 |
+
const zScores = {};
|
| 1454 |
+
Object.keys(baselines.baselineLong).forEach(metric => {
|
| 1455 |
+
if (metric.endsWith('_std')) return;
|
| 1456 |
+
const std = baselines.baselineLong[metric + '_std'] || 1;
|
| 1457 |
+
const baseline = baselines.baselineLong[metric] || adjustedMetrics[metric] || 0;
|
| 1458 |
+
let z = (adjustedMetrics[metric] - baseline) / std;
|
| 1459 |
+
z = Math.max(-appState.settings.clipZ, Math.min(appState.settings.clipZ, z));
|
| 1460 |
+
zScores[metric] = z;
|
| 1461 |
+
});
|
| 1462 |
+
|
| 1463 |
+
// Step 4: Update today's adjusted metrics
|
| 1464 |
+
Object.assign(appState.today, adjustedMetrics);
|
| 1465 |
+
|
| 1466 |
+
// Step 5: Calculate recovery
|
| 1467 |
+
appState.today.recovery = computeRecoveryScore(appState, baselines, zScores);
|
| 1468 |
+
|
| 1469 |
+
// Step 6: Calculate sleep coach recommendations
|
| 1470 |
+
computeSleepCoach(appState);
|
| 1471 |
+
|
| 1472 |
+
// Step 7: Calculate strain target
|
| 1473 |
+
computeStrainTarget(appState, baselines, zScores);
|
| 1474 |
+
|
| 1475 |
+
// Step 8: Save state
|
| 1476 |
+
saveStateToStorage();
|
| 1477 |
+
|
| 1478 |
+
// Step 9: Update UI
|
| 1479 |
+
updateAllUI();
|
| 1480 |
+
}
|
| 1481 |
+
|
| 1482 |
+
// Storage functions
|
| 1483 |
+
function saveStateToStorage() {
|
| 1484 |
+
localStorage.setItem('vitaltrack_state', JSON.stringify(appState));
|
| 1485 |
+
}
|
| 1486 |
+
|
| 1487 |
+
function loadStateFromStorage() {
|
| 1488 |
+
const saved = localStorage.getItem('vitaltrack_state');
|
| 1489 |
+
if (saved) {
|
| 1490 |
+
Object.assign(appState, JSON.parse(saved));
|
| 1491 |
+
}
|
| 1492 |
+
}
|
| 1493 |
+
|
| 1494 |
+
// Advance day for testing
|
| 1495 |
+
function advanceDay() {
|
| 1496 |
+
const today = new Date(appState.today.date);
|
| 1497 |
+
today.setDate(today.getDate() + 1);
|
| 1498 |
+
const newDate = today.toISOString().split('T')[0];
|
| 1499 |
+
|
| 1500 |
+
// Move today to history
|
| 1501 |
+
appState.history.days.push({...appState.today, date: appState.today.date});
|
| 1502 |
+
if (appState.history.days.length > 90) {
|
| 1503 |
+
appState.history.days.shift();
|
| 1504 |
+
}
|
| 1505 |
+
|
| 1506 |
+
// Reset today with new date
|
| 1507 |
+
appState.today = {
|
| 1508 |
+
date: newDate,
|
| 1509 |
+
strain: 5 + Math.random() * 15,
|
| 1510 |
+
hrv_ms: 60 + Math.random() * 60,
|
| 1511 |
+
rhr_bpm: 45 + Math.random() * 20,
|
| 1512 |
+
resp_rate: 12 + Math.random() * 8,
|
| 1513 |
+
skin_temp_delta: (Math.random() - 0.5) * 0.6,
|
| 1514 |
+
sleep_actual_h: 6 + Math.random() * 3,
|
| 1515 |
+
sleep_efficiency_pct: 75 + Math.random() * 20,
|
| 1516 |
+
sleep_need_h: 7.5,
|
| 1517 |
+
ill: false,
|
| 1518 |
+
recovery: 0,
|
| 1519 |
+
strain_target: 21
|
| 1520 |
+
};
|
| 1521 |
+
|
| 1522 |
+
runDailyCalculations();
|
| 1523 |
+
}
|
| 1524 |
+
// Language data
|
| 1525 |
const translations = {
|
| 1526 |
th: {
|
| 1527 |
"app-title": "VitalTrack Pro",
|
|
|
|
| 1709 |
}
|
| 1710 |
return data;
|
| 1711 |
}
|
|
|
|
| 1712 |
// Initialize app
|
| 1713 |
function initApp() {
|
| 1714 |
feather.replace();
|
| 1715 |
+
loadStateFromStorage();
|
| 1716 |
+
runDailyCalculations();
|
| 1717 |
updateDateTime();
|
| 1718 |
setInterval(updateDateTime, 60000);
|
| 1719 |
setInterval(updateLiveHR, 1000);
|
| 1720 |
renderWorkoutList();
|
| 1721 |
}
|
| 1722 |
+
// Update date/time
|
|
|
|
| 1723 |
function updateDateTime() {
|
| 1724 |
const now = new Date();
|
| 1725 |
const options = { year: 'numeric', month: 'long', day: 'numeric' };
|
|
|
|
| 1781 |
}
|
| 1782 |
});
|
| 1783 |
}
|
|
|
|
| 1784 |
// Modal functions
|
| 1785 |
function openJournalModal() {
|
| 1786 |
+
const modal = document.getElementById('journal-modal');
|
| 1787 |
+
modal.classList.add('active');
|
| 1788 |
+
|
| 1789 |
+
// Load today's journal state
|
| 1790 |
+
const today = new Date().toISOString().split('T')[0];
|
| 1791 |
+
const todayJournal = appState.journal_logs.find(log => log.date === today);
|
| 1792 |
+
|
| 1793 |
+
// Clear previous selections
|
| 1794 |
+
modal.querySelectorAll('input[type="checkbox"]').forEach(cb => {
|
| 1795 |
+
cb.checked = false;
|
| 1796 |
+
cb.dataset.intensity = 1;
|
| 1797 |
+
});
|
| 1798 |
+
|
| 1799 |
+
// Set current selections
|
| 1800 |
+
if (todayJournal) {
|
| 1801 |
+
todayJournal.items.forEach(item => {
|
| 1802 |
+
const checkbox = modal.querySelector(`input[data-behaviour="${item.id}"]`);
|
| 1803 |
+
if (checkbox) {
|
| 1804 |
+
checkbox.checked = true;
|
| 1805 |
+
checkbox.dataset.intensity = item.intensity;
|
| 1806 |
+
}
|
| 1807 |
+
});
|
| 1808 |
+
}
|
| 1809 |
}
|
| 1810 |
|
| 1811 |
function closeJournalModal() {
|
|
|
|
| 1814 |
|
| 1815 |
function openSettingsModal() {
|
| 1816 |
document.getElementById('settings-modal').classList.add('active');
|
| 1817 |
+
loadSettingsUI();
|
| 1818 |
}
|
| 1819 |
|
| 1820 |
function closeSettingsModal() {
|
| 1821 |
document.getElementById('settings-modal').classList.remove('active');
|
| 1822 |
}
|
| 1823 |
|
| 1824 |
+
function loadSettingsUI() {
|
| 1825 |
+
// Load sensitivity
|
| 1826 |
+
document.getElementById('sensitivity-slider').value = appState.settings.sensitivity;
|
| 1827 |
+
document.getElementById('sensitivity-value').textContent = appState.settings.sensitivity.toFixed(1);
|
| 1828 |
+
|
| 1829 |
+
// Load aggressiveness
|
| 1830 |
+
document.getElementById('aggressiveness-slider').value = appState.settings.aggressiveness;
|
| 1831 |
+
document.getElementById('aggressiveness-value').textContent = appState.settings.aggressiveness.toFixed(2);
|
| 1832 |
+
|
| 1833 |
+
// Load weights
|
| 1834 |
+
Object.entries(appState.settings.weights).forEach(([key, value]) => {
|
| 1835 |
+
const input = document.getElementById(`weight-${key}`);
|
| 1836 |
+
if (input) {
|
| 1837 |
+
input.value = value.toFixed(3);
|
| 1838 |
+
}
|
| 1839 |
+
});
|
| 1840 |
+
}
|
| 1841 |
+
|
| 1842 |
+
function saveSettings() {
|
| 1843 |
+
// Save sensitivity
|
| 1844 |
+
appState.settings.sensitivity = parseFloat(document.getElementById('sensitivity-slider').value);
|
| 1845 |
+
|
| 1846 |
+
// Save aggressiveness
|
| 1847 |
+
appState.settings.aggressiveness = parseFloat(document.getElementById('aggressiveness-slider').value);
|
| 1848 |
+
|
| 1849 |
+
// Save weights
|
| 1850 |
+
Object.keys(appState.settings.weights).forEach(key => {
|
| 1851 |
+
const input = document.getElementById(`weight-${key}`);
|
| 1852 |
+
if (input) {
|
| 1853 |
+
appState.settings.weights[key] = parseFloat(input.value);
|
| 1854 |
+
}
|
| 1855 |
+
});
|
| 1856 |
+
|
| 1857 |
+
// Save chronotype
|
| 1858 |
+
const chronotypeSelect = document.getElementById('chronotype-select');
|
| 1859 |
+
if (chronotypeSelect) {
|
| 1860 |
+
appState.profile.chronotype = chronotypeSelect.value;
|
| 1861 |
+
}
|
| 1862 |
+
|
| 1863 |
+
saveStateToStorage();
|
| 1864 |
+
runDailyCalculations();
|
| 1865 |
+
closeSettingsModal();
|
| 1866 |
+
showToast('Settings saved!');
|
| 1867 |
+
}
|
| 1868 |
+
|
| 1869 |
+
function calibrateBaselines() {
|
| 1870 |
+
const days = parseInt(document.getElementById('calibrate-days').value);
|
| 1871 |
+
|
| 1872 |
+
// Generate new historical data
|
| 1873 |
+
appState.history.days = generateRealisticHistory(days);
|
| 1874 |
+
|
| 1875 |
+
// Clear carry-over and journal
|
| 1876 |
+
appState.carryOver.effects = [];
|
| 1877 |
+
appState.journal_logs = [];
|
| 1878 |
+
|
| 1879 |
+
// Reset today
|
| 1880 |
+
appState.today = {
|
| 1881 |
+
date: new Date().toISOString().split('T')[0],
|
| 1882 |
+
strain: 10,
|
| 1883 |
+
hrv_ms: 75,
|
| 1884 |
+
rhr_bpm: 55,
|
| 1885 |
+
resp_rate: 14,
|
| 1886 |
+
skin_temp_delta: 0,
|
| 1887 |
+
sleep_actual_h: 7.5,
|
| 1888 |
+
sleep_efficiency_pct: 90,
|
| 1889 |
+
sleep_need_h: 7.5,
|
| 1890 |
+
ill: false,
|
| 1891 |
+
recovery: 0,
|
| 1892 |
+
strain_target: 21
|
| 1893 |
};
|
| 1894 |
|
| 1895 |
+
runDailyCalculations();
|
| 1896 |
+
showToast(`Calibrated with ${days} days of data!`);
|
| 1897 |
+
}
|
| 1898 |
+
// Save journal with behavior effects
|
| 1899 |
+
function saveJournal() {
|
| 1900 |
+
const today = new Date().toISOString().split('T')[0];
|
| 1901 |
+
const items = [];
|
| 1902 |
+
|
| 1903 |
document.querySelectorAll('#journal-modal input[type="checkbox"]:checked').forEach(checkbox => {
|
| 1904 |
+
const label = checkbox.nextElementSibling.textContent.trim();
|
| 1905 |
+
const behaviour = appState.behaviour_catalog.find(b => b.label_th === label);
|
| 1906 |
+
if (behaviour) {
|
| 1907 |
+
items.push({
|
| 1908 |
+
id: behaviour.id,
|
| 1909 |
+
intensity: parseInt(checkbox.dataset.intensity || 1)
|
| 1910 |
+
});
|
| 1911 |
+
}
|
| 1912 |
});
|
| 1913 |
|
| 1914 |
+
// Add or update today's journal
|
| 1915 |
+
const existingIndex = appState.journal_logs.findIndex(log => log.date === today);
|
| 1916 |
+
if (existingIndex >= 0) {
|
| 1917 |
+
appState.journal_logs[existingIndex].items = items;
|
| 1918 |
+
} else {
|
| 1919 |
+
appState.journal_logs.push({
|
| 1920 |
+
date: today,
|
| 1921 |
+
items: items
|
| 1922 |
+
});
|
| 1923 |
+
}
|
| 1924 |
|
| 1925 |
+
// Recalculate everything
|
| 1926 |
+
runDailyCalculations();
|
| 1927 |
+
closeJournalModal();
|
| 1928 |
showToast('Journal saved successfully!');
|
| 1929 |
}
|
| 1930 |
+
// Workout functions
|
|
|
|
| 1931 |
function startWorkout() {
|
| 1932 |
document.getElementById('live-workout').classList.remove('hidden');
|
| 1933 |
workoutStartTime = Date.now();
|
|
|
|
| 2263 |
// Redraw charts with new period
|
| 2264 |
drawTrendsCharts();
|
| 2265 |
}
|
| 2266 |
+
// Update all UI components
|
| 2267 |
+
function updateAllUI() {
|
| 2268 |
+
// Update Today tab
|
| 2269 |
+
document.getElementById('strain-value').textContent = appState.today.strain.toFixed(1);
|
| 2270 |
+
document.getElementById('recovery-value').textContent = appState.today.recovery + '%';
|
| 2271 |
+
document.getElementById('sleep-value').textContent = Math.round(appState.today.sleep_efficiency_pct) + '%';
|
| 2272 |
+
document.getElementById('hrv-value').textContent = Math.round(appState.today.hrv_ms);
|
| 2273 |
+
document.getElementById('current-hr').textContent = Math.round(appState.today.rhr_bpm);
|
| 2274 |
+
|
| 2275 |
+
// Update donut charts
|
| 2276 |
+
updateDonutChart('strain-ring', appState.today.strain, 21);
|
| 2277 |
+
updateDonutChart('recovery-ring', appState.today.recovery, 100);
|
| 2278 |
+
updateDonutChart('sleep-ring', appState.today.sleep_efficiency_pct, 100);
|
| 2279 |
+
|
| 2280 |
+
// Update Sleep Coach
|
| 2281 |
+
document.getElementById('sleep-coach-bedtime').textContent = appState.sleep.coach.recommended_bedtime;
|
| 2282 |
+
document.getElementById('sleep-coach-waketime').textContent = appState.sleep.coach.recommended_waketime;
|
| 2283 |
+
|
| 2284 |
+
// Update Sleep Coach rationale
|
| 2285 |
+
const rationaleDiv = document.getElementById('sleep-rationale');
|
| 2286 |
+
if (rationaleDiv) {
|
| 2287 |
+
rationaleDiv.innerHTML = appState.sleep.coach.rationale.map(r =>
|
| 2288 |
+
`<div class="text-xs text-gray-400">• ${r}</div>`
|
| 2289 |
+
).join('');
|
| 2290 |
+
}
|
| 2291 |
+
|
| 2292 |
+
// Update Strain Target
|
| 2293 |
+
const strainTargetDiv = document.getElementById('strain-target-display');
|
| 2294 |
+
if (strainTargetDiv) {
|
| 2295 |
+
strainTargetDiv.textContent = `Target: ${appState.today.strain_target}`;
|
| 2296 |
+
|
| 2297 |
+
// Show guard status
|
| 2298 |
+
let guardStatus = '';
|
| 2299 |
+
if (appState.today.recovery < 33) {
|
| 2300 |
+
guardStatus = '<span class="text-red-400">Guard: Rest</span>';
|
| 2301 |
+
} else if (appState.today.recovery < 50) {
|
| 2302 |
+
guardStatus = '<span class="text-yellow-400">Guard: Light</span>';
|
| 2303 |
+
} else if (appState.today.recovery < 67) {
|
| 2304 |
+
guardStatus = '<span class="text-blue-400">Guard: Normal</span>';
|
| 2305 |
+
} else {
|
| 2306 |
+
guardStatus = '<span class="text-green-400">Guard: Go</span>';
|
| 2307 |
+
}
|
| 2308 |
+
|
| 2309 |
+
const guardDiv = document.getElementById('guard-status');
|
| 2310 |
+
if (guardDiv) {
|
| 2311 |
+
guardDiv.innerHTML = guardStatus;
|
| 2312 |
+
}
|
| 2313 |
+
}
|
| 2314 |
+
|
| 2315 |
+
// Update waterfall if recovery tab
|
| 2316 |
+
if (document.getElementById('tab-recovery').classList.contains('active')) {
|
| 2317 |
+
drawWaterfallChart();
|
| 2318 |
+
}
|
| 2319 |
+
|
| 2320 |
+
// Update charts
|
| 2321 |
+
drawAllCharts();
|
| 2322 |
+
}
|
| 2323 |
+
|
| 2324 |
+
function updateDonutChart(elementId, value, maxValue) {
|
| 2325 |
+
const circle = document.getElementById(elementId);
|
| 2326 |
+
if (!circle) return;
|
| 2327 |
+
|
| 2328 |
+
const circumference = 2 * Math.PI * 50;
|
| 2329 |
+
const offset = circumference - (value / maxValue) * circumference;
|
| 2330 |
+
circle.style.strokeDashoffset = offset;
|
| 2331 |
+
}
|
| 2332 |
+
|
| 2333 |
+
function drawWaterfallChart() {
|
| 2334 |
+
const canvas = document.getElementById('waterfall-chart');
|
| 2335 |
+
if (!canvas) return;
|
| 2336 |
+
|
| 2337 |
+
const ctx = canvas.getContext('2d');
|
| 2338 |
+
canvas.width = canvas.offsetWidth;
|
| 2339 |
+
canvas.height = 200;
|
| 2340 |
+
|
| 2341 |
+
const contributions = appState.contributions;
|
| 2342 |
+
const labels = ['HRV', 'RHR', 'Sleep', 'Resp', 'Skin', 'Strain', 'Illness', 'Behaviors'];
|
| 2343 |
+
const values = [
|
| 2344 |
+
contributions.hrv,
|
| 2345 |
+
contributions.rhr,
|
| 2346 |
+
contributions.sleep,
|
| 2347 |
+
contributions.resp,
|
| 2348 |
+
contributions.skin,
|
| 2349 |
+
contributions.strainLoad,
|
| 2350 |
+
contributions.ill,
|
| 2351 |
+
contributions.behaviours
|
| 2352 |
+
];
|
| 2353 |
+
|
| 2354 |
+
const barWidth = canvas.width / (values.length + 1);
|
| 2355 |
+
let xPos = barWidth / 2;
|
| 2356 |
+
|
| 2357 |
+
values.forEach((value, index) => {
|
| 2358 |
+
const height = Math.abs(value) * 2;
|
| 2359 |
+
const yPos = value >= 0 ? canvas.height / 2 - height : canvas.height / 2;
|
| 2360 |
+
|
| 2361 |
+
ctx.fillStyle = value >= 0 ? '#10b981' : '#ef4444';
|
| 2362 |
+
ctx.fillRect(xPos, yPos, barWidth * 0.8, height);
|
| 2363 |
+
|
| 2364 |
+
ctx.fillStyle = '#f1f5f9';
|
| 2365 |
+
ctx.font = '10px sans-serif';
|
| 2366 |
+
ctx.textAlign = 'center';
|
| 2367 |
+
ctx.fillText(labels[index], xPos + barWidth * 0.4, canvas.height - 5);
|
| 2368 |
+
ctx.fillText(value.toFixed(1), xPos + barWidth * 0.4, yPos - 5);
|
| 2369 |
+
|
| 2370 |
+
xPos += barWidth;
|
| 2371 |
+
});
|
| 2372 |
+
|
| 2373 |
+
// Draw zero line
|
| 2374 |
+
ctx.strokeStyle = '#6b7280';
|
| 2375 |
+
ctx.beginPath();
|
| 2376 |
+
ctx.moveTo(0, canvas.height / 2);
|
| 2377 |
+
ctx.lineTo(canvas.width, canvas.height / 2);
|
| 2378 |
+
ctx.stroke();
|
| 2379 |
+
}
|
| 2380 |
|
| 2381 |
// Update live HR
|
| 2382 |
function updateLiveHR() {
|
| 2383 |
+
const hr = appState.today.rhr_bpm + Math.floor(Math.random() * 10 - 5);
|
| 2384 |
document.getElementById('current-hr').textContent = hr;
|
| 2385 |
+
if (document.getElementById('hr_current')) {
|
| 2386 |
+
document.getElementById('hr_current').textContent = hr;
|
| 2387 |
+
}
|
| 2388 |
}
|
|
|
|
| 2389 |
// Export data
|
| 2390 |
function exportData() {
|
| 2391 |
+
const dataStr = JSON.stringify(appState, null, 2);
|
| 2392 |
const dataBlob = new Blob([dataStr], {type: 'application/json'});
|
| 2393 |
const url = URL.createObjectURL(dataBlob);
|
| 2394 |
const link = document.createElement('a');
|
|
|
|
| 2400 |
|
| 2401 |
// Reset data
|
| 2402 |
function resetData() {
|
| 2403 |
+
if (confirm('Are you sure you want to reset all data?')) {
|
| 2404 |
+
localStorage.clear();
|
| 2405 |
location.reload();
|
| 2406 |
}
|
| 2407 |
}
|
| 2408 |
+
// Toast notification
|
|
|
|
| 2409 |
function showToast(message) {
|
| 2410 |
const toast = document.createElement('div');
|
| 2411 |
toast.className = 'fixed top-20 left-1/2 transform -translate-x-1/2 bg-green-500 text-white px-4 py-2 rounded-lg shadow-lg z-50';
|
|
|
|
| 2431 |
toggleLanguage();
|
| 2432 |
}
|
| 2433 |
}
|
|
|
|
| 2434 |
// Initialize on load
|
| 2435 |
document.addEventListener('DOMContentLoaded', () => {
|
| 2436 |
loadPreferences();
|
| 2437 |
initApp();
|
| 2438 |
+
|
| 2439 |
+
// Add test button for development
|
| 2440 |
+
if (window.location.search.includes('test=true')) {
|
| 2441 |
+
const testBtn = document.createElement('button');
|
| 2442 |
+
testBtn.textContent = 'Advance Day';
|
| 2443 |
+
testBtn.className = 'fixed top-20 right-4 bg-purple-500 text-white px-3 py-1 rounded text-sm z-50';
|
| 2444 |
+
testBtn.onclick = advanceDay;
|
| 2445 |
+
document.body.appendChild(testBtn);
|
| 2446 |
+
}
|
| 2447 |
});
|
| 2448 |
+
// Handle resize
|
|
|
|
| 2449 |
window.addEventListener('resize', () => {
|
| 2450 |
drawHRChart();
|
| 2451 |
if (document.getElementById('tab-recovery').classList.contains('active')) {
|