diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java index 7cd6ef84c0e0177d0f71e522a10042f920bfde17..a2945421600b8eeb73dfeccf07b6f90e580374c4 100644 --- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java +++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java @@ -68,6 +68,7 @@ import de.thm.arsnova.entities.PossibleAnswer; import de.thm.arsnova.entities.Question; import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.SessionInfo; +import de.thm.arsnova.entities.Statistics; import de.thm.arsnova.entities.User; import de.thm.arsnova.entities.VisitedSession; import de.thm.arsnova.entities.transport.AnswerQueueElement; @@ -1075,57 +1076,45 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware } @Override - public int countSessions() { - return sessionsCountValue("openSessions") - + sessionsCountValue("closedSessions"); - } - - @Override - public int countClosedSessions() { - return sessionsCountValue("closedSessions"); - } - - @Override - public int countOpenSessions() { - return sessionsCountValue("openSessions"); - } - - @Override - public int countAnswers() { - return sessionsCountValue("answers"); - } - - @Override - public int countQuestions() { - return sessionsCountValue("questions"); - } - - private int sessionsCountValue(final String key) { + public Statistics getStatistics() { + final Statistics stats = new Statistics(); try { - final View view = new View("statistic/count_sessions"); + final View view = new View("statistics/statistics"); view.setGroup(true); final ViewResults results = getDatabase().view(view); if (isEmptyResults(results)) { - return 0; + return stats; } - int result = 0; final JSONArray rows = results.getJSONArray("rows"); for (int i = 0; i < rows.size(); i++) { final JSONObject row = rows.getJSONObject(i); - if ( - row.getString("key").equals(key) - ) { - result += row.getInt("value"); + 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 "questions": + stats.setQuestions(stats.getQuestions() + value); + break; + case "interposedQuestions": + stats.setInterposedQuestions(stats.getInterposedQuestions() + value); + break; } } - return result; + return stats; } catch (final Exception e) { LOGGER.error("Error while retrieving session count", e); } - return 0; + return stats; } @Override diff --git a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java index 217bbed2ba194a1961ab9e280aa0a472e037fe87..c60e1e296d6c7305367e6a3bbab2197aa65a7415 100644 --- a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java +++ b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java @@ -29,6 +29,7 @@ import de.thm.arsnova.entities.LoggedIn; import de.thm.arsnova.entities.Question; import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.SessionInfo; +import de.thm.arsnova.entities.Statistics; import de.thm.arsnova.entities.User; import de.thm.arsnova.entities.transport.ImportExportSession; @@ -104,16 +105,6 @@ public interface IDatabaseDao { List<InterposedQuestion> getInterposedQuestions(Session session, User user); - int countSessions(); - - int countOpenSessions(); - - int countClosedSessions(); - - int countAnswers(); - - int countQuestions(); - InterposedQuestion getInterposedQuestion(String questionId); void markInterposedQuestionAsRead(InterposedQuestion question); @@ -203,4 +194,6 @@ public interface IDatabaseDao { void deleteAllLectureAnswers(Session session); SessionInfo importSession(User user, ImportExportSession importSession); + + Statistics getStatistics(); } diff --git a/src/main/java/de/thm/arsnova/entities/Statistics.java b/src/main/java/de/thm/arsnova/entities/Statistics.java index 1cb01a7f0f5919d913c72b60a190d825fdf55d72..51fc6846798b2c56e4bbf86de928975ae2ad5d8d 100644 --- a/src/main/java/de/thm/arsnova/entities/Statistics.java +++ b/src/main/java/de/thm/arsnova/entities/Statistics.java @@ -25,10 +25,12 @@ public class Statistics { private int closedSessions; private int activeUsers; private int loggedinUsers; + private int interposedQuestions; public int getAnswers() { return answers; } + public void setAnswers(final int answers) { this.answers = answers; } @@ -36,6 +38,7 @@ public class Statistics { public int getQuestions() { return questions; } + public void setQuestions(final int questions) { this.questions = questions; } @@ -43,6 +46,7 @@ public class Statistics { public int getOpenSessions() { return openSessions; } + public void setOpenSessions(final int openSessions) { this.openSessions = openSessions; } @@ -50,6 +54,7 @@ public class Statistics { public int getClosedSessions() { return closedSessions; } + public void setClosedSessions(final int closedSessions) { this.closedSessions = closedSessions; } @@ -57,6 +62,7 @@ public class Statistics { public int getActiveUsers() { return activeUsers; } + public void setActiveUsers(final int activeUsers) { this.activeUsers = activeUsers; } @@ -64,18 +70,17 @@ public class Statistics { public int getLoggedinUsers() { return loggedinUsers; } + public void setLoggedinUsers(final int loggedinUsers) { this.loggedinUsers = loggedinUsers; } - public int getAverageAnswersPerQuestion() { - if (getQuestions() > 0) { - return getAnswers() / getQuestions(); - } - return 0; + public int getInterposedQuestions() { + return interposedQuestions; } - public void setAverageAnswersPerQuestion(final int value) { - // NOP + + public void setInterposedQuestions(int interposedQuestions) { + this.interposedQuestions = interposedQuestions; } @Override @@ -102,7 +107,6 @@ public class Statistics { final Statistics other = (Statistics) obj; return hashCode() == other.hashCode(); } - return false; } } diff --git a/src/main/java/de/thm/arsnova/services/StatisticsService.java b/src/main/java/de/thm/arsnova/services/StatisticsService.java index d5292ba0e77d39ab6b9204ef7a08eb8d8f4345af..979c03126217bc179a0c2c0985d0a90e2dbcb993 100644 --- a/src/main/java/de/thm/arsnova/services/StatisticsService.java +++ b/src/main/java/de/thm/arsnova/services/StatisticsService.java @@ -18,7 +18,6 @@ package de.thm.arsnova.services; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.session.SessionRegistry; import org.springframework.stereotype.Service; import de.thm.arsnova.dao.IDatabaseDao; @@ -33,27 +32,10 @@ public class StatisticsService implements IStatisticsService { @Autowired private IUserService userService; - @Autowired - private SessionRegistry sessionRegistry; - @Override public Statistics getStatistics() { - final Statistics statistics = new Statistics(); - statistics.setOpenSessions(databaseDao.countOpenSessions()); - statistics.setClosedSessions(databaseDao.countClosedSessions()); - statistics.setAnswers(databaseDao.countAnswers()); - statistics.setQuestions(databaseDao.countQuestions()); - /* TODO: Are both of the following do the same, now? If so, remove one of them. */ + final Statistics statistics = databaseDao.getStatistics(); statistics.setActiveUsers(userService.loggedInUsers()); - statistics.setLoggedinUsers(countLoggedInUsers()); - return statistics; } - - private int countLoggedInUsers() { - if (sessionRegistry == null) { - return 0; - } - return sessionRegistry.getAllPrincipals().size(); - } } diff --git a/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java b/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java index c28876acbf77012dee6564d5cc6297529e71785e..8fff3582094ab1f5ca88019b49735d6688ab584b 100644 --- a/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java +++ b/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java @@ -83,7 +83,8 @@ public class StatisticsControllerTest { .andExpect(jsonPath("$.questions").value(0)) .andExpect(jsonPath("$.openSessions").value(3)) .andExpect(jsonPath("$.closedSessions").value(0)) - .andExpect(jsonPath("$.activeUsers").exists()); + .andExpect(jsonPath("$.activeUsers").exists()) + .andExpect(jsonPath("$.interposedQuestions").exists()); } @Test diff --git a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java index 49cdf41e632f4e616037d2bbb64c324a67a4f3a6..864ba25ec61b2e1ade7a596da215729fc01c4284 100644 --- a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java +++ b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java @@ -34,6 +34,7 @@ import de.thm.arsnova.entities.LoggedIn; import de.thm.arsnova.entities.Question; import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.SessionInfo; +import de.thm.arsnova.entities.Statistics; import de.thm.arsnova.entities.User; import de.thm.arsnova.entities.transport.ImportExportSession; import de.thm.arsnova.exceptions.NoContentException; @@ -112,29 +113,6 @@ public class StubDatabaseDao implements IDatabaseDao { return session; } - @Override - public int countSessions() { - return stubSessions.size(); - } - - @Override - public int countOpenSessions() { - int result = 0; - for (Session session : stubSessions.values()) { - if (session.isActive()) result++; - } - return result; - } - - @Override - public int countClosedSessions() { - int result = 0; - for (Session session : stubSessions.values()) { - if (! session.isActive()) result++; - } - return result; - } - @Override public boolean sessionKeyAvailable(String keyword) { return (stubSessions.get(keyword) == null); @@ -258,18 +236,6 @@ public class StubDatabaseDao implements IDatabaseDao { return null; } - @Override - public int countAnswers() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public int countQuestions() { - // TODO Auto-generated method stub - return 0; - } - @Override public InterposedQuestion saveQuestion(Session session, InterposedQuestion question, User user) { // TODO Auto-generated method stub @@ -592,9 +558,19 @@ public class StubDatabaseDao implements IDatabaseDao { } @Override - public Answer saveAnswer(Answer answer, User user, Question question, - Session session) { + public Answer saveAnswer(Answer answer, User user, Question question, Session session) { // TODO Auto-generated method stub return null; } + + @Override + public Statistics getStatistics() { + final Statistics stats = new Statistics(); + stats.setOpenSessions(3); + stats.setClosedSessions(0); + stats.setQuestions(0); + stats.setAnswers(0); + stats.setInterposedQuestions(0); + return stats; + } } diff --git a/src/test/java/de/thm/arsnova/domain/package-info.java b/src/test/java/de/thm/arsnova/domain/package-info.java deleted file mode 100644 index 375154f331521f8a400d59988c228ae7ad8512f5..0000000000000000000000000000000000000000 --- a/src/test/java/de/thm/arsnova/domain/package-info.java +++ /dev/null @@ -1,18 +0,0 @@ -/* - * This file is part of ARSnova Backend. - * Copyright (C) 2012-2015 The ARSnova Team - * - * ARSnova Backend is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * ARSnova Backend is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -package de.thm.arsnova.domain; \ No newline at end of file diff --git a/src/test/java/de/thm/arsnova/services/StatisticsServiceTest.java b/src/test/java/de/thm/arsnova/services/StatisticsServiceTest.java deleted file mode 100644 index 405af87d62c4bdd91dd5910b83bf2bdfb91fb833..0000000000000000000000000000000000000000 --- a/src/test/java/de/thm/arsnova/services/StatisticsServiceTest.java +++ /dev/null @@ -1,66 +0,0 @@ -package de.thm.arsnova.services; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.when; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import de.thm.arsnova.dao.IDatabaseDao; -import de.thm.arsnova.entities.Statistics; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = { - "file:src/main/webapp/WEB-INF/spring/arsnova-servlet.xml", - "file:src/main/webapp/WEB-INF/spring/spring-main.xml", - "file:src/test/resources/test-config.xml" -}) -@ActiveProfiles("test") -public class StatisticsServiceTest { - - @InjectMocks - private final IStatisticsService statisticsService = new StatisticsService(); - - @Mock - private IDatabaseDao databaseDao; - - @Mock - private IUserService userService; - - @Before - public void startup() { - MockitoAnnotations.initMocks(this); - when(userService.loggedInUsers()).thenReturn(42); - when(databaseDao.countQuestions()).thenReturn(123); - when(databaseDao.countOpenSessions()).thenReturn(1978); - when(databaseDao.countClosedSessions()).thenReturn(1984); - when(databaseDao.countAnswers()).thenReturn(2014); - } - - @After - public void cleanup() { - } - - @Test - public void testShouldReturnEqualStatistics() { - final Statistics actual = statisticsService.getStatistics(); - - final Statistics expected = new Statistics(); - expected.setActiveUsers(42); - expected.setAnswers(2014); - expected.setClosedSessions(1984); - expected.setOpenSessions(1978); - expected.setQuestions(123); - expected.setLoggedinUsers(0); - - assertEquals(expected, actual); - } -}