feat: complete deployment codebase for BackOne DPI dashboard with dynamic telemetry, rebranding, database size tracking, and modular design

This commit is contained in:
Rafif-Riqullah-Siregar committed 2026-07-10 11:52:58 +07:00
1 parent 6cf3c6c7e5
commit c316f3171b
226 files changed
+21152 -8397

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
// backend/db/capacityTracker.js
// ─────────────────────────────────────────────────────────────────────────────
// MongoDB Capacity & Data Size Breakdown per Network Agent.
// Measures logical document sizes per agent_uuid across all collections.
// ─────────────────────────────────────────────────────────────────────────────
const mongoose = require('mongoose');
async function logCapacityStats(prefix = '[MongoDB]') {
try {
if (!mongoose.connection || !mongoose.connection.db) {
return;
}
const db = mongoose.connection.db;
// 1. Fetch overall dbStats
const stats = await db.command({ dbStats: 1 });
const dataSizeMB = (stats.dataSize / (1024 * 1024)).toFixed(2);
const storageSizeMB = (stats.storageSize / (1024 * 1024)).toFixed(2);
console.log(`${prefix} Capacity Used: Data Size = ${dataSizeMB} MB, Storage Size = ${storageSizeMB} MB`);
// 2. Fetch breakdown per Agent
const agentSizes = {};
const collections = await db.listCollections().toArray();
for (const colInfo of collections) {
const colName = colInfo.name;
if (colName.startsWith('system.')) continue;
const col = db.collection(colName);
// Check if collection contains at least one document with an agent_uuid field
const sampleDoc = await col.findOne({ agent_uuid: { $ne: null } });
if (!sampleDoc) continue;
const pipeline = [
{ $project: { agent_uuid: 1, docSize: { $bsonSize: "$$ROOT" } } },
{ $group: { _id: "$agent_uuid", totalBytes: { $sum: "$docSize" } } }
];
const results = await col.aggregate(pipeline).toArray();
for (const res of results) {
const agent = res._id || 'Unknown';
agentSizes[agent] = (agentSizes[agent] || 0) + res.totalBytes;
}
}
// 3. Format and log the breakdown
const sortedAgents = Object.entries(agentSizes)
.map(([agent, bytes]) => ({ agent, sizeMB: parseFloat((bytes / (1024 * 1024)).toFixed(2)) }))
.sort((a, b) => b.sizeMB - a.sizeMB);
if (sortedAgents.length > 0) {
console.log(`${prefix} Data Size Breakdown per Agent:`);
for (const { agent, sizeMB } of sortedAgents) {
console.log(` - ${agent}: ${sizeMB.toFixed(2)} MB`);
}
}
} catch (err) {
console.warn(`${prefix} Could not retrieve DB capacity breakdown:`, err.message);
}
}
module.exports = { logCapacityStats };
+30 -15
View File
@@ -1,27 +1,42 @@
const mongoose = require('mongoose');
// backend/db/mongoose.js
// Connects the backend to MongoDB.
// The backend is READ-ONLY — all writes are done by the proxy server.
// MongoDB URI is provided via MONGODB_URI environment variable.
const mongoose = require('mongoose');
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '../../.env.local') });
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/backone_dpi';
async function connectDB() {
try {
if (mongoose.connection.readyState >= 1) return;
await mongoose.connect(MONGODB_URI, { serverSelectionTimeoutMS: 2000 });
console.log('[MongoDB] Connected successfully');
} catch (error) {
console.error('[MongoDB] Connection failed, switching to In-Memory DB:', error.message);
if (mongoose.connection.readyState >= 1) return; // already connected
const MAX_RETRIES = 5;
const RETRY_DELAY = 5000;
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try {
const { MongoMemoryServer } = require('mongodb-memory-server');
const mongod = await MongoMemoryServer.create();
const uri = mongod.getUri();
await mongoose.connect(uri, {});
console.log('[MongoDB] Connected successfully to Virtual Memory Database!');
} catch (memErr) {
console.error('[MongoDB] Memory DB failed:', memErr.message);
console.log(`[MongoDB] Connecting... (attempt ${attempt}/${MAX_RETRIES})`);
await mongoose.connect(MONGODB_URI, {
serverSelectionTimeoutMS: 10000,
connectTimeoutMS: 10000,
});
console.log('[MongoDB] ✓ Connected successfully');
const { logCapacityStats } = require('./capacityTracker');
await logCapacityStats('[MongoDB]');
return;
} catch (error) {
console.error(`[MongoDB] ✗ Attempt ${attempt} failed: ${error.message}`);
if (attempt < MAX_RETRIES) {
console.log(`[MongoDB] Retrying in ${RETRY_DELAY / 1000}s...`);
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
}
}
}
console.error('[MongoDB] All connection attempts failed. Backend cannot serve dashboard data.');
// Do NOT exit — allow health check endpoint to remain available
}
module.exports = connectDB;