]> git.tue.mpg.de Git - paraslash.git/commitdiff
mood: Fix compute_score().
authorAndre Noll <maan@tuebingen.mpg.de>
Sat, 22 Oct 2022 17:59:52 +0000 (19:59 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 24 Oct 2022 14:54:48 +0000 (16:54 +0200)
This fixes a bug which was introduced 10 months ago in merge
commit 88bf6848d1c (Merge branch 'refs/heads/t/rm_v1_moods'). This
merge conflicted in mood.c, and the conflicting hunks that touched
compute_score() were resolved incorrectly. Put simply, we kept the
old code and disregarded the correction factors that were introduced
by the other side of the merge. As a result, at mood load time the
correction factors were initialized correctly but not taken into
account for computing the score of the admissible files.

The fact that the bug went unnoticed for so long can only mean that
the correction factors don't make much of a difference in practice.
However, the bug was found because one particular mood behaved
unexpectedly, likely because its admissible files consisted of a
bunch of very new files among many very old ones.

mood.c

diff --git a/mood.c b/mood.c
index bcc9bc57d1a37520ca71f93d5d93a7fe1e2e2927..d47c54efa360b856c63b660fd4cdb5b81d442b68 100644 (file)
--- a/mood.c
+++ b/mood.c
@@ -249,20 +249,22 @@ int mood_check_callback(struct afs_callback_arg *aca)
  * overflows and rounding errors we store the common divisor of the
  * correction factors separately.
  */
-static int64_t normalized_value(int64_t x, int64_t n, int64_t sum, int64_t qd)
-{
-       if (!n || !qd)
-               return 0;
-       return 100 * (n * x - sum) / (int64_t)int_sqrt(n) / (int64_t)int_sqrt(qd);
-}
-
 static long compute_score(struct afs_info *afsi)
 {
-       long score = -normalized_value(afsi->num_played, statistics.num,
-               statistics.num_played_sum, statistics.num_played_qd);
-       score -= normalized_value(afsi->last_played, statistics.num,
-               statistics.last_played_sum, statistics.last_played_qd);
-       return score / 2;
+       int64_t mean_n, mean_l,score_n, score_l;
+
+       assert(statistics.normalization_divisor > 0);
+       assert(statistics.num > 0);
+       mean_n = statistics.num_played_sum / statistics.num;
+       mean_l = statistics.last_played_sum / statistics.num;
+
+       score_n = -((int64_t)afsi->num_played - mean_n)
+               * statistics.num_played_correction
+               / statistics.normalization_divisor;
+       score_l = -((int64_t)afsi->last_played - mean_l)
+               * statistics.last_played_correction
+               / statistics.normalization_divisor;
+       return (score_n + score_l) / 2;
 }
 
 static int add_afs_statistics(const struct osl_row *row)