Browse Source

new front update

master
gsd 2 years ago
parent
commit
53f59baf7b
  1. 8
      src/main/java/app/controllers/StatsController.java
  2. 9
      src/main/java/app/entities/Stats.java
  3. 60
      src/main/java/app/services/db/DetectService.java

8
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);
}
}

9
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<>();
}
}
}

60
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<SteamID> getAccountsPerSteamID(SteamID steamID) {
List<String> founded_steamid = new ArrayList<>();
for (Map.Entry<String, Server> 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<SteamID> getAccountsPerSteamIDThreaded(SteamID steamID) {
ExecutorService executor = Executors.newCachedThreadPool();
List tasks = new ArrayList<>();
AtomicReference<Integer> em_selector = new AtomicReference<>(0);
for (Map.Entry<String, Server> stringServerEntry : stats.getServers().entrySet()) {
tasks.add((Callable<List<String>>) () -> {
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<String> 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();
}

Loading…
Cancel
Save