From 5955a4b0da0037470bae4289cb37c303d02015f6 Mon Sep 17 00:00:00 2001
From: Christoph Thelen <christoph.thelen@mni.thm.de>
Date: Tue, 3 Mar 2015 15:46:01 +0100
Subject: [PATCH] Fix for #14551: Count unique session creators

---
 .../java/de/thm/arsnova/dao/CouchDBDao.java   | 72 +++++++++++--------
 .../de/thm/arsnova/entities/Statistics.java   |  9 +++
 2 files changed, 50 insertions(+), 31 deletions(-)

diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
index dc00c2d44..b0e7ccf26 100644
--- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
+++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
@@ -1079,39 +1079,49 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware
 	public Statistics getStatistics() {
 		final Statistics stats = new Statistics();
 		try {
-			final View view = new View("statistics/statistics");
-			view.setGroup(true);
-
-			final ViewResults results = getDatabase().view(view);
-			if (isEmptyResults(results)) {
-				return stats;
+			final View statsView = new View("statistics/statistics");
+			final View creatorView = new View("statistics/unique_session_creators");
+			statsView.setGroup(true);
+			creatorView.setGroup(true);
+
+			final ViewResults statsResults = getDatabase().view(statsView);
+			final ViewResults creatorResults = getDatabase().view(creatorView);
+
+			if (!isEmptyResults(statsResults)) {
+				final JSONArray rows = statsResults.getJSONArray("rows");
+				for (int i = 0; i < rows.size(); i++) {
+					final JSONObject row = rows.getJSONObject(i);
+					final int value = row.getInt("value");
+					switch (row.getString("key")) {
+					case "openSessions":
+						stats.setOpenSessions(stats.getOpenSessions() + value);
+						break;
+					case "closedSessions":
+						stats.setClosedSessions(stats.getClosedSessions() + value);
+						break;
+					case "answers":
+						stats.setAnswers(stats.getAnswers() + value);
+						break;
+					case "lectureQuestions":
+						stats.setLectureQuestions(stats.getLectureQuestions() + value);
+						break;
+					case "preparationQuestions":
+						stats.setPreparationQuestions(stats.getPreparationQuestions() + value);
+						break;
+					case "interposedQuestions":
+						stats.setInterposedQuestions(stats.getInterposedQuestions() + value);
+						break;
+					}
+				}
 			}
-
-			final JSONArray rows = results.getJSONArray("rows");
-
-			for (int i = 0; i < rows.size(); i++) {
-				final JSONObject row = rows.getJSONObject(i);
-				final int value = row.getInt("value");
-				switch (row.getString("key")) {
-				case "openSessions":
-					stats.setOpenSessions(stats.getOpenSessions() + value);
-					break;
-				case "closedSessions":
-					stats.setClosedSessions(stats.getClosedSessions() + value);
-					break;
-				case "answers":
-					stats.setAnswers(stats.getAnswers() + value);
-					break;
-				case "lectureQuestions":
-					stats.setLectureQuestions(stats.getLectureQuestions() + value);
-					break;
-				case "preparationQuestions":
-					stats.setPreparationQuestions(stats.getPreparationQuestions() + value);
-					break;
-				case "interposedQuestions":
-					stats.setInterposedQuestions(stats.getInterposedQuestions() + value);
-					break;
+			if (!isEmptyResults(creatorResults)) {
+				final JSONArray rows = creatorResults.getJSONArray("rows");
+				Set<String> creators = new HashSet<String>();
+				for (int i = 0; i < rows.size(); i++) {
+					final JSONObject row = rows.getJSONObject(i);
+					creators.add(row.getString("key"));
 				}
+				stats.setCreators(creators.size());
 			}
 			return stats;
 		} catch (final Exception e) {
diff --git a/src/main/java/de/thm/arsnova/entities/Statistics.java b/src/main/java/de/thm/arsnova/entities/Statistics.java
index eff50018d..e0cae9b62 100644
--- a/src/main/java/de/thm/arsnova/entities/Statistics.java
+++ b/src/main/java/de/thm/arsnova/entities/Statistics.java
@@ -24,6 +24,7 @@ public class Statistics {
 	private int preparationQuestions;
 	private int openSessions;
 	private int closedSessions;
+	private int creators;
 	private int activeUsers;
 	private int loggedinUsers;
 	private int interposedQuestions;
@@ -100,6 +101,14 @@ public class Statistics {
 		this.interposedQuestions = interposedQuestions;
 	}
 
+	public int getCreators() {
+		return creators;
+	}
+
+	public void setCreators(int creators) {
+		this.creators = creators;
+	}
+
 	@Override
 	public int hashCode() {
 		return (this.getClass().getName()
-- 
GitLab