dev add violation notion
This commit is contained in:
@@ -5,11 +5,11 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.nocopy.configuration.search.SearchProperties;
|
||||
import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
||||
import ru.soune.nocopy.dto.tarriff.TariffDTO;
|
||||
import ru.soune.nocopy.entity.monitoring.FileMonitoringEntity;
|
||||
import ru.soune.nocopy.entity.monitoring.MonitoringType;
|
||||
import ru.soune.nocopy.entity.search.GlobalSearchResult;
|
||||
import ru.soune.nocopy.entity.tarif.TariffInfo;
|
||||
import ru.soune.nocopy.entity.tarif.TariffType;
|
||||
import ru.soune.nocopy.entity.user.User;
|
||||
@@ -24,6 +24,7 @@ import ru.soune.nocopy.service.violation.ViolationService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
@@ -45,6 +46,8 @@ public class MonitoringSearchService {
|
||||
|
||||
private final ViolationService violationService;
|
||||
|
||||
private final SearchProperties searchProperties;
|
||||
|
||||
@Transactional
|
||||
public void processFileSearch(FileMonitoringEntity monitoring) throws MessagingException, IOException {
|
||||
monitoring.setLastRun(LocalDateTime.now());
|
||||
@@ -53,36 +56,78 @@ public class MonitoringSearchService {
|
||||
|
||||
MonitoringType monitoringType = monitoring.getMonitoringType();
|
||||
TariffDTO tariffMonitoring = tariffService.getTariffByType(TariffType.valueOf(monitoringType.name()));
|
||||
|
||||
try {
|
||||
tariffInfoService.writeOffTokens(monitoring.getUserId(), tariffMonitoring.getTokens());
|
||||
|
||||
String yandexResponse = searchImageService.searchReverseByPublicUrl(
|
||||
monitoring.getFile(), "yandex_reverse_image", "visual_matches");
|
||||
boolean useYandex = searchProperties.getEngines().getOrDefault("yandex",
|
||||
new SearchProperties.EngineConfig()).isEnabled();
|
||||
boolean useGoogle = searchProperties.getEngines().getOrDefault("google",
|
||||
new SearchProperties.EngineConfig()).isEnabled();
|
||||
|
||||
List<YandexSearchResponse.ImageResult> yandexImages =
|
||||
searchImageService.getAllImagesWithoutPagination(yandexResponse, "visual_matches");
|
||||
log.info("Monitoring search settings: useYandex={}, useGoogle={}", useYandex, useGoogle);
|
||||
|
||||
for (YandexSearchResponse.ImageResult imageResult : yandexImages) {
|
||||
List<YandexSearchResponse.ImageResult> allResults = new ArrayList<>();
|
||||
|
||||
if (useYandex) {
|
||||
try {
|
||||
String yandexResponse = searchImageService.searchReverseByPublicUrl(
|
||||
monitoring.getFile(), "yandex_reverse_image", "visual_matches");
|
||||
|
||||
List<YandexSearchResponse.ImageResult> yandexImages =
|
||||
searchImageService.getAllImagesWithoutPagination(yandexResponse, "visual_matches");
|
||||
|
||||
allResults.addAll(yandexImages);
|
||||
log.info("Yandex search found {} results", yandexImages.size());
|
||||
|
||||
} catch (TimeoutException e) {
|
||||
log.warn("Yandex search timeout for monitoring file {}", monitoring.getFile().getId());
|
||||
} catch (IOException e) {
|
||||
log.error("Yandex search failed for monitoring file {}", monitoring.getFile().getId(), e);
|
||||
}
|
||||
} else {
|
||||
log.info("Yandex search is disabled by settings");
|
||||
}
|
||||
|
||||
if (useGoogle) {
|
||||
try {
|
||||
String googleResponse = searchImageService.searchReverseByPublicUrl(
|
||||
monitoring.getFile(), "google_lens", "exact_matches");
|
||||
|
||||
List<YandexSearchResponse.ImageResult> googleImages =
|
||||
searchImageService.getAllImagesWithoutPagination(googleResponse, "exact_matches");
|
||||
|
||||
allResults.addAll(googleImages);
|
||||
log.info("Google search found {} results", googleImages.size());
|
||||
|
||||
} catch (TimeoutException e) {
|
||||
log.warn("Google search timeout for monitoring file {}", monitoring.getFile().getId());
|
||||
} catch (IOException e) {
|
||||
log.error("Google search failed for monitoring file {}", monitoring.getFile().getId(), e);
|
||||
}
|
||||
} else {
|
||||
log.info("Google search is disabled by settings");
|
||||
}
|
||||
|
||||
List<YandexSearchResponse.ImageResult> uniqueResults = allResults;
|
||||
if (useYandex && useGoogle) {
|
||||
uniqueResults = searchImageService.removeDuplicateUrls(
|
||||
useYandex ? allResults : new ArrayList<>(),
|
||||
useGoogle ? allResults : new ArrayList<>()
|
||||
);
|
||||
log.info("After deduplication: {} unique results", uniqueResults.size());
|
||||
}
|
||||
|
||||
for (YandexSearchResponse.ImageResult imageResult : uniqueResults) {
|
||||
violationService.processViolation(imageResult, monitoring.getFile(), null);
|
||||
}
|
||||
|
||||
// try {
|
||||
// String googleResponse = searchImageService.searchReverseByPublicUrl(
|
||||
// monitoring.getFile(), "google_lens", "exact_matches");
|
||||
//
|
||||
// List<YandexSearchResponse.ImageResult> googleImages =
|
||||
// searchImageService.getAllImagesWithoutPagination(googleResponse, "exact_matches");
|
||||
//
|
||||
//
|
||||
// for (YandexSearchResponse.ImageResult imageResult : googleImages) {
|
||||
// violationService.processViolation(imageResult, monitoring.getFile(), null);
|
||||
// }
|
||||
//
|
||||
// } catch (TimeoutException | IOException e) {
|
||||
// log.warn("Google search failed");
|
||||
// }
|
||||
|
||||
monitoring.setLastRunStatus("SUCCESS");
|
||||
|
||||
if (uniqueResults.isEmpty()) {
|
||||
log.info("No results found for monitoring file {}", monitoring.getFile().getId());
|
||||
}
|
||||
|
||||
} catch (TariffNotFoundException e) {
|
||||
User user = userRepository.findById(monitoring.getUserId()).orElseThrow();
|
||||
TariffInfo activeTariffInfo = user.getActiveTariffInfo();
|
||||
@@ -91,10 +136,12 @@ public class MonitoringSearchService {
|
||||
emailService.sendTokensNotFoundEmail(user, currentTokens, tariffMonitoring.getTokens());
|
||||
monitoring.setLastRunStatus("ERROR: " + e.getMessage());
|
||||
updateNextRun(monitoring);
|
||||
|
||||
monitoringRepository.save(monitoring);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Error processing monitoring search", e);
|
||||
monitoring.setLastRunStatus("ERROR: " + e.getMessage());
|
||||
|
||||
} finally {
|
||||
updateNextRun(monitoring);
|
||||
monitoringRepository.save(monitoring);
|
||||
|
||||
Reference in New Issue
Block a user