1313import java .util .Date ;
1414import java .util .List ;
1515
16+ import org .apache .commons .lang .StringUtils ;
1617import org .apache .poi .ss .usermodel .Sheet ;
1718import org .apache .poi .ss .usermodel .Workbook ;
1819
@@ -37,7 +38,8 @@ public class ServerMonitoringUsecaseImpl implements ServerMonitoringUsecase {
3738 private ServerMonitoringRepository serverCheckRepository ;
3839 private ReportRepository reportRepository ;
3940
40- public ServerMonitoringUsecaseImpl (ServerMonitoringRepository serverCheckRepository , ReportRepository reportRepository ) {
41+ public ServerMonitoringUsecaseImpl (ServerMonitoringRepository serverCheckRepository ,
42+ ReportRepository reportRepository ) {
4143 this .serverCheckRepository = serverCheckRepository ;
4244 this .reportRepository = reportRepository ;
4345 }
@@ -266,8 +268,12 @@ public List<OSDiskUsage> getCurrentOSDiskUsage() {
266268 }
267269
268270 @ Override
269- public AlertLog getAlertLogDuringPeriod (AlertLogCommand alc , String startDate , String endDate ) {
270- log .debug (String .format ("alert log file monitoring, %s (%s ~ %s)" , alc .getReadFilePath (), startDate , endDate ));
271+ public AlertLog getAlertLogDuringPeriod (AlertLogCommand alc , String startDate , String endDate ,
272+ String ... searchKeywords ) {
273+ log .debug (String .format ("alert log file monitoring, %s (%s ~ %s), Search Keywords: %s" , alc .getReadFilePath (),
274+ startDate , endDate , StringUtils .join (searchKeywords , "," )));
275+
276+ long start = System .currentTimeMillis ();
271277
272278 AlertLog alertLog = new AlertLog ();
273279 String fullAlertLogString = getAlertLogStringFromCertainDate (alc , startDate );
@@ -285,10 +291,11 @@ public AlertLog getAlertLogDuringPeriod(AlertLogCommand alc, String startDate, S
285291 String logTimeStamp = "" ;
286292 List <String > logContents = new ArrayList <>();
287293 StringBuffer sb = new StringBuffer ();
294+ boolean containsSearchKeyword = searchKeywords .length == 0 ;
288295
289296 for (int i = 0 ; i < lines .length ; i ++) {
290297 String line = lines [i ];
291-
298+
292299 // 조회시작일자 찾기
293300 if (!isStartDate ) {
294301 LocalDate parsedDate = DateUtils .parse (line );
@@ -301,15 +308,15 @@ public AlertLog getAlertLogDuringPeriod(AlertLogCommand alc, String startDate, S
301308 logTimeStamp = line ;
302309
303310 // [조회종료일자 > 조회 시작일자 >= 최초 로그기록일자]일 때 최초 로그기록일자부터 읽기 시작
304- if (DateUtils .getDateDiffTime ("yyyy-MM-dd" , parsedDateString , endDate ) > 0 ) {
311+ if (DateUtils .getDateDiffTime ("yyyy-MM-dd" , parsedDateString , endDate ) > 0 ) {
305312 isEndDate = true ;
306313 readEndIndex = i ;
307314 break ;
308315 }
309316 }
310317 }
311318 }
312-
319+
313320 // 로그 저장 시작 & 조회종료일자 찾기
314321 if (isStartDate ) {
315322 LocalDate parsedDate = DateUtils .parse (line );
@@ -327,11 +334,22 @@ public AlertLog getAlertLogDuringPeriod(AlertLogCommand alc, String startDate, S
327334 }
328335
329336 if (i != readStartIndex ) {
330- alertLog .addLog (new Log (alertLog .getAlertLogs ().size (), logTimeStamp , logContents ));
337+ if (containsSearchKeyword ) {
338+ alertLog .addLog (new Log (alertLog .getAlertLogs ().size (), logTimeStamp , logContents ));
339+ containsSearchKeyword = searchKeywords .length == 0 ;
340+ }
331341 logContents = new ArrayList <>();
332342 logTimeStamp = line ;
333343 }
334344 } else { // Log Content Line
345+
346+ // 검색 키워드가 포함되었는지 확인
347+ for (String keyword : searchKeywords ) {
348+ if (line .contains (keyword ) || containsSearchKeyword ) {
349+ containsSearchKeyword = true ;
350+ break ;
351+ }
352+ }
335353 logContents .add (line );
336354 }
337355
@@ -353,10 +371,15 @@ public AlertLog getAlertLogDuringPeriod(AlertLogCommand alc, String startDate, S
353371 } catch (Exception e ) {
354372 log .error (e .getMessage ());
355373 }
356-
374+
375+ long end = System .currentTimeMillis ();
376+ log .info (String .format ("Alert Log monitoring result (Log count: %d, Total line count: %d" ,
377+ alertLog .getAlertLogs ().size (), alertLog .getTotalLineCount ()));
378+ log .debug (String .format ("Alert Log monitoring elapsed time: (%,.3f)ms" , (end - start ) / 1000.0 ));
379+
357380 return alertLog ;
358381 }
359-
382+
360383 private String getAlertLogStringFromCertainDate (AlertLogCommand alc , String startDate ) {
361384 int alertLogFileLineCnt = serverCheckRepository .getAlertLogFileLineCount (alc );
362385 String fullAlertLogString = serverCheckRepository .checkAlertLog (alc );
0 commit comments