@@ -18,6 +18,7 @@ import ru.soune.nocopy.repository.ViolationRepository;
|
||||
import ru.soune.nocopy.service.file.FileEntityService;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
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) {
|
||||
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,
|
||||
LocalDateTime startDate, LocalDateTime endDate) {
|
||||
|
||||
@@ -181,4 +194,18 @@ public class ViolationService {
|
||||
.resolvedViolations(resolvedViolations)
|
||||
.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