mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-12 05:36:38 +00:00
Break out bandwidth
This commit is contained in:
@@ -28,6 +28,27 @@ export const receiveBandwidth = async (
|
|||||||
throw new Error("Invalid bandwidth data");
|
throw new Error("Invalid bandwidth data");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await updateSiteBandwidth(bandwidthData);
|
||||||
|
|
||||||
|
return response(res, {
|
||||||
|
data: {},
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Bandwidth data updated successfully",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("Error updating bandwidth data:", error);
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||||||
|
"An error occurred..."
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function updateSiteBandwidth(bandwidthData: PeerBandwidth[]) {
|
||||||
const currentTime = new Date();
|
const currentTime = new Date();
|
||||||
const oneMinuteAgo = new Date(currentTime.getTime() - 60000); // 1 minute ago
|
const oneMinuteAgo = new Date(currentTime.getTime() - 60000); // 1 minute ago
|
||||||
|
|
||||||
@@ -35,11 +56,11 @@ export const receiveBandwidth = async (
|
|||||||
|
|
||||||
await db.transaction(async (trx) => {
|
await db.transaction(async (trx) => {
|
||||||
// First, handle sites that are actively reporting bandwidth
|
// First, handle sites that are actively reporting bandwidth
|
||||||
const activePeers = bandwidthData.filter(peer => peer.bytesIn > 0); // Bytesout will have data as it tries to send keep alive messages
|
const activePeers = bandwidthData.filter((peer) => peer.bytesIn > 0); // Bytesout will have data as it tries to send keep alive messages
|
||||||
|
|
||||||
if (activePeers.length > 0) {
|
if (activePeers.length > 0) {
|
||||||
// Remove any active peers from offline tracking since they're sending data
|
// Remove any active peers from offline tracking since they're sending data
|
||||||
activePeers.forEach(peer => offlineSites.delete(peer.publicKey));
|
activePeers.forEach((peer) => offlineSites.delete(peer.publicKey));
|
||||||
|
|
||||||
// Aggregate usage data by organization
|
// Aggregate usage data by organization
|
||||||
const orgUsageMap = new Map<string, number>();
|
const orgUsageMap = new Map<string, number>();
|
||||||
@@ -61,7 +82,7 @@ export const receiveBandwidth = async (
|
|||||||
online: sites.online,
|
online: sites.online,
|
||||||
orgId: sites.orgId,
|
orgId: sites.orgId,
|
||||||
siteId: sites.siteId,
|
siteId: sites.siteId,
|
||||||
lastBandwidthUpdate: sites.lastBandwidthUpdate,
|
lastBandwidthUpdate: sites.lastBandwidthUpdate
|
||||||
});
|
});
|
||||||
|
|
||||||
if (updatedSite.length > 0) {
|
if (updatedSite.length > 0) {
|
||||||
@@ -83,22 +104,29 @@ export const receiveBandwidth = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle sites that reported zero bandwidth but need online status updated
|
// Handle sites that reported zero bandwidth but need online status updated
|
||||||
const zeroBandwidthPeers = bandwidthData.filter(peer =>
|
const zeroBandwidthPeers = bandwidthData.filter(
|
||||||
peer.bytesIn === 0 && !offlineSites.has(peer.publicKey) // Bytesout will have data as it tries to send keep alive messages
|
(peer) => peer.bytesIn === 0 && !offlineSites.has(peer.publicKey) // Bytesout will have data as it tries to send keep alive messages
|
||||||
);
|
);
|
||||||
|
|
||||||
if (zeroBandwidthPeers.length > 0) {
|
if (zeroBandwidthPeers.length > 0) {
|
||||||
const zeroBandwidthSites = await trx
|
const zeroBandwidthSites = await trx
|
||||||
.select()
|
.select()
|
||||||
.from(sites)
|
.from(sites)
|
||||||
.where(inArray(sites.pubKey, zeroBandwidthPeers.map(p => p.publicKey)));
|
.where(
|
||||||
|
inArray(
|
||||||
|
sites.pubKey,
|
||||||
|
zeroBandwidthPeers.map((p) => p.publicKey)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
for (const site of zeroBandwidthSites) {
|
for (const site of zeroBandwidthSites) {
|
||||||
let newOnlineStatus = site.online;
|
let newOnlineStatus = site.online;
|
||||||
|
|
||||||
// Check if site should go offline based on last bandwidth update WITH DATA
|
// Check if site should go offline based on last bandwidth update WITH DATA
|
||||||
if (site.lastBandwidthUpdate) {
|
if (site.lastBandwidthUpdate) {
|
||||||
const lastUpdateWithData = new Date(site.lastBandwidthUpdate);
|
const lastUpdateWithData = new Date(
|
||||||
|
site.lastBandwidthUpdate
|
||||||
|
);
|
||||||
if (lastUpdateWithData < oneMinuteAgo) {
|
if (lastUpdateWithData < oneMinuteAgo) {
|
||||||
newOnlineStatus = false;
|
newOnlineStatus = false;
|
||||||
}
|
}
|
||||||
@@ -125,21 +153,4 @@ export const receiveBandwidth = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return response(res, {
|
|
||||||
data: {},
|
|
||||||
success: true,
|
|
||||||
error: false,
|
|
||||||
message: "Bandwidth data updated successfully",
|
|
||||||
status: HttpCode.OK
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
logger.error("Error updating bandwidth data:", error);
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.INTERNAL_SERVER_ERROR,
|
|
||||||
"An error occurred..."
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user