Improve metadata quality assessment with new Metadata Match Score and filtering feature
This commit is contained in:
committed by
Aditya Chandel
parent
986599a913
commit
49b56b78e9
+11
-2
@@ -3,9 +3,11 @@ package com.adityachandel.booklore.controller;
|
||||
import com.adityachandel.booklore.mapper.BookMetadataMapper;
|
||||
import com.adityachandel.booklore.model.dto.BookMetadata;
|
||||
import com.adityachandel.booklore.model.dto.request.*;
|
||||
import com.adityachandel.booklore.model.dto.settings.MetadataMatchWeights;
|
||||
import com.adityachandel.booklore.quartz.JobSchedulerService;
|
||||
import com.adityachandel.booklore.service.metadata.BookMetadataService;
|
||||
import com.adityachandel.booklore.service.metadata.BookMetadataUpdater;
|
||||
import com.adityachandel.booklore.service.metadata.MetadataMatchService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
@@ -24,6 +26,7 @@ public class MetadataController {
|
||||
private final BookMetadataUpdater bookMetadataUpdater;
|
||||
private final JobSchedulerService jobSchedulerService;
|
||||
private final BookMetadataMapper bookMetadataMapper;
|
||||
private final MetadataMatchService metadataMatchService;
|
||||
|
||||
@PostMapping("/{bookId}/metadata/prospective")
|
||||
@PreAuthorize("@securityUtil.canEditMetadata() or @securityUtil.isAdmin()")
|
||||
@@ -36,8 +39,7 @@ public class MetadataController {
|
||||
public ResponseEntity<BookMetadata> updateMetadata(
|
||||
@RequestBody BookMetadata setMetadataRequest, @PathVariable long bookId,
|
||||
@RequestParam(defaultValue = "true") boolean mergeCategories) {
|
||||
BookMetadata bookMetadata = bookMetadataMapper.toBookMetadata(
|
||||
bookMetadataUpdater.setBookMetadata(bookId, setMetadataRequest, true, mergeCategories), true);
|
||||
BookMetadata bookMetadata = bookMetadataMapper.toBookMetadata(bookMetadataUpdater.setBookMetadata(bookId, setMetadataRequest, true, mergeCategories), true);
|
||||
return ResponseEntity.ok(bookMetadata);
|
||||
}
|
||||
|
||||
@@ -79,4 +81,11 @@ public class MetadataController {
|
||||
public void regenerateCovers(@PathVariable Long bookId) {
|
||||
bookMetadataService.regenerateCover(bookId);
|
||||
}
|
||||
|
||||
@PostMapping("/metadata/recalculate-match-scores")
|
||||
@PreAuthorize("@securityUtil.isAdmin()")
|
||||
public ResponseEntity<Void> recalculateMatchScores() {
|
||||
metadataMatchService.recalculateAllMatchScores();
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ public class Book {
|
||||
private Instant lastReadTime;
|
||||
private Instant addedOn;
|
||||
private BookMetadata metadata;
|
||||
private Float metadataMatchScore;
|
||||
private PdfProgress pdfProgress;
|
||||
private EpubProgress epubProgress;
|
||||
private CbxProgress cbxProgress;
|
||||
|
||||
+1
@@ -10,6 +10,7 @@ public enum AppSettingKey {
|
||||
SIDEBAR_LIBRARY_SORTING("sidebar_library_sorting", true),
|
||||
SIDEBAR_SHELF_SORTING("sidebar_shelf_sorting", true),
|
||||
METADATA_PROVIDER_SETTINGS("metadata_provider_settings", true),
|
||||
METADATA_MATCH_WEIGHTS("metadata_match_weights", true),
|
||||
|
||||
AUTO_BOOK_SEARCH("auto_book_search", false),
|
||||
COVER_IMAGE_RESOLUTION("cover_image_resolution", false),
|
||||
|
||||
+1
@@ -26,4 +26,5 @@ public class AppSettings {
|
||||
private OidcProviderDetails oidcProviderDetails;
|
||||
private OidcAutoProvisionDetails oidcAutoProvisionDetails;
|
||||
private MetadataProviderSettings metadataProviderSettings;
|
||||
private MetadataMatchWeights metadataMatchWeights;
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.adityachandel.booklore.model.dto.settings;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MetadataMatchWeights {
|
||||
private int title;
|
||||
private int subtitle;
|
||||
private int description;
|
||||
private int authors;
|
||||
private int publisher;
|
||||
private int publishedDate;
|
||||
private int seriesName;
|
||||
private int seriesNumber;
|
||||
private int seriesTotal;
|
||||
private int isbn13;
|
||||
private int isbn10;
|
||||
private int language;
|
||||
private int pageCount;
|
||||
private int categories;
|
||||
private int amazonRating;
|
||||
private int amazonReviewCount;
|
||||
private int goodreadsRating;
|
||||
private int goodreadsReviewCount;
|
||||
private int hardcoverRating;
|
||||
private int hardcoverReviewCount;
|
||||
private int coverImage;
|
||||
|
||||
public int totalWeight() {
|
||||
return title + subtitle + description + authors + publisher + publishedDate +
|
||||
seriesName + seriesNumber + seriesTotal + isbn13 + isbn10 + language +
|
||||
pageCount + categories + amazonRating + amazonReviewCount +
|
||||
goodreadsRating + goodreadsReviewCount + hardcoverRating +
|
||||
hardcoverReviewCount + coverImage;
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,9 @@ public class BookEntity {
|
||||
@Column(name = "file_size_kb")
|
||||
private Long fileSizeKb;
|
||||
|
||||
@Column(name = "metadata_match_score")
|
||||
private Float metadataMatchScore;
|
||||
|
||||
@OneToOne(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
|
||||
private BookMetadataEntity metadata;
|
||||
|
||||
|
||||
+1
@@ -68,6 +68,7 @@ public class AppSettingService {
|
||||
builder.oidcProviderDetails(settingPersistenceHelper.getJsonSetting(settingsMap, AppSettingKey.OIDC_PROVIDER_DETAILS, OidcProviderDetails.class, null, false));
|
||||
builder.oidcAutoProvisionDetails(settingPersistenceHelper.getJsonSetting(settingsMap, AppSettingKey.OIDC_AUTO_PROVISION_DETAILS, OidcAutoProvisionDetails.class, null, false));
|
||||
builder.metadataProviderSettings(settingPersistenceHelper.getJsonSetting(settingsMap, AppSettingKey.METADATA_PROVIDER_SETTINGS, MetadataProviderSettings.class, settingPersistenceHelper.getDefaultMetadataProviderSettings(), true));
|
||||
builder.metadataMatchWeights(settingPersistenceHelper.getJsonSetting(settingsMap, AppSettingKey.METADATA_MATCH_WEIGHTS, MetadataMatchWeights.class, settingPersistenceHelper.getDefaultMetadataMatchWeights(), true));
|
||||
|
||||
builder.coverResolution(settingPersistenceHelper.getOrCreateSetting(AppSettingKey.COVER_IMAGE_RESOLUTION, "250x350"));
|
||||
builder.autoBookSearch(Boolean.parseBoolean(settingPersistenceHelper.getOrCreateSetting(AppSettingKey.AUTO_BOOK_SEARCH, "true")));
|
||||
|
||||
+27
@@ -2,6 +2,7 @@ package com.adityachandel.booklore.service.appsettings;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.request.MetadataRefreshOptions;
|
||||
import com.adityachandel.booklore.model.dto.settings.AppSettingKey;
|
||||
import com.adityachandel.booklore.model.dto.settings.MetadataMatchWeights;
|
||||
import com.adityachandel.booklore.model.dto.settings.MetadataProviderSettings;
|
||||
import com.adityachandel.booklore.model.entity.AppSettingEntity;
|
||||
import com.adityachandel.booklore.model.enums.MetadataProvider;
|
||||
@@ -140,4 +141,30 @@ public class SettingPersistenceHelper {
|
||||
fieldOptions
|
||||
);
|
||||
}
|
||||
|
||||
public MetadataMatchWeights getDefaultMetadataMatchWeights() {
|
||||
return MetadataMatchWeights.builder()
|
||||
.title(10)
|
||||
.subtitle(1)
|
||||
.description(10)
|
||||
.authors(10)
|
||||
.publisher(5)
|
||||
.publishedDate(3)
|
||||
.seriesName(2)
|
||||
.seriesNumber(2)
|
||||
.seriesTotal(1)
|
||||
.isbn13(3)
|
||||
.isbn10(5)
|
||||
.language(2)
|
||||
.pageCount(1)
|
||||
.categories(10)
|
||||
.amazonRating(3)
|
||||
.amazonReviewCount(2)
|
||||
.goodreadsRating(4)
|
||||
.goodreadsReviewCount(2)
|
||||
.hardcoverRating(2)
|
||||
.hardcoverReviewCount(1)
|
||||
.coverImage(5)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+4
@@ -8,6 +8,7 @@ import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import com.adityachandel.booklore.repository.BookMetadataRepository;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.service.BookCreatorService;
|
||||
import com.adityachandel.booklore.service.metadata.MetadataMatchService;
|
||||
import com.adityachandel.booklore.util.FileUtils;
|
||||
import com.github.junrar.Archive;
|
||||
import com.github.junrar.rarfile.FileHeader;
|
||||
@@ -37,6 +38,7 @@ public class CbxProcessor implements FileProcessor {
|
||||
private final BookMapper bookMapper;
|
||||
private final FileProcessingUtils fileProcessingUtils;
|
||||
private final BookMetadataRepository bookMetadataRepository;
|
||||
private final MetadataMatchService metadataMatchService;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
@Override
|
||||
@@ -58,6 +60,8 @@ public class CbxProcessor implements FileProcessor {
|
||||
fileProcessingUtils.setBookCoverPath(bookEntity.getId(), bookEntity.getMetadata());
|
||||
}
|
||||
setMetadata(bookEntity);
|
||||
Float score = metadataMatchService.calculateMatchScore(bookEntity);
|
||||
bookEntity.setMetadataMatchScore(score);
|
||||
bookCreatorService.saveConnections(bookEntity);
|
||||
bookEntity = bookRepository.save(bookEntity);
|
||||
bookRepository.flush();
|
||||
|
||||
+14
-12
@@ -23,6 +23,7 @@ import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.service.metadata.MetadataMatchService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
@@ -60,6 +61,7 @@ public class EpubProcessor implements FileProcessor {
|
||||
private final BookMapper bookMapper;
|
||||
private final FileProcessingUtils fileProcessingUtils;
|
||||
private final BookMetadataRepository bookMetadataRepository;
|
||||
private final MetadataMatchService metadataMatchService;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
@Override
|
||||
@@ -80,6 +82,8 @@ public class EpubProcessor implements FileProcessor {
|
||||
protected Book processNewFile(LibraryFile libraryFile) {
|
||||
BookEntity bookEntity = bookCreatorService.createShellBook(libraryFile, BookFileType.EPUB);
|
||||
setBookMetadata(bookEntity);
|
||||
Float score = metadataMatchService.calculateMatchScore(bookEntity);
|
||||
bookEntity.setMetadataMatchScore(score);
|
||||
if (generateCover(bookEntity)) {
|
||||
fileProcessingUtils.setBookCoverPath(bookEntity.getId(), bookEntity.getMetadata());
|
||||
}
|
||||
@@ -104,7 +108,7 @@ public class EpubProcessor implements FileProcessor {
|
||||
String href = res.getHref();
|
||||
|
||||
if ((id != null && id.toLowerCase().contains("cover")) ||
|
||||
(href != null && href.toLowerCase().contains("cover"))) {
|
||||
(href != null && href.toLowerCase().contains("cover"))) {
|
||||
|
||||
if (res.getMediaType() != null && res.getMediaType().getName().startsWith("image")) {
|
||||
coverImage = res;
|
||||
@@ -133,14 +137,12 @@ public class EpubProcessor implements FileProcessor {
|
||||
|
||||
private void setBookMetadata(BookEntity bookEntity) {
|
||||
log.debug("***Setting metadata for book {}", bookEntity.getFileName());
|
||||
try (FileInputStream fis =
|
||||
new FileInputStream(FileUtils.getBookFullPath(bookEntity))) {
|
||||
try (FileInputStream fis = new FileInputStream(FileUtils.getBookFullPath(bookEntity))) {
|
||||
|
||||
io.documentnode.epub4j.domain.Book book =
|
||||
new EpubReader().readEpub(fis);
|
||||
io.documentnode.epub4j.domain.Book book = new EpubReader().readEpub(fis);
|
||||
|
||||
BookMetadataEntity bookMetadata = bookEntity.getMetadata();
|
||||
Metadata epubMetadata = book.getMetadata();
|
||||
Metadata epubMetadata = book.getMetadata();
|
||||
|
||||
if (epubMetadata != null) {
|
||||
bookMetadata.setTitle(truncate(epubMetadata.getFirstTitle(), 1000));
|
||||
@@ -207,7 +209,7 @@ public class EpubProcessor implements FileProcessor {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Calibre (EPUB2) series tags
|
||||
String seriesName = epubMetadata.getMetaAttribute("calibre:series");
|
||||
if (seriesName != null && !seriesName.isEmpty()) {
|
||||
@@ -228,18 +230,18 @@ public class EpubProcessor implements FileProcessor {
|
||||
extractFromOpf(FileUtils.getBookFullPath(bookEntity), bookMetadata);
|
||||
|
||||
bookCreatorService.addAuthorsToBook(getAuthors(book), bookEntity);
|
||||
|
||||
|
||||
// Get subjects and filter out invalid ones
|
||||
List<String> subjects = epubMetadata.getSubjects();
|
||||
List<String> validSubjects = new ArrayList<>();
|
||||
|
||||
|
||||
if (subjects != null && !subjects.isEmpty()) {
|
||||
for (String subject : subjects) {
|
||||
// Skip null or empty subjects
|
||||
if (subject == null || subject.trim().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Skip subjects that are too long (likely not real categories)
|
||||
// Real categories are typically short, descriptive terms
|
||||
if (subject.length() > 100) {
|
||||
@@ -248,7 +250,7 @@ public class EpubProcessor implements FileProcessor {
|
||||
subject.substring(0, Math.min(50, subject.length())) + "...");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Skip subjects that contain newlines or excessive whitespace (likely descriptions)
|
||||
if (subject.contains("\n") || subject.contains("\r") || subject.contains(" ")) {
|
||||
log.warn("Skipping subject with newlines/whitespace in {}: '{}'",
|
||||
@@ -260,7 +262,7 @@ public class EpubProcessor implements FileProcessor {
|
||||
validSubjects.add(subject);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bookCreatorService.addCategoriesToBook(validSubjects, bookEntity);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
+4
@@ -8,6 +8,7 @@ import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import com.adityachandel.booklore.repository.BookMetadataRepository;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.service.BookCreatorService;
|
||||
import com.adityachandel.booklore.service.metadata.MetadataMatchService;
|
||||
import com.adityachandel.booklore.util.FileUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -39,6 +40,7 @@ public class PdfProcessor implements FileProcessor {
|
||||
private final BookMapper bookMapper;
|
||||
private final FileProcessingUtils fileProcessingUtils;
|
||||
private final BookMetadataRepository bookMetadataRepository;
|
||||
private final MetadataMatchService metadataMatchService;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
@Override
|
||||
@@ -62,6 +64,8 @@ public class PdfProcessor implements FileProcessor {
|
||||
fileProcessingUtils.setBookCoverPath(bookEntity.getId(), bookEntity.getMetadata());
|
||||
}
|
||||
setMetadata(bookEntity);
|
||||
Float score = metadataMatchService.calculateMatchScore(bookEntity);
|
||||
bookEntity.setMetadataMatchScore(score);
|
||||
bookCreatorService.saveConnections(bookEntity);
|
||||
bookEntity = bookRepository.save(bookEntity);
|
||||
bookRepository.flush();
|
||||
|
||||
-1
@@ -136,7 +136,6 @@ public class BookMetadataService {
|
||||
if (metadata != null) {
|
||||
BookMetadataEntity bookMetadata = bookMetadataUpdater.setBookMetadata(bookEntity.getId(), metadata, replaceCover, mergeCategories);
|
||||
bookEntity.setMetadata(bookMetadata);
|
||||
bookRepository.save(bookEntity);
|
||||
Book book = bookMapper.toBook(bookEntity);
|
||||
notificationService.sendMessage(Topic.BOOK_METADATA_UPDATE, book);
|
||||
notificationService.sendMessage(Topic.LOG, createLogNotification("Book metadata updated: " + book.getMetadata().getTitle()));
|
||||
|
||||
+6
@@ -32,6 +32,7 @@ public class BookMetadataUpdater {
|
||||
private final BookMetadataRepository bookMetadataRepository;
|
||||
private final CategoryRepository categoryRepository;
|
||||
private final FileService fileService;
|
||||
private final MetadataMatchService metadataMatchService;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public BookMetadataEntity setBookMetadata(long bookId, BookMetadata newMetadata, boolean setThumbnail, boolean mergeCategories) {
|
||||
@@ -51,6 +52,11 @@ public class BookMetadataUpdater {
|
||||
updateThumbnailIfNeeded(bookId, newMetadata, metadata, setThumbnail);
|
||||
|
||||
bookMetadataRepository.save(metadata);
|
||||
|
||||
Float score = metadataMatchService.calculateMatchScore(bookEntity);
|
||||
bookEntity.setMetadataMatchScore(score);
|
||||
bookRepository.save(bookEntity);
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.adityachandel.booklore.service.metadata;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.dto.BookMetadata;
|
||||
import com.adityachandel.booklore.model.dto.settings.AppSettings;
|
||||
import com.adityachandel.booklore.model.dto.settings.MetadataMatchWeights;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
||||
import com.adityachandel.booklore.service.BookQueryService;
|
||||
import com.adityachandel.booklore.service.BookService;
|
||||
import com.adityachandel.booklore.service.appsettings.AppSettingService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class MetadataMatchService {
|
||||
|
||||
private final AppSettingService appSettingsService;
|
||||
private final BookQueryService bookQueryService;
|
||||
|
||||
public void recalculateAllMatchScores() {
|
||||
List<BookEntity> allBooks = bookQueryService.getAllFullBookEntities();
|
||||
for (BookEntity book : allBooks) {
|
||||
Float score = calculateMatchScore(book);
|
||||
book.setMetadataMatchScore(score);
|
||||
}
|
||||
bookQueryService.saveAll(allBooks);
|
||||
}
|
||||
|
||||
public Float calculateMatchScore(BookEntity book) {
|
||||
if (book == null || book.getMetadata() == null) return 0f;
|
||||
|
||||
BookMetadataEntity metadata = book.getMetadata();
|
||||
|
||||
AppSettings appSettings = appSettingsService.getAppSettings();
|
||||
if (appSettings == null || appSettings.getMetadataMatchWeights() == null) return 0f;
|
||||
|
||||
MetadataMatchWeights weights = appSettings.getMetadataMatchWeights();
|
||||
float totalWeight = weights.totalWeight();
|
||||
if (totalWeight == 0) return 0f;
|
||||
|
||||
float score = 0f;
|
||||
|
||||
if (isPresent(metadata.getTitle())) score += weights.getTitle();
|
||||
if (isPresent(metadata.getSubtitle())) score += weights.getSubtitle();
|
||||
if (isPresent(metadata.getDescription())) score += weights.getDescription();
|
||||
if (hasContent(metadata.getAuthors())) score += weights.getAuthors();
|
||||
if (isPresent(metadata.getPublisher())) score += weights.getPublisher();
|
||||
if (metadata.getPublishedDate() != null) score += weights.getPublishedDate();
|
||||
if (isPresent(metadata.getSeriesName())) score += weights.getSeriesName();
|
||||
if (metadata.getSeriesNumber() != null && metadata.getSeriesNumber() > 0) score += weights.getSeriesNumber();
|
||||
if (metadata.getSeriesTotal() != null && metadata.getSeriesTotal() > 0) score += weights.getSeriesTotal();
|
||||
if (isPresent(metadata.getIsbn13())) score += weights.getIsbn13();
|
||||
if (isPresent(metadata.getIsbn10())) score += weights.getIsbn10();
|
||||
if (isPresent(metadata.getLanguage())) score += weights.getLanguage();
|
||||
if (metadata.getPageCount() != null && metadata.getPageCount() > 0) score += weights.getPageCount();
|
||||
if (hasContent(metadata.getCategories())) score += weights.getCategories();
|
||||
if (isPositive(metadata.getAmazonRating())) score += weights.getAmazonRating();
|
||||
if (isPositive(metadata.getAmazonReviewCount())) score += weights.getAmazonReviewCount();
|
||||
if (isPositive(metadata.getGoodreadsRating())) score += weights.getGoodreadsRating();
|
||||
if (isPositive(metadata.getGoodreadsReviewCount())) score += weights.getGoodreadsReviewCount();
|
||||
if (isPositive(metadata.getHardcoverRating())) score += weights.getHardcoverRating();
|
||||
if (isPositive(metadata.getHardcoverReviewCount())) score += weights.getHardcoverReviewCount();
|
||||
if (isPresent(metadata.getThumbnail())) score += weights.getCoverImage();
|
||||
|
||||
return score / totalWeight;
|
||||
}
|
||||
|
||||
private boolean isPresent(String value) {
|
||||
return value != null && !value.isBlank();
|
||||
}
|
||||
|
||||
private boolean hasContent(Iterable<?> iterable) {
|
||||
return iterable != null && iterable.iterator().hasNext();
|
||||
}
|
||||
|
||||
private boolean isPositive(Number number) {
|
||||
return number != null && number.doubleValue() > 0;
|
||||
}
|
||||
}
|
||||
+19
-8
@@ -1,17 +1,12 @@
|
||||
package com.adityachandel.booklore.service.migration;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookLoreUser;
|
||||
import com.adityachandel.booklore.model.dto.settings.UserSettingKey;
|
||||
import com.adityachandel.booklore.model.entity.AppMigrationEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookLoreUserEntity;
|
||||
import com.adityachandel.booklore.model.entity.UserSettingEntity;
|
||||
import com.adityachandel.booklore.repository.AppMigrationRepository;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.repository.UserRepository;
|
||||
import com.adityachandel.booklore.service.user.UserProvisioningService;
|
||||
import com.adityachandel.booklore.service.BookQueryService;
|
||||
import com.adityachandel.booklore.service.metadata.MetadataMatchService;
|
||||
import com.adityachandel.booklore.util.FileUtils;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -19,7 +14,6 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
@@ -28,6 +22,8 @@ public class AppMigrationService {
|
||||
|
||||
private AppMigrationRepository migrationRepository;
|
||||
private BookRepository bookRepository;
|
||||
private BookQueryService bookQueryService;
|
||||
private MetadataMatchService metadataMatchService;
|
||||
|
||||
@Transactional
|
||||
public void populateMissingFileSizesOnce() {
|
||||
@@ -52,4 +48,19 @@ public class AppMigrationService {
|
||||
migrationRepository.save(migration);
|
||||
log.info("Migration 'populateFileSizes' executed successfully.");
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void populateMetadataScoresOnce() {
|
||||
if (migrationRepository.existsById("populateMetadataScores")) return;
|
||||
|
||||
List<BookEntity> books = bookQueryService.getAllFullBookEntities();
|
||||
for (BookEntity book : books) {
|
||||
Float score = metadataMatchService.calculateMatchScore(book);
|
||||
book.setMetadataMatchScore(score);
|
||||
}
|
||||
bookRepository.saveAll(books);
|
||||
|
||||
log.info("Migration 'populateMetadataScores' applied to {} books.", books.size());
|
||||
migrationRepository.save(new AppMigrationEntity("populateMetadataScores", LocalDateTime.now(), "Calculate and store metadata match score for all books"));
|
||||
}
|
||||
}
|
||||
|
||||
+1
@@ -14,5 +14,6 @@ public class AppMigrationStartup {
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void runMigrationsOnce() {
|
||||
appMigrationService.populateMissingFileSizesOnce();
|
||||
appMigrationService.populateMetadataScoresOnce();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE book
|
||||
ADD COLUMN metadata_match_score FLOAT;
|
||||
Reference in New Issue
Block a user