@@ -33,4 +33,7 @@ public class ViolationRequest {
|
|||||||
|
|
||||||
@JsonProperty("action")
|
@JsonProperty("action")
|
||||||
private String action;
|
private String action;
|
||||||
|
|
||||||
|
@JsonProperty("token")
|
||||||
|
private String token;
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,8 @@ import ru.soune.nocopy.dto.violation.ViolationResponse;
|
|||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
import ru.soune.nocopy.entity.file.FileEntity;
|
||||||
import ru.soune.nocopy.entity.violation.Violation;
|
import ru.soune.nocopy.entity.violation.Violation;
|
||||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||||
|
import ru.soune.nocopy.service.file.FileEntityService;
|
||||||
|
import ru.soune.nocopy.service.register.AuthService;
|
||||||
import ru.soune.nocopy.service.violation.ViolationService;
|
import ru.soune.nocopy.service.violation.ViolationService;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@@ -31,6 +33,10 @@ public class ViolationHandler implements RequestHandler {
|
|||||||
|
|
||||||
private final FileEntityRepository fileRepository;
|
private final FileEntityRepository fileRepository;
|
||||||
|
|
||||||
|
private final AuthService authService;
|
||||||
|
|
||||||
|
private final FileEntityService fileEntityService;
|
||||||
|
|
||||||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -38,9 +44,19 @@ public class ViolationHandler implements RequestHandler {
|
|||||||
ViolationRequest violationRequest = objectMapper.convertValue(request.getMessageBody(), ViolationRequest.class);
|
ViolationRequest violationRequest = objectMapper.convertValue(request.getMessageBody(), ViolationRequest.class);
|
||||||
|
|
||||||
validateRequest(violationRequest);
|
validateRequest(violationRequest);
|
||||||
//TODO add response
|
|
||||||
FileEntity file = fileRepository.findById(violationRequest.getFileId())
|
String token = violationRequest.getToken();
|
||||||
.orElseThrow(() -> new FileNotFoundException("File not found with id: " + violationRequest.getFileId()));
|
Long userId = authService.useUserAuthToken(token);
|
||||||
|
|
||||||
|
List<FileEntity> targetFiles;
|
||||||
|
|
||||||
|
if (violationRequest.getFileId() != null && !violationRequest.getFileId().isEmpty()) {
|
||||||
|
FileEntity file = fileRepository.findById(violationRequest.getFileId())
|
||||||
|
.orElseThrow(() -> new FileNotFoundException("File not found with id: " + violationRequest.getFileId()));
|
||||||
|
targetFiles = List.of(file);
|
||||||
|
} else {
|
||||||
|
targetFiles = fileEntityService.getAllUserFiles(userId);
|
||||||
|
}
|
||||||
|
|
||||||
LocalDateTime startDate = null;
|
LocalDateTime startDate = null;
|
||||||
LocalDateTime endDate = null;
|
LocalDateTime endDate = null;
|
||||||
@@ -52,12 +68,11 @@ public class ViolationHandler implements RequestHandler {
|
|||||||
|
|
||||||
if ("group".equalsIgnoreCase(violationRequest.getAction())) {
|
if ("group".equalsIgnoreCase(violationRequest.getAction())) {
|
||||||
Map<String, Long> groupedData = violationService.getGroupedViolations(
|
Map<String, Long> groupedData = violationService.getGroupedViolations(
|
||||||
file,
|
targetFiles,
|
||||||
violationRequest.getGroupBy(),
|
violationRequest.getGroupBy(),
|
||||||
violationRequest.getStatus(),
|
violationRequest.getStatus(),
|
||||||
startDate,
|
startDate,
|
||||||
endDate
|
endDate);
|
||||||
);
|
|
||||||
|
|
||||||
return BaseResponse.builder()
|
return BaseResponse.builder()
|
||||||
.msgId(request.getMsgId())
|
.msgId(request.getMsgId())
|
||||||
@@ -67,18 +82,18 @@ public class ViolationHandler implements RequestHandler {
|
|||||||
Page<Violation> violationPage;
|
Page<Violation> violationPage;
|
||||||
|
|
||||||
if (startDate != null && endDate != null) {
|
if (startDate != null && endDate != null) {
|
||||||
violationPage = violationService.getViolationsByFileAndDateRange(
|
violationPage = violationService.getViolationsByFilesAndDateRange(
|
||||||
file, startDate, endDate,
|
targetFiles, startDate, endDate,
|
||||||
violationRequest.getPage(), violationRequest.getSize(), violationRequest.getSortDirection()
|
violationRequest.getPage(), violationRequest.getSize(), violationRequest.getSortDirection()
|
||||||
);
|
);
|
||||||
} else if (violationRequest.getStatus() != null && !violationRequest.getStatus().isEmpty()) {
|
} else if (violationRequest.getStatus() != null && !violationRequest.getStatus().isEmpty()) {
|
||||||
violationPage = violationService.getViolationsByFileAndStatus(
|
violationPage = violationService.getViolationsByFilesAndStatus(
|
||||||
file, violationRequest.getStatus(),
|
targetFiles, violationRequest.getStatus(),
|
||||||
violationRequest.getPage(), violationRequest.getSize(), violationRequest.getSortDirection()
|
violationRequest.getPage(), violationRequest.getSize(), violationRequest.getSortDirection()
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
violationPage = violationService.getViolationsByFile(
|
violationPage = violationService.getViolationsByFiles(
|
||||||
file,
|
targetFiles,
|
||||||
violationRequest.getPage(), violationRequest.getSize(), violationRequest.getSortDirection()
|
violationRequest.getPage(), violationRequest.getSize(), violationRequest.getSortDirection()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -105,12 +120,7 @@ public class ViolationHandler implements RequestHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO add response exception
|
|
||||||
private void validateRequest(ViolationRequest request) {
|
private void validateRequest(ViolationRequest request) {
|
||||||
if (request.getFileId() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.getPage() < 0) {
|
if (request.getPage() < 0) {
|
||||||
request.setPage(0);
|
request.setPage(0);
|
||||||
}
|
}
|
||||||
@@ -125,7 +135,6 @@ public class ViolationHandler implements RequestHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO add response exception
|
|
||||||
private LocalDateTime parseDate(String dateStr) {
|
private LocalDateTime parseDate(String dateStr) {
|
||||||
try {
|
try {
|
||||||
return LocalDateTime.parse(dateStr, DATE_FORMATTER);
|
return LocalDateTime.parse(dateStr, DATE_FORMATTER);
|
||||||
|
|||||||
@@ -41,6 +41,18 @@ public interface ViolationRepository extends JpaRepository<Violation, Long> {
|
|||||||
|
|
||||||
List<Violation> findByFileEntityIn(List<FileEntity> files);
|
List<Violation> findByFileEntityIn(List<FileEntity> files);
|
||||||
|
|
||||||
|
Page<Violation> findByFileEntityIn(List<FileEntity> files, Pageable pageable);
|
||||||
|
|
||||||
|
Page<Violation> findByFileEntityInAndStatus(List<FileEntity> files, String status, Pageable pageable);
|
||||||
|
|
||||||
|
List<Violation> findByFileEntityInAndStatus(List<FileEntity> files, String status);
|
||||||
|
|
||||||
|
Page<Violation> findByFileEntityInAndCreatedDateBetween(List<FileEntity> files, LocalDateTime startDate,
|
||||||
|
LocalDateTime endDate, Pageable pageable);
|
||||||
|
|
||||||
|
List<Violation> findByFileEntityInAndStatusAndCreatedDateBetween(List<FileEntity> files, String status,
|
||||||
|
LocalDateTime startDate, LocalDateTime endDate);
|
||||||
|
|
||||||
List<Violation> findByFileEntityInAndCreatedDateBetween(List<FileEntity> files,
|
List<Violation> findByFileEntityInAndCreatedDateBetween(List<FileEntity> files,
|
||||||
LocalDateTime startDate,
|
LocalDateTime startDate,
|
||||||
LocalDateTime endDate);
|
LocalDateTime endDate);
|
||||||
|
|||||||
@@ -146,6 +146,46 @@ public class ViolationService {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Page<Violation> getViolationsByFiles(List<FileEntity> files, 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.findByFileEntityIn(files, pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Page<Violation> getViolationsByFilesAndStatus(List<FileEntity> files, 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.findByFileEntityInAndStatus(files, status, pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Page<Violation> getViolationsByFilesAndDateRange(List<FileEntity> files, 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.findByFileEntityInAndCreatedDateBetween(files, startDate, endDate, pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Long> getGroupedViolations(List<FileEntity> files, String groupBy, String status,
|
||||||
|
LocalDateTime startDate, LocalDateTime endDate) {
|
||||||
|
List<Violation> violations;
|
||||||
|
|
||||||
|
if (status != null && !status.isEmpty() && startDate != null && endDate != null) {
|
||||||
|
violations = violationRepository.findByFileEntityInAndStatusAndCreatedDateBetween(files, status, startDate, endDate);
|
||||||
|
} else if (status != null && !status.isEmpty()) {
|
||||||
|
violations = violationRepository.findByFileEntityInAndStatus(files, status);
|
||||||
|
} else if (startDate != null && endDate != null) {
|
||||||
|
violations = violationRepository.findByFileEntityInAndCreatedDateBetween(files, startDate, endDate);
|
||||||
|
} else {
|
||||||
|
violations = violationRepository.findByFileEntityIn(files);
|
||||||
|
}
|
||||||
|
|
||||||
|
return violations.stream()
|
||||||
|
.collect(Collectors.groupingBy(
|
||||||
|
v -> extractGroupKey(v.getPageUrl(), groupBy),
|
||||||
|
Collectors.counting()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
public ViolationStatisticsResponse getViolationStatistics(Long userId, String fileId,
|
public ViolationStatisticsResponse getViolationStatistics(Long userId, String fileId,
|
||||||
LocalDateTime startDate, LocalDateTime endDate) {
|
LocalDateTime startDate, LocalDateTime endDate) {
|
||||||
|
|
||||||
@@ -207,7 +247,6 @@ public class ViolationService {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String extractGroupKey(String url, String groupBy) {
|
private String extractGroupKey(String url, String groupBy) {
|
||||||
try {
|
try {
|
||||||
String domain = url.replaceAll("https?://(www\\.)?", "").split("/")[0];
|
String domain = url.replaceAll("https?://(www\\.)?", "").split("/")[0];
|
||||||
|
|||||||
Reference in New Issue
Block a user