@@ -7,6 +7,7 @@ import { AuthRequest } from '../../middleware/auth';
77import logger from '../../utils/logger' ;
88import { DashboardService } from './dashboard.service' ;
99import { GetMetricsQueryDto , GetRecentAlertsQueryDto } from './dto' ;
10+ import { dashboardAnalyticsService } from './services/dashboard-analytics.service' ;
1011
1112const 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+ } ;
0 commit comments