mirror of
https://github.com/LukeHagar/pypistats.dev.git
synced 2025-12-06 04:21:09 +00:00
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:
@@ -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 }>> {
|
||||
|
||||
Reference in New Issue
Block a user