anaylitics dashboard
This commit is contained in:
70
map/app/controllers/dashboardController.js
Normal file
70
map/app/controllers/dashboardController.js
Normal file
@@ -0,0 +1,70 @@
|
||||
const nocodbService = require('../services/nocodb');
|
||||
const logger = require('../utils/logger');
|
||||
const config = require('../config');
|
||||
|
||||
class DashboardController {
|
||||
async getStats(req, res) {
|
||||
try {
|
||||
// Get all locations for support level stats
|
||||
const locationsResponse = await nocodbService.getAll(config.nocodb.tableId);
|
||||
const locations = locationsResponse.list || [];
|
||||
|
||||
// Calculate support level distribution
|
||||
const supportLevels = { '1': 0, '2': 0, '3': 0, '4': 0 };
|
||||
let signRequests = 0;
|
||||
|
||||
locations.forEach(loc => {
|
||||
if (loc['Support Level']) {
|
||||
supportLevels[loc['Support Level']]++;
|
||||
}
|
||||
if (loc.Sign || loc.sign) {
|
||||
signRequests++;
|
||||
}
|
||||
});
|
||||
|
||||
// Calculate overall score (weighted average)
|
||||
const totalResponses = Object.values(supportLevels).reduce((a, b) => a + b, 0);
|
||||
const weightedScore = (supportLevels['1'] * 4 + supportLevels['2'] * 3 +
|
||||
supportLevels['3'] * 2 + supportLevels['4'] * 1) /
|
||||
(totalResponses || 1);
|
||||
|
||||
// Get user stats
|
||||
const usersResponse = await nocodbService.getAll(config.nocodb.loginSheetId);
|
||||
const users = usersResponse.list || [];
|
||||
|
||||
// Get daily entry counts for the last 30 days
|
||||
const thirtyDaysAgo = new Date();
|
||||
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
|
||||
|
||||
const dailyEntries = {};
|
||||
locations.forEach(loc => {
|
||||
const createdAt = new Date(loc.CreatedAt || loc.created_at);
|
||||
if (createdAt >= thirtyDaysAgo) {
|
||||
const dateKey = createdAt.toISOString().split('T')[0];
|
||||
dailyEntries[dateKey] = (dailyEntries[dateKey] || 0) + 1;
|
||||
}
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
supportLevels,
|
||||
signRequests,
|
||||
totalLocations: locations.length,
|
||||
overallScore: weightedScore.toFixed(2),
|
||||
totalUsers: users.length,
|
||||
dailyEntries
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
logger.error('Error fetching dashboard stats:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Failed to fetch dashboard statistics'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new DashboardController();
|
||||
Reference in New Issue
Block a user