48hr.email/api/routes/stats.js
ClaraCrazy fb3d8a60aa
[AI][Feat]: Add API
Also adding API docs <3
2026-01-05 10:29:12 +01:00

54 lines
1.4 KiB
JavaScript

const express = require('express')
/**
* Statistics API Routes
* GET / - Get lightweight statistics
* GET /enhanced - Get full statistics with historical data
*/
function createStatsRouter(dependencies) {
const router = express.Router()
const { statisticsStore, mailProcessingService, imapService, config } = dependencies
if (!config.http.statisticsEnabled) {
router.all('*', (req, res) => {
res.apiError('Statistics are disabled', 'FEATURE_DISABLED', 503)
})
return router
}
/**
* GET / - Get lightweight statistics (no historical analysis)
*/
router.get('/', async(req, res, next) => {
try {
const stats = statisticsStore.getLightweightStats()
res.apiSuccess(stats)
} catch (error) {
next(error)
}
})
/**
* GET /enhanced - Get full statistics with historical data
*/
router.get('/enhanced', async(req, res, next) => {
try {
// Analyze all existing emails for historical data
if (mailProcessingService) {
const allMails = mailProcessingService.getAllMailSummaries()
statisticsStore.analyzeHistoricalData(allMails)
}
const stats = statisticsStore.getEnhancedStats()
res.apiSuccess(stats)
} catch (error) {
next(error)
}
})
return router
}
module.exports = createStatsRouter