2026-03-10 22:47:43 +07:00
|
|
|
package ru.soune.nocopy.service.violation;
|
|
|
|
|
|
|
|
|
|
import lombok.AllArgsConstructor;
|
2026-03-11 15:42:06 +07:00
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
2026-03-10 22:47:43 +07:00
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
|
|
|
|
import ru.soune.nocopy.entity.file.FileEntity;
|
|
|
|
|
import ru.soune.nocopy.entity.search.GlobalSearchResult;
|
|
|
|
|
import ru.soune.nocopy.entity.violation.Violation;
|
|
|
|
|
import ru.soune.nocopy.exception.ViolationNotFoundException;
|
|
|
|
|
import ru.soune.nocopy.repository.ViolationRepository;
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
|
public class ViolationService {
|
|
|
|
|
|
|
|
|
|
private final ViolationRepository violationRepository;
|
|
|
|
|
|
|
|
|
|
public void processViolation(YandexSearchResponse.ImageResult imageResult, FileEntity file,
|
|
|
|
|
GlobalSearchResult result) {
|
|
|
|
|
String url = imageResult.getUrl();
|
2026-03-11 15:42:06 +07:00
|
|
|
String urlHash = DigestUtils.sha256Hex(url);
|
2026-03-10 22:47:43 +07:00
|
|
|
|
2026-03-11 15:42:06 +07:00
|
|
|
if (!violationRepository.existsByUrlHash(urlHash)) {
|
2026-03-10 22:47:43 +07:00
|
|
|
Violation violation = new Violation();
|
|
|
|
|
|
|
|
|
|
if (result != null) {
|
|
|
|
|
violation.setGlobalSearchResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
violation.setHost(imageResult.getHost());
|
|
|
|
|
violation.setUrl(url);
|
2026-03-11 15:42:06 +07:00
|
|
|
violation.setUrlHash(urlHash);
|
2026-03-10 22:47:43 +07:00
|
|
|
violation.setPageUrl(imageResult.getPageUrl());
|
|
|
|
|
violation.setPageTitle(imageResult.getPageTitle());
|
|
|
|
|
violation.setFileEntity(file);
|
|
|
|
|
violation.setStatus("CREATED");
|
|
|
|
|
|
|
|
|
|
violationRepository.save(violation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Violation> violationsByFileId(FileEntity file) {
|
|
|
|
|
return violationRepository.findByFileEntity(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void changeStatusViolation(Long violationId, String status) {
|
|
|
|
|
Violation violation = violationRepository.findById(violationId)
|
|
|
|
|
.orElseThrow(() -> new ViolationNotFoundException("Violation not found with id: " + violationId));
|
|
|
|
|
violation.setStatus(status);
|
|
|
|
|
violationRepository.save(violation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Page<Violation> getViolationsByFile(FileEntity file, int page, int size, String sortDirection) {
|
|
|
|
|
Sort sort = Sort.by(sortDirection.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC, "createdDate");
|
|
|
|
|
Pageable pageable = PageRequest.of(page, size, sort);
|
|
|
|
|
return violationRepository.findByFileEntity(file, pageable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Page<Violation> getViolationsByFileAndStatus(FileEntity file, String status, int page, int size, String sortDirection) {
|
|
|
|
|
Sort sort = Sort.by(sortDirection.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC, "createdDate");
|
|
|
|
|
Pageable pageable = PageRequest.of(page, size, sort);
|
|
|
|
|
return violationRepository.findByFileEntityAndStatus(file, status, pageable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Page<Violation> getViolationsByFileAndDateRange(FileEntity file, LocalDateTime startDate, LocalDateTime endDate,
|
|
|
|
|
int page, int size, String sortDirection) {
|
|
|
|
|
Sort sort = Sort.by(sortDirection.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC, "createdDate");
|
|
|
|
|
Pageable pageable = PageRequest.of(page, size, sort);
|
|
|
|
|
return violationRepository.findByFileEntityAndCreatedDateBetween(file, startDate, endDate, pageable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Map<String, Long> getGroupedViolations(FileEntity file, String groupBy, String status,
|
|
|
|
|
LocalDateTime startDate, LocalDateTime endDate) {
|
|
|
|
|
|
|
|
|
|
List<Violation> violations;
|
|
|
|
|
|
|
|
|
|
if (status != null && !status.isEmpty() && startDate != null && endDate != null) {
|
|
|
|
|
violations = violationRepository.findByFileEntityAndStatusAndCreatedDateBetween(file, status, startDate, endDate);
|
|
|
|
|
} else if (status != null && !status.isEmpty()) {
|
|
|
|
|
violations = violationRepository.findByFileEntityAndStatus(file, status);
|
|
|
|
|
} else if (startDate != null && endDate != null) {
|
|
|
|
|
violations = violationRepository.findByFileEntityAndCreatedDateBetween(file, startDate, endDate);
|
|
|
|
|
} else {
|
|
|
|
|
violations = violationRepository.findByFileEntity(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return violations.stream()
|
|
|
|
|
.collect(Collectors.groupingBy(
|
2026-03-11 16:30:26 +07:00
|
|
|
v -> extractGroupKey(v.getPageUrl(), groupBy),
|
2026-03-10 22:47:43 +07:00
|
|
|
Collectors.counting()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|