This commit is contained in:
@@ -54,51 +54,52 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
|||||||
Map.of("fileId",fileId)));
|
Map.of("fileId",fileId)));
|
||||||
});
|
});
|
||||||
|
|
||||||
// YandexSearchResponse response = yandexSearchService.searchByFileEntity(fileEntity);
|
// YandexSearchResponse response = yandexSearchService.searchByFileEntity(fileId);
|
||||||
|
|
||||||
//TODO uncommited when add billing
|
//TODO uncommited when add billing
|
||||||
// GoogleVisionSearchResponse googleVisionSearchResponse = googleVisionSearchService.searchByFileEntity(fileId);
|
// GoogleVisionSearchResponse googleVisionSearchResponse = googleVisionSearchService.searchByFileEntity(fileId);
|
||||||
|
|
||||||
String searchResponseYandex;
|
|
||||||
String searchResponseGoogle;
|
|
||||||
List<YandexSearchResponse.ImageResult> allUniqueImages;
|
|
||||||
//TODO отдавать любой полученный результат,даже если по таймауту падают
|
//TODO отдавать любой полученный результат,даже если по таймауту падают
|
||||||
|
|
||||||
|
String searchResponseYandex = null;
|
||||||
|
String searchResponseGoogle = null;
|
||||||
|
List<YandexSearchResponse.ImageResult> allYandexImages = new ArrayList<>();
|
||||||
|
List<YandexSearchResponse.ImageResult> allGoogleImages = new ArrayList<>();
|
||||||
|
List<YandexSearchResponse.ImageResult> allUniqueImages;
|
||||||
|
boolean hasTimeout = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
searchResponseYandex = searchImageService.searchReverseByPublicUrl(fileEntity, "yandex_reverse_image",
|
searchResponseYandex = searchImageService.searchReverseByPublicUrl(fileEntity, "yandex_reverse_image",
|
||||||
"visual_matches");
|
"visual_matches");
|
||||||
log.info("searchResponseYandex OK");
|
log.info("searchResponseYandex OK");
|
||||||
List<YandexSearchResponse.ImageResult> allYandexImages = searchImageService.getAllImagesWithoutPagination(
|
allYandexImages = searchImageService.getAllImagesWithoutPagination(
|
||||||
searchResponseYandex, "visual_matches");
|
searchResponseYandex, "visual_matches");
|
||||||
// searchResponseGoogle = searchImageService.searchReverseByPublicUrl(fileEntity, "google_lens",
|
|
||||||
// "exact_matches");
|
|
||||||
|
|
||||||
log.info("searchResponseGoogle OK");
|
|
||||||
// List<YandexSearchResponse.ImageResult> allGoogleImages = searchImageService.getAllImagesWithoutPagination(
|
|
||||||
// searchResponseGoogle, "exact_matches");
|
|
||||||
// allUniqueImages = searchImageService.removeDuplicateUrls(allYandexImages,
|
|
||||||
// allGoogleImages);
|
|
||||||
allUniqueImages = searchImageService.removeDuplicateUrls(allYandexImages,
|
|
||||||
new ArrayList<>());
|
|
||||||
log.info("allUniqueImages OK");
|
|
||||||
|
|
||||||
} catch (TimeoutException e) {
|
} catch (TimeoutException e) {
|
||||||
log.warn("Search timeout for file {}, returning empty results", fileId);
|
log.warn("Yandex search timeout for file {}", fileId);
|
||||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
hasTimeout = true;
|
||||||
"Search completed (partial)", Map.of(
|
|
||||||
"results", List.of(),
|
|
||||||
"total", 0,
|
|
||||||
"searchStatus", "timeout"
|
|
||||||
));
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Search failed for file {}", fileId, e);
|
log.error("Yandex search failed for file {}", fileId, e);
|
||||||
return new BaseResponse(request.getMsgId(), 20007,
|
|
||||||
"Search service temporarily unavailable",
|
|
||||||
Map.of("fileId", fileId));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
try {
|
||||||
|
searchResponseGoogle = searchImageService.searchReverseByPublicUrl(fileEntity, "google_lens",
|
||||||
|
"exact_matches");
|
||||||
|
log.info("searchResponseGoogle OK");
|
||||||
|
allGoogleImages = searchImageService.getAllImagesWithoutPagination(
|
||||||
|
searchResponseGoogle, "exact_matches");
|
||||||
|
} catch (TimeoutException e) {
|
||||||
|
log.warn("Google search timeout for file {}", fileId);
|
||||||
|
hasTimeout = true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Google search failed for file {}", fileId, e);
|
||||||
|
}
|
||||||
|
|
||||||
checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
allUniqueImages = searchImageService.removeDuplicateUrls(allYandexImages, allGoogleImages);
|
||||||
|
log.info("allUniqueImages OK: {} images", allUniqueImages.size());
|
||||||
|
|
||||||
|
if (!allUniqueImages.isEmpty()) {
|
||||||
|
tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
||||||
|
checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
||||||
|
}
|
||||||
|
|
||||||
int page = imageSearchRequest.getPage() != null ? imageSearchRequest.getPage() : 1;
|
int page = imageSearchRequest.getPage() != null ? imageSearchRequest.getPage() : 1;
|
||||||
int pageSize = 5;
|
int pageSize = 5;
|
||||||
@@ -113,7 +114,84 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
|||||||
finalResponse.setTotalResults(allUniqueImages.size());
|
finalResponse.setTotalResults(allUniqueImages.size());
|
||||||
finalResponse.setTotalPages((int) Math.ceil((double) allUniqueImages.size() / (pageSize * 2)));
|
finalResponse.setTotalPages((int) Math.ceil((double) allUniqueImages.size() / (pageSize * 2)));
|
||||||
|
|
||||||
|
String message = hasTimeout ? "Search completed (partial)" : MessageCode.SUCCESS.getDescription();
|
||||||
|
|
||||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||||
MessageCode.SUCCESS.getDescription(), finalResponse);
|
message, finalResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// public BaseResponse handle(BaseRequest request) throws Exception {
|
||||||
|
// ImageSearchRequest imageSearchRequest = objectMapper.convertValue(request.getMessageBody(),
|
||||||
|
// ImageSearchRequest.class);
|
||||||
|
//
|
||||||
|
// String fileId = imageSearchRequest.getFileId();
|
||||||
|
//
|
||||||
|
// FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
||||||
|
// .orElseThrow(() -> {
|
||||||
|
// throw new NotValidFieldException("File not found", new BaseResponse(20007,
|
||||||
|
// MessageCode.FILE_NOT_FOUND.getCode(), MessageCode.FILE_NOT_FOUND.getDescription(),
|
||||||
|
// Map.of("fileId",fileId)));
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
//// YandexSearchResponse response = yandexSearchService.searchByFileEntity(fileEntity);
|
||||||
|
//
|
||||||
|
// //TODO uncommited when add billing
|
||||||
|
//// GoogleVisionSearchResponse googleVisionSearchResponse = googleVisionSearchService.searchByFileEntity(fileId);
|
||||||
|
// //TODO отдавать любой полученный результат,даже если по таймауту падают
|
||||||
|
//
|
||||||
|
// String searchResponseYandex;
|
||||||
|
// String searchResponseGoogle;
|
||||||
|
// List<YandexSearchResponse.ImageResult> allUniqueImages;
|
||||||
|
// try {
|
||||||
|
// searchResponseYandex = searchImageService.searchReverseByPublicUrl(fileEntity, "yandex_reverse_image",
|
||||||
|
// "visual_matches");
|
||||||
|
// log.info("searchResponseYandex OK");
|
||||||
|
// List<YandexSearchResponse.ImageResult> allYandexImages = searchImageService.getAllImagesWithoutPagination(
|
||||||
|
// searchResponseYandex, "visual_matches");
|
||||||
|
// searchResponseGoogle = searchImageService.searchReverseByPublicUrl(fileEntity, "google_lens",
|
||||||
|
// "exact_matches");
|
||||||
|
//
|
||||||
|
// log.info("searchResponseGoogle OK");
|
||||||
|
// List<YandexSearchResponse.ImageResult> allGoogleImages = searchImageService.getAllImagesWithoutPagination(
|
||||||
|
// searchResponseGoogle, "exact_matches");
|
||||||
|
// allUniqueImages = searchImageService.removeDuplicateUrls(allYandexImages,
|
||||||
|
// allGoogleImages);
|
||||||
|
// log.info("allUniqueImages OK");
|
||||||
|
//
|
||||||
|
// } catch (TimeoutException e) {
|
||||||
|
// log.warn("Search timeout for file {}, returning empty results", fileId);
|
||||||
|
// return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||||
|
// "Search completed (partial)", Map.of(
|
||||||
|
// "results", List.of(),
|
||||||
|
// "total", 0,
|
||||||
|
// "searchStatus", "timeout"
|
||||||
|
// ));
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// log.error("Search failed for file {}", fileId, e);
|
||||||
|
// return new BaseResponse(request.getMsgId(), 20007,
|
||||||
|
// "Search service temporarily unavailable",
|
||||||
|
// Map.of("fileId", fileId));
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
||||||
|
//
|
||||||
|
// checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
||||||
|
//
|
||||||
|
// int page = imageSearchRequest.getPage() != null ? imageSearchRequest.getPage() : 1;
|
||||||
|
// int pageSize = 5;
|
||||||
|
//
|
||||||
|
// List<YandexSearchResponse.ImageResult> pagedResults = searchImageService.paginateResults(allUniqueImages, page,
|
||||||
|
// pageSize * 2);
|
||||||
|
//
|
||||||
|
// YandexSearchResponse finalResponse = new YandexSearchResponse();
|
||||||
|
// finalResponse.setImages(pagedResults);
|
||||||
|
// finalResponse.setPage(page);
|
||||||
|
// finalResponse.setPageSize(pageSize * 2);
|
||||||
|
// finalResponse.setTotalResults(allUniqueImages.size());
|
||||||
|
// finalResponse.setTotalPages((int) Math.ceil((double) allUniqueImages.size() / (pageSize * 2)));
|
||||||
|
//
|
||||||
|
// return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||||
|
// MessageCode.SUCCESS.getDescription(), finalResponse);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user