StatsController.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package es.uv.saic.web;
  2. import java.text.SimpleDateFormat;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.security.access.annotation.Secured;
  7. import org.springframework.security.core.Authentication;
  8. import org.springframework.security.core.session.SessionInformation;
  9. import org.springframework.security.core.session.SessionRegistry;
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.ui.Model;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import es.uv.saic.domain.Usuari;
  14. import es.uv.saic.feign.StatsClient;
  15. // Controller to handle admin statistics page
  16. @Controller
  17. public class StatsController {
  18. @Autowired
  19. private SessionRegistry sessionRegistry;
  20. @Autowired
  21. private StatsClient sc;
  22. public static class ActiveSession {
  23. private String id;
  24. private String username;
  25. private String fullName;
  26. private String npi;
  27. private String lastRequest;
  28. private boolean expired;
  29. public void setId(String id) {this.id = id;}
  30. public void setUsername(String username) {this.username = username;}
  31. public void setFullName(String fullName) {this.fullName = fullName;}
  32. public void setNpi(String npi) {this.npi = npi;}
  33. public void setExpired(boolean expired) {this.expired = expired;}
  34. public void setLastRequest(String lastRequest) {this.lastRequest = lastRequest;}
  35. public String getId() {return this.id;}
  36. public String getUsername() {return this.username;}
  37. public String getFullName() {return this.fullName;}
  38. public String getNpi() {return this.npi;}
  39. public boolean getExpired() {return this.expired;}
  40. public String getLastRequest() {return this.lastRequest;}
  41. }
  42. public static class PendingEmail{
  43. private String username;
  44. private String fullName;
  45. private String email;
  46. public void setUsername(String username) {this.username = username;}
  47. public void setFullName(String fullName) {this.fullName = fullName;}
  48. public void setEmail(String email) {this.email = email;}
  49. public String getUsername() {return this.username;}
  50. public String getFullName() {return this.fullName;}
  51. public String getEmail() {return this.email;}
  52. }
  53. /*
  54. * Renders the admin stats page
  55. *
  56. * @param model The model to pass data to the view
  57. * @param auth The authentication object
  58. * @return The name of the view to render
  59. *
  60. @GetMapping("/admin/stats")
  61. @Secured({"ROLE_ADMIN"})
  62. public String getStats(Model model, Authentication auth) {
  63. HashMap<String, Object> response =
  64. sc.getStats();
  65. if (response == null) {
  66. return "401";
  67. }
  68. model.addAllAttributes(response);
  69. return "adminStats";
  70. }*/
  71. @GetMapping("/admin/stats")
  72. @Secured({"ROLE_ADMIN"})
  73. public String getStats(Model model, Authentication auth) {
  74. final List<Object> allPrincipals = sessionRegistry.getAllPrincipals();
  75. List<ActiveSession> allLoggedUsers = new ArrayList<ActiveSession>();
  76. int totalActiveSessions = 0;
  77. int totalExpiredSessions = 0;
  78. for(Object principal : allPrincipals) {
  79. final List<SessionInformation> allSessions = sessionRegistry.getAllSessions(principal, true);
  80. for(final SessionInformation session : allSessions) {
  81. final ActiveSession s = new ActiveSession();
  82. if(session.isExpired()) {
  83. totalExpiredSessions++;
  84. }
  85. else {
  86. totalActiveSessions++;
  87. }
  88. Usuari x = ((Usuari)principal);
  89. s.setId(Integer.toString(session.hashCode()));
  90. s.setExpired(session.isExpired());
  91. SimpleDateFormat sdt = new SimpleDateFormat("dd-mm-yyyy HH:mm:ss");
  92. s.setLastRequest(sdt.format(session.getLastRequest()));
  93. s.setUsername(x.getUsuari());
  94. s.setFullName(x.getNom() + " " + x.getCognoms());
  95. s.setNpi("");
  96. allLoggedUsers.add(s);
  97. }
  98. }
  99. model.addAttribute("activeSessions", allLoggedUsers);
  100. model.addAttribute("totalActiveSessions", totalActiveSessions);
  101. model.addAttribute("totalExpiredSessions", totalExpiredSessions);
  102. model.addAttribute("pendingEmails", sc.getPendingEmails());
  103. return "adminStats";
  104. }
  105. }