Browse Source

Migrada lógica AdminStats al back

Mario Martínez Hernández 2 weeks ago
parent
commit
fe99ce7930

+ 12 - 0
src/main/java/es/uv/saic/feign/StatsClient.java

@@ -0,0 +1,12 @@
+package es.uv.saic.feign;
+
+import java.util.HashMap;
+
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.GetMapping;
+
+@FeignClient(value = "stats-controller", url = "${saic.url.domain}")
+public interface StatsClient {
+    @GetMapping("/admin/stats")
+	public HashMap<String, Object> getStats();
+}

+ 0 - 32
src/main/java/es/uv/saic/web/ProcesController.java

@@ -1,32 +0,0 @@
-package es.uv.saic.web;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.RestController;
-
-import es.uv.saic.domain.Proces;
-import es.uv.saic.dto.ProcesDTO;
-import es.uv.saic.service.ProcesService;
-
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.PostMapping;
-
-
-@RestController
-public class ProcesController {
-    @Autowired
-    private ProcesService procesService;
-
-    @PostMapping("/proces/{id}")
-    public ResponseEntity<?> findById(@PathVariable Integer id) {
-        try {
-            Proces proces = procesService.findByID(id);
-            ProcesDTO procesDTO = new ProcesDTO(proces);
-
-            return (procesDTO != null) ? ResponseEntity.ok(procesDTO) : ResponseEntity.badRequest().body("El proces con id " + id + " esta vacio");
-        } catch (Exception e) {
-             e.printStackTrace();
-            return ResponseEntity.badRequest().body("Error al obtener el proces por el id:" +  e.getMessage());
-        }
-    }    
-}

+ 12 - 45
src/main/java/es/uv/saic/web/StatsController.java

@@ -2,6 +2,7 @@ package es.uv.saic.web;
 
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 
 import org.springframework.beans.factory.annotation.Autowired;
@@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.GetMapping;
 
 import es.uv.saic.domain.Email;
 import es.uv.saic.domain.Usuari;
+import es.uv.saic.feign.StatsClient;
 import es.uv.saic.service.EmailService;
 
 // Controller to handle admin statistics page
@@ -25,7 +27,7 @@ public class StatsController {
     private SessionRegistry sessionRegistry;
 	
 	@Autowired
-    private EmailService emailService;
+    private StatsClient sc;
 
 	public static class ActiveSession {
 		private String id;
@@ -71,49 +73,14 @@ public class StatsController {
 	@GetMapping("/admin/stats")
 	@Secured({"ROLE_ADMIN"})
 	public String getStats(Model model, Authentication auth) {
-		
-		final List<Object> allPrincipals = sessionRegistry.getAllPrincipals();
-		List<ActiveSession> allLoggedUsers = new ArrayList<ActiveSession>();
-		int totalActiveSessions = 0;
-		int totalExpiredSessions = 0;
-		for(Object principal : allPrincipals) {
-			final List<SessionInformation> allSessions = sessionRegistry.getAllSessions(principal, true);
-			for(final SessionInformation session : allSessions) {
-				final ActiveSession s = new ActiveSession();
-				if(session.isExpired()) {
-					totalExpiredSessions++;
-				}
-				else {
-					totalActiveSessions++;
-				}
-				Usuari x = ((Usuari)principal);
-				s.setId(Integer.toString(session.hashCode()));
-				s.setExpired(session.isExpired());
-				SimpleDateFormat sdt = new SimpleDateFormat("dd-mm-yyyy HH:mm:ss");
-				s.setLastRequest(sdt.format(session.getLastRequest()));
-				s.setUsername(x.getUsuari());
-				s.setFullName(x.getNom() + " " + x.getCognoms());
-				s.setNpi("");
-				allLoggedUsers.add(s);
-			}
-		}
-		model.addAttribute("activeSessions", allLoggedUsers);
-		model.addAttribute("totalActiveSessions", totalActiveSessions);
-		model.addAttribute("totalExpiredSessions", totalExpiredSessions);
-		
-		List<PendingEmail> pendingEmails = new ArrayList<PendingEmail>();
-		for(Email e : emailService.getPendingQueue()) {
-			PendingEmail p = new PendingEmail();
-			p.setUsername(e.getUsusari().getUsuari());
-			p.setFullName(e.getUsusari().getNom()+ " " +e.getUsusari().getCognoms());
-			p.setEmail(e.getUsusari().getEmail());
-			pendingEmails.add(p);
-		}
-		model.addAttribute("pendingEmails", pendingEmails);
-		
-		return "adminStats";
-	}
-	
+		HashMap<String, Object> response =
+                sc.getStats();
 
-	
+        if (response != null && response.get("redirect") != null) {
+			model.addAllAttributes(response);
+            return response.get("redirect").toString();
+        }
+
+        return "401";
+	}	
 }