|
|
@@ -14,7 +14,9 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
@@ -23,6 +25,7 @@ import java.util.stream.Collectors;
|
|
|
public class LogSearchService {
|
|
|
|
|
|
private final LogEntryRepository logEntryRepository;
|
|
|
+ private static final Map<String, String> SORT_FIELD_MAPPING = createSortFieldMapping();
|
|
|
|
|
|
public LogSearchResponse searchLogs(LogSearchRequest request) {
|
|
|
log.info("Searching logs with criteria: {}", request);
|
|
|
@@ -90,9 +93,23 @@ public class LogSearchService {
|
|
|
private Pageable createPageable(LogSearchRequest request) {
|
|
|
Sort.Direction direction = "asc".equalsIgnoreCase(request.getSortDirection()) ?
|
|
|
Sort.Direction.ASC : Sort.Direction.DESC;
|
|
|
+
|
|
|
+ String requestedSortBy = request.getSortBy() == null ? "timestamp" : request.getSortBy();
|
|
|
+ String actualSortField = SORT_FIELD_MAPPING.getOrDefault(requestedSortBy, requestedSortBy);
|
|
|
|
|
|
return PageRequest.of(request.getPage(), request.getSize(),
|
|
|
- Sort.by(direction, request.getSortBy()));
|
|
|
+ Sort.by(direction, actualSortField));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Map<String, String> createSortFieldMapping() {
|
|
|
+ Map<String, String> mapping = new HashMap<>();
|
|
|
+ mapping.put("timestamp", "@timestamp");
|
|
|
+ mapping.put("level", "log_level");
|
|
|
+ mapping.put("serviceName", "service_name");
|
|
|
+ mapping.put("traceId", "trace_id");
|
|
|
+ mapping.put("spanId", "span_id");
|
|
|
+ mapping.put("bizName", "bizName");
|
|
|
+ return mapping;
|
|
|
}
|
|
|
|
|
|
private LogSearchResponse.LogEntryDto convertToDto(LogEntry logEntry) {
|