From 53f59baf7b0d2073cfa8bdb447763f1f0a697dad Mon Sep 17 00:00:00 2001 From: gsd Date: Thu, 12 Oct 2023 21:20:11 +0300 Subject: [PATCH] new front update --- .../java/app/controllers/StatsController.java | 8 ++- src/main/java/app/entities/Stats.java | 9 +++ .../java/app/services/db/DetectService.java | 60 ++++++++++++++++--- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/main/java/app/controllers/StatsController.java b/src/main/java/app/controllers/StatsController.java index 883aa60..029ec51 100644 --- a/src/main/java/app/controllers/StatsController.java +++ b/src/main/java/app/controllers/StatsController.java @@ -7,8 +7,11 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + @RestController @RequestMapping("api/stats") public class StatsController { @@ -21,7 +24,10 @@ public class StatsController { @GetMapping @BurstUpdatePlayers - public ResponseEntity GetStats(){ + public ResponseEntity GetStats(@RequestParam(name = "filter", required = false) String filter){ + if (filter != null && !filter.isEmpty()) { + return new ResponseEntity(stats.getOnlyThis(filter), HttpStatus.OK); + } return new ResponseEntity<>(stats, HttpStatus.OK); } } diff --git a/src/main/java/app/entities/Stats.java b/src/main/java/app/entities/Stats.java index ea405fb..29da757 100644 --- a/src/main/java/app/entities/Stats.java +++ b/src/main/java/app/entities/Stats.java @@ -61,4 +61,13 @@ public class Stats { uniq.merge(key, value, (x, y) -> y); } + public HashMap getOnlyThis(String value) { + switch (value) { + case "statistic": return new HashMap<>(){{put(value, getStatistic());}}; + case "servers": return new HashMap<>(){{put(value, getServers());}}; + case "uniq": return new HashMap<>(){{put(value, getUniq());}}; + default: return new HashMap<>(); + } + } + } diff --git a/src/main/java/app/services/db/DetectService.java b/src/main/java/app/services/db/DetectService.java index dd5eed5..6320751 100644 --- a/src/main/java/app/services/db/DetectService.java +++ b/src/main/java/app/services/db/DetectService.java @@ -5,32 +5,41 @@ import app.entities.other.SteamID; import app.entities.server.Server; import app.utils.SteamIDConverter; import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; @Service -@Transactional("RoTransactionManager") public class DetectService { - EntityManager entityManager; + EntityManager entityManager_ro; + EntityManager entityManager_rw; private Stats stats; @Autowired - public DetectService(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager) { - this.entityManager = entityManager; + public DetectService( + Stats stats, + @Qualifier(value = "RoEntityManager") EntityManager entityManager_ro, + @Qualifier(value = "RwEntityManager") EntityManager entityManager_rw) { + this.entityManager_ro = entityManager_ro; + this.entityManager_rw = entityManager_rw; this.stats = stats; } + @Transactional("RoTransactionManager") public List getAccountsPerSteamID(SteamID steamID) { List founded_steamid = new ArrayList<>(); for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { - founded_steamid.addAll(entityManager.createNativeQuery("SELECT `steam_id` FROM `"+stringServerEntry.getValue().getDb()+"`."+"`user_connections` WHERE `connect_ip` in (SELECT `connect_ip` FROM `" + stringServerEntry.getValue().getDb() + "`." +"`user_connections` WHERE `steam_id` LIKE ?1 AND `connect_ip` NOT LIKE '10.%' GROUP BY `connect_ip` ORDER BY `id` DESC) GROUP BY `steam_id`") + founded_steamid.addAll(entityManager_ro.createNativeQuery("SELECT `steam_id` FROM `"+stringServerEntry.getValue().getDb()+"`."+"`user_connections` WHERE `connect_ip` in (SELECT `connect_ip` FROM `" + stringServerEntry.getValue().getDb() + "`." +"`user_connections` WHERE `steam_id` LIKE ?1 AND `connect_ip` NOT LIKE '10.%' GROUP BY `connect_ip` ORDER BY `id` DESC) GROUP BY `steam_id`") .setParameter(1, steamID.steam2) .getResultStream().map(String::valueOf).toList()); } @@ -38,8 +47,45 @@ public class DetectService { return founded_steamid.stream().distinct().map(SteamIDConverter::getSteamID).filter(Objects::nonNull).toList(); } + public List getAccountsPerSteamIDThreaded(SteamID steamID) { + ExecutorService executor = Executors.newCachedThreadPool(); + List tasks = new ArrayList<>(); + AtomicReference em_selector = new AtomicReference<>(0); + + for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { + tasks.add((Callable>) () -> { + if (em_selector.get() == 0) { + em_selector.set(1); + } else { + em_selector.set(0); + } + + String sql = "SELECT `steam_id` FROM `"+stringServerEntry.getValue().getDb()+"`."+"`user_connections` WHERE `connect_ip` in (SELECT `connect_ip` FROM `" + stringServerEntry.getValue().getDb() + "`." +"`user_connections` WHERE `steam_id` LIKE ?1 AND `connect_ip` NOT LIKE '10.%' GROUP BY `connect_ip` ORDER BY `id` DESC) GROUP BY `steam_id`"; + + switch (em_selector.get()) { + case 0:return entityManager_ro.createNativeQuery(sql) + .setParameter(1, steamID.steam2) + .getResultStream().map(String::valueOf).toList(); + case 1:return entityManager_rw.createNativeQuery(sql) + .setParameter(1, steamID.steam2) + .getResultStream().map(String::valueOf).toList(); + default: return new ArrayList<>(); + } + }); + } + + List founded_steamid = new ArrayList<>(); + try { + executor.invokeAll(tasks).stream().forEach(l -> founded_steamid.addAll(founded_steamid)); + } catch (InterruptedException err) { + //pass + } + return founded_steamid.stream().distinct().map(SteamIDConverter::getSteamID).filter(Objects::nonNull).toList(); + } + + @Transactional("RoTransactionManager") public List getAttachedDiscordAccountPerSteam(SteamID steamID) { - return entityManager.createNativeQuery("SELECT `discord_id`, UNIX_TIMESTAMP(`timestamp`) as utime, `active` FROM `steam2discord` WHERE `steam_id` LIKE ?1 GROUP BY `discord_id`") + return entityManager_ro.createNativeQuery("SELECT `discord_id`, UNIX_TIMESTAMP(`timestamp`) as utime, `active` FROM `steam2discord` WHERE `steam_id` LIKE ?1 GROUP BY `discord_id`") .setParameter(1, steamID.steam2) .getResultStream().map(d -> Map.of("discord_id", ((Object[]) d)[0], "utime", ((Object[]) d)[1], "active", ((Object[]) d)[2])).toList(); }