mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 09:36:08 +00:00
feat(admin): track and display per-user download egress for Bunny version downloads
This commit is contained in:
@@ -11,6 +11,10 @@ interface BunnyStorageStats {
|
||||
byVideoId: Record<string, number>;
|
||||
}
|
||||
|
||||
function bigintToNumber(value: bigint): number {
|
||||
return value > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(value);
|
||||
}
|
||||
|
||||
function getBunnyConfig(): { apiKey: string; libraryId: string } {
|
||||
const apiKey = process.env.BUNNY_STREAM_API_KEY;
|
||||
const libraryId = process.env.BUNNY_STREAM_LIBRARY_ID || process.env.NEXT_PUBLIC_BUNNY_STREAM_LIBRARY_ID;
|
||||
@@ -237,3 +241,29 @@ export const getCachedUserMediaStorage = unstable_cache(
|
||||
['admin-user-media-storage'],
|
||||
{ revalidate: STORAGE_CACHE_SECONDS }
|
||||
);
|
||||
|
||||
export const getCachedUserDownloadEgress = unstable_cache(
|
||||
async () => {
|
||||
const perUserDownloadEgress: Record<string, number> = {};
|
||||
try {
|
||||
const grouped = await db.downloadEgressEvent.groupBy({
|
||||
by: ['billedUserId'],
|
||||
_sum: {
|
||||
estimatedBytes: true,
|
||||
},
|
||||
});
|
||||
|
||||
for (const row of grouped) {
|
||||
perUserDownloadEgress[row.billedUserId] = row._sum.estimatedBytes
|
||||
? bigintToNumber(row._sum.estimatedBytes)
|
||||
: 0;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to calculate per-user download egress:', err);
|
||||
}
|
||||
|
||||
return perUserDownloadEgress;
|
||||
},
|
||||
['admin-user-download-egress'],
|
||||
{ revalidate: STORAGE_CACHE_SECONDS }
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user