|
|
@@ -10,7 +10,9 @@ import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.time.Instant;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneOffset;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -24,19 +26,22 @@ public class LogAnalysisService {
|
|
|
|
|
|
public LogStatistics getLogStatistics(LocalDateTime startTime, LocalDateTime endTime) {
|
|
|
log.info("Calculating log statistics from {} to {}", startTime, endTime);
|
|
|
+
|
|
|
+ Instant startInstant = toUtcInstant(startTime);
|
|
|
+ Instant endInstant = toUtcInstant(endTime);
|
|
|
|
|
|
- long totalLogs = logEntryRepository.countByTimestampBetween(startTime, endTime);
|
|
|
+ long totalLogs = logEntryRepository.countByTimestampBetween(startInstant, endInstant);
|
|
|
|
|
|
Map<String, Long> logsByLevel = new HashMap<>();
|
|
|
for (String level : Arrays.asList("ERROR", "WARN", "INFO", "DEBUG")) {
|
|
|
- long count = logEntryRepository.countByTimestampBetweenAndLevel(startTime, endTime, level);
|
|
|
+ long count = logEntryRepository.countByTimestampBetweenAndLevel(startInstant, endInstant, level);
|
|
|
if (count > 0) {
|
|
|
logsByLevel.put(level, count);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Pageable pageable = PageRequest.of(0, 1000);
|
|
|
- Page<LogEntry> allLogs = logEntryRepository.findByTimestampBetween(startTime, endTime, pageable);
|
|
|
+ Page<LogEntry> allLogs = logEntryRepository.findByTimestampBetween(startInstant, endInstant, pageable);
|
|
|
|
|
|
Map<String, Long> logsByService = allLogs.getContent().stream()
|
|
|
.filter(log -> log.getServiceName() != null)
|
|
|
@@ -61,7 +66,9 @@ public class LogAnalysisService {
|
|
|
}
|
|
|
|
|
|
private List<LogStatistics.ErrorStatistics> calculateErrorStatistics(LocalDateTime startTime, LocalDateTime endTime) {
|
|
|
- List<LogEntry> errorLogs = logEntryRepository.findByTimestampBetweenAndLevel(startTime, endTime, "ERROR");
|
|
|
+ Instant startInstant = toUtcInstant(startTime);
|
|
|
+ Instant endInstant = toUtcInstant(endTime);
|
|
|
+ List<LogEntry> errorLogs = logEntryRepository.findByTimestampBetweenAndLevel(startInstant, endInstant, "ERROR");
|
|
|
|
|
|
Map<String, List<LogEntry>> errorsByType = errorLogs.stream()
|
|
|
.collect(Collectors.groupingBy(log -> {
|
|
|
@@ -79,7 +86,8 @@ public class LogAnalysisService {
|
|
|
|
|
|
String lastOccurrence = logs.stream()
|
|
|
.max(Comparator.comparing(LogEntry::getTimestamp))
|
|
|
- .map(log -> log.getTimestamp().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
|
|
|
+ .map(log -> DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(
|
|
|
+ LocalDateTime.ofInstant(log.getTimestamp(), ZoneOffset.UTC)))
|
|
|
.orElse("");
|
|
|
|
|
|
List<String> sampleMessages = logs.stream()
|
|
|
@@ -100,8 +108,10 @@ public class LogAnalysisService {
|
|
|
}
|
|
|
|
|
|
private List<LogStatistics.PerformanceStatistics> calculatePerformanceStatistics(LocalDateTime startTime, LocalDateTime endTime) {
|
|
|
+ Instant startInstant = toUtcInstant(startTime);
|
|
|
+ Instant endInstant = toUtcInstant(endTime);
|
|
|
Pageable pageable = PageRequest.of(0, 1000);
|
|
|
- Page<LogEntry> allLogs = logEntryRepository.findByTimestampBetween(startTime, endTime, pageable);
|
|
|
+ Page<LogEntry> allLogs = logEntryRepository.findByTimestampBetween(startInstant, endInstant, pageable);
|
|
|
|
|
|
Map<String, List<LogEntry>> logsByEndpoint = allLogs.getContent().stream()
|
|
|
.filter(log -> log.getContext() != null && log.getContext().get("responseTime") != null)
|
|
|
@@ -181,12 +191,14 @@ public class LogAnalysisService {
|
|
|
}
|
|
|
|
|
|
private LogStatistics.TrendData calculateTrendForInterval(LocalDateTime startTime, LocalDateTime endTime) {
|
|
|
- long totalCount = logEntryRepository.countByTimestampBetween(startTime, endTime);
|
|
|
- long errorCount = logEntryRepository.countByTimestampBetweenAndLevel(startTime, endTime, "ERROR");
|
|
|
- long warningCount = logEntryRepository.countByTimestampBetweenAndLevel(startTime, endTime, "WARN");
|
|
|
+ Instant startInstant = toUtcInstant(startTime);
|
|
|
+ Instant endInstant = toUtcInstant(endTime);
|
|
|
+ long totalCount = logEntryRepository.countByTimestampBetween(startInstant, endInstant);
|
|
|
+ long errorCount = logEntryRepository.countByTimestampBetweenAndLevel(startInstant, endInstant, "ERROR");
|
|
|
+ long warningCount = logEntryRepository.countByTimestampBetweenAndLevel(startInstant, endInstant, "WARN");
|
|
|
|
|
|
Pageable pageable = PageRequest.of(0, 1000);
|
|
|
- Page<LogEntry> logs = logEntryRepository.findByTimestampBetween(startTime, endTime, pageable);
|
|
|
+ Page<LogEntry> logs = logEntryRepository.findByTimestampBetween(startInstant, endInstant, pageable);
|
|
|
|
|
|
double avgResponseTime = logs.getContent().stream()
|
|
|
.filter(log -> log.getContext() != null && log.getContext().get("responseTime") != null)
|
|
|
@@ -205,4 +217,8 @@ public class LogAnalysisService {
|
|
|
.avgResponseTime(avgResponseTime)
|
|
|
.build();
|
|
|
}
|
|
|
+
|
|
|
+ private Instant toUtcInstant(LocalDateTime dateTime) {
|
|
|
+ return dateTime.atZone(ZoneOffset.UTC).toInstant();
|
|
|
+ }
|
|
|
}
|