fix(dashboard): pinpoint zoom-scaled Agent red markers, city-center blue markers, and standardize netify./siab. to backone. everywhere
This commit is contained in:
1 parent
2482e0c670
commit
87dc19f63f
10 files changed
+130
-146
No files matched your search
@@ -34,7 +34,14 @@ module.exports = async function appDetailsHandler(req, res, helpers) {
|
||||
if (agentUuid) baseFilter.agent_uuid = agentUuid;
|
||||
|
||||
// ── Step 1: Query DeviceAppStat (Primary source for per-device bandwidth per-app) ──
|
||||
const queryFilter = { ...baseFilter, app_label: label };
|
||||
const baseName = label.replace(/^(?:backone|netify|siab|nexus)\./i, '');
|
||||
const labelVariants = Array.from(new Set([
|
||||
label,
|
||||
`netify.${baseName}`,
|
||||
`backone.${baseName}`,
|
||||
`siab.${baseName}`,
|
||||
]));
|
||||
const queryFilter = { ...baseFilter, app_label: { $in: labelVariants } };
|
||||
const deviceApps = await DeviceAppStat.find(queryFilter).sort({ timestamp: -1 }).lean();
|
||||
|
||||
if (deviceApps.length > 0) {
|
||||
@@ -75,7 +82,7 @@ module.exports = async function appDetailsHandler(req, res, helpers) {
|
||||
// Enrich domain & protocol info from Flow if available
|
||||
const flows = await Flow.find({
|
||||
...baseFilter,
|
||||
app_label: label
|
||||
app_label: { $in: labelVariants }
|
||||
}).sort({ timestamp: -1 }).limit(100).lean();
|
||||
|
||||
flows.forEach(f => {
|
||||
@@ -90,7 +97,7 @@ module.exports = async function appDetailsHandler(req, res, helpers) {
|
||||
.sort((a, b) => (b.download + b.upload) - (a.download + a.upload));
|
||||
|
||||
// Ambil total download/upload dari sum AppStat over the time range
|
||||
const appStats = await AppStat.find({ ...baseFilter, app_label: label }).lean();
|
||||
const appStats = await AppStat.find({ ...baseFilter, app_label: { $in: labelVariants } }).lean();
|
||||
const totalDl = appStats.reduce((s, x) => s + (x.download || 0), 0);
|
||||
const totalUl = appStats.reduce((s, x) => s + (x.upload || 0), 0);
|
||||
|
||||
|
||||
+19
-56
@@ -10,66 +10,25 @@ const axios = require('axios');
|
||||
|
||||
const PROXY_URL = process.env.PROXY_URL || 'http://localhost:4010';
|
||||
|
||||
// ─── Rebranding Helper (Memory Safe & Fast) ──────────────────────────────────
|
||||
const BRAND_NAMES = {
|
||||
'1959bb55_045b_47c7_bbdd_f33b7db197b9': 'Office',
|
||||
'6681452d_9cae_4ff4_8ae8_0d504774265e': 'SIAB',
|
||||
'default': 'BackOne'
|
||||
};
|
||||
|
||||
function getBrandNameForRequest(req) {
|
||||
const requestedSiteUuid = req.headers['x-backone-site-uuid'];
|
||||
const userSiteUuid = req.user?.site_uuid;
|
||||
|
||||
const siteUuid = (req.user?.role === 'SUPER_ADMIN' || !userSiteUuid || userSiteUuid === 'default')
|
||||
? (requestedSiteUuid || 'default')
|
||||
: userSiteUuid;
|
||||
|
||||
return BRAND_NAMES[siteUuid] || 'BackOne';
|
||||
}
|
||||
|
||||
function rebrandString(str, brandName) {
|
||||
function rebrandString(str) {
|
||||
if (typeof str !== 'string') return str;
|
||||
|
||||
if (brandName === 'Nexus') {
|
||||
return str
|
||||
.replace(/netify\.unclassified/gi, 'nexus.unclassified')
|
||||
.replace(/netify\.(?!ai)/gi, 'nexus.')
|
||||
.replace(/Netify's/g, "Nexus'")
|
||||
.replace(/netify's/g, "nexus'")
|
||||
.replace(/Netify(?!(\.ai))/g, 'Nexus')
|
||||
.replace(/netify(?!(\.ai))/g, 'nexus')
|
||||
.replace(/BackOne's/g, "Nexus'")
|
||||
.replace(/backone's/g, "nexus'")
|
||||
.replace(/BackOne/g, 'Nexus')
|
||||
.replace(/backone/g, 'nexus')
|
||||
.replace(/PT\.?\s*Data\s*Bisnis\s*Solusi/g, 'PT. Nexus Solusi');
|
||||
} else if (brandName === 'SIAB') {
|
||||
return str
|
||||
.replace(/netify\.unclassified/gi, 'siab.unclassified')
|
||||
.replace(/netify\.(?!ai)/gi, 'siab.')
|
||||
.replace(/Netify's/g, "SIAB's")
|
||||
.replace(/netify's/g, "siab's")
|
||||
.replace(/Netify(?!(\.ai))/g, 'SIAB')
|
||||
.replace(/netify(?!(\.ai))/g, 'siab');
|
||||
}
|
||||
|
||||
// Default (BackOne)
|
||||
// Standardize all netify./siab./nexus. prefixes across protocols, apps, and domains to backone.
|
||||
return str
|
||||
.replace(/netify\.unclassified/gi, 'backone.unclassified')
|
||||
.replace(/netify\.(?!ai)/gi, 'backone.')
|
||||
.replace(/Netify's/g, "BackOne's")
|
||||
.replace(/netify's/g, "backone's")
|
||||
.replace(/\b(?:netify|siab|nexus)\.unclassified/gi, 'backone.unclassified')
|
||||
.replace(/\b(?:netify|siab|nexus)\.(?!ai)/gi, 'backone.')
|
||||
.replace(/(?:Netify|SIAB)'s/g, "BackOne's")
|
||||
.replace(/(?:netify|siab)'s/g, "backone's")
|
||||
.replace(/Netify(?!(\.ai))/g, 'BackOne')
|
||||
.replace(/netify(?!(\.ai))/g, 'backone');
|
||||
}
|
||||
|
||||
function rebrandObj(obj, brandName) {
|
||||
function rebrandObj(obj) {
|
||||
if (obj === null || obj === undefined) return obj;
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
for (let i = 0; i < obj.length; i++) {
|
||||
obj[i] = rebrandObj(obj[i], brandName);
|
||||
obj[i] = rebrandObj(obj[i]);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
@@ -78,9 +37,9 @@ function rebrandObj(obj, brandName) {
|
||||
for (const key in obj) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
if (typeof obj[key] === 'string') {
|
||||
obj[key] = rebrandString(obj[key], brandName);
|
||||
obj[key] = rebrandString(obj[key]);
|
||||
} else if (typeof obj[key] === 'object') {
|
||||
obj[key] = rebrandObj(obj[key], brandName);
|
||||
obj[key] = rebrandObj(obj[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,20 +47,21 @@ function rebrandObj(obj, brandName) {
|
||||
}
|
||||
|
||||
if (typeof obj === 'string') {
|
||||
return rebrandString(obj, brandName);
|
||||
return rebrandString(obj);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
// ─── Rebranding Middleware ────────────────────────────────────────────────────
|
||||
router.use((req, res, next) => {
|
||||
const brandName = getBrandNameForRequest(req);
|
||||
function rebrandMiddleware(req, res, next) {
|
||||
if (res._rebrandAttached) return next();
|
||||
res._rebrandAttached = true;
|
||||
const originalJson = res.json.bind(res);
|
||||
res.json = function (body) {
|
||||
if (body) {
|
||||
try {
|
||||
body = rebrandObj(body, brandName);
|
||||
body = rebrandObj(body);
|
||||
} catch (err) {
|
||||
console.error('[Dashboard] Rebrand error:', err.message);
|
||||
}
|
||||
@@ -109,7 +69,9 @@ router.use((req, res, next) => {
|
||||
return originalJson(body);
|
||||
};
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
router.use(rebrandMiddleware);
|
||||
|
||||
// POST /api/dashboard/refresh
|
||||
router.post('/refresh', async (req, res) => {
|
||||
@@ -140,4 +102,5 @@ router.use(require('./dashboard/tenantConfig'));
|
||||
router.use(require('./dashboard/agentLocations'));
|
||||
router.use(require('./dashboard/blacklist'));
|
||||
|
||||
router.rebrandMiddleware = rebrandMiddleware;
|
||||
module.exports = router;
|
||||
@@ -6,14 +6,8 @@ const { getTimeFilter } = require('./helpers');
|
||||
// ─── 1. GET /api/dashboard/agent-locations ──────────────────────────────────────
|
||||
router.get('/agent-locations', async (req, res) => {
|
||||
try {
|
||||
const isGlobalUser = req.user?.role === 'SUPER_ADMIN' ||
|
||||
((!req.user?.site_uuid || req.user?.site_uuid === 'default') && ['SOC_ANALYST', 'ENGINEER'].includes(req.user?.role));
|
||||
|
||||
let query = {};
|
||||
if (!isGlobalUser && req.user?.site_uuid) {
|
||||
query.site_uuid = req.user.site_uuid;
|
||||
}
|
||||
if (req.user?.role === 'AGENT_VIEWER' && req.user?.agent_uuid) {
|
||||
if (req.user?.role === 'AGENT_VIEWER' && req.user?.agent_uuid && !req.user?._viewAsMode) {
|
||||
query.agent_uuid = req.user.agent_uuid;
|
||||
}
|
||||
|
||||
@@ -28,7 +22,8 @@ router.get('/agent-locations', async (req, res) => {
|
||||
// ─── 2. POST /api/dashboard/agent-locations ─────────────────────────────────────
|
||||
router.post('/agent-locations', async (req, res) => {
|
||||
try {
|
||||
if (req.user?.role !== 'SUPER_ADMIN' && req.user?.role !== 'TENANT_ADMIN') {
|
||||
const effectiveRole = req.user?._originalRole || req.user?.role;
|
||||
if (effectiveRole !== 'SUPER_ADMIN' && effectiveRole !== 'TENANT_ADMIN') {
|
||||
return res.status(403).json({ ok: false, error: 'Only administrators can configure agent geolocations.' });
|
||||
}
|
||||
const { agent_uuid, latitude, longitude, label } = req.body;
|
||||
@@ -36,35 +31,11 @@ router.post('/agent-locations', async (req, res) => {
|
||||
return res.status(400).json({ ok: false, error: 'agent_uuid, latitude, and longitude are required' });
|
||||
}
|
||||
|
||||
// Determine site_uuid
|
||||
let siteUuid = null;
|
||||
const isGlobalUser = req.user?.role === 'SUPER_ADMIN' ||
|
||||
((!req.user?.site_uuid || req.user?.site_uuid === 'default') && ['SOC_ANALYST', 'ENGINEER'].includes(req.user?.role));
|
||||
|
||||
if (!isGlobalUser && req.user?.site_uuid) {
|
||||
const agentBelongs = await Summary.findOne({ agent_uuid, site_uuid: req.user.site_uuid });
|
||||
if (!agentBelongs) {
|
||||
return res.status(403).json({ ok: false, error: 'Unauthorized: This agent does not belong to your tenant.' });
|
||||
}
|
||||
siteUuid = req.user.site_uuid;
|
||||
} else {
|
||||
// Find the site_uuid from Summary collection for this agent
|
||||
const summaryDoc = await Summary.findOne({ agent_uuid });
|
||||
if (summaryDoc) {
|
||||
siteUuid = summaryDoc.site_uuid;
|
||||
} else {
|
||||
// Fallback or use standard env site_uuid
|
||||
siteUuid = process.env.BACKONE_SITE_UUID || process.env.NETIFY_SITE_UUID || '6681452d_9cae_4ff4_8ae8_0d504774265e';
|
||||
}
|
||||
}
|
||||
|
||||
const findQuery = { agent_uuid };
|
||||
if (!isGlobalUser && req.user?.site_uuid) {
|
||||
findQuery.site_uuid = req.user.site_uuid;
|
||||
}
|
||||
const summaryDoc = await Summary.findOne({ agent_uuid }).lean();
|
||||
const siteUuid = summaryDoc?.site_uuid || process.env.BACKONE_SITE_UUID || process.env.NETIFY_SITE_UUID || '6681452d_9cae_4ff4_8ae8_0d504774265e';
|
||||
|
||||
const upserted = await CustomAgentLocation.findOneAndUpdate(
|
||||
findQuery,
|
||||
{ agent_uuid },
|
||||
{
|
||||
agent_uuid,
|
||||
site_uuid: siteUuid,
|
||||
@@ -85,20 +56,12 @@ router.post('/agent-locations', async (req, res) => {
|
||||
// ─── 3. DELETE /api/dashboard/agent-locations/:agent_uuid ────────────────────────
|
||||
router.delete('/agent-locations/:agent_uuid', async (req, res) => {
|
||||
try {
|
||||
if (req.user?.role !== 'SUPER_ADMIN' && req.user?.role !== 'TENANT_ADMIN') {
|
||||
const effectiveRole = req.user?._originalRole || req.user?.role;
|
||||
if (effectiveRole !== 'SUPER_ADMIN' && effectiveRole !== 'TENANT_ADMIN') {
|
||||
return res.status(403).json({ ok: false, error: 'Only administrators can delete agent geolocations.' });
|
||||
}
|
||||
const { agent_uuid } = req.params;
|
||||
|
||||
let query = { agent_uuid };
|
||||
const isGlobalUser = req.user?.role === 'SUPER_ADMIN' ||
|
||||
((!req.user?.site_uuid || req.user?.site_uuid === 'default') && ['SOC_ANALYST', 'ENGINEER'].includes(req.user?.role));
|
||||
|
||||
if (!isGlobalUser && req.user?.site_uuid) {
|
||||
query.site_uuid = req.user.site_uuid;
|
||||
}
|
||||
|
||||
const resDelete = await CustomAgentLocation.deleteOne(query);
|
||||
const resDelete = await CustomAgentLocation.deleteOne({ agent_uuid });
|
||||
res.json({ ok: true, deleted: resDelete.deletedCount > 0 });
|
||||
} catch (err) {
|
||||
console.error('[DELETE /agent-locations]', err.message);
|
||||
|
||||
@@ -77,6 +77,7 @@ const {
|
||||
|
||||
// ─── Protected Dashboard Routes ───────────────────────────────────────────────
|
||||
const dashboardRoutes = require('./routes/dashboard');
|
||||
app.use('/api/dashboard', dashboardRoutes.rebrandMiddleware);
|
||||
|
||||
// Override /api/dashboard/app-details to show real-time device mapping per application
|
||||
app.get('/api/dashboard/app-details', requireAuth, (req, res) => {
|
||||
|
||||
Reference in new issue
Block a user