Skip to content

Commit

Permalink
✨ Cache Session Agenda closes #77
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Fihlon <[email protected]>
  • Loading branch information
McPringle committed Apr 7, 2024
1 parent 9daa446 commit 027040f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
63 changes: 34 additions & 29 deletions src/main/java/swiss/fihlon/apus/service/ConferenceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

import jakarta.annotation.PreDestroy;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Service;
import swiss.fihlon.apus.conference.Room;
import swiss.fihlon.apus.conference.Session;
import swiss.fihlon.apus.conference.SessionImportException;
import swiss.fihlon.apus.conference.doag.ConferenceAPI;
import swiss.fihlon.apus.configuration.Configuration;

Expand All @@ -38,11 +41,11 @@
public final class ConferenceService {

private static final Duration UPDATE_FREQUENCY = Duration.ofMinutes(5);
private static final Logger LOGGER = LoggerFactory.getLogger(ConferenceService.class);

private final Configuration configuration;
private final ScheduledFuture<?> updateScheduler;
private List<Session> sessions;
private List<Room> rooms;
private Map<Room, List<Session>> roomsWithSessions = new TreeMap<>();

public ConferenceService(@NotNull final TaskScheduler taskScheduler,
@NotNull final Configuration configuration) {
Expand All @@ -57,38 +60,40 @@ public void stopUpdateScheduler() {
}

private void updateSessions() {
final var newSessions = new ConferenceAPI(configuration).getSessions().stream()
.sorted()
.toList();
final var newRooms = newSessions.stream()
.map(Session::room)
.distinct()
.sorted()
.toList();
synchronized (this) {
sessions = newSessions;
rooms = newRooms;
}
}
try {
final var sessions = new ConferenceAPI(configuration).getSessions().stream()
.sorted()
.toList();

public List<Session> getAllSessions() {
synchronized (this) {
return List.copyOf(sessions);
final var rooms = sessions.stream()
.map(Session::room)
.distinct()
.sorted()
.toList();

final LocalDateTime now = LocalDateTime.now();
final Map<Room, List<Session>> newRoomsWithSessions = new TreeMap<>();
for (final Room room : rooms) {
newRoomsWithSessions.put(room, new ArrayList<>());
}
final List<Session> runningAndNextSessions = sessions.stream()
.filter(session -> session.endDate().isAfter(now))
.toList();
for (final Session session : runningAndNextSessions) {
newRoomsWithSessions.get(session.room()).add(session);
}

synchronized (this) {
roomsWithSessions = newRoomsWithSessions;
}
} catch (final SessionImportException e) {
LOGGER.error("Failed to import sessions: {}", e.getMessage());
}
}

public Map<Room, List<Session>> getRoomsWithSessions() {
final LocalDateTime now = LocalDateTime.now();
final Map<Room, List<Session>> roomsWithSessions = new TreeMap<>();
for (final Room room : rooms) {
roomsWithSessions.put(room, new ArrayList<>());
}
final List<Session> runningAndNextSessions = sessions.stream()
.filter(session -> session.endDate().isAfter(now))
.toList();
for (final Session session : runningAndNextSessions) {
roomsWithSessions.get(session.room()).add(session);
synchronized (this) {
return new TreeMap<>(roomsWithSessions);
}
return roomsWithSessions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@ void displaySampleData() {

final ConferenceService conferenceService = new ConferenceService(new NoOpTaskScheduler(), configuration);
assertEquals(12, conferenceService.getRoomsWithSessions().size());
assertEquals(159, conferenceService.getAllSessions().size());
}
}

0 comments on commit 027040f

Please sign in to comment.