diff --git a/src/main/java/ch/climbd/newsfeed/controller/MlController.java b/src/main/java/ch/climbd/newsfeed/controller/MlController.java index 1fbe27e..e727b9b 100644 --- a/src/main/java/ch/climbd/newsfeed/controller/MlController.java +++ b/src/main/java/ch/climbd/newsfeed/controller/MlController.java @@ -1,6 +1,7 @@ package ch.climbd.newsfeed.controller; import ch.climbd.newsfeed.data.NewsEntry; +import jakarta.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.ai.chat.client.ChatClient; @@ -24,14 +25,23 @@ public class MlController { this.mongo = mongoController; } + @PostConstruct + public void fixQueueAfterRestart() { + var todaysNews = mongo.findAllPostedToday(); + todaysNews.stream() + .filter(news -> news.getSummary() == null || news.getSummary().isBlank()) + .filter(news -> news.getContent() != null && !news.getContent().isBlank() && news.getContent().length() > 1000) + .forEach(this::queueSummarize); + } + public void queueSummarize(NewsEntry news) { LOG.info("Queued article for summarization: {}", news.getTitle()); queue.add(news); } - @Scheduled(fixedDelay = 1, initialDelay = 5, timeUnit = TimeUnit.MINUTES) + @Scheduled(fixedDelay = 15, initialDelay = 30, timeUnit = TimeUnit.SECONDS) public void summarize() { - NewsEntry news = null; + NewsEntry news; try { news = queue.poll(); diff --git a/src/main/java/ch/climbd/newsfeed/controller/MongoController.java b/src/main/java/ch/climbd/newsfeed/controller/MongoController.java index b84bbf8..d6228af 100644 --- a/src/main/java/ch/climbd/newsfeed/controller/MongoController.java +++ b/src/main/java/ch/climbd/newsfeed/controller/MongoController.java @@ -113,6 +113,16 @@ public List findAllFilterdBySite(String host) { return template.find(query, NewsEntry.class); } + public List findAllPostedToday() { + Query query = new Query(); + query.addCriteria(Criteria.where("publishedAt").gte(ZonedDateTime.now().truncatedTo(ChronoUnit.DAYS).toInstant())); + query.with(Sort.by(Sort.Direction.DESC, "publishedAt")); + query.limit(100); + query.maxTimeMsec(1000); + + return template.find(query, NewsEntry.class); + } + public void save(NewsEntry newsEntry) { template.save(newsEntry); } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index c1f69cf..4af26b3 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -11,7 +11,7 @@ spring: ollama: base-url: http://ollama:11434 chat.options: - model: qwen2:0.5b + model: qwen2:1.5b temperature: 0.7 keep_alive: 60m num-thread: 1