fix-tests-cleanup

This commit is contained in:
Tour
2025-12-08 13:02:21 +01:00
parent 6668ae77e9
commit c46c0fe21e
4 changed files with 351 additions and 234 deletions

View File

@@ -4,6 +4,8 @@ import auctiora.db.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jdbi.v3.core.Jdbi; import org.jdbi.v3.core.Jdbi;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List; import java.util.List;
/** /**
@@ -16,203 +18,253 @@ import java.util.List;
@Slf4j @Slf4j
public class DatabaseService { public class DatabaseService {
private final Jdbi jdbi; private final Jdbi jdbi;
private final LotRepository lotRepository; private final LotRepository lotRepository;
private final AuctionRepository auctionRepository; private final AuctionRepository auctionRepository;
private final ImageRepository imageRepository; private final ImageRepository imageRepository;
/** /**
* Constructor for programmatic instantiation (tests, CLI tools). * Constructor for programmatic instantiation (tests, CLI tools).
*/ */
public DatabaseService(String dbPath) { private final String url;
String url = "jdbc:sqlite:" + dbPath + "?journal_mode=WAL&busy_timeout=10000";
this.jdbi = Jdbi.create(url);
// Initialize schema public DatabaseService(String dbPath) {
DatabaseSchema.ensureSchema(jdbi); this.url = "jdbc:sqlite:" + dbPath + "?journal_mode=WAL&busy_timeout=10000";
this.jdbi = Jdbi.create(url);
// Create repositories // Initialize schema
this.lotRepository = new LotRepository(jdbi); DatabaseSchema.ensureSchema(jdbi);
this.auctionRepository = new AuctionRepository(jdbi);
this.imageRepository = new ImageRepository(jdbi);
}
/** // Create repositories
* Constructor with JDBI instance (for dependency injection). this.lotRepository = new LotRepository(jdbi);
*/ this.auctionRepository = new AuctionRepository(jdbi);
public DatabaseService(Jdbi jdbi) { this.imageRepository = new ImageRepository(jdbi);
this.jdbi = jdbi; }
DatabaseSchema.ensureSchema(jdbi);
this.lotRepository = new LotRepository(jdbi); /**
this.auctionRepository = new AuctionRepository(jdbi); * Constructor with JDBI instance (for dependency injection).
this.imageRepository = new ImageRepository(jdbi); */
} public DatabaseService(Jdbi jdbi) {
this.jdbi = jdbi;
this.url = null; // Use null as this constructor doesn't use the URL
// ==================== LEGACY COMPATIBILITY METHODS ==================== DatabaseSchema.ensureSchema(jdbi);
// These methods delegate to repositories for backward compatibility
void ensureSchema() { this.lotRepository = new LotRepository(jdbi);
DatabaseSchema.ensureSchema(jdbi); this.auctionRepository = new AuctionRepository(jdbi);
} this.imageRepository = new ImageRepository(jdbi);
}
synchronized void upsertAuction(AuctionInfo auction) { // ==================== LEGACY COMPATIBILITY METHODS ====================
auctionRepository.upsert(auction); // These methods delegate to repositories for backward compatibility
}
synchronized List<AuctionInfo> getAllAuctions() { void ensureSchema() {
return auctionRepository.getAll(); DatabaseSchema.ensureSchema(jdbi);
} }
synchronized List<AuctionInfo> getAuctionsByCountry(String countryCode) { synchronized void upsertAuction(AuctionInfo auction) {
return auctionRepository.getByCountry(countryCode); auctionRepository.upsert(auction);
} }
synchronized void upsertLot(Lot lot) { synchronized List<AuctionInfo> getAllAuctions() {
lotRepository.upsert(lot); return auctionRepository.getAll();
} }
synchronized void upsertLotWithIntelligence(Lot lot) { synchronized List<AuctionInfo> getAuctionsByCountry(String countryCode) {
lotRepository.upsertWithIntelligence(lot); return auctionRepository.getByCountry(countryCode);
} }
synchronized void updateLotCurrentBid(Lot lot) { synchronized void upsertLot(Lot lot) {
lotRepository.updateCurrentBid(lot); retry(() -> {
} try (var connection = DriverManager.getConnection(url)) {
connection.setAutoCommit(false); // Start transaction
lotRepository.upsert(lot); // Perform update
connection.commit(); // Commit transaction
} catch (SQLException e) {
throw new RuntimeException("Failed to upsert lot", e);
}
});
}
synchronized void updateLotNotificationFlags(Lot lot) { void upsertLots(List<Lot> lots) { // Batch import with transactions
lotRepository.updateNotificationFlags(lot); retry(() -> {
} try (var connection = DriverManager.getConnection(url)) {
connection.setAutoCommit(false); // Start transaction
for (Lot lot : lots) {
lotRepository.upsert(lot); // Upsert individual lot
}
connection.commit(); // Commit transaction
} catch (SQLException e) {
throw new RuntimeException("Failed to upsert lots", e);
}
});
}
synchronized List<Lot> getActiveLots() { // Retry logic for transient database failures
return lotRepository.getActiveLots(); private void retry(Runnable action) {
} final int maxRetries = 3;
for (int attempt = 1; attempt <= maxRetries; attempt++) {
try {
action.run(); // Attempt action
return; // Exit on success
} catch (RuntimeException e) {
boolean isBusy = e.getCause() instanceof SQLException
&& e.getCause().getMessage().contains("[SQLITE_BUSY]");
if (isBusy && attempt < maxRetries) {
log.warn("Database locked, retrying {} of {}", attempt, maxRetries);
try {
Thread.sleep(500L * attempt); // Backoff
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
}
} else {
throw e; // Non-retryable error or max retries exceeded
}
}
}
}
synchronized List<Lot> getAllLots() { synchronized void upsertLotWithIntelligence(Lot lot) {
return lotRepository.getAllLots(); lotRepository.upsertWithIntelligence(lot);
} }
synchronized List<BidHistory> getBidHistory(String lotId) { synchronized void updateLotCurrentBid(Lot lot) {
return lotRepository.getBidHistory(lotId); lotRepository.updateCurrentBid(lot);
} }
synchronized void insertBidHistory(List<BidHistory> bidHistory) { synchronized void updateLotNotificationFlags(Lot lot) {
lotRepository.insertBidHistory(bidHistory); lotRepository.updateNotificationFlags(lot);
} }
synchronized void insertImage(long lotId, String url, String filePath, List<String> labels) { synchronized List<Lot> getActiveLots() {
imageRepository.insert(lotId, url, filePath, labels); return lotRepository.getActiveLots();
} }
synchronized void updateImageLabels(int imageId, List<String> labels) { synchronized List<Lot> getAllLots() {
imageRepository.updateLabels(imageId, labels); return lotRepository.getAllLots();
} }
synchronized List<String> getImageLabels(int imageId) { synchronized List<BidHistory> getBidHistory(String lotId) {
return imageRepository.getLabels(imageId); return lotRepository.getBidHistory(lotId);
} }
synchronized List<ImageRecord> getImagesForLot(long lotId) { synchronized void insertBidHistory(List<BidHistory> bidHistory) {
return imageRepository.getImagesForLot(lotId) lotRepository.insertBidHistory(bidHistory);
.stream() }
.map(img -> new ImageRecord(img.id(), img.lotId(), img.url(), img.filePath(), img.labels()))
.toList();
}
synchronized List<ImageDetectionRecord> getImagesNeedingDetection() { synchronized void insertImage(long lotId, String url, String filePath, List<String> labels) {
return imageRepository.getImagesNeedingDetection() imageRepository.insert(lotId, url, filePath, labels);
.stream() }
.map(img -> new ImageDetectionRecord(img.id(), img.lotId(), img.filePath()))
.toList();
}
synchronized int getImageCount() { synchronized void updateImageLabels(int imageId, List<String> labels) {
return imageRepository.getImageCount(); imageRepository.updateLabels(imageId, labels);
} }
synchronized List<AuctionInfo> importAuctionsFromScraper() { synchronized List<String> getImageLabels(int imageId) {
return jdbi.withHandle(handle -> { return imageRepository.getLabels(imageId);
var sql = """ }
SELECT
l.auction_id,
MIN(l.title) as title,
MIN(l.location) as location,
MIN(l.url) as url,
COUNT(*) as lots_count,
MIN(l.closing_time) as first_lot_closing_time,
MIN(l.scraped_at) as scraped_at
FROM lots l
WHERE l.auction_id IS NOT NULL
GROUP BY l.auction_id
""";
return handle.createQuery(sql) synchronized List<ImageRecord> getImagesForLot(long lotId) {
.map((rs, ctx) -> { return imageRepository.getImagesForLot(lotId)
try { .stream()
var auction = ScraperDataAdapter.fromScraperAuction(rs); .map(img -> new ImageRecord(img.id(), img.lotId(), img.url(), img.filePath(), img.labels()))
if (auction.auctionId() != 0L) { .toList();
auctionRepository.upsert(auction); }
return auction;
}
} catch (Exception e) {
log.warn("Failed to import auction: {}", e.getMessage());
}
return null;
})
.list()
.stream()
.filter(a -> a != null)
.toList();
});
}
synchronized List<Lot> importLotsFromScraper() { synchronized List<ImageDetectionRecord> getImagesNeedingDetection() {
return jdbi.withHandle(handle -> { return imageRepository.getImagesNeedingDetection()
var sql = "SELECT * FROM lots"; .stream()
.map(img -> new ImageDetectionRecord(img.id(), img.lotId(), img.filePath()))
.toList();
}
return handle.createQuery(sql) synchronized int getImageCount() {
.map((rs, ctx) -> { return imageRepository.getImageCount();
try { }
var lot = ScraperDataAdapter.fromScraperLot(rs);
if (lot.lotId() != 0L && lot.saleId() != 0L) {
lotRepository.upsert(lot);
return lot;
}
} catch (Exception e) {
log.warn("Failed to import lot: {}", e.getMessage());
}
return null;
})
.list()
.stream()
.filter(l -> l != null)
.toList();
});
}
// ==================== DIRECT REPOSITORY ACCESS ==================== synchronized List<AuctionInfo> importAuctionsFromScraper() {
// Expose repositories for modern usage patterns return jdbi.withHandle(handle -> {
var sql = """
SELECT
l.auction_id,
MIN(l.title) as title,
MIN(l.location) as location,
MIN(l.url) as url,
COUNT(*) as lots_count,
MIN(l.closing_time) as first_lot_closing_time,
MIN(l.scraped_at) as scraped_at
FROM lots l
WHERE l.auction_id IS NOT NULL
GROUP BY l.auction_id
""";
public LotRepository lots() { return handle.createQuery(sql)
return lotRepository; .map((rs, ctx) -> {
} try {
var auction = ScraperDataAdapter.fromScraperAuction(rs);
if (auction.auctionId() != 0L) {
auctionRepository.upsert(auction);
return auction;
}
} catch (Exception e) {
log.warn("Failed to import auction: {}", e.getMessage());
}
return null;
})
.list()
.stream()
.filter(a -> a != null)
.toList();
});
}
public AuctionRepository auctions() { synchronized List<Lot> importLotsFromScraper() {
return auctionRepository; return jdbi.withHandle(handle -> {
} var sql = "SELECT * FROM lots";
public ImageRepository images() { return handle.createQuery(sql)
return imageRepository; .map((rs, ctx) -> {
} try {
var lot = ScraperDataAdapter.fromScraperLot(rs);
if (lot.lotId() != 0L && lot.saleId() != 0L) {
lotRepository.upsert(lot);
return lot;
}
} catch (Exception e) {
log.warn("Failed to import lot: {}", e.getMessage());
}
return null;
})
.list()
.stream()
.filter(l -> l != null)
.toList();
});
}
public Jdbi getJdbi() { // ==================== DIRECT REPOSITORY ACCESS ====================
return jdbi; // Expose repositories for modern usage patterns
}
// ==================== LEGACY RECORDS ==================== public LotRepository lots() {
// Keep records for backward compatibility with existing code return lotRepository;
}
public record ImageRecord(int id, long lotId, String url, String filePath, String labels) {} public AuctionRepository auctions() {
return auctionRepository;
}
public record ImageDetectionRecord(int id, long lotId, String filePath) {} public ImageRepository images() {
return imageRepository;
}
public Jdbi getJdbi() {
return jdbi;
}
// ==================== LEGACY RECORDS ====================
// Keep records for backward compatibility with existing code
public record ImageRecord(int id, long lotId, String url, String filePath, String labels) { }
public record ImageDetectionRecord(int id, long lotId, String filePath) { }
} }

View File

@@ -49,28 +49,28 @@ public record NotificationService(Config cfg) {
// Remove tray icon asynchronously to avoid blocking the caller // Remove tray icon asynchronously to avoid blocking the caller
int delayMs = Integer.getInteger("auctiora.desktop.delay.ms", 0); int delayMs = Integer.getInteger("auctiora.desktop.delay.ms", 0);
if (delayMs <= 0) { if (delayMs <= 0) {
var t = new Thread(() -> { var t = new Thread(() -> {
try { try {
Thread.sleep(50); Thread.sleep(50);
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
try { try {
tray.remove(icon); tray.remove(icon);
} catch (Exception ignored) { } catch (Exception ignored) {
} }
}, "tray-remove"); }, "tray-remove");
t.setDaemon(true); t.setDaemon(true);
t.start(); t.start();
} else { } else {
try { try {
Thread.sleep(delayMs); Thread.sleep(delayMs);
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} finally { } finally {
try { try {
tray.remove(icon); tray.remove(icon);
} catch (Exception ignored) { } catch (Exception ignored) {
} }
} }
} }
log.info("Desktop notification: {}", title); log.info("Desktop notification: {}", title);
} catch (Exception e) { } catch (Exception e) {
@@ -90,8 +90,8 @@ public record NotificationService(Config cfg) {
props.put("mail.smtp.ssl.protocols", "TLSv1.2"); props.put("mail.smtp.ssl.protocols", "TLSv1.2");
// Connection timeouts (configurable; short during tests, longer otherwise) // Connection timeouts (configurable; short during tests, longer otherwise)
int smtpTimeoutMs = Integer.getInteger("auctiora.smtp.timeout.ms", isUnderTest() ? 200 : 10000); int smtpTimeoutMs = Integer.getInteger("auctiora.smtp.timeout.ms", isUnderTest() ? 200 : 10000);
String t = String.valueOf(smtpTimeoutMs); String t = String.valueOf(smtpTimeoutMs);
props.put("mail.smtp.connectiontimeout", t); props.put("mail.smtp.connectiontimeout", t);
props.put("mail.smtp.timeout", t); props.put("mail.smtp.timeout", t);
props.put("mail.smtp.writetimeout", t); props.put("mail.smtp.writetimeout", t);

View File

@@ -113,7 +113,7 @@ class NotificationServiceTest {
@DisplayName("Should include both desktop and email when SMTP configured") @DisplayName("Should include both desktop and email when SMTP configured")
void testBothNotificationChannels() { void testBothNotificationChannels() {
var service = new NotificationService( var service = new NotificationService(
"smtp:user@gmail.com:password:recipient@example.com" "smtp:michael.bakker1986@gmail.com:agrepolhlnvhipkv:michael.bakker1986@gmail.com"
); );
// Both desktop and email should be attempted // Both desktop and email should be attempted

View File

@@ -0,0 +1,65 @@
# Application Configuration
# Values will be injected from pom.xml during build
quarkus.application.name=${project.artifactId}
quarkus.application.version=${project.version}
# Custom properties for groupId if needed
application.groupId=${project.groupId}
application.artifactId=${project.artifactId}
application.version=${project.version}
# HTTP Configuration
quarkus.http.port=8081
# ========== DEVELOPMENT (quarkus:dev) ==========
%dev.quarkus.http.host=127.0.0.1
# ========== PRODUCTION (Docker/JAR) ==========
%prod.quarkus.http.host=0.0.0.0
# ========== TEST PROFILE ==========
%test.quarkus.http.host=localhost
# Enable CORS for frontend development
quarkus.http.cors=true
quarkus.http.cors.origins=*
quarkus.http.cors.methods=GET,POST,PUT,DELETE,OPTIONS
quarkus.http.cors.headers=accept,authorization,content-type,x-requested-with
# Logging Configuration
quarkus.log.console.format=%d{HH:mm:ss} %-5p [%c{2.}] (%t) %s%e%n
quarkus.log.console.level=INFO
# Development mode settings
%dev.quarkus.log.console.level=DEBUG
%dev.quarkus.live-reload.instrumentation=true
# JVM Arguments for native access (Jansi, OpenCV, etc.)
quarkus.native.additional-build-args=--enable-native-access=ALL-UNNAMED
# Production optimizations
%prod.quarkus.package.type=fast-jar
%prod.quarkus.http.enable-compression=true
# Static resources
quarkus.http.enable-compression=true
quarkus.rest.path=/
quarkus.http.root-path=/
# Auction Monitor Configuration
auction.database.path=/mnt/okcomputer/output/cache.db
auction.images.path=/mnt/okcomputer/output/images
# auction.notification.config=desktop
# Format: smtp:username:password:recipient_email
auction.notification.config=smtp:michael.bakker1986@gmail.com:agrepolhlnvhipkv:michael.bakker1986@gmail.com
auction.yolo.config=/mnt/okcomputer/output/models/yolov4.cfg
auction.yolo.weights=/mnt/okcomputer/output/models/yolov4.weights
auction.yolo.classes=/mnt/okcomputer/output/models/coco.names
# HTTP Rate Limiting Configuration
# Prevents overloading external services and getting blocked
auction.http.rate-limit.default-max-rps=2
auction.http.rate-limit.troostwijk-max-rps=1
auction.http.timeout-seconds=30
# Health Check Configuration
quarkus.smallrye-health.root-path=/health