Skip to content

Commit

Permalink
stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Esposito committed Aug 18, 2017
1 parent 379e1cb commit 3619dc4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
* Created by fabio.pires on 18.08.17.
*/
public class AnswerSubmitted {

private Integer pollID;

private Map<Integer, Integer> answers;

private Map<Integer, Double> stats;

public AnswerSubmitted() {
}

public AnswerSubmitted(Integer pollID, Map<Integer, Integer> answers) {
public AnswerSubmitted(Integer pollID, Map<Integer, Integer> answers, Map<Integer, Double> stats) {
this.pollID = pollID;
this.answers = answers;
this.stats = stats;
}

public Integer getPollID() {
Expand All @@ -33,4 +37,12 @@ public Map<Integer, Integer> getAnswers() {
public void setAnswers(Map<Integer, Integer> answers) {
this.answers = answers;
}

public Map<Integer, Double> getStats() {
return stats;
}

public void setStats(Map<Integer, Double> stats) {
this.stats = stats;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ private Handler<Message<String>> answerSubmittedEventListener() {
AnswerSubmitted.class);

Poll p = polls.get(answerSubmitted.getPollID());
// Todo stats

if (p != null) {
p.getAnswers().putAll(answerSubmitted.getAnswers());
p.getStats().putAll(answerSubmitted.getStats());

polls.put(p.getPollID(), p);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;


/**
Expand Down Expand Up @@ -90,9 +91,16 @@ private Handler<Message<Object>> submitAnswer() {
p.getAnswers().merge(a, 1, Integer::sum);
});

Double sum = p.getAnswers().values().stream().mapToDouble(s -> s).sum();

p.getAnswers().forEach((key, value) -> {
Double percentage = value / sum;
p.getStats().put(key, percentage);
});

polls.put(p.getPollID(), p);

final AnswerSubmitted answerSubmitted = new AnswerSubmitted(p.getPollID(), p.getAnswers());
final AnswerSubmitted answerSubmitted = new AnswerSubmitted(p.getPollID(), p.getAnswers(), p.getStats());
vertx.eventBus().send("answerSubmittedEvent", Json.encode(answerSubmitted));

handler.reply(Json.encodePrettily(p));
Expand Down

0 comments on commit 3619dc4

Please sign in to comment.