Skip to content
Snippets Groups Projects
Commit 824e96ce authored by Christoph Thelen's avatar Christoph Thelen
Browse files

Only load data for visited sessions that is actually displayed

parent 060ee8bf
No related merge requests found
...@@ -156,77 +156,54 @@ public class CouchDBDao implements IDatabaseDao { ...@@ -156,77 +156,54 @@ public class CouchDBDao implements IDatabaseDao {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
interposedCountView.setGroup(true); interposedCountView.setGroup(true);
return getSessionInfoData(sessions, questionCountView, answerCountView, interposedCountView, null, null); return getSessionInfoData(sessions, questionCountView, answerCountView, interposedCountView);
} }
private List<SessionInfo> getInfosForVisitedSessions(final List<Session> sessions, final User user) { private List<SessionInfo> getInfosForVisitedSessions(final List<Session> sessions, final User user) {
final ExtendedView questionCountView = new ExtendedView("skill_question/count_by_session");
final ExtendedView answerCountView = new ExtendedView("skill_question/count_answers_by_session");
final ExtendedView interposedCountUserView = new ExtendedView("interposed_question/count_by_session_reading_for_creator");
final ExtendedView answeredQuestionsView = new ExtendedView("answer/by_user"); final ExtendedView answeredQuestionsView = new ExtendedView("answer/by_user");
final ExtendedView questionIdsView = new ExtendedView("skill_question/by_session_only_id_for_all"); final ExtendedView questionIdsView = new ExtendedView("skill_question/by_session_only_id_for_all");
questionCountView.setSessionIdKeys(sessions);
questionCountView.setGroup(true);
answerCountView.setSessionIdKeys(sessions);
answerCountView.setGroup(true);
questionIdsView.setSessionIdKeys(sessions); questionIdsView.setSessionIdKeys(sessions);
List<String> interposedQueryKeys = new ArrayList<String>();
for (Session s : sessions) {
interposedQueryKeys.add("[\"" + s.get_id() + "\",\"" + user.getUsername() + "\",\"unread\"]");
}
List<String> answeredQuestionQueryKeys = new ArrayList<String>(); List<String> answeredQuestionQueryKeys = new ArrayList<String>();
for (Session s : sessions) { for (Session s : sessions) {
answeredQuestionQueryKeys.add("[\"" + user.getUsername() + "\",\"" + s.get_id() + "\"]"); answeredQuestionQueryKeys.add("[\"" + user.getUsername() + "\",\"" + s.get_id() + "\"]");
} }
try { try {
interposedCountUserView.setKeys(URLEncoder.encode("["+StringUtils.join(interposedQueryKeys, ",")+"]", "UTF-8"));
answeredQuestionsView.setKeys(URLEncoder.encode("["+StringUtils.join(answeredQuestionQueryKeys, ",")+"]", "UTF-8")); answeredQuestionsView.setKeys(URLEncoder.encode("["+StringUtils.join(answeredQuestionQueryKeys, ",")+"]", "UTF-8"));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
interposedCountUserView.setGroup(true); return getVisitedSessionInfoData(sessions, answeredQuestionsView, questionIdsView);
return getSessionInfoData(sessions, questionCountView, answerCountView, interposedCountUserView, answeredQuestionsView, questionIdsView);
} }
private List<SessionInfo> getSessionInfoData(final List<Session> sessions, private List<SessionInfo> getVisitedSessionInfoData(List<Session> sessions,
final ExtendedView questionCountView, ExtendedView answeredQuestionsView, ExtendedView questionIdsView) {
final ExtendedView answerCountView, final Map<String, Set<String>> answeredQuestionsMap = new HashMap<String, Set<String>>();
final ExtendedView interposedCountView, final Map<String, Set<String>> questionIdMap = new HashMap<String, Set<String>>();
final ExtendedView answeredQuestionsView, final ViewResults answeredQuestionsViewResults = getDatabase().view(answeredQuestionsView);
final ExtendedView questionIdsView) { final ViewResults questionIdsViewResults = getDatabase().view(questionIdsView);
final ViewResults questionCountViewResults = getDatabase().view(questionCountView);
final ViewResults answerCountViewResults = getDatabase().view(answerCountView);
final ViewResults interposedCountViewResults = getDatabase().view(interposedCountView);
Map<String, Set<String>> answeredQuestionsMap = new HashMap<String, Set<String>>(); // Maps a session ID to a set of question IDs of answered questions of that session
Map<String, Set<String>> questionIdMap = new HashMap<String, Set<String>>(); for (final Document d : answeredQuestionsViewResults.getResults()) {
if (answeredQuestionsView != null && questionIdsView != null) { final String sessionId = d.getJSONArray("key").getString(1);
final ViewResults answeredQuestionsViewResults = getDatabase().view(answeredQuestionsView); final String questionId = d.getString("value");
final ViewResults questionIdsViewResults = getDatabase().view(questionIdsView); Set<String> questionIdsInSession = answeredQuestionsMap.get(sessionId);
if (questionIdsInSession == null) {
// Maps a session ID to a set of question IDs of answered questions of that session questionIdsInSession = new HashSet<String>();
for (final Document d : answeredQuestionsViewResults.getResults()) {
final String sessionId = d.getJSONArray("key").getString(1);
final String questionId = d.getString("value");
Set<String> questionIdsInSession = answeredQuestionsMap.get(sessionId);
if (questionIdsInSession == null) {
questionIdsInSession = new HashSet<String>();
}
questionIdsInSession.add(questionId);
answeredQuestionsMap.put(sessionId, questionIdsInSession);
} }
questionIdsInSession.add(questionId);
answeredQuestionsMap.put(sessionId, questionIdsInSession);
}
// Maps a session ID to a set of question IDs of that session // Maps a session ID to a set of question IDs of that session
for (final Document d : questionIdsViewResults.getResults()) { for (final Document d : questionIdsViewResults.getResults()) {
final String sessionId = d.getString("key"); final String sessionId = d.getString("key");
final String questionId = d.getId(); final String questionId = d.getId();
Set<String> questionIdsInSession = questionIdMap.get(sessionId); Set<String> questionIdsInSession = questionIdMap.get(sessionId);
if (questionIdsInSession == null) { if (questionIdsInSession == null) {
questionIdsInSession = new HashSet<String>(); questionIdsInSession = new HashSet<String>();
}
questionIdsInSession.add(questionId);
questionIdMap.put(sessionId, questionIdsInSession);
} }
questionIdsInSession.add(questionId);
questionIdMap.put(sessionId, questionIdsInSession);
} }
// For each session, count the question IDs that are not yet answered // For each session, count the question IDs that are not yet answered
...@@ -245,6 +222,28 @@ public class CouchDBDao implements IDatabaseDao { ...@@ -245,6 +222,28 @@ public class CouchDBDao implements IDatabaseDao {
unansweredQuestionsCountMap.put(s.get_id(), questionIdsInSession.size()); unansweredQuestionsCountMap.put(s.get_id(), questionIdsInSession.size());
} }
List<SessionInfo> sessionInfos = new ArrayList<SessionInfo>();
for (Session session : sessions) {
int numUnanswered = 0;
if (unansweredQuestionsCountMap.containsKey(session.get_id())) {
numUnanswered = unansweredQuestionsCountMap.get(session.get_id());
}
SessionInfo info = new SessionInfo(session);
info.setNumUnanswered(numUnanswered);
sessionInfos.add(info);
}
return sessionInfos;
}
private List<SessionInfo> getSessionInfoData(final List<Session> sessions,
final ExtendedView questionCountView,
final ExtendedView answerCountView,
final ExtendedView interposedCountView) {
final ViewResults questionCountViewResults = getDatabase().view(questionCountView);
final ViewResults answerCountViewResults = getDatabase().view(answerCountView);
final ViewResults interposedCountViewResults = getDatabase().view(interposedCountView);
Map<String, Integer> questionCountMap = new HashMap<String, Integer>(); Map<String, Integer> questionCountMap = new HashMap<String, Integer>();
for (final Document d : questionCountViewResults.getResults()) { for (final Document d : questionCountViewResults.getResults()) {
questionCountMap.put(d.getString("key"), d.getInt("value")); questionCountMap.put(d.getString("key"), d.getInt("value"));
...@@ -262,7 +261,6 @@ public class CouchDBDao implements IDatabaseDao { ...@@ -262,7 +261,6 @@ public class CouchDBDao implements IDatabaseDao {
int numQuestions = 0; int numQuestions = 0;
int numAnswers = 0; int numAnswers = 0;
int numInterposed = 0; int numInterposed = 0;
int numUnanswered = 0;
if (questionCountMap.containsKey(session.get_id())) { if (questionCountMap.containsKey(session.get_id())) {
numQuestions = questionCountMap.get(session.get_id()); numQuestions = questionCountMap.get(session.get_id());
} }
...@@ -272,14 +270,10 @@ public class CouchDBDao implements IDatabaseDao { ...@@ -272,14 +270,10 @@ public class CouchDBDao implements IDatabaseDao {
if (interposedCountMap.containsKey(session.get_id())) { if (interposedCountMap.containsKey(session.get_id())) {
numInterposed = interposedCountMap.get(session.get_id()); numInterposed = interposedCountMap.get(session.get_id());
} }
if (unansweredQuestionsCountMap.containsKey(session.get_id())) {
numUnanswered = unansweredQuestionsCountMap.get(session.get_id());
}
SessionInfo info = new SessionInfo(session); SessionInfo info = new SessionInfo(session);
info.setNumQuestions(numQuestions); info.setNumQuestions(numQuestions);
info.setNumAnswers(numAnswers); info.setNumAnswers(numAnswers);
info.setNumInterposed(numInterposed); info.setNumInterposed(numInterposed);
info.setNumUnanswered(numUnanswered);
sessionInfos.add(info); sessionInfos.add(info);
} }
return sessionInfos; return sessionInfos;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment