nandi/gleanpublic Fork 0
39604ad
Commits
Clone
git clone https://git.rickub.com/nandi/glean.git
git clone ssh://git@rickub.com/nandi/glean.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

fix: Limit on demand recompute when no recsUnverified

Julien Robert committed 2026-06-05T23:30:54+02:00 Browse files
39604ad parent: 4fa381d
modified internal/cluster/precompute.go +7 -14
@@ -79,9 +79,9 @@ func (e *Engine) precomputeForUser(ctx context.Context, userDID string) error {
7979 }
8080 }
8181
82- e.storePrecomputed(ctx, userDID, "feed", mustJSON(feedRecs))
83- e.storePrecomputed(ctx, userDID, "article", mustJSON(articleRecs))
84- e.storePrecomputed(ctx, userDID, "person", mustJSON(peopleRecs))
82+ e.storeRecs(ctx, userDID, "feed", feedRecs)
83+ e.storeRecs(ctx, userDID, "article", articleRecs)
84+ e.storeRecs(ctx, userDID, "person", peopleRecs)
8585
8686 e.logger.Debug("precomputed recommendations for user",
8787 "did", userDID,
@@ -128,9 +128,10 @@ func (e *Engine) getPrecomputed(ctx context.Context, userDID, recType string) (s
128128 return data, true
129129 }
130130
131-func (e *Engine) storePrecomputed(ctx context.Context, userDID, recType string, data string) {
132- if data == "null" {
133- return
131+func (e *Engine) storeRecs(ctx context.Context, userDID, recType string, v any) {
132+ data := "[]"
133+ if b, err := json.Marshal(v); err == nil {
134+ data = string(b)
134135 }
135136 _, _ = e.db.ExecContext(ctx, `
136137 INSERT INTO recs.precomputed_recommendations (user_did, rec_type, data, computed_at)
@@ -138,11 +139,3 @@ func (e *Engine) storePrecomputed(ctx context.Context, userDID, recType string,
138139 ON CONFLICT(user_did, rec_type) DO UPDATE SET data = excluded.data, computed_at = excluded.computed_at
139140 `, userDID, recType, data)
140141 }
141-
142-func mustJSON(v any) string {
143- b, err := json.Marshal(v)
144- if err != nil {
145- return "null"
146- }
147- return string(b)
148-}
@@ -79,9 +79,9 @@ func (e *Engine) precomputeForUser(ctx context.Context, userDID string) error {
79 }79 }
80 }80 }
81 81
82- e.storePrecomputed(ctx, userDID, "feed", mustJSON(feedRecs))82+ e.storeRecs(ctx, userDID, "feed", feedRecs)
83- e.storePrecomputed(ctx, userDID, "article", mustJSON(articleRecs))83+ e.storeRecs(ctx, userDID, "article", articleRecs)
84- e.storePrecomputed(ctx, userDID, "person", mustJSON(peopleRecs))84+ e.storeRecs(ctx, userDID, "person", peopleRecs)
85 85
86 e.logger.Debug("precomputed recommendations for user",86 e.logger.Debug("precomputed recommendations for user",
87 "did", userDID,87 "did", userDID,
@@ -128,9 +128,10 @@ func (e *Engine) getPrecomputed(ctx context.Context, userDID, recType string) (s
128 return data, true128 return data, true
129 }129 }
130 130
131-func (e *Engine) storePrecomputed(ctx context.Context, userDID, recType string, data string) {131+func (e *Engine) storeRecs(ctx context.Context, userDID, recType string, v any) {
132- if data == "null" {132+ data := "[]"
133- return133+ if b, err := json.Marshal(v); err == nil {
134+ data = string(b)
134 }135 }
135 _, _ = e.db.ExecContext(ctx, `136 _, _ = e.db.ExecContext(ctx, `
136 INSERT INTO recs.precomputed_recommendations (user_did, rec_type, data, computed_at)137 INSERT INTO recs.precomputed_recommendations (user_did, rec_type, data, computed_at)
@@ -138,11 +139,3 @@ func (e *Engine) storePrecomputed(ctx context.Context, userDID, recType string,
138 ON CONFLICT(user_did, rec_type) DO UPDATE SET data = excluded.data, computed_at = excluded.computed_at139 ON CONFLICT(user_did, rec_type) DO UPDATE SET data = excluded.data, computed_at = excluded.computed_at
139 `, userDID, recType, data)140 `, userDID, recType, data)
140 }141 }
141-
142-func mustJSON(v any) string {
143- b, err := json.Marshal(v)
144- if err != nil {
145- return "null"
146- }
147- return string(b)
148-}
modified internal/cluster/scoring.go +4 -4
@@ -81,7 +81,7 @@ func (e *Engine) GetFeedRecommendations(ctx context.Context, userDID string, lim
8181 if err == nil && len(recs) > 0 {
8282 normalizeFeedScores(recs)
8383 result := ApplyDiversity(recs, limit)
84- e.storePrecomputed(ctx, userDID, "feed", mustJSON(result))
84+ e.storeRecs(ctx, userDID, "feed", result)
8585 e.logger.Info("feed recommendations computed (cold-start)", "did", userDID, "count", len(result), "duration", time.Since(start))
8686 return result, nil
8787 }
@@ -94,7 +94,7 @@ func (e *Engine) GetFeedRecommendations(ctx context.Context, userDID string, lim
9494
9595 normalizeFeedScores(recs)
9696 result := ApplyDiversity(recs, limit)
97- e.storePrecomputed(ctx, userDID, "feed", mustJSON(result))
97+ e.storeRecs(ctx, userDID, "feed", result)
9898 e.logger.Info("feed recommendations computed (on-demand)", "did", userDID, "count", len(result), "duration", time.Since(start))
9999 return result, nil
100100 }
@@ -131,7 +131,7 @@ func (e *Engine) GetPeopleRecommendations(ctx context.Context, userDID string, l
131131 recs = append(recs, outNet...)
132132
133133 normalizePersonScores(recs)
134- e.storePrecomputed(ctx, userDID, "person", mustJSON(recs))
134+ e.storeRecs(ctx, userDID, "person", recs)
135135 e.logger.Info("people recommendations computed (on-demand)", "did", userDID, "count", len(recs), "duration", time.Since(start))
136136 return recs, nil
137137 }
@@ -156,7 +156,7 @@ func (e *Engine) GetArticleRecommendations(ctx context.Context, userDID string,
156156 return nil, err
157157 }
158158 normalizeArticleScores(recs)
159- e.storePrecomputed(ctx, userDID, "article", mustJSON(recs))
159+ e.storeRecs(ctx, userDID, "article", recs)
160160 e.logger.Info("article recommendations computed (on-demand)", "did", userDID, "count", len(recs), "duration", time.Since(start))
161161 return recs, nil
162162 }
@@ -81,7 +81,7 @@ func (e *Engine) GetFeedRecommendations(ctx context.Context, userDID string, lim
81 if err == nil && len(recs) > 0 {81 if err == nil && len(recs) > 0 {
82 normalizeFeedScores(recs)82 normalizeFeedScores(recs)
83 result := ApplyDiversity(recs, limit)83 result := ApplyDiversity(recs, limit)
84- e.storePrecomputed(ctx, userDID, "feed", mustJSON(result))84+ e.storeRecs(ctx, userDID, "feed", result)
85 e.logger.Info("feed recommendations computed (cold-start)", "did", userDID, "count", len(result), "duration", time.Since(start))85 e.logger.Info("feed recommendations computed (cold-start)", "did", userDID, "count", len(result), "duration", time.Since(start))
86 return result, nil86 return result, nil
87 }87 }
@@ -94,7 +94,7 @@ func (e *Engine) GetFeedRecommendations(ctx context.Context, userDID string, lim
94 94
95 normalizeFeedScores(recs)95 normalizeFeedScores(recs)
96 result := ApplyDiversity(recs, limit)96 result := ApplyDiversity(recs, limit)
97- e.storePrecomputed(ctx, userDID, "feed", mustJSON(result))97+ e.storeRecs(ctx, userDID, "feed", result)
98 e.logger.Info("feed recommendations computed (on-demand)", "did", userDID, "count", len(result), "duration", time.Since(start))98 e.logger.Info("feed recommendations computed (on-demand)", "did", userDID, "count", len(result), "duration", time.Since(start))
99 return result, nil99 return result, nil
100 }100 }
@@ -131,7 +131,7 @@ func (e *Engine) GetPeopleRecommendations(ctx context.Context, userDID string, l
131 recs = append(recs, outNet...)131 recs = append(recs, outNet...)
132 132
133 normalizePersonScores(recs)133 normalizePersonScores(recs)
134- e.storePrecomputed(ctx, userDID, "person", mustJSON(recs))134+ e.storeRecs(ctx, userDID, "person", recs)
135 e.logger.Info("people recommendations computed (on-demand)", "did", userDID, "count", len(recs), "duration", time.Since(start))135 e.logger.Info("people recommendations computed (on-demand)", "did", userDID, "count", len(recs), "duration", time.Since(start))
136 return recs, nil136 return recs, nil
137 }137 }
@@ -156,7 +156,7 @@ func (e *Engine) GetArticleRecommendations(ctx context.Context, userDID string,
156 return nil, err156 return nil, err
157 }157 }
158 normalizeArticleScores(recs)158 normalizeArticleScores(recs)
159- e.storePrecomputed(ctx, userDID, "article", mustJSON(recs))159+ e.storeRecs(ctx, userDID, "article", recs)
160 e.logger.Info("article recommendations computed (on-demand)", "did", userDID, "count", len(recs), "duration", time.Since(start))160 e.logger.Info("article recommendations computed (on-demand)", "did", userDID, "count", len(recs), "duration", time.Since(start))
161 return recs, nil161 return recs, nil
162 }162 }