Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
Use FavoriteCounts from PublicEventStats if possible otherwise use va…
Browse files Browse the repository at this point in the history
…lue from talk itself
  • Loading branch information
mklaehn committed Oct 5, 2023
1 parent ea83c4c commit 69c8740
Showing 1 changed file with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.TreeMap;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.random.RandomGenerator;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -72,6 +71,7 @@ public final class ConferenceClientImpl implements ConferenceClient, RatingClien
private final Map<String, SessionType> sessionTypes;
private final Map<String, Room> rooms;
private final Map<String, Track> tracks;
private final Map<String, Integer> talkFavoriteCounts = new TreeMap<>();
private final ExpiringValue<Map<WeekDay, List<RatedTalk>>> ratedTalks;

public ConferenceClientImpl() {
Expand All @@ -96,7 +96,10 @@ public ConferenceClientImpl() {
.map(this::convertTrack)
.collect(Collectors.toMap(Identifiable::getId, Function.identity()));
LOG.info("Track IDs: {}", tracks.keySet());
this.ratedTalks = new ExpiringValue<>(this::getVotingResults, Duration.ofSeconds(20));
this.ratedTalks = new ExpiringValue<>(this::getPublicEventStats, Duration.ofSeconds(20));

// trigger initialization of ratedTalks
LOG.trace("Initialized ratedTalks: {}", ratedTalks.getValue());
}

@Override
Expand Down Expand Up @@ -125,6 +128,7 @@ public List<ScheduleSlot> getSchedule(final String conferenceDay) {

@Override
public List<ScheduleSlot> getSchedule(final String conferenceDay, final String roomName) {

return RestCallHelper
.readOptionalFrom(config.getEventBaseUri() + "schedules/" + conferenceDay + '/' + roomName,
listOfMaps())
Expand Down Expand Up @@ -173,12 +177,16 @@ public List<Track> getTracks() {
return List.copyOf(tracks.values());
}

@Override
public Optional<RatingClient> getRatingClient() {
private Optional<ConferenceClientSettings> getRatingClientEnabledConfig() {
return Optional.of(config)
.filter(ccs -> Objects.nonNull(ccs.getVotingResultsUri()))
.filter(ccs -> Objects.nonNull(ccs.getVotingResultsToken()))
.filter(ccs -> Objects.nonNull(ccs.getVotingResultsEvent()))
.filter(ccs -> Objects.nonNull(ccs.getVotingResultsEvent()));
}

@Override
public Optional<RatingClient> getRatingClient() {
return getRatingClientEnabledConfig()
.map(_ignored_ -> this);
}

Expand Down Expand Up @@ -226,15 +234,18 @@ private List<RatedTalk> randomizedRatedTalks() {
.toList();
}

private Map<WeekDay, List<RatedTalk>> getVotingResults() {
return RestCallHelper.getOptionalResponse(
config.getVotingResultsUri(),
Map.of(
"eventId", config.getVotingResultsEvent(),
"publicToken", config.getVotingResultsToken()))
private Map<WeekDay, List<RatedTalk>> getPublicEventStats() {
LOG.info("Loading PublicEventStats", new IllegalArgumentException("Loading PublicEventStats"));

return getRatingClientEnabledConfig()
.flatMap(_ignored -> RestCallHelper.getOptionalResponse(
config.getVotingResultsUri(),
Map.of(
"eventId", config.getVotingResultsEvent(),
"publicToken", config.getVotingResultsToken())))
.flatMap(r -> RestCallHelper.readOptionalFrom(r, map()))
.map(this::convertTalkRatings)
.orElse(Map.of());
.map(this::convertPublicEventStats)
.orElseGet(Map::of);
}

private static GenericType<List<Map<String, Object>>> listOfMaps() {
Expand All @@ -248,8 +259,22 @@ private static GenericType<Map<String, Object>> map() {
}

@SuppressWarnings("unchecked")
private Map<WeekDay, List<RatedTalk>> convertTalkRatings(final Map<String, Object> input) {
LOG.debug("Converting TalkRatings: {}", input);
private Map<WeekDay, List<RatedTalk>> convertPublicEventStats(final Map<String, Object> input) {
LOG.debug("Converting PublicEventStats: {}", input);

final Map<String, Integer> tfcs = retrieveValue(input, "perTalkStats", List.class,
perTalkStatsList -> ((List<?>) perTalkStatsList).stream()
.map(o -> (Map<String, Object>) o)
.collect(Collectors.toMap(
perTalkStats -> retrieveValue(perTalkStats, "talkId", String.class),
perTalkStats -> retrieveValue(perTalkStats, "totalFavoritesCount", Number.class,
Number::intValue))));

if (null != tfcs && !tfcs.isEmpty()) {
this.talkFavoriteCounts.putAll(tfcs);
LOG.info("Updated talkFavoriteCounts to: {}", talkFavoriteCounts);
}

return retrieveValue(input, "dailyTalksStats", List.class,
dailyTalksStatsList -> ((List<?>) dailyTalksStatsList).stream()
.map(o -> (Map<String, Object>) o)
Expand Down Expand Up @@ -353,8 +378,9 @@ private Speaker convertSpeaker(final Map<String, Object> input) {
@SuppressWarnings("unchecked")
private Talk convertTalk(final Map<String, Object> input) {
LOG.debug("Converting to Talk: {}", input);
String talkId = retrieveValue(input, "id", Number.class, Number::toString);
return TalkImpl.builder()
.withId(retrieveValue(input, "id", Number.class, Number::toString))
.withId(talkId)
.withName(retrieveValue(input, "title", String.class))
.withAudienceLevel(retrieveValue(input, "audienceLevel", String.class))
.withSessionType(sessionTypes.get(alternatives(
Expand All @@ -363,7 +389,11 @@ private Talk convertTalk(final Map<String, Object> input) {
// or by having the session type object as value
retrieveValue(input, "sessionType", Map.class,
m -> retrieveValue(m, "id", Number.class, Number::toString)))))
.withFavoriteCount(retrieveValue(input, "totalFavourites", Number.class, Number::intValue))
.withFavoriteCount(alternatives(
// if value is available from public event stats
talkFavoriteCounts.get(talkId),
// otherwise fall back to value from talk
retrieveValue(input, "totalFavourites", Number.class, Number::intValue)))
.withLanguage(Locale.ENGLISH)
.withScheduleSlots(retrieveValue(input, "timeSlots", List.class,
list -> ((List<?>) list).stream()
Expand Down

0 comments on commit 69c8740

Please sign in to comment.