feat(dashboard): disponibilidad por piso, métricas reales, auditoría y UI/UX v3.8.17

This commit is contained in:
2026-10-26 15:33:02 -03:00
parent 91ea4bbfc7
commit 6e0ab7faaa
17 changed files with 618 additions and 204 deletions
+120 -44
View File
@@ -261,12 +261,33 @@ const handleOccupancyMetrics = async (req, res) => {
console.warn('API error fetching metrics data:', apiErr.message);
}
const physicalRooms = allClassrooms.filter(c => !c.is_virtual && (!c.code || !c.code.includes('VIRTUAL')));
const virtualRooms = allClassrooms.filter(c => c.is_virtual || (c.code && c.code.includes('VIRTUAL')));
const activeReservations = allReservations.filter(r => (r.status || '').toUpperCase() !== 'CANCELLED');
const physical_rooms_count = physicalRooms.length || 29;
const virtualRoomIds = new Set(
allClassrooms.filter(c => c.is_virtual || (c.code && c.code.toUpperCase().includes('VIRTUAL')) || (c.building && c.building.toUpperCase().includes('VIRTUAL'))).map(c => c.id)
);
const physicalRooms = allClassrooms.filter(c => !virtualRoomIds.has(c.id));
const physical_rooms_count = physicalRooms.length;
// Shift counts
const virtualReservations = activeReservations.filter(r => virtualRoomIds.has(r.classroom_id));
const physicalReservations = activeReservations.filter(r => !virtualRoomIds.has(r.classroom_id));
const total_month_classes = activeReservations.length;
const v_month = virtualReservations.length;
const todayStr = new Date().toISOString().slice(0, 10);
const now = new Date();
const startOfWeek = new Date(now);
startOfWeek.setDate(now.getDate() - ((now.getDay() + 6) % 7));
startOfWeek.setHours(0, 0, 0, 0);
const v_today = virtualReservations.filter(r => r.start_time && r.start_time.startsWith(todayStr)).length;
const v_week = virtualReservations.filter(r => {
if (!r.start_time) return false;
return new Date(r.start_time) >= startOfWeek;
}).length;
// Turnos reales
const shift_counts = {
Mañana: 0,
Tarde: 0,
@@ -274,51 +295,106 @@ const handleOccupancyMetrics = async (req, res) => {
Noche: 0
};
allReservations.forEach(r => {
const shift = r.shift || 'Noche';
if (shift_counts[shift] !== undefined) {
shift_counts[shift]++;
activeReservations.forEach(r => {
let s = r.shift;
if (!s && r.start_time) {
const timePart = r.start_time.includes('T') ? r.start_time.split('T')[1] : (r.start_time.split(' ')[1] || '00:00');
const h = parseInt(timePart.split(':')[0], 10);
if (h < 13) s = 'Mañana';
else if (h < 18) s = 'Tarde';
else if (h < 20) s = 'Vespertino';
else s = 'Noche';
}
if (s && shift_counts[s] !== undefined) {
shift_counts[s]++;
} else {
shift_counts['Noche']++;
shift_counts.Noche++;
}
});
// Ensure reasonable minimums for visualization
if (allReservations.length === 0) {
shift_counts.Mañana = 28;
shift_counts.Tarde = 22;
shift_counts.Vespertino = 15;
shift_counts.Noche = 20;
// Agrupación por piso real
const floorMap = {};
physicalRooms.forEach(c => {
const rawFloor = c.floor !== null && c.floor !== undefined ? String(c.floor).trim() : '0';
let floorLabel = rawFloor;
if (rawFloor === '0' || rawFloor.toUpperCase() === 'PB' || rawFloor.toLowerCase().includes('baja')) {
floorLabel = 'Planta Baja';
} else if (!isNaN(rawFloor) && !rawFloor.toLowerCase().includes('piso')) {
floorLabel = `Piso ${rawFloor}`;
}
if (!floorMap[floorLabel]) {
floorMap[floorLabel] = { floor: floorLabel, count: 0, res_count: 0, room_ids: new Set() };
}
floorMap[floorLabel].count++;
floorMap[floorLabel].room_ids.add(c.id);
});
physicalReservations.forEach(r => {
for (const fKey in floorMap) {
if (floorMap[fKey].room_ids.has(r.classroom_id)) {
floorMap[fKey].res_count++;
break;
}
}
});
const floor_summary = Object.values(floorMap).map(f => {
const baseCapacity = Math.max(1, f.count * 15);
const avg_pct = Math.min(100, Math.round((f.res_count / baseCapacity) * 100));
return {
floor: f.floor,
count: f.count,
res_count: f.res_count,
avg_pct: avg_pct
};
});
floor_summary.sort((a, b) => a.floor.localeCompare(b.floor, 'es', { numeric: true }));
// Métricas globales reales
const totalPhysicalCapacityMonth = Math.max(1, physical_rooms_count * 15);
const avg_month_pct = physical_rooms_count > 0 ? Math.min(100, Math.round((physicalReservations.length / totalPhysicalCapacityMonth) * 100)) : 0;
const pTodayRes = physicalReservations.filter(r => r.start_time && r.start_time.startsWith(todayStr)).length;
const pWeekRes = physicalReservations.filter(r => {
if (!r.start_time) return false;
return new Date(r.start_time) >= startOfWeek;
}).length;
const avg_day_pct = physical_rooms_count > 0 ? Math.min(100, Math.round((pTodayRes / Math.max(1, physical_rooms_count * 3)) * 100)) : 0;
const avg_week_pct = physical_rooms_count > 0 ? Math.min(100, Math.round((pWeekRes / totalPhysicalCapacityMonth) * 100)) : 0;
// Top y Low rooms reales
const roomReservationsCount = {};
physicalReservations.forEach(r => {
roomReservationsCount[r.classroom_id] = (roomReservationsCount[r.classroom_id] || 0) + 1;
});
const calculatedRooms = physicalRooms.map(c => {
const rawFloor = c.floor !== null && c.floor !== undefined ? String(c.floor).trim() : '0';
let floor_display = rawFloor;
if (rawFloor === '0' || rawFloor.toUpperCase() === 'PB' || rawFloor.toLowerCase().includes('baja')) {
floor_display = 'PB';
} else if (!isNaN(rawFloor) && !rawFloor.toLowerCase().includes('piso')) {
floor_display = `Piso ${rawFloor}`;
}
const count = roomReservationsCount[c.id] || 0;
const month_pct = Math.min(100, Math.round((count / 15) * 100));
return {
classroom: {
id: c.id,
code: c.code || c.name || `Aula ${c.id}`,
floor_display: floor_display,
capacity: c.capacity || 30
},
month_count: count,
month_pct: month_pct
};
});
calculatedRooms.sort((a, b) => b.month_count - a.month_count);
const top_rooms = calculatedRooms.filter(r => r.month_count > 0);
if (top_rooms.length === 0 && calculatedRooms.length > 0) {
top_rooms.push(...calculatedRooms.slice(0, 4));
}
const total_month_classes = allReservations.length > 0 ? (allReservations.length * 4) : 85;
const v_month = Math.round(total_month_classes * 0.28);
const v_week = Math.round(v_month / 4);
const v_today = Math.round(v_week / 5) || 2;
const avg_day_pct = 64;
const avg_week_pct = 72;
const avg_month_pct = 68;
const floor_summary = [
{ floor: 'Planta Baja', count: 8, avg_pct: 78 },
{ floor: 'Piso 1', count: 10, avg_pct: 65 },
{ floor: 'Piso 2', count: 7, avg_pct: 54 },
{ floor: 'Piso 3', count: 4, avg_pct: 42 }
];
const top_rooms = [
{ classroom: { id: 1, code: 'Aula 101', floor_display: 'Piso 1', capacity: 45 }, month_pct: 88, month_count: 32 },
{ classroom: { id: 2, code: 'Aula Magna', floor_display: 'PB', capacity: 120 }, month_pct: 82, month_count: 28 },
{ classroom: { id: 3, code: 'Lab Informática 1', floor_display: 'Piso 2', capacity: 30 }, month_pct: 79, month_count: 26 },
{ classroom: { id: 4, code: 'Aula 204', floor_display: 'Piso 2', capacity: 35 }, month_pct: 75, month_count: 24 }
];
const low_rooms = [
{ classroom: { id: 5, code: 'Aula 302', floor_display: 'Piso 3', capacity: 25 }, month_pct: 18, month_count: 6 },
{ classroom: { id: 6, code: 'Seminario B', floor_display: 'Piso 3', capacity: 20 }, month_pct: 22, month_count: 7 },
{ classroom: { id: 7, code: 'Taller 1', floor_display: 'PB', capacity: 30 }, month_pct: 28, month_count: 9 }
];
const low_rooms = [...calculatedRooms].sort((a, b) => a.month_count - b.month_count);
res.render('admin/metrics/occupancy', {
title: 'Gestión de Ocupación y Métricas - Edu-Space Admin',