|
@@ -1,10 +1,10 @@
|
|
-import { Request, Response, NextFunction } from 'express';
|
|
|
|
-import { DrizzleError, eq } from 'drizzle-orm';
|
|
|
|
-import { sites, resources, targets, exitNodes } from '@server/db/schema';
|
|
|
|
-import db from '@server/db';
|
|
|
|
-import logger from '@server/logger';
|
|
|
|
-import createHttpError from 'http-errors';
|
|
|
|
-import HttpCode from '@server/types/HttpCode';
|
|
|
|
|
|
+import { Request, Response, NextFunction } from "express";
|
|
|
|
+import { DrizzleError, eq } from "drizzle-orm";
|
|
|
|
+import { sites, resources, targets, exitNodes } from "@server/db/schema";
|
|
|
|
+import db from "@server/db";
|
|
|
|
+import logger from "@server/logger";
|
|
|
|
+import createHttpError from "http-errors";
|
|
|
|
+import HttpCode from "@server/types/HttpCode";
|
|
import response from "@server/utils/response";
|
|
import response from "@server/utils/response";
|
|
|
|
|
|
interface PeerBandwidth {
|
|
interface PeerBandwidth {
|
|
@@ -13,62 +13,76 @@ interface PeerBandwidth {
|
|
bytesOut: number;
|
|
bytesOut: number;
|
|
}
|
|
}
|
|
|
|
|
|
-export const receiveBandwidth = async (req: Request, res: Response, next: NextFunction): Promise<any> => {
|
|
|
|
|
|
+export const receiveBandwidth = async (
|
|
|
|
+ req: Request,
|
|
|
|
+ res: Response,
|
|
|
|
+ next: NextFunction
|
|
|
|
+): Promise<any> => {
|
|
try {
|
|
try {
|
|
const bandwidthData: PeerBandwidth[] = req.body;
|
|
const bandwidthData: PeerBandwidth[] = req.body;
|
|
|
|
|
|
if (!Array.isArray(bandwidthData)) {
|
|
if (!Array.isArray(bandwidthData)) {
|
|
- throw new Error('Invalid bandwidth data');
|
|
|
|
|
|
+ throw new Error("Invalid bandwidth data");
|
|
}
|
|
}
|
|
|
|
|
|
- for (const peer of bandwidthData) {
|
|
|
|
- const { publicKey, bytesIn, bytesOut } = peer;
|
|
|
|
|
|
+ await db.transaction(async (trx) => {
|
|
|
|
+ for (const peer of bandwidthData) {
|
|
|
|
+ const { publicKey, bytesIn, bytesOut } = peer;
|
|
|
|
|
|
- // Find the site by public key
|
|
|
|
- const site = await db.query.sites.findFirst({
|
|
|
|
- where: eq(sites.pubKey, publicKey),
|
|
|
|
- });
|
|
|
|
|
|
+ // Find the site by public key
|
|
|
|
+ const site = await trx.query.sites.findFirst({
|
|
|
|
+ where: eq(sites.pubKey, publicKey)
|
|
|
|
+ });
|
|
|
|
|
|
- if (!site) {
|
|
|
|
- logger.warn(`Site not found for public key: ${publicKey}`);
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- let online = site.online;
|
|
|
|
-
|
|
|
|
- // if the bandwidth for the site is > 0 then set it to online. if it has been less than 0 (no update) for 5 minutes then set it to offline
|
|
|
|
- if (bytesIn > 0 || bytesOut > 0) {
|
|
|
|
- online = true;
|
|
|
|
- } else if (site.lastBandwidthUpdate) {
|
|
|
|
- const lastBandwidthUpdate = new Date(site.lastBandwidthUpdate);
|
|
|
|
- const currentTime = new Date();
|
|
|
|
- const diff = currentTime.getTime() - lastBandwidthUpdate.getTime();
|
|
|
|
- if (diff < 300000) {
|
|
|
|
- online = false;
|
|
|
|
|
|
+ if (!site) {
|
|
|
|
+ logger.warn(`Site not found for public key: ${publicKey}`);
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ let online = site.online;
|
|
|
|
|
|
- // Update the site's bandwidth usage
|
|
|
|
- await db.update(sites)
|
|
|
|
- .set({
|
|
|
|
- megabytesIn: (site.megabytesIn || 0) + bytesIn,
|
|
|
|
- megabytesOut: (site.megabytesOut || 0) + bytesOut,
|
|
|
|
- lastBandwidthUpdate: new Date().toISOString(),
|
|
|
|
- online,
|
|
|
|
- })
|
|
|
|
- .where(eq(sites.siteId, site.siteId));
|
|
|
|
|
|
+ // if the bandwidth for the site is > 0 then set it to online. if it has been less than 0 (no update) for 5 minutes then set it to offline
|
|
|
|
+ if (bytesIn > 0 || bytesOut > 0) {
|
|
|
|
+ online = true;
|
|
|
|
+ } else if (site.lastBandwidthUpdate) {
|
|
|
|
+ const lastBandwidthUpdate = new Date(
|
|
|
|
+ site.lastBandwidthUpdate
|
|
|
|
+ );
|
|
|
|
+ const currentTime = new Date();
|
|
|
|
+ const diff =
|
|
|
|
+ currentTime.getTime() - lastBandwidthUpdate.getTime();
|
|
|
|
+ if (diff < 300000) {
|
|
|
|
+ online = false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ // Update the site's bandwidth usage
|
|
|
|
+ await trx
|
|
|
|
+ .update(sites)
|
|
|
|
+ .set({
|
|
|
|
+ megabytesIn: (site.megabytesIn || 0) + bytesIn,
|
|
|
|
+ megabytesOut: (site.megabytesOut || 0) + bytesOut,
|
|
|
|
+ lastBandwidthUpdate: new Date().toISOString(),
|
|
|
|
+ online
|
|
|
|
+ })
|
|
|
|
+ .where(eq(sites.siteId, site.siteId));
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
|
|
return response(res, {
|
|
return response(res, {
|
|
data: {},
|
|
data: {},
|
|
success: true,
|
|
success: true,
|
|
error: false,
|
|
error: false,
|
|
message: "Organization retrieved successfully",
|
|
message: "Organization retrieved successfully",
|
|
- status: HttpCode.OK,
|
|
|
|
|
|
+ status: HttpCode.OK
|
|
});
|
|
});
|
|
} catch (error) {
|
|
} catch (error) {
|
|
- logger.error('Error updating bandwidth data:', error);
|
|
|
|
- return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred..."));
|
|
|
|
|
|
+ logger.error("Error updating bandwidth data:", error);
|
|
|
|
+ return next(
|
|
|
|
+ createHttpError(
|
|
|
|
+ HttpCode.INTERNAL_SERVER_ERROR,
|
|
|
|
+ "An error occurred..."
|
|
|
|
+ )
|
|
|
|
+ );
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|