diff --git a/apps/vpnmanager/src/lib/statistics.ts b/apps/vpnmanager/src/lib/statistics.ts index f176c9605..d4b994e9b 100644 --- a/apps/vpnmanager/src/lib/statistics.ts +++ b/apps/vpnmanager/src/lib/statistics.ts @@ -2,37 +2,10 @@ import { NextApiRequest } from "next/types"; import { OutlineVPN } from "./outline"; import { Filters, Model, Record } from "@/vpnmanager/lib/data/database"; -interface UserDataUsage { - outlineId: string | number; - usage: number; -} - const vpnManager = new OutlineVPN({ apiUrl: process.env.NEXT_APP_VPN_API_URL as string, }); -function calculateDailyDataUsage(userData: UserDataUsage) { - if (!userData) { - return 0; - } - - const { usage, outlineId } = userData; - const yesterday = new Date(); - yesterday.setDate(yesterday.getDate() - 1); - - const lastWeek = new Date(); - lastWeek.setDate(yesterday.getDate() - 7); - const [res] = Model.getAll({ - orderBy: "date DESC", - date: { - start: `${lastWeek.getFullYear()}-${lastWeek.getMonth() + 1}-${lastWeek.getDate()}`, - end: `${yesterday.getFullYear()}-${yesterday.getMonth() + 1}-${yesterday.getDate()}`, - }, - userId: outlineId?.toString(), - }) as Record[]; - return usage - (res?.cumulativeData || 0); -} - export async function processUserStats() { const date = `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()}`; const { bytesTransferredByUserId = {} } = await vpnManager.getDataUsage(); @@ -43,10 +16,7 @@ export async function processUserStats() { const userDetails = allUsers.find(({ id }) => id === key); const newData = { userId: key, - usage: calculateDailyDataUsage({ - outlineId: key, - usage: bytesTransferredByUserId[key], - }), + usage: Math.ceil(bytesTransferredByUserId[key] / 30), date, cumulativeData: bytesTransferredByUserId[key], email: userDetails?.name || "",