Skip to content

Commit

Permalink
Fix issues with aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
jordansimsmith committed Sep 6, 2024
1 parent 5c7fc84 commit 92eb590
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
Expand All @@ -23,8 +24,8 @@

public class GetProgressHandler
implements RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse> {
private static final int MINUTES_PER_EPISODE = 20;
@VisibleForTesting static final ZoneId ZONE_ID = ZoneId.of("Pacific/Auckland");
private static final int MINUTES_PER_EPISODE = 20;

private final Clock clock;
private final ObjectMapper objectMapper;
Expand Down Expand Up @@ -102,17 +103,23 @@ private APIGatewayV2HTTPResponse doHandleRequest(APIGatewayV2HTTPEvent event, Co
.map(e -> new EpisodeShow(e, showsByFolderName.get(e.getFolderName())))
.toList();
var unknownShows = showEpisodes.stream().filter(e -> e.show() == null).toList();
var unknownShowsProgress = new ShowProgress(null, unknownShows.size());
var unknownShowsProgress =
!unknownShows.isEmpty()
? Stream.of(new ShowProgress(null, unknownShows.size()))
: Stream.<ShowProgress>empty();
var knownShows =
showEpisodes.stream()
.filter(e -> e.show() != null)
.collect(Collectors.groupingBy(EpisodeShow::show));
.collect(Collectors.groupingBy(e -> Objects.requireNonNull(e.show().getTvdbId())));
var knownShowsProgress =
knownShows.entrySet().stream()
.map(e -> new ShowProgress(e.getKey().getTvdbName(), e.getValue().size()));
knownShows.values().stream()
.map(
e ->
new ShowProgress(
Objects.requireNonNull(e.get(0).show()).getTvdbName(), e.size()));

var progresses =
Stream.concat(Stream.of(unknownShowsProgress), knownShowsProgress)
Stream.concat(unknownShowsProgress, knownShowsProgress)
.sorted(Comparator.comparing(e -> e.episodesWatched, Comparator.reverseOrder()))
.toList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import com.jordansimsmith.time.ClockModule;
import dagger.Component;
import javax.inject.Singleton;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

@Singleton
@Component(
Expand All @@ -24,10 +22,6 @@ public interface ImmersionTrackerFactory {

ObjectMapper objectMapper();

DynamoDbClient dynamoDbClient();

DynamoDbEnhancedClient dynamoDbEnhancedClient();

DynamoDbTable<ImmersionTrackerItem> immersionTrackerTable();

static ImmersionTrackerFactory create() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,25 @@ void handleRequestShouldCalculateProgress() throws Exception {
var episode1 =
ImmersionTrackerItem.createEpisode("jordansimsmith", "show1", "episode1", now - 100);
var episode2 =
ImmersionTrackerItem.createEpisode("jordansimsmith", "show1", "episode2", now + 100);
ImmersionTrackerItem.createEpisode("jordansimsmith", "show2", "episode2", now + 100);
var episode3 =
ImmersionTrackerItem.createEpisode("jordansimsmith", "show2", "episode1", now - 100);
var show = ImmersionTrackerItem.createShow("jordansimsmith", "show1");
show.setTvdbId(1);
show.setTvdbName("my show");
ImmersionTrackerItem.createEpisode("jordansimsmith", "show3", "episode1", now - 100);
var show1 = ImmersionTrackerItem.createShow("jordansimsmith", "show1");
show1.setTvdbId(1);
show1.setTvdbName("my show");
var show2 = ImmersionTrackerItem.createShow("jordansimsmith", "show2");
show2.setTvdbId(1);
show2.setTvdbName("my show");
var show3 = ImmersionTrackerItem.createShow("jordansimsmith", "show3");
show3.setTvdbId(2);
show3.setTvdbName("my other show");

immersionTrackerTable.putItem(episode1);
immersionTrackerTable.putItem(episode2);
immersionTrackerTable.putItem(episode3);
immersionTrackerTable.putItem(show);
immersionTrackerTable.putItem(show1);
immersionTrackerTable.putItem(show2);
immersionTrackerTable.putItem(show3);

// act
var res = getProgressHandler.handleRequest(APIGatewayV2HTTPEvent.builder().build(), null);
Expand All @@ -76,6 +84,42 @@ void handleRequestShouldCalculateProgress() throws Exception {
assertThat(progress.totalHoursWatched()).isEqualTo(1);
assertThat(progress.episodesWatchedToday()).isEqualTo(1);

var shows = progress.shows();
assertThat(shows).hasSize(2);
assertThat(shows.get(0).name()).isEqualTo("my show");
assertThat(shows.get(0).episodesWatched()).isEqualTo(2);
assertThat(shows.get(1).name()).isEqualTo("my other show");
assertThat(shows.get(1).episodesWatched()).isEqualTo(1);
}

@Test
void handleRequestShouldReturnUnknownShow() throws Exception {
// arrange
var now = (int) fakeClock.now().atZone(GetProgressHandler.ZONE_ID).toInstant().getEpochSecond();
var episode1 =
ImmersionTrackerItem.createEpisode("jordansimsmith", "show1", "episode1", now - 100);
var episode2 =
ImmersionTrackerItem.createEpisode("jordansimsmith", "show1", "episode2", now + 100);
var episode3 =
ImmersionTrackerItem.createEpisode("jordansimsmith", "show3", "episode1", now - 100);
var show1 = ImmersionTrackerItem.createShow("jordansimsmith", "show1");
show1.setTvdbId(1);
show1.setTvdbName("my show");

immersionTrackerTable.putItem(episode1);
immersionTrackerTable.putItem(episode2);
immersionTrackerTable.putItem(episode3);
immersionTrackerTable.putItem(show1);

// act
var res = getProgressHandler.handleRequest(APIGatewayV2HTTPEvent.builder().build(), null);

// assert
assertThat(res.getStatusCode()).isEqualTo(200);
assertThat(res.getHeaders()).containsEntry("Content-Type", "application/json; charset=utf-8");

var progress = objectMapper.readValue(res.getBody(), GetProgressHandler.ProgressResponse.class);
assertThat(progress).isNotNull();
var shows = progress.shows();
assertThat(shows).hasSize(2);
assertThat(shows.get(0).name()).isEqualTo("my show");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.URI;
import javax.inject.Named;
import javax.inject.Singleton;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

@Singleton
@Component(
Expand All @@ -21,6 +22,8 @@
public interface ImmersionTrackerTestFactory extends ImmersionTrackerFactory {
FakeClock fakeClock();

DynamoDbClient dynamoDbClient();

@Component.Factory
interface Factory {
ImmersionTrackerTestFactory create(
Expand Down

0 comments on commit 92eb590

Please sign in to comment.