@@ -14,6 +14,7 @@ import ru.soune.nocopy.exception.NotFoundAuthToken;
|
|||||||
import ru.soune.nocopy.repository.GlobalSearchTaskRepository;
|
import ru.soune.nocopy.repository.GlobalSearchTaskRepository;
|
||||||
import ru.soune.nocopy.service.register.AuthService;
|
import ru.soune.nocopy.service.register.AuthService;
|
||||||
import ru.soune.nocopy.service.search.GlobalSearchService;
|
import ru.soune.nocopy.service.search.GlobalSearchService;
|
||||||
|
import ru.soune.nocopy.service.violation.ViolationService;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@@ -31,6 +32,8 @@ public class GlobalSearchController {
|
|||||||
|
|
||||||
private final GlobalSearchTaskRepository searchTaskRepository;
|
private final GlobalSearchTaskRepository searchTaskRepository;
|
||||||
|
|
||||||
|
private final ViolationService violationService;
|
||||||
|
|
||||||
@PostMapping("/start")
|
@PostMapping("/start")
|
||||||
public ResponseEntity<?> startSearch(
|
public ResponseEntity<?> startSearch(
|
||||||
@RequestBody GlobalSearchStartRequest request,
|
@RequestBody GlobalSearchStartRequest request,
|
||||||
@@ -101,6 +104,35 @@ public class GlobalSearchController {
|
|||||||
return ResponseEntity.ok(taskOptional.get());
|
return ResponseEntity.ok(taskOptional.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/violation-summary")
|
||||||
|
public ResponseEntity<?> getSummary(@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
||||||
|
|
||||||
|
if (tokenHeader == null || tokenHeader.isBlank()) {
|
||||||
|
Map<String, Object> errorData = new HashMap<>();
|
||||||
|
errorData.put("token", tokenHeader);
|
||||||
|
|
||||||
|
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Map<String, Object>> fileViolationsSummary;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Long userId = authService.useUserAuthToken(tokenHeader);
|
||||||
|
|
||||||
|
fileViolationsSummary =
|
||||||
|
violationService.getFileViolationsSummary(userId);
|
||||||
|
|
||||||
|
if (fileViolationsSummary.isEmpty()) return ResponseEntity.ok().body("Violations summary not found");
|
||||||
|
} catch (NotFoundAuthToken e) {
|
||||||
|
Map<String, Object> errorData = new HashMap<>();
|
||||||
|
errorData.put("token", tokenHeader);
|
||||||
|
|
||||||
|
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.ok(fileViolationsSummary);
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/status/{taskId}")
|
@GetMapping("/status/{taskId}")
|
||||||
public ResponseEntity<?> getStatus(@PathVariable String taskId,
|
public ResponseEntity<?> getStatus(@PathVariable String taskId,
|
||||||
@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
||||||
|
|||||||
@@ -61,8 +61,7 @@ public class ViolationStatisticsHandler implements RequestHandler {
|
|||||||
userId,
|
userId,
|
||||||
statRequest.getFileId(),
|
statRequest.getFileId(),
|
||||||
startDate,
|
startDate,
|
||||||
endDate
|
endDate);
|
||||||
);
|
|
||||||
|
|
||||||
return BaseResponse.builder()
|
return BaseResponse.builder()
|
||||||
.msgId(request.getMsgId())
|
.msgId(request.getMsgId())
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import ru.soune.nocopy.repository.ViolationRepository;
|
|||||||
import ru.soune.nocopy.service.file.FileEntityService;
|
import ru.soune.nocopy.service.file.FileEntityService;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -56,6 +57,31 @@ public class ViolationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<String, Map<String, Object>> getFileViolationsSummary(Long userId) {
|
||||||
|
List<FileEntity> userFiles = fileEntityService.getAllUserFiles(userId);
|
||||||
|
List<Violation> violations = violationRepository.findByFileEntityIn(userFiles);
|
||||||
|
|
||||||
|
Map<String, List<Violation>> violationsByFileId = violations.stream()
|
||||||
|
.collect(Collectors.groupingBy(v -> v.getFileEntity().getId()));
|
||||||
|
|
||||||
|
Map<String, Map<String, Object>> result = new HashMap<>();
|
||||||
|
|
||||||
|
for (Map.Entry<String, List<Violation>> entry : violationsByFileId.entrySet()) {
|
||||||
|
List<Violation> fileViolations = entry.getValue();
|
||||||
|
|
||||||
|
Map<String, Object> info = new HashMap<>();
|
||||||
|
info.put("count", fileViolations.size());
|
||||||
|
info.put("latestDate", fileViolations.stream()
|
||||||
|
.map(Violation::getCreatedDate)
|
||||||
|
.max(LocalDateTime::compareTo)
|
||||||
|
.orElse(null));
|
||||||
|
|
||||||
|
result.put(entry.getKey(), info);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public List<Violation> violationsByFileId(FileEntity file) {
|
public List<Violation> violationsByFileId(FileEntity file) {
|
||||||
return violationRepository.findByFileEntity(file);
|
return violationRepository.findByFileEntity(file);
|
||||||
}
|
}
|
||||||
@@ -108,19 +134,6 @@ public class ViolationService {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String extractGroupKey(String url, String groupBy) {
|
|
||||||
try {
|
|
||||||
String domain = url.replaceAll("https?://(www\\.)?", "").split("/")[0];
|
|
||||||
if ("tld".equalsIgnoreCase(groupBy)) {
|
|
||||||
int lastDot = domain.lastIndexOf('.');
|
|
||||||
return lastDot > 0 ? domain.substring(lastDot + 1) : domain;
|
|
||||||
}
|
|
||||||
return domain;
|
|
||||||
} catch (Exception e) {
|
|
||||||
return "unknown";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ViolationStatisticsResponse getViolationStatistics(Long userId, String fileId,
|
public ViolationStatisticsResponse getViolationStatistics(Long userId, String fileId,
|
||||||
LocalDateTime startDate, LocalDateTime endDate) {
|
LocalDateTime startDate, LocalDateTime endDate) {
|
||||||
|
|
||||||
@@ -181,4 +194,18 @@ public class ViolationService {
|
|||||||
.resolvedViolations(resolvedViolations)
|
.resolvedViolations(resolvedViolations)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String extractGroupKey(String url, String groupBy) {
|
||||||
|
try {
|
||||||
|
String domain = url.replaceAll("https?://(www\\.)?", "").split("/")[0];
|
||||||
|
if ("tld".equalsIgnoreCase(groupBy)) {
|
||||||
|
int lastDot = domain.lastIndexOf('.');
|
||||||
|
return lastDot > 0 ? domain.substring(lastDot + 1) : domain;
|
||||||
|
}
|
||||||
|
return domain;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "unknown";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user