fix: Fix mood calculation key derivation

Updates the logic for finding the nearest mood option key by comparing the numeric key directly to the average value.

This resolves incorrect mood determinations by ensuring that comparisons are based on numeric values, thus preventing potential mismatches in mood calculations.
This commit is contained in:
Kumi 2024-11-28 15:22:11 +01:00
parent 2c4e02e1af
commit 36835c9fad
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -23,7 +23,7 @@ fetch("/mood/statistics/heatmap/values/")
x: "date", x: "date",
y: d => { y: d => {
if (d.average) { if (d.average) {
const key = Object.keys(moodOptions).reduce((a, b) => Math.abs(moodOptions[a] - d.average) < Math.abs(moodOptions[b] - d.average) ? a : b); const key = Object.keys(moodOptions).reduce((a, b) => Math.abs(a - d.average) < Math.abs(b - d.average) ? a : b);
return key; return key;
} }
return 0; return 0;
@ -73,7 +73,7 @@ fetch("/mood/statistics/heatmap/values/")
return `${date_str}<br>Mood Count: ${obj.count}<br>Average Mood: N/A` return `${date_str}<br>Mood Count: ${obj.count}<br>Average Mood: N/A`
} }
const key = Object.keys(moodOptions).reduce((a, b) => Math.abs(moodOptions[a] - average) < Math.abs(moodOptions[b] - average) ? a : b); const key = Object.keys(moodOptions).reduce((a, b) => Math.abs(a - average) < Math.abs(b - average) ? a : b);
const mood = moodOptions[key]; const mood = moodOptions[key];
return `${date_str}<br>Mood Count: ${obj.count}<br>Average Mood: ${mood.name}` return `${date_str}<br>Mood Count: ${obj.count}<br>Average Mood: ${mood.name}`