Refactor getPackageCount function to use overall download counts for more reliable package counting. Added error handling to return 0 in case of failures.

This commit is contained in:
Luke Hagar
2025-08-19 11:50:29 -05:00
parent 879aa69f52
commit 04173b8002

View File

@@ -330,18 +330,20 @@ export async function searchPackages(searchTerm: string) {
}
export async function getPackageCount() {
// First try recent monthly snapshot as authoritative
const recent = await prisma.recentDownloadCount.findMany({
where: { category: 'month' },
distinct: ['package'],
select: { package: true }
});
try {
// Count distinct packages from overall downloads table (most reliable)
const overall = await prisma.overallDownloadCount.groupBy({
by: ['package'],
where: {
category: 'without_mirrors' // Use without_mirrors as canonical source
}
});
const distinct = new Set<string>(recent.map((r) => r.package));
const count = distinct.size;
return count;
return overall.length;
} catch (error) {
console.error('Error getting package count:', error);
return 0;
}
}
export async function getPopularPackages(limit = 10, days = 30): Promise<Array<{ package: string; downloads: number }>> {