committed

This commit is contained in:
2025-01-10 13:03:41 +05:30
parent 4ba496b8eb
commit 52f746308f
17 changed files with 1689 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
const express = require('express');
const router = express.Router();
const Notification = require('../models/notification');
router.get('/analytics', async (req, res) => {
try {
const totalSent = await Notification.countDocuments({ status: 'sent' });
const totalFailed = await Notification.countDocuments({ status: 'failed' });
const totalRetried = await Notification.countDocuments({ status: 'retried' });
const deliveryStats = {
totalSent,
totalFailed,
totalRetried
};
const sentNotifications = await Notification.find({ status: 'sent' });
const totalDeliveryTime = sentNotifications.reduce((sum, notification) => {
return sum + (notification.sentAt - notification.createdAt);
}, 0);
const averageDeliveryTime = totalDeliveryTime / sentNotifications.length;
const totalResponses = await Notification.countDocuments({ userResponded: true });
const responseRate = totalResponses / totalSent;
const userEngagement = {
averageDeliveryTime,
responseRate
};
res.json({
deliveryStats,
userEngagement
});
} catch (error) {
console.error('Error generating analytics:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
module.exports = router;