Adds in dashboard page showing basic Mantis statistics

This commit is contained in:
Cameron Redmore 2025-04-26 09:32:59 +01:00
parent 7564937faa
commit 92230f8a07
13 changed files with 595 additions and 19 deletions

View file

@ -3,6 +3,7 @@ import { PrismaClient } from '@prisma/client'; // Import Prisma Client
import { getMantisSettings, saveTicketToDatabase } from '../services/mantisDownloader.js';
import axios from 'axios';
import reader from '@kenjiuno/msgreader';
import SuperJSON from 'superjson';
const MsgReader = reader.default;
const prisma = new PrismaClient(); // Instantiate Prisma Client
@ -281,4 +282,78 @@ router.get('/msg-extract/:ticketId/:attachmentId/:innerAttachmentId', async(req,
}
});
// GET /mantis/stats/issues - Get daily count of new Mantis issues (last 7 days)
router.get('/stats/issues', async(req, res) =>
{
try
{
// Calculate the date range (last 7 days)
const endDate = new Date();
endDate.setHours(23, 59, 59, 999); // End of today
const startDate = new Date();
startDate.setDate(startDate.getDate() - 6); // 7 days ago
startDate.setHours(0, 0, 0, 0); // Start of the day
// Query for daily counts of issues created in the last 7 days
const dailyIssues = await prisma.$queryRaw`
SELECT
DATE(created_at) as date,
COUNT(*) as count
FROM
"MantisIssue"
WHERE
created_at >= ${startDate} AND created_at <= ${endDate}
GROUP BY
DATE(created_at)
ORDER BY
date ASC
`;
res.status(200).json(dailyIssues);
}
catch (error)
{
console.error('Error fetching Mantis issue statistics:', error.message);
res.status(500).json({ error: 'Failed to fetch Mantis issue statistics' });
}
});
// GET /mantis/stats/comments - Get daily count of new Mantis comments (last 7 days)
router.get('/stats/comments', async(req, res) =>
{
try
{
// Calculate the date range (last 7 days)
const endDate = new Date();
endDate.setHours(23, 59, 59, 999); // End of today
const startDate = new Date();
startDate.setDate(startDate.getDate() - 6); // 7 days ago
startDate.setHours(0, 0, 0, 0); // Start of the day
// Query for daily counts of comments created in the last 7 days
const dailyComments = await prisma.$queryRaw`
SELECT
DATE(created_at) as date,
COUNT(*) as count
FROM
"MantisComment"
WHERE
created_at >= ${startDate} AND created_at <= ${endDate}
GROUP BY
DATE(created_at)
ORDER BY
date ASC
`;
res.status(200).json(dailyComments);
}
catch (error)
{
console.error('Error fetching Mantis comment statistics:', error.message);
res.status(500).json({ error: 'Failed to fetch Mantis comment statistics' });
}
});
export default router;

View file

@ -22,7 +22,7 @@ import authRoutes from './routes/auth.js';
import chatRoutes from './routes/chat.js';
import settingsRoutes from './routes/settings.js';
import userPreferencesRoutes from './routes/userPreferences.js';
import mantisRoutes from './routes/mantis.js'; // Import Mantis routes
import mantisRoutes from './routes/mantis.js';
import cron from 'node-cron';
import { generateAndStoreMantisSummary } from './services/mantisSummarizer.js';
import { requireAuth } from './middlewares/authMiddleware.js';
@ -30,6 +30,7 @@ import { requireAuth } from './middlewares/authMiddleware.js';
import { setup as setupMantisDownloader } from './services/mantisDownloader.js';
import { logger } from './utils/logging.js';
import SuperJSON from 'superjson';
dotenv.config();
@ -100,12 +101,26 @@ app.disable('x-powered-by');
// Add JSON body parsing middleware
app.use(express.json());
app.use((req, res, next) =>
{
res.json = (data) =>
{
if (res.headersSent)
{
return;
}
res.setHeader('Content-Type', 'application/json');
res.send(SuperJSON.stringify(data));
};
next();
});
// Add API routes
app.use('/api/auth', authRoutes);
app.use('/api/chat', requireAuth, chatRoutes);
app.use('/api/user-preferences', requireAuth, userPreferencesRoutes);
app.use('/api/settings', requireAuth, settingsRoutes);
app.use('/api/mantis', requireAuth, mantisRoutes); // Register Mantis routes
app.use('/api/mantis', requireAuth, mantisRoutes);
app.use('/api', requireAuth, apiRoutes);
if (process.env.PROD)