Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import cors from 'cors';
import dotenv from 'dotenv';
import uploadRoutes from './routes/upload';
import allLogs from './routes/getAllLogs';
import getStats from './routes/getStats';
import logtypeStatus from './routes/logTypeStatus';
import connectToDatabase from './db/connect';
import severityInfo from './routes/severityInfo';

dotenv.config();

Expand All @@ -24,6 +27,9 @@ app.get('/', (req: Request, res: Response): void => {

app.use('/api', uploadRoutes);
app.use('/api', allLogs);
app.use('/api', getStats)
app.use('/api', logtypeStatus)
app.use('/api', severityInfo)

app.listen(PORT, (): void => {
console.log(`Server is running on http://localhost:${PORT}`);
Expand Down
177 changes: 177 additions & 0 deletions backend/src/routes/getStats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import express from 'express';
import { Request, Response } from 'express';
import { LinuxLogModel } from '../models/LinuxLogModel';

const router = express.Router();

router.get('/stats', async (req: Request, res: Response) => {
try {
const startDate = new Date();
startDate.setDate(startDate.getDate() - 90); // 90 days ago

const totalLogsAgg = await LinuxLogModel.aggregate([
{
$match: { timestamp: { $gte: startDate } }
},
{
$group: {
_id: {
year: { $year: '$timestamp' },
month: { $month: '$timestamp' },
day: { $dayOfMonth: '$timestamp' },
},
count: { $sum: 1 },
}
},
{
$sort: {
"_id.year": 1,
"_id.month": 1,
"_id.day": 1,
}
}
]);

const errorLogsAgg = await LinuxLogModel.aggregate([
{
$match: {
timestamp: { $gte: startDate },
severity: { $in: ['ERROR'] }
},
},
{
$group: {
_id: {
year: { $year: '$timestamp' },
month: { $month: '$timestamp' },
day: { $dayOfMonth: '$timestamp' },
},
count: { $sum: 1 },
}
},
{
$sort: {
"_id.year": 1,
"_id.month": 1,
"_id.day": 1,
}
}
]);

const warningLogsAgg = await LinuxLogModel.aggregate([
{
$match: {
timestamp: { $gte: startDate },
severity: { $in: ['WARNING'] } // Adjust this based on your severity levels
},
},
{
$group: {
_id: {
year: { $year: '$timestamp' },
month: { $month: '$timestamp' },
day: { $dayOfMonth: '$timestamp' },
},
count: { $sum: 1 },
}
},
{
$sort: {
"_id.year": 1,
"_id.month": 1,
"_id.day": 1,
}
}
]);


// Format Helper to build a 90 days array
const totalLogsData = buildDailyArray(totalLogsAgg, startDate);
const errorLogsData = buildDailyArray(errorLogsAgg, startDate);
const warningLogsData = buildDailyArray(warningLogsAgg, startDate);

// sum up total logs
const totalLogsSum = totalLogsData.reduce((acc, count) => acc + count, 0);
const errorLogsSum = errorLogsData.reduce((acc, count) => acc + count, 0);
const warningLogsSum = warningLogsData.reduce((acc, count) => acc + count, 0);

//compute tends
const totalLogsTend = computeTrend(totalLogsData);
const errorLogsTend = computeTrend(errorLogsData);
const warningLogsTend = computeTrend(warningLogsData);



res.json([
{
title: "Total Logs",
value: totalLogsSum, // e.g. "14k" or a number
interval: "Last 90 days",
trend: totalLogsTend, // "up", "down", or "neutral"
data: totalLogsData, // [0, 3, 5, 2, ...] for 180 days
},
{
title: "Error Count",
value: errorLogsSum,
interval: "Last 90 days",
trend: errorLogsTend,
data: errorLogsData,
},
{
title: "Warning Count",
value: warningLogsSum,
interval: "Last 90 days",
trend: warningLogsTend,
data: warningLogsData,
},
])



} catch (error) {
console.error(error);
res.status(500).json({ error: "Something went wrong." });
}
});

function buildDailyArray(
aggResults: { _id: { year: number; month: number; day: number }; count: number }[],
startDate: Date
): number[] {
const daysCount = 90;
const dailyArray = new Array(daysCount).fill(0);

for (const doc of aggResults) {
const { year, month, day } = doc._id;
const thisDate = new Date(year, month - 1, day); // month is 0-indexed
const index = Math.floor((thisDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));

if (index >= 0 && index < daysCount) {
dailyArray[index] = doc.count;
}
}

return dailyArray;
}


function computeTrend(data: number[]) {
if (data.length < 2) return "neutral";

const recentValue = data[data.length - 1];
const previousValue = data[data.length - 2];

if (recentValue > previousValue) {
return "up";
} else if (recentValue < previousValue) {
return "down";
} else {
return "neutral";
}
}


export default router;



37 changes: 37 additions & 0 deletions backend/src/routes/logTypeStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import express from 'express';
import { Request, Response } from 'express';
import { LinuxLogModel } from '../models/LinuxLogModel';

const router = express.Router();

router.get('/logtypeStatus', async (req: Request, res: Response) => {
try {
const syslogCount = await LinuxLogModel.countDocuments({ logType: 'SYSLOG' });
const windowlogCount = await LinuxLogModel.countDocuments({ logType: 'WINDOWLOG' });
const authlogCount = await LinuxLogModel.countDocuments({ logType: 'AUTH' });
const kernelCount = await LinuxLogModel.countDocuments({ logType: 'KERNEL' });
const unknownCount = await LinuxLogModel.countDocuments({ logType: { $regex: 'UNKNOWN' } });

const data = [
{ label: "SYSLOG", value: syslogCount },
{ label: "WINDOWLOG", value: windowlogCount },
{ label: "AUTHLOG", value: authlogCount },
{ label: "KERNEL", value: kernelCount },
{ label: "UNKNOWN", value: unknownCount },
];

res.json(data);


} catch (error) {
console.error(error);
res.status(500).json({ error: "Something went wrong." });
}
});



export default router;



74 changes: 74 additions & 0 deletions backend/src/routes/severityInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// total no of each severity "INFO" | "WARNING" | "ERROR" | "CRITICAL" and from last 7 months count for each severity in following format const [logData, setLogData] = React.useState({
// info: [0, 0, 0, 0, 0, 0, 0],
// warning: [0, 0, 0, 0, 0, 0, 0],
// error: [0, 0, 0, 0, 0, 0, 0],
// critical: [0, 0, 0, 0, 0, 0, 0],
// });

import express from 'express';
import { Request, Response } from 'express';
import { LinuxLogModel } from '../models/LinuxLogModel';

const router = express.Router();

router.get('/severityInfo', async (req: Request, res: Response) => {
try {

const info = [0, 0, 0, 0, 0, 0, 0];
const warning = [0, 0, 0, 0, 0, 0, 0];
const error = [0, 0, 0, 0, 0, 0, 0];
const critical = [0, 0, 0, 0, 0, 0, 0];

// Get monthly data for the last 7 months
for (let i = 0; i < 7; i++) {
const monthStart = new Date();
monthStart.setMonth(monthStart.getMonth() - i);
monthStart.setDate(1);
monthStart.setHours(0, 0, 0, 0);

const monthEnd = new Date(monthStart);
monthEnd.setMonth(monthStart.getMonth() + 1);
monthEnd.setDate(0);
monthEnd.setHours(23, 59, 59, 999);

info[6 - i] = await LinuxLogModel.countDocuments({
severity: 'INFO',
timestamp: { $gte: monthStart, $lte: monthEnd }
});

warning[6 - i] = await LinuxLogModel.countDocuments({
severity: 'WARNING',
timestamp: { $gte: monthStart, $lte: monthEnd }
});

error[6 - i] = await LinuxLogModel.countDocuments({
severity: 'ERROR',
timestamp: { $gte: monthStart, $lte: monthEnd }
});

critical[6 - i] = await LinuxLogModel.countDocuments({
severity: 'CRITICAL',
timestamp: { $gte: monthStart, $lte: monthEnd }
});
}

// Prepare response with total counts and monthly data
res.json({
info,
warning,
error,
critical,
});

} catch (error) {
console.error(error);
res.status(500).json({ error: "Something went wrong." });
}
});



export default router;



4 changes: 2 additions & 2 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@react-spring/web": "^9.7.5",
"d3": "^7.9.0",
"dayjs": "^1.11.13",
"leaflet": "^1.9.4",
"lucide-react": "^0.485.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
Expand Down
Loading