From 89dd3b348ea0799732ecf0832d820a48c6a141b3 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer <paul-christian.volkmer@mni.thm.de> Date: Sun, 4 Nov 2012 12:27:11 +0100 Subject: [PATCH] Add number of active users to statistics resource --- .../controller/StatisticsController.java | 2 +- .../java/de/thm/arsnova/dao/CouchDBDao.java | 50 +++++++++---------- .../de/thm/arsnova/entities/Statistics.java | 14 ++++-- .../arsnova/services/StatisticsService.java | 9 ++-- .../controller/StatisticsControllerTest.java | 4 +- 5 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/main/java/de/thm/arsnova/controller/StatisticsController.java b/src/main/java/de/thm/arsnova/controller/StatisticsController.java index 9d8fede10..999652151 100644 --- a/src/main/java/de/thm/arsnova/controller/StatisticsController.java +++ b/src/main/java/de/thm/arsnova/controller/StatisticsController.java @@ -20,7 +20,7 @@ public class StatisticsController { public final Statistics getStatistics() { return statisticsService.getStatistics(); } - + @RequestMapping(method = RequestMethod.GET, value = "/statistics/activeusercount") @ResponseBody public final int countActiveUsers() { diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java index ac1bae7cb..48a94fd66 100644 --- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java +++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java @@ -954,7 +954,9 @@ public class CouchDBDao implements IDatabaseDao { try { View view = new View("answer/by_user_and_session"); - view.setKey("[" + URLEncoder.encode("\"" + user.getUsername() + "\",\"" + s.get_id() + "\"", "UTF-8") + "]"); + view.setKey( + "[" + URLEncoder.encode("\"" + user.getUsername() + "\",\"" + s.get_id() + "\"", "UTF-8") + "]" + ); ViewResults results = this.getDatabase().view(view); if (results.getResults().isEmpty()) { throw new NotFoundException(); @@ -1046,21 +1048,21 @@ public class CouchDBDao implements IDatabaseDao { } return null; } - + @Override public void vote(String menu) { User u = this.userService.getCurrentUser(); - if(u == null) { + if (u == null) { throw new UnauthorizedException(); } - - String date = new SimpleDateFormat("dd-mm-yyyyy").format(new Date()); + + String date = new SimpleDateFormat("dd-mm-yyyyy").format(new Date()); try { View view = new View("food_vote/get_user_vote"); view.setKey("[" + URLEncoder.encode("\"" + date + "\",\"" + u.getUsername() + "\"", "UTF-8") + "]"); ViewResults results = this.getDatabase().view(view); - - if(results.getResults().isEmpty()) { + + if (results.getResults().isEmpty()) { Document vote = new Document(); vote.put("type", "food_vote"); vote.put("name", menu); @@ -1070,7 +1072,7 @@ public class CouchDBDao implements IDatabaseDao { } else { Document vote = results.getResults().get(0); vote.put("name", menu); - this.database.saveDocument(vote); + this.database.saveDocument(vote); } } catch (UnsupportedEncodingException e) { LOGGER.error("Error while retrieving user food vote", e); @@ -1078,7 +1080,7 @@ public class CouchDBDao implements IDatabaseDao { LOGGER.error("Error while saving user food vote", e); } } - + @Override public List<FoodVote> getFoodVote() { List<FoodVote> foodVotes = new ArrayList<FoodVote>(); @@ -1089,22 +1091,21 @@ public class CouchDBDao implements IDatabaseDao { view.setEndKey("[" + URLEncoder.encode("\"" + date + "\",{}", "UTF-8") + "]"); view.setGroup(true); ViewResults results = this.getDatabase().view(view); - for(Document d : results.getResults()) { + for (Document d : results.getResults()) { FoodVote vote = new FoodVote(); vote.setCount(d.getJSONObject().optInt("value")); vote.setDay(date); vote.setName(d.getJSONObject().getJSONArray("key").getString(1)); foodVotes.add(vote); } - + return foodVotes; - } catch (UnsupportedEncodingException e) { LOGGER.error("Error while retrieving food vote count", e); } return foodVotes; } - + @Override public int getFoodVoteCount() { String date = new SimpleDateFormat("dd-mm-yyyyy").format(new Date()); @@ -1114,7 +1115,7 @@ public class CouchDBDao implements IDatabaseDao { view.setEndKey("[" + URLEncoder.encode("\"" + date + "\",{}", "UTF-8") + "]"); view.setGroup(false); ViewResults results = this.getDatabase().view(view); - if(results.size() == 0 || results.getResults().size() == 0) { + if (results.size() == 0 || results.getResults().size() == 0) { return 0; } return results.getJSONArray("rows").optJSONObject(0).optInt("value"); @@ -1123,33 +1124,33 @@ public class CouchDBDao implements IDatabaseDao { } return 0; } - + @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("anwers"); } - + @Override public int countQuestions() { return sessionsCountValue("questions"); } - + private int sessionsCountValue(String key) { try { View view = new View("session/count"); @@ -1158,11 +1159,11 @@ public class CouchDBDao implements IDatabaseDao { if (isEmptyResults(results)) { return 0; } - - int result = 0; - + + int result = 0; + JSONArray rows = results.getJSONArray("rows"); - for(int i = 0; i < rows.size(); i++) { + for (int i = 0; i < rows.size(); i++) { JSONObject row = rows.getJSONObject(i); if ( row.getString("key").equals(key) @@ -1174,7 +1175,6 @@ public class CouchDBDao implements IDatabaseDao { } catch (Exception e) { LOGGER.error("Error while retrieving session count", e); } - return 0; } } diff --git a/src/main/java/de/thm/arsnova/entities/Statistics.java b/src/main/java/de/thm/arsnova/entities/Statistics.java index 051229bc0..870178951 100644 --- a/src/main/java/de/thm/arsnova/entities/Statistics.java +++ b/src/main/java/de/thm/arsnova/entities/Statistics.java @@ -6,6 +6,7 @@ public class Statistics { private int questions; private int openSessions; private int closedSessions; + private int activeUsers; public int getAnsers() { return ansers; @@ -13,25 +14,32 @@ public class Statistics { public void setAnsers(int ansers) { this.ansers = ansers; } - + public int getQuestions() { return questions; } public void setQuestions(int questions) { this.questions = questions; } - + public int getOpenSessions() { return openSessions; } public void setOpenSessions(int openSessions) { this.openSessions = openSessions; } - + public int getClosedSessions() { return closedSessions; } public void setClosedSessions(int closedSessions) { this.closedSessions = closedSessions; } + + public int getActiveUsers() { + return activeUsers; + } + public void setActiveUsers(int activeUsers) { + this.activeUsers = activeUsers; + } } diff --git a/src/main/java/de/thm/arsnova/services/StatisticsService.java b/src/main/java/de/thm/arsnova/services/StatisticsService.java index 8b0cd4c4e..1467475d7 100644 --- a/src/main/java/de/thm/arsnova/services/StatisticsService.java +++ b/src/main/java/de/thm/arsnova/services/StatisticsService.java @@ -9,24 +9,25 @@ import de.thm.arsnova.entities.Statistics; @Service public class StatisticsService implements IStatisticsService { - private static final int SINCEDURATION = 3 * 60 * 1000; + private static final int DURATION_IN_MILLIS = 3 * 60 * 1000; @Autowired private IDatabaseDao databaseDao; @Override public final int countActiveUsers() { - long since = System.currentTimeMillis() - SINCEDURATION; + long since = System.currentTimeMillis() - DURATION_IN_MILLIS; return databaseDao.countActiveUsers(since); } - + @Override - public Statistics getStatistics() { + public final Statistics getStatistics() { Statistics statistics = new Statistics(); statistics.setOpenSessions(databaseDao.countOpenSessions()); statistics.setClosedSessions(databaseDao.countClosedSessions()); statistics.setAnsers(databaseDao.countAnswers()); statistics.setQuestions(databaseDao.countQuestions()); + statistics.setActiveUsers(databaseDao.countActiveUsers(DURATION_IN_MILLIS)); return statistics; } } diff --git a/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java b/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java index bfe061cab..ca44203da 100644 --- a/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java +++ b/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java @@ -14,6 +14,7 @@ import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.servlet.HandlerAdapter; +import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter; @RunWith(SpringJUnit4ClassRunner.class) @@ -64,8 +65,7 @@ public class StatisticsControllerTest { request.setRequestURI("/statistics"); handlerAdapter.handle(request, response, statisticsController); - String expected = "{\"ansers\":0,\"questions\":0,\"openSessions\":2,\"closedSessions\":0}"; - + String expected = "{\"ansers\":0,\"questions\":0,\"openSessions\":2,\"closedSessions\":0,\"activeUsers\":0}"; assertEquals(expected, response.getContentAsString()); } } -- GitLab