Skip to content

Commit 31ee547

Browse files
authored
New dashboard (#62)
* new_dashbroard * Redesign ensures consistent log display * update search interface * update search log * uniqueId search * display optimization
1 parent ed46623 commit 31ee547

File tree

18 files changed

+2561
-210
lines changed

18 files changed

+2561
-210
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Logs
22
logs
3+
!apps/api/src/domains/logs
4+
!apps/web/src/components/pages/Logs.tsx
35
*.log
46
npm-debug.log*
57
yarn-debug.log*

apps/api/src/domains/dashboard/dashboard.controller.ts

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { AuthRequest } from '../../middleware/auth';
77
import logger from '../../utils/logger';
88
import { DashboardService } from './dashboard.service';
99
import { GetMetricsQueryDto, GetRecentAlertsQueryDto } from './dto';
10+
import { dashboardAnalyticsService } from './services/dashboard-analytics.service';
1011

1112
const dashboardService = new DashboardService();
1213

@@ -82,3 +83,196 @@ export const getRecentAlerts = async (
8283
});
8384
}
8485
};
86+
87+
/**
88+
* Get request trend analytics (auto-refresh every 5s)
89+
*/
90+
export const getRequestTrend = async (
91+
req: AuthRequest,
92+
res: Response
93+
): Promise<void> => {
94+
try {
95+
const { interval = 5 } = req.query;
96+
const intervalSeconds = Math.max(5, Math.min(60, Number(interval)));
97+
98+
const trend = await dashboardAnalyticsService.getRequestTrend(intervalSeconds);
99+
100+
res.json({
101+
success: true,
102+
data: trend,
103+
});
104+
} catch (error) {
105+
logger.error('Get request trend error:', error);
106+
res.status(500).json({
107+
success: false,
108+
message: 'Failed to get request trend',
109+
});
110+
}
111+
};
112+
113+
/**
114+
* Get slow requests from performance monitoring
115+
*/
116+
export const getSlowRequests = async (
117+
req: AuthRequest,
118+
res: Response
119+
): Promise<void> => {
120+
try {
121+
const { limit = 10 } = req.query;
122+
const slowRequests = await dashboardAnalyticsService.getSlowRequests(Number(limit));
123+
124+
res.json({
125+
success: true,
126+
data: slowRequests,
127+
});
128+
} catch (error) {
129+
logger.error('Get slow requests error:', error);
130+
res.status(500).json({
131+
success: false,
132+
message: 'Failed to get slow requests',
133+
});
134+
}
135+
};
136+
137+
/**
138+
* Get latest attack statistics (top 5 in 24h)
139+
*/
140+
export const getLatestAttackStats = async (
141+
req: AuthRequest,
142+
res: Response
143+
): Promise<void> => {
144+
try {
145+
const { limit = 5 } = req.query;
146+
const attacks = await dashboardAnalyticsService.getLatestAttacks(Number(limit));
147+
148+
res.json({
149+
success: true,
150+
data: attacks,
151+
});
152+
} catch (error) {
153+
logger.error('Get latest attack stats error:', error);
154+
res.status(500).json({
155+
success: false,
156+
message: 'Failed to get latest attack statistics',
157+
});
158+
}
159+
};
160+
161+
/**
162+
* Get latest security news/events
163+
*/
164+
export const getLatestNews = async (
165+
req: AuthRequest,
166+
res: Response
167+
): Promise<void> => {
168+
try {
169+
const { limit = 20 } = req.query;
170+
const news = await dashboardAnalyticsService.getLatestNews(Number(limit));
171+
172+
res.json({
173+
success: true,
174+
data: news,
175+
});
176+
} catch (error) {
177+
logger.error('Get latest news error:', error);
178+
res.status(500).json({
179+
success: false,
180+
message: 'Failed to get latest news',
181+
});
182+
}
183+
};
184+
185+
/**
186+
* Get request analytics (top IPs by period)
187+
*/
188+
export const getRequestAnalytics = async (
189+
req: AuthRequest,
190+
res: Response
191+
): Promise<void> => {
192+
try {
193+
const { period = 'day' } = req.query;
194+
const validPeriod = ['day', 'week', 'month'].includes(period as string)
195+
? (period as 'day' | 'week' | 'month')
196+
: 'day';
197+
198+
const analytics = await dashboardAnalyticsService.getRequestAnalytics(validPeriod);
199+
200+
res.json({
201+
success: true,
202+
data: analytics,
203+
});
204+
} catch (error) {
205+
logger.error('Get request analytics error:', error);
206+
res.status(500).json({
207+
success: false,
208+
message: 'Failed to get request analytics',
209+
});
210+
}
211+
};
212+
213+
/**
214+
* Get attack vs normal request ratio
215+
*/
216+
export const getAttackRatio = async (
217+
req: AuthRequest,
218+
res: Response
219+
): Promise<void> => {
220+
try {
221+
const ratio = await dashboardAnalyticsService.getAttackRatio();
222+
223+
res.json({
224+
success: true,
225+
data: ratio,
226+
});
227+
} catch (error) {
228+
logger.error('Get attack ratio error:', error);
229+
res.status(500).json({
230+
success: false,
231+
message: 'Failed to get attack ratio',
232+
});
233+
}
234+
};
235+
236+
/**
237+
* Get complete dashboard analytics
238+
*/
239+
export const getDashboardAnalytics = async (
240+
req: AuthRequest,
241+
res: Response
242+
): Promise<void> => {
243+
try {
244+
const [
245+
requestTrend,
246+
slowRequests,
247+
latestAttacks,
248+
latestNews,
249+
requestAnalytics,
250+
attackRatio,
251+
] = await Promise.all([
252+
dashboardAnalyticsService.getRequestTrend(5),
253+
dashboardAnalyticsService.getSlowRequests(10),
254+
dashboardAnalyticsService.getLatestAttacks(5),
255+
dashboardAnalyticsService.getLatestNews(20),
256+
dashboardAnalyticsService.getRequestAnalytics('day'),
257+
dashboardAnalyticsService.getAttackRatio(),
258+
]);
259+
260+
res.json({
261+
success: true,
262+
data: {
263+
requestTrend,
264+
slowRequests,
265+
latestAttacks,
266+
latestNews,
267+
requestAnalytics,
268+
attackRatio,
269+
},
270+
});
271+
} catch (error) {
272+
logger.error('Get dashboard analytics error:', error);
273+
res.status(500).json({
274+
success: false,
275+
message: 'Failed to get dashboard analytics',
276+
});
277+
}
278+
};

apps/api/src/domains/dashboard/dashboard.routes.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,26 @@ router.get('/metrics', dashboardController.getSystemMetrics);
2121
// Get recent alerts
2222
router.get('/recent-alerts', dashboardController.getRecentAlerts);
2323

24+
// Dashboard Analytics Endpoints
25+
// Get request trend (auto-refresh every 5s)
26+
router.get('/analytics/request-trend', dashboardController.getRequestTrend);
27+
28+
// Get slow requests
29+
router.get('/analytics/slow-requests', dashboardController.getSlowRequests);
30+
31+
// Get latest attack statistics (top 5 in 24h)
32+
router.get('/analytics/latest-attacks', dashboardController.getLatestAttackStats);
33+
34+
// Get latest security news/events
35+
router.get('/analytics/latest-news', dashboardController.getLatestNews);
36+
37+
// Get request analytics (top IPs by period)
38+
router.get('/analytics/request-analytics', dashboardController.getRequestAnalytics);
39+
40+
// Get attack vs normal request ratio
41+
router.get('/analytics/attack-ratio', dashboardController.getAttackRatio);
42+
43+
// Get complete dashboard analytics (all in one)
44+
router.get('/analytics', dashboardController.getDashboardAnalytics);
45+
2446
export default router;

0 commit comments

Comments
 (0)