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

Commit

Permalink
Configured the voting client implementation of the conference client
Browse files Browse the repository at this point in the history
  • Loading branch information
mklaehn committed Sep 30, 2023
1 parent bf4c25e commit 73ff1de
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -116,7 +119,9 @@ 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())
return RestCallHelper
.readOptionalFrom(config.getEventBaseUri() + "schedules/" + conferenceDay + '/' + roomName,
listOfMaps())
.orElse(List.of())
.stream()
.map(this::convertScheduleSlot)
Expand All @@ -125,7 +130,10 @@ public List<ScheduleSlot> getSchedule(final String conferenceDay, final String r

@Override
public List<Speaker> getSpeakers() {
return RestCallHelper.readOptionalFrom(config.getEventBaseUri() + "speakers", listOfMaps(), (a, b) -> {a.addAll(b); return a;})
return RestCallHelper.readOptionalFrom(config.getEventBaseUri() + "speakers", listOfMaps(), (a, b) -> {
a.addAll(b);
return a;
})
.orElse(List.of())
.stream()
.map(this::convertSpeaker)
Expand Down Expand Up @@ -161,28 +169,40 @@ public List<Track> getTracks() {
@Override
public Optional<RatingClient> getRatingClient() {
return Optional.of(config)
.map(ConferenceClientSettings::getVotingResultsToken)
.filter(ccs -> Objects.nonNull(ccs.getVotingResultsUri()))
.filter(ccs -> Objects.nonNull(ccs.getVotingResultsToken()))
.filter(ccs -> Objects.nonNull(ccs.getVotingResultsEvent()))
.map(_ignored_ -> this);
}

@Override
public List<RatedTalk> getRatedTalks(final String conferenceDay) {
return RestCallHelper.readOptionalFrom(config.getEventBaseUri() + "ratings/" + conferenceDay.toLowerCase(Locale.ENGLISH).substring(0, 3) + '/' + config.getVotingResultsToken(), listOfMaps())
.orElse(List.of())
.stream()
.map(this::convertRatedTalk)
.toList();
return getVotingResults().entrySet().stream()
.filter(e -> e.getKey().dayId().equals(conferenceDay))
.map(Map.Entry::getValue)
.findFirst()
.orElse(List.of());
}

@Override
public List<RatedTalk> getRatedTalksOverall() {
return RestCallHelper.readOptionalFrom(config.getEventBaseUri() + "ratings/" + config.getVotingResultsToken(), listOfMaps())
.orElse(List.of())
.stream()
.map(this::convertRatedTalk)
return getVotingResults().entrySet().stream()
.map(Map.Entry::getValue)
.flatMap(List::stream)
.toList();
}

private Map<WeekDay, List<RatedTalk>> getVotingResults() {
return RestCallHelper.getOptionalResponse(
config.getVotingResultsUri(),
Map.of(
"eventId", config.getVotingResultsEvent(),
"publicToken", config.getVotingResultsToken()))
.flatMap(r -> RestCallHelper.readOptionalFrom(r, map()))
.map(this::convertTalkRatings)
.orElse(Map.of());
}

private static GenericType<List<Map<String, Object>>> listOfMaps() {
return new GenericType<List<Map<String, Object>>>() {
};
Expand All @@ -193,12 +213,28 @@ 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);
return retrieveValue(input, "dailyTalksStats", List.class,
dailyTalksStatsList -> ((List<?>) dailyTalksStatsList).stream()
.map(o -> (Map<String, Object>) o)
.collect(Collectors.toMap(
dailyStats -> retrieveValue(dailyStats, "date", String.class, WeekDay::of),
dailyStats -> retrieveValue(dailyStats, "topTalks", List.class,
topTalksList -> ((List<?>) topTalksList).stream()
.map(o -> (Map<String, Object>) o)
.map(this::convertRatedTalk)
.toList()))));
}

private RatedTalk convertRatedTalk(final Map<String, Object> input) {
LOG.debug("Converting to RatedTalk: {}", input);
return RatedTalkImpl.builder()
.withAverageRating(retrieveValue(input, "avgRatings", Number.class, Number::doubleValue))
.withTotalRating(retrieveValue(input, "totalRatings", Number.class, Number::intValue))
.withTalk(retrieveValue(input, "id", Integer.class, talkId -> getTalk(Integer.toString(talkId)).get()))
.withAverageRating(retrieveValue(input, "averageRating", Number.class, Number::doubleValue))
.withTotalRating(retrieveValue(input, "numberOfVotes", Number.class, Number::intValue))
.withTalk(retrieveValue(input, "talkId", String.class,
talkId -> getTalk(talkId).get()))
.build();
}

Expand Down Expand Up @@ -324,7 +360,8 @@ private static <T> T retrieveValue(final Map<String, Object> data, final String
return type.cast(data.get(key));
}

private static <T, R> R retrieveValue(final Map<String, Object> data, final String key, final Class<T> type, final Function<T, R> converter) {
private static <T, R> R retrieveValue(final Map<String, Object> data, final String key, final Class<T> type,
final Function<T, R> converter) {
final T t = retrieveValue(data, key, type);
return null == t
? null
Expand All @@ -347,4 +384,11 @@ private static <T> T alternatives(final T... ts) {

return null;
}

protected static record WeekDay(String dayId) {

static WeekDay of(String date) {
return new WeekDay(LocalDate.parse(date).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public class ConferenceClientSettings {
*/
public static final String CONFIG_KEY = "conferenceClient";
private String eventBaseUri;
private String votingResultsUri;
private String votingResultsToken;
private String votingResultsEvent;

/**
* Returns the Event Base URI from where all standard calls are executed.
Expand All @@ -65,9 +67,7 @@ public void setEventBaseUri(final String eventBaseUri) {
}

/**
* Returns the Token required to retrieve voting results.
*
* @return the voting result token
* {@return the Token required to retrieve voting results.}
*/
public String getVotingResultsToken() {
return votingResultsToken;
Expand All @@ -83,12 +83,48 @@ public void setVotingResultsToken(String votingResultsToken) {
this.votingResultsToken = votingResultsToken;
}

/**
* {@return the URI to retrieve voting results from.}
*/
public String getVotingResultsUri() {
return votingResultsUri;
}

/**
* Sets the URI to retrieve voting results from.
*
* @param votingResultsUri the voting result URI
*/
public void setVotingResultsUri(String votingResultsUri) {
Objects.requireNonNull(votingResultsUri, "votingResultsUri must not be null!");
this.votingResultsUri = votingResultsUri;
}

/**
* {@return the voting results event id.}
*/
public String getVotingResultsEvent() {
return votingResultsEvent;
}

/**
* Sets the voting results event id.
*
* @param votingResultsEvent the voting result event id
*/
public void setVotingResultsEvent(String votingResultsEvent) {
Objects.requireNonNull(votingResultsEvent, "votingResultsEvent must not be null!");
this.votingResultsEvent = votingResultsEvent;
}

@Override
public String toString() {
return createToString(this, map(
"eventBaseUri", getEventBaseUri(),
"votingResultsToken", getVotingResultsToken()
)) + " extends " + super.toString();
"votingResultsToken", getVotingResultsToken(),
"votingResultsUri", getVotingResultsUri(),
"votingResultsEvent", getVotingResultsEvent()
));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion conference-impl/src/main/resources/tweetwallConfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"conferenceClient": {
"eventBaseUri": "https://dvbe23.cfp.dev/api/public",
"votingResultsToken": "PutYourActualTokenHere"
"votingResultsUri": "https://us-central1-voxxrin-v3.cloudfunctions.net/publicEventStats",
"votingResultsEvent": "dvbe23"
}
}

0 comments on commit 73ff1de

Please sign in to comment.