@@ -3,7 +3,7 @@ const router = express.Router();
33const User = require ( '../models/user' ) ;
44const checkAuth = require ( '../services/checkauth' ) ;
55const { getAllDvrs } = require ( "../controllers/dvrController" ) ;
6- const { activeStreams } = require ( '../utils/streamManager' ) ;
6+ const dvrManager = require ( '../utils/streamManager' ) ;
77const bcrypt = require ( 'bcrypt' ) ;
88require ( 'dotenv' ) . config ( ) ;
99
@@ -23,55 +23,46 @@ router.get('/logout', checkAuth, (req, res) => {
2323// routes/dashboard.js
2424router . get ( '/dashboard' , checkAuth , async ( req , res ) => {
2525 try {
26- // 1. Fetch all DVRs from your database
26+ // 1. Fetch all DVRs from DB
2727 const dvrs = await getAllDvrs ( ) ;
2828
29- // 2. Group all currently active streams by their DVR ID
30- const activeDvrsSummary = Object . values ( activeStreams ) . reduce ( ( acc , stream ) => {
31- const dvrId = stream . camera_details . dvr_id ;
32-
33- if ( ! acc [ dvrId ] ) {
34- // Initialize the entry if it's the first camera from this DVR
35- acc [ dvrId ] = {
36- dvr_id : dvrId ,
37- activeCameraCount : 0 ,
38- lastActivity : 0
39- } ;
40- }
41-
42- acc [ dvrId ] . activeCameraCount ++ ;
43- if ( stream . lastAccess > acc [ dvrId ] . lastActivity ) {
44- acc [ dvrId ] . lastActivity = stream . lastAccess ;
45- }
46-
47- return acc ;
48- } , { } ) ;
49-
50- // 3. Create a new array containing ONLY active DVRs, but with their FULL details
29+ // 2. Build active DVR summary from Map
30+ const activeDvrsSummary = { } ;
31+
32+ for ( const [ dvrId , streamInstance ] of dvrManager . streams . entries ( ) ) {
33+ activeDvrsSummary [ dvrId ] = {
34+ dvr_id : dvrId ,
35+ activeCameraCount : streamInstance . activeCameraCount || 1 ,
36+ lastActivity : streamInstance . lastAccess || Date . now ( ) ,
37+ } ;
38+ }
39+
40+ // 3. Merge active DVRs with full DVR details
5141 const activeDvrsWithDetails = dvrs
52- . filter ( dvr => activeDvrsSummary [ dvr . id ] ) // Keep only DVRs that have an entry in the summary
53- . map ( dvr => {
54- // Merge the full DVR object with its corresponding activity summary
55- return {
56- ...dvr , // e.g., { id, dvr_name, location_name, ... }
57- ...activeDvrsSummary [ dvr . id ] // e.g., { activeCameraCount, lastActivity }
58- } ;
59- } ) ;
60-
61- // 4. Calculate final statistics for the dashboard cards
42+ . filter ( dvr => activeDvrsSummary [ dvr . id ] )
43+ . map ( dvr => ( {
44+ ...dvr ,
45+ ...activeDvrsSummary [ dvr . id ] ,
46+ } ) ) ;
47+
48+ // 4. Dashboard statistics
6249 const total_dvrs = dvrs . length ;
63- const total_cameras = dvrs . reduce ( ( count , dvr ) => count + ( dvr . total_cameras || 0 ) , 0 ) ;
64- const active_streams_count = Object . keys ( activeStreams ) . length ;
50+ const total_cameras = dvrs . reduce (
51+ ( count , dvr ) => count + ( dvr . total_cameras || 0 ) ,
52+ 0
53+ ) ;
54+
55+ const active_streams = dvrManager . streams . size ;
6556
66- // 5. Render the EJS template with all the necessary data
57+ // 5. Render dashboard
6758 res . render ( "dashboard" , {
6859 title : "Dashboard" ,
6960 user : req . user ,
70- total_dvrs : total_dvrs ,
71- dvrs : dvrs , // Full list of all DVRs
72- total_cameras : total_cameras ,
73- active_streams : active_streams_count ,
74- activeDvrs : activeDvrsWithDetails , // The final, detailed list of active DVRs
61+ total_dvrs,
62+ total_cameras ,
63+ active_streams ,
64+ dvrs ,
65+ activeDvrs : activeDvrsWithDetails ,
7566 } ) ;
7667
7768 } catch ( error ) {
0 commit comments