Skip to content

Commit af73062

Browse files
committed
Refactoring: Move duplicated logic in ServerMonitoringRepository to ServerMonitoringUsecase
1 parent 9424ffc commit af73062

File tree

6 files changed

+130
-284
lines changed

6 files changed

+130
-284
lines changed

src/main/java/root/core/repository/constracts/ServerMonitoringRepository.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.List;
44

5-
import root.core.domain.AlertLog;
65
import root.core.domain.AlertLogCommand;
76
import root.core.domain.OSDiskUsage;
87

@@ -13,7 +12,5 @@ public interface ServerMonitoringRepository {
1312

1413
String checkAlertLog(AlertLogCommand alc);
1514

16-
AlertLog checkAlertLogDuringPeriod(AlertLogCommand alc, String startDate, String endDate);
17-
1815
List<OSDiskUsage> checkOSDiskUsage();
1916
}
Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package root.core.repository.implement;
22

33
import java.io.InputStream;
4-
import java.time.LocalDate;
54
import java.util.ArrayList;
65
import java.util.Arrays;
76
import java.util.List;
@@ -14,12 +13,9 @@
1413

1514
import lombok.extern.slf4j.Slf4j;
1615
import root.common.server.implement.JschServer;
17-
import root.core.domain.AlertLog;
1816
import root.core.domain.AlertLogCommand;
19-
import root.core.domain.Log;
2017
import root.core.domain.OSDiskUsage;
2118
import root.core.repository.constracts.ServerMonitoringRepository;
22-
import root.utils.DateUtils;
2319
import root.utils.NumberUnitUtils;
2420
import root.utils.NumberUnitUtils.Unit;
2521

@@ -63,83 +59,6 @@ public String checkAlertLog(AlertLogCommand alc) {
6359
return result;
6460
}
6561

66-
@Override
67-
public AlertLog checkAlertLogDuringPeriod(AlertLogCommand alc, String startDate, String endDate) {
68-
AlertLog alertLog = new AlertLog();
69-
70-
String fullAlertLogString = getAlertLogStringFromCertainDate(alc, startDate);
71-
72-
try {
73-
// 조회기간동안의 로그만을 취하여 StringBuffer에 저장한다.
74-
String[] lines = fullAlertLogString.split("\n");
75-
76-
boolean isStartDate = false;
77-
boolean isEndDate = false;
78-
79-
int readStartIndex = 0;
80-
int readEndIndex = lines.length;
81-
82-
String logTimeStamp = "";
83-
List<String> logContents = new ArrayList<>();
84-
StringBuffer sb = new StringBuffer();
85-
86-
for (int i = 0; i < lines.length; i++) {
87-
String line = lines[i];
88-
89-
// 조회시작일자 찾기
90-
if (!isStartDate) {
91-
LocalDate parsedDate = DateUtils.parse(line);
92-
if (parsedDate != null && DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate).equals(startDate)) {
93-
isStartDate = true;
94-
readStartIndex = i;
95-
}
96-
}
97-
98-
// 로그 저장 시작 & 조회종료일자 찾기
99-
if (isStartDate) {
100-
LocalDate parsedDate = DateUtils.parse(line);
101-
if (parsedDate != null) { // Log TimeStamp Line
102-
String logDate = DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate);
103-
if (logDate.startsWith(DateUtils.addDate(endDate, 0, 0, 1))) {
104-
isEndDate = true;
105-
readEndIndex = i;
106-
}
107-
108-
if (i == readStartIndex) {
109-
logTimeStamp = line;
110-
}
111-
112-
if (i != readStartIndex && !isEndDate) {
113-
alertLog.addLog(new Log(logTimeStamp, logContents));
114-
logContents = new ArrayList<>();
115-
logTimeStamp = line;
116-
}
117-
} else { // Log Content Line
118-
logContents.add(line);
119-
}
120-
121-
// 로그 저장 중지
122-
if (!isEndDate) {
123-
sb.append(line);
124-
} else {
125-
break;
126-
}
127-
}
128-
}
129-
130-
// 종료 후 마지막 로그 추가하기
131-
alertLog.addLog(new Log(logTimeStamp, logContents));
132-
alertLog.setFullLogString(sb.toString());
133-
134-
log.info("\t▶ Alert Log READ LINE: " + (readEndIndex - readStartIndex) + "/" + alc.getReadLine());
135-
136-
} catch (Exception e) {
137-
log.error(e.getMessage());
138-
}
139-
140-
return alertLog;
141-
}
142-
14362
@Override
14463
public List<OSDiskUsage> checkOSDiskUsage() {
14564
List<OSDiskUsage> list = new ArrayList<>();
@@ -210,44 +129,4 @@ public List<OSDiskUsage> stringToOsDiskUsageList(String result) {
210129

211130
return list;
212131
}
213-
214-
private String getAlertLogStringFromCertainDate(AlertLogCommand alc, String startDate) {
215-
int alertLogFileLineCnt = this.getAlertLogFileLineCount(alc);
216-
String fullAlertLogString = this.checkAlertLog(alc);
217-
218-
// 조회시작일자의 로그를 모두 포함하도록 readLine 수를 점진적으로 늘리면서 읽는다.
219-
while (true) {
220-
String[] lines = fullAlertLogString.split("\n");
221-
222-
// 현재 Read Line 수가 파일 최대 Line 수를 초과했을 시, 파일 전체를 읽고 반환한다.
223-
if (lines.length >= alertLogFileLineCnt) {
224-
break;
225-
}
226-
227-
// 조회한 로그 내에서 가장 처음으로 나타나는 로그의 기록일자를 얻어낸다.
228-
String logDate = "";
229-
for (String line : lines) {
230-
LocalDate parsedDate = DateUtils.parse(line);
231-
if (parsedDate != null) {
232-
logDate = DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate);
233-
break;
234-
}
235-
}
236-
237-
if(logDate == null || logDate.equals("")) {
238-
break;
239-
}
240-
241-
// 조회시작일자와 로그의 처음 기록일자를 비교한다.
242-
long diffTime = DateUtils.getDateDiffTime("yyyy-MM-dd", logDate, startDate);
243-
if (diffTime >= 0) { // 조회 Line 수를 더 늘려서 다시 조회
244-
alc.setReadLine(alc.getReadLine() * 2);
245-
fullAlertLogString = this.checkAlertLog(alc);
246-
} else {
247-
break;
248-
}
249-
}
250-
251-
return fullAlertLogString;
252-
}
253132
}
Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package root.core.repository.implement;
22

3-
import java.time.LocalDate;
4-
import java.util.ArrayList;
53
import java.util.List;
64
import java.util.StringTokenizer;
75

86
import lombok.extern.slf4j.Slf4j;
97
import root.common.server.implement.JschServer;
10-
import root.core.domain.AlertLog;
118
import root.core.domain.AlertLogCommand;
12-
import root.core.domain.Log;
139
import root.core.domain.OSDiskUsage;
1410
import root.core.repository.constracts.ServerMonitoringRepository;
15-
import root.utils.DateUtils;
1611

1712
@Slf4j
1813
public class WindowServerMonitoringRepository implements ServerMonitoringRepository {
@@ -62,138 +57,9 @@ public String checkAlertLog(AlertLogCommand alc) {
6257
return result;
6358
}
6459

65-
@Override
66-
public AlertLog checkAlertLogDuringPeriod(AlertLogCommand alc, String startDate, String endDate) {
67-
AlertLog alertLog = new AlertLog();
68-
69-
String fullAlertLogString = getAlertLogStringFromCertainDate(alc, startDate);
70-
71-
try {
72-
// 조회기간동안의 로그만을 취하여 StringBuffer에 저장한다.
73-
String[] lines = fullAlertLogString.split(System.lineSeparator());
74-
75-
boolean isStartDate = false;
76-
boolean isEndDate = false;
77-
78-
int readStartIndex = 0;
79-
int readEndIndex = lines.length;
80-
81-
String logTimeStamp = "";
82-
List<String> logContents = new ArrayList<>();
83-
StringBuffer sb = new StringBuffer();
84-
85-
for (int i = 0; i < lines.length; i++) {
86-
String line = lines[i];
87-
88-
// 조회시작일자 찾기
89-
if (!isStartDate) {
90-
LocalDate parsedDate = DateUtils.parse(line);
91-
if (parsedDate != null) {
92-
// [조회시작일자 >= 최초 로그기록일자]일 때, 최초 로그기록일자부터 읽기 시작
93-
String parsedDateString = DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate);
94-
if (DateUtils.getDateDiffTime("yyyy-MM-dd", parsedDateString, startDate) >= 0) {
95-
isStartDate = true;
96-
readStartIndex = i;
97-
logTimeStamp = line;
98-
99-
// [조회종료일자 > 조회 시작일자 >= 최초 로그기록일자]일 때 최초 로그기록일자부터 읽기 시작
100-
if(DateUtils.getDateDiffTime("yyyy-MM-dd", parsedDateString, endDate) > 0) {
101-
isEndDate = true;
102-
readEndIndex = i;
103-
break;
104-
}
105-
}
106-
}
107-
}
108-
109-
// 로그 저장 시작 & 조회종료일자 찾기
110-
if (isStartDate) {
111-
LocalDate parsedDate = DateUtils.parse(line);
112-
if (parsedDate != null) { // Log TimeStamp Line
113-
114-
// 현재 로그기록일자가 조회종료일자 + 1일인지 확인
115-
String logDate = DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate);
116-
if (logDate.startsWith(DateUtils.addDate(endDate, 0, 0, 1))) {
117-
isEndDate = true;
118-
readEndIndex = i;
119-
}
120-
121-
if (i == readStartIndex) {
122-
logTimeStamp = line;
123-
}
124-
125-
if (i != readStartIndex) {
126-
alertLog.addLog(new Log(logTimeStamp, logContents));
127-
logContents = new ArrayList<>();
128-
logTimeStamp = line;
129-
}
130-
} else { // Log Content Line
131-
logContents.add(line);
132-
}
133-
134-
// 로그 저장 중지
135-
if (!isEndDate) {
136-
sb.append(line);
137-
} else {
138-
break;
139-
}
140-
}
141-
}
142-
143-
// 종료 후 fullLogString 추가
144-
alertLog.setFullLogString(sb.toString());
145-
146-
log.info("\t▶ Alert Log READ LINE: " + (readEndIndex - readStartIndex) + "/" + alc.getReadLine());
147-
148-
} catch (Exception e) {
149-
log.error(e.getMessage());
150-
}
151-
return alertLog;
152-
}
153-
15460
@Override
15561
public List<OSDiskUsage> checkOSDiskUsage() {
15662
// TODO Auto-generated method stub
15763
return null;
15864
}
159-
160-
private String getAlertLogStringFromCertainDate(AlertLogCommand alc, String startDate) {
161-
int alertLogFileLineCnt = getAlertLogFileLineCount(alc);
162-
String fullAlertLogString = checkAlertLog(alc);
163-
164-
// 조회시작일자의 로그를 모두 포함하도록 readLine 수를 점진적으로 늘리면서 읽는다.
165-
while (true) {
166-
String[] lines = fullAlertLogString.split(System.lineSeparator());
167-
168-
// 현재 Read Line 수가 파일 최대 Line 수를 초과했을 시, 파일 전체를 읽고 반환한다.
169-
if (lines.length >= alertLogFileLineCnt) {
170-
break;
171-
}
172-
173-
// 조회한 로그 내에서 가장 처음으로 나타나는 로그의 기록일자를 얻어낸다.
174-
String logDate = "";
175-
for (String line : lines) {
176-
LocalDate parsedDate = DateUtils.parse(line);
177-
if (parsedDate != null) {
178-
logDate = DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate);
179-
break;
180-
}
181-
}
182-
183-
if (logDate == null || logDate.equals("")) {
184-
break;
185-
}
186-
187-
// 조회시작일자와 로그의 처음 기록일자를 비교한다.
188-
long diffTime = DateUtils.getDateDiffTime("yyyy-MM-dd", logDate, startDate);
189-
if (diffTime >= 0) { // 조회 Line 수를 더 늘려서 다시 조회
190-
alc.setReadLine((alc.getReadLine()) * 2);
191-
fullAlertLogString = checkAlertLog(alc);
192-
} else {
193-
break;
194-
}
195-
}
196-
197-
return fullAlertLogString;
198-
}
19965
}

0 commit comments

Comments
 (0)