Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 9 additions & 53 deletions apps/web/app/lib/leaderboard/recompute-all-skills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import { sql, eq } from "drizzle-orm";
import type { Database } from "~/lib/db";
import { skills } from "~/lib/db/schema";
import {
computeBayesianRating,
computeCompositeScore,
computeTrendingScore,
} from "./leaderboard-scoring";
type SkillAggregates,
buildScoreUpdate,
} from "./recompute-score-update";
import {
type RecomputeState,
type RecomputeOptions,
Expand Down Expand Up @@ -102,15 +101,6 @@ export async function recomputeBatch(
// Batch-fetch per-skill aggregates in one query (avoids N+1)
const skillIds = batch.map((s) => s.id);

interface SkillAggregates {
skill_id: string;
fav_count: number;
recent_ratings: number;
recent_usage: number;
total_usage: number;
success_count: number;
}

const perSkillStats: SkillAggregates[] = skillIds.length > 0
? (await db
.select({
Expand All @@ -129,48 +119,14 @@ export async function recomputeBatch(

// Compute and update each skill
for (const skill of batch) {
const extra = statsMap.get(skill.id);
const favoriteCount = extra?.fav_count ?? 0;
const successRate =
(extra?.total_usage ?? 0) > 0
? (extra?.success_count ?? 0) / extra!.total_usage
: 0.5;

const bayesianRating = computeBayesianRating(
skill.avgRating ?? 0,
skill.ratingCount ?? 0,
globalStats.globalAvgRating,
);

const compositeScore = computeCompositeScore({
bayesianRating,
installCount: skill.installCount ?? 0,
githubStars: skill.githubStars ?? 0,
netVotes: skill.netVotes ?? 0,
successRate,
updatedAt: skill.updatedAt,
favoriteCount,
maxInstalls: globalStats.maxInstalls,
maxStars: globalStats.maxStars,
maxNetVotes: Math.max(globalStats.maxNetVotes, 1),
maxFavorites: Math.max(globalStats.maxFavorites, 1),
const update = buildScoreUpdate({
skill,
aggregates: statsMap.get(skill.id),
globalStats,
maxTrendingRaw: maxTrendingResult.maxRaw,
});

const trendingScore = computeTrendingScore(
extra?.recent_ratings ?? 0,
extra?.recent_usage ?? 0,
maxTrendingResult.maxRaw,
);

await db
.update(skills)
.set({
bayesian_rating: bayesianRating,
composite_score: compositeScore,
trending_score: trendingScore,
favorite_count: favoriteCount,
})
.where(eq(skills.id, skill.id));
await db.update(skills).set(update).where(eq(skills.id, skill.id));
}

const newOffset = offset + batch.length;
Expand Down
157 changes: 157 additions & 0 deletions apps/web/app/lib/leaderboard/recompute-score-update.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/**
* The shared scoring step behind both recompute paths.
*
* These pin the four columns the two callers write, and the one behaviour that
* is easy to regress while de-duplicating: a skill with no recorded usage is a
* neutral 50%, not a 0% failure rate.
*/

import { describe, expect, it } from "vitest";
import {
DEFAULT_SUCCESS_RATE,
buildScoreUpdate,
successRateFrom,
type GlobalStats,
type SkillAggregates,
} from "./recompute-score-update";

const globalStats: GlobalStats = {
globalAvgRating: 6,
maxInstalls: 1000,
maxStars: 500,
maxFavorites: 200,
maxNetVotes: 50,
};

const skill = {
avgRating: 8,
ratingCount: 20,
installCount: 100,
githubStars: 40,
netVotes: 5,
updatedAt: new Date("2026-10-01T00:00:00Z"),
};

function aggregates(overrides: Partial<SkillAggregates> = {}): SkillAggregates {
return {
skill_id: "skill-1",
fav_count: 7,
recent_ratings: 4,
recent_usage: 10,
total_usage: 20,
success_count: 15,
...overrides,
};
}

describe("successRateFrom", () => {
it("treats a skill with no usage as neutral instead of failed", () => {
expect(successRateFrom(undefined)).toBe(DEFAULT_SUCCESS_RATE);
expect(successRateFrom(aggregates({ total_usage: 0, success_count: 0 }))).toBe(
DEFAULT_SUCCESS_RATE,
);
});

it("divides successes by recorded usage", () => {
expect(successRateFrom(aggregates({ total_usage: 20, success_count: 15 }))).toBe(0.75);
expect(successRateFrom(aggregates({ total_usage: 4, success_count: 0 }))).toBe(0);
});
});

describe("buildScoreUpdate", () => {
it("returns the four precomputed columns", () => {
const update = buildScoreUpdate({
skill,
aggregates: aggregates(),
globalStats,
maxTrendingRaw: 100,
});

expect(Object.keys(update).sort()).toEqual([
"bayesian_rating",
"composite_score",
"favorite_count",
"trending_score",
]);
expect(update.favorite_count).toBe(7);
expect(update.composite_score).toBeGreaterThan(0);
expect(update.composite_score).toBeLessThanOrEqual(1);
expect(update.trending_score).toBeGreaterThanOrEqual(0);
expect(update.trending_score).toBeLessThanOrEqual(1);
});

it("carries the favourite count from the aggregate row, defaulting to zero", () => {
expect(
buildScoreUpdate({ skill, aggregates: aggregates(), globalStats, maxTrendingRaw: 100 })
.favorite_count,
).toBe(7);
expect(
buildScoreUpdate({ skill, globalStats, maxTrendingRaw: 100 }).favorite_count,
).toBe(0);
});

it("reads missing skill columns as zero rather than NaN", () => {
const update = buildScoreUpdate({
skill: {
avgRating: null,
ratingCount: null,
installCount: null,
githubStars: null,
netVotes: null,
updatedAt: null,
},
globalStats,
maxTrendingRaw: 100,
});

expect(Number.isNaN(update.bayesian_rating)).toBe(false);
expect(Number.isNaN(update.composite_score)).toBe(false);
expect(Number.isNaN(update.trending_score)).toBe(false);
expect(update.favorite_count).toBe(0);
});

it("does not divide by a zero maximum", () => {
const update = buildScoreUpdate({
skill,
aggregates: aggregates({ recent_ratings: 0, recent_usage: 0 }),
globalStats: { ...globalStats, maxNetVotes: 0, maxFavorites: 0 },
maxTrendingRaw: 0,
});

expect(update.trending_score).toBe(0);
expect(Number.isFinite(update.composite_score)).toBe(true);
});

it("ranks a stronger skill above a weaker one", () => {
const strong = buildScoreUpdate({
skill: { ...skill, installCount: 900, githubStars: 400, netVotes: 40 },
aggregates: aggregates({ fav_count: 180, total_usage: 100, success_count: 95 }),
globalStats,
maxTrendingRaw: 100,
});
const weak = buildScoreUpdate({
skill: { ...skill, installCount: 1, githubStars: 0, netVotes: 0 },
aggregates: aggregates({ fav_count: 0, total_usage: 10, success_count: 1 }),
globalStats,
maxTrendingRaw: 100,
});

expect(strong.composite_score).toBeGreaterThan(weak.composite_score);
});

it("pulls a low-sample rating toward the global mean", () => {
const lowSample = buildScoreUpdate({
skill: { ...skill, avgRating: 10, ratingCount: 1 },
globalStats,
maxTrendingRaw: 100,
});
const highSample = buildScoreUpdate({
skill: { ...skill, avgRating: 10, ratingCount: 500 },
globalStats,
maxTrendingRaw: 100,
});

expect(lowSample.bayesian_rating).toBeLessThan(highSample.bayesian_rating);
expect(lowSample.bayesian_rating).toBeGreaterThan(globalStats.globalAvgRating);
});
});
113 changes: 113 additions & 0 deletions apps/web/app/lib/leaderboard/recompute-score-update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Pure scoring step shared by the bulk and the single-skill recompute paths.
*
* Both paths used to carry their own copy of the same Bayesian / composite /
* trending math, down to the `Math.max(..., 1)` guards. They now build the four
* precomputed columns here, and each module keeps only its own data fetching and
* the single `UPDATE`.
*/

import {
computeBayesianRating,
computeCompositeScore,
computeTrendingScore,
} from "./leaderboard-scoring";

/** Per-skill aggregate row read by the bulk path's batched query. */
export interface SkillAggregates {
skill_id: string;
fav_count: number;
recent_ratings: number;
recent_usage: number;
total_usage: number;
success_count: number;
}

/** The skill columns the score depends on. */
export interface ScoreInputs {
avgRating: number | null;
ratingCount: number | null;
installCount: number | null;
githubStars: number | null;
netVotes: number | null;
updatedAt: Date | null;
}

/** Column maxima used to normalize each signal. */
export interface GlobalStats {
globalAvgRating: number;
maxInstalls: number;
maxStars: number;
maxFavorites: number;
maxNetVotes: number;
}

/** The four precomputed leaderboard columns, ready for a single UPDATE. */
export interface ScoreUpdate {
bayesian_rating: number;
composite_score: number;
trending_score: number;
favorite_count: number;
}

/** Success rate assumed when a skill has no recorded usage yet. */
export const DEFAULT_SUCCESS_RATE = 0.5;

/**
* Share of successful runs, or the neutral default when there is nothing to
* divide by. A skill with zero usage is not a 0% skill.
*/
export function successRateFrom(aggregates?: SkillAggregates): number {
if (!aggregates || !(aggregates.total_usage > 0)) return DEFAULT_SUCCESS_RATE;
return aggregates.success_count / aggregates.total_usage;
}

/**
* Build the four precomputed leaderboard columns for one skill.
*
* Pure: every input is a plain number or date, so it can be unit-tested without
* a D1 or KV binding, and the caller decides how the row is written.
*/
export function buildScoreUpdate(input: {
skill: ScoreInputs;
aggregates?: SkillAggregates;
globalStats: GlobalStats;
maxTrendingRaw: number;
}): ScoreUpdate {
const { skill, aggregates, globalStats, maxTrendingRaw } = input;

const favoriteCount = aggregates?.fav_count ?? 0;

const bayesianRating = computeBayesianRating(
skill.avgRating ?? 0,
skill.ratingCount ?? 0,
globalStats.globalAvgRating,
);

const compositeScore = computeCompositeScore({
bayesianRating,
installCount: skill.installCount ?? 0,
githubStars: skill.githubStars ?? 0,
netVotes: skill.netVotes ?? 0,
successRate: successRateFrom(aggregates),
updatedAt: skill.updatedAt,
favoriteCount,
maxInstalls: globalStats.maxInstalls,
maxStars: globalStats.maxStars,
maxNetVotes: Math.max(globalStats.maxNetVotes, 1),
maxFavorites: Math.max(globalStats.maxFavorites, 1),
});

const trendingScore = computeTrendingScore(
aggregates?.recent_ratings ?? 0,
aggregates?.recent_usage ?? 0,
maxTrendingRaw,
);

return {
bayesian_rating: bayesianRating,
composite_score: compositeScore,
trending_score: trendingScore,
favorite_count: favoriteCount,
};
}
Loading
Loading