diff --git a/src/main/java/app/entities/db/Gametime.java b/src/main/java/app/entities/db/Gametime.java index f820561..be2c366 100644 --- a/src/main/java/app/entities/db/Gametime.java +++ b/src/main/java/app/entities/db/Gametime.java @@ -1,7 +1,9 @@ package app.entities.db; +import app.entities.server.ServerDB; import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonIgnore; +import jakarta.persistence.*; import lombok.Data; import java.sql.ResultSet; @@ -23,28 +25,58 @@ CREATE TABLE `user_connections` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; */ @Data +@Entity +@Table(schema = "tf2_facti13", name = "user_connections") public class Gametime { @JsonIgnore - int id; - String player_name; + @Id + @Column(name = "id") + private Long id; + + @Column(name = "migration_id") + private Long migrationId; + @JsonIgnore - String steam_id; + @ManyToOne + @JoinColumn(name = "srv_id", referencedColumnName = "srv_id") + private ServerDB serverDB; + + @Column(name = "player_name", length = 64) + private String player_name; + @JsonIgnore - String connect_ip; + @Column(name = "steam_id", length = 64) + private String steam_id; + + @Column(name = "connect_ip", length = 15) @JsonIgnore - long account_id; + private String connect_ip; + + @Column(name = "account_id") @JsonIgnore - Timestamp timestamp; + private Long account_id; + + @Column(name = "timestamp") @JsonIgnore - String map; + private Timestamp timestamp; + + @Column(name = "map", length = 128) @JsonIgnore - String connection_type; - int connect_duration; - String reason; + private String map; + + @Column(name = "connection_type", length = 10) + @JsonIgnore + private String connection_type; + + @Column(name = "connect_duration") + private Long connect_duration; + + @Column(name = "reason", length = 256) + private String reason; public Gametime(Object[] obj) { - id = (int) obj[0]; + id = (long) obj[0]; player_name = (String) obj[1]; steam_id = (String) obj[2]; connect_ip = (String) obj[3]; @@ -52,12 +84,12 @@ public class Gametime { timestamp = (Timestamp) obj[5]; map = (String) obj[6]; connection_type = (String) obj[7]; - connect_duration = (int) obj[8]; + connect_duration = (long) obj[8]; reason = (String) obj[9]; } public Gametime(ResultSet rs) throws SQLException { - id = rs.getInt("id"); + id = rs.getLong("id"); player_name = rs.getString("player_name"); steam_id = rs.getString("steam_id"); connect_ip = rs.getString("connect_ip"); @@ -65,10 +97,14 @@ public class Gametime { timestamp = rs.getTimestamp("timestamp"); map = rs.getString("map"); connection_type = rs.getString("connection_type"); - connect_duration = rs.getInt("connect_duration"); + connect_duration = rs.getLong("connect_duration"); reason = rs.getString("reason"); } + public Gametime() { + + } + @JsonGetter public String getMap() { return map.replace("workshop/","").split(".ugc", 2)[0]; diff --git a/src/main/java/app/entities/messages/Message.java b/src/main/java/app/entities/messages/Message.java index bbf1103..e55a9fa 100644 --- a/src/main/java/app/entities/messages/Message.java +++ b/src/main/java/app/entities/messages/Message.java @@ -1,11 +1,18 @@ package app.entities.messages; +import app.entities.db.Gametime; +import app.entities.server.ServerDB; import app.services.db.NicknameService; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; +import jakarta.persistence.OrderBy; +import jakarta.persistence.Table; import lombok.Data; +import org.hibernate.annotations.*; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.List; /* CREATE TABLE user_messages ( @@ -36,6 +43,16 @@ public class Message { @Column(name = "server_id", length = 32) String server_id; + @JsonIgnore + @OneToMany + @JoinColumn(name = "account_id", referencedColumnName = "account_id", insertable=false, updatable=false) + private List gametime; + + @ManyToOne + @JoinColumn(name = "server_id", referencedColumnName = "srv_id", insertable = false, updatable = false) + @JsonIgnore + private ServerDB serverDB; + @Transient String account_name; public Message(Object obj) { @@ -56,12 +73,11 @@ public class Message { } - public Message setAccount_name(NicknameService nicknameService) { - this.account_name = nicknameService.grabNickname(account_id.intValue(), server_id); - return this; + public String getServerName() { + return serverDB == null ? server_id : serverDB.getName(); } public String getAccount_name() { - return ""; + return gametime == null | gametime.isEmpty() ? "" : gametime.get(0).getPlayer_name(); } } \ No newline at end of file diff --git a/src/main/java/app/entities/server/Server.java b/src/main/java/app/entities/server/Server.java index af1315b..f31c422 100644 --- a/src/main/java/app/entities/server/Server.java +++ b/src/main/java/app/entities/server/Server.java @@ -17,6 +17,7 @@ import java.util.List; @Data public class Server extends ExternalValveClient { + String server_id; String name; String description; String address; diff --git a/src/main/java/app/entities/server/ServerDB.java b/src/main/java/app/entities/server/ServerDB.java new file mode 100644 index 0000000..be35b15 --- /dev/null +++ b/src/main/java/app/entities/server/ServerDB.java @@ -0,0 +1,35 @@ +package app.entities.server; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +@Entity +@Table(schema = "tf2_facti13", name = "servers_settings") +public class ServerDB { + @Id + @Column(name = "srv_id") + private String server_id; + + @Column(name = "name") + private String name; + + @Column(name = "description") + private String description; + + public ServerDB() { + } + + public String getServer_id() { + return server_id; + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } +} diff --git a/src/main/java/app/repositories/MessageRepository.java b/src/main/java/app/repositories/MessageRepository.java index b793a86..4a9549b 100644 --- a/src/main/java/app/repositories/MessageRepository.java +++ b/src/main/java/app/repositories/MessageRepository.java @@ -16,7 +16,7 @@ public interface MessageRepository extends PagingAndSortingRepository= m.utime) and " + "(:end_date is null or :end_date <= m.utime) and " + "(:message_contain is null or m.message like :message_contain) and " + - "(:server_id is null or m.server_id like :server_id)") + "(:server_id is null or m.server_id like :server_id) order by m.id desc ") Page getMessages(Pageable pageable, @Param(value = "account_ids") List account_ids, @Param(value = "begin_date") Long begin_date, diff --git a/src/main/java/app/services/db/DetectService.java b/src/main/java/app/services/db/DetectService.java index 625c2b3..72b18bc 100644 --- a/src/main/java/app/services/db/DetectService.java +++ b/src/main/java/app/services/db/DetectService.java @@ -38,47 +38,12 @@ public class DetectService { } public List getAccountsPerSteamID(SteamID steamID) { - List founded_steamid = new ArrayList<>(); - for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { - founded_steamid.addAll(jdbcTemplate_ro.query("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 ? AND connect_ip NOT LIKE '10.%' GROUP BY connect_ip, id ORDER BY id DESC) GROUP BY steam_id", - new Object[]{steamID.steam2}, - (rs, n) -> rs.getString("steam_id"))); - } - //SELECT steam_id FROM tf2.facti13.five.user_connections WHERE connect_ip in (SELECT connect_ip FROM tf2.facti13.five.user_connections WHERE steam_id LIKE 'STEAM_0:0:63666481' AND connect_ip NOT LIKE '10.%' GROUP BY connect_ip ORDER BY id DESC) GROUP BY steam_id; + List founded_steamid = jdbcTemplate_ro.query("SELECT steam_id FROM user_connections WHERE steam_id LIKE ? AND connect_ip NOT LIKE '10.%' GROUP BY connect_ip, steam_id, id ORDER BY id DESC", + new Object[]{steamID.steam2}, + (rs, n) -> rs.getString("steam_id")); 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 ? 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 jdbcTemplate_ro.query(sql, new Object[]{steamID.steam2}, (rs, n) -> rs.getString("steam_id")); - case 1:return jdbcTemplate_rw.query(sql, new Object[]{steamID.steam2}, (rs, n) -> rs.getString("steam_id")); - default: return new ArrayList<>(); - } - }); - } - - List founded_steamid = new ArrayList<>(); - try { - executor.invokeAll(tasks).forEach(l -> founded_steamid.addAll(founded_steamid)); - } catch (InterruptedException err) { - //pass - } - return founded_steamid.stream().distinct().map(SteamIDConverter::getSteamID).filter(Objects::nonNull).toList(); - } public List> getAttachedDiscordAccountPerSteam(SteamID steamID) { return jdbcTemplate_ro.query("SELECT discord_id, extract(epoch from timestamp) as utime, active FROM steam2discord WHERE steam_id LIKE ? GROUP BY discord_id, active, timestamp", diff --git a/src/main/java/app/services/db/KillfeedService.java b/src/main/java/app/services/db/KillfeedService.java index 6869b89..c6db3b5 100644 --- a/src/main/java/app/services/db/KillfeedService.java +++ b/src/main/java/app/services/db/KillfeedService.java @@ -281,7 +281,7 @@ public class KillfeedService { } public TopInFeed setNicknames(NicknameService nicknameService) { - this.name = uid <= 0 || server_id.isEmpty() || server_id == null ?"":nicknameService.grabNickname(uid, server_id); + this.name = uid <= 0 || server_id.isEmpty() || server_id == null ?"":nicknameService.grabNickname(uid); return this; } } @@ -335,9 +335,9 @@ public class KillfeedService { } public KillsInFeed setNicknames(NicknameService nicknameService) { - this.attacker_name = attacker_id == -1 || server_id.isEmpty() || server_id == null?"":nicknameService.grabNickname(attacker_id, server_id); - this.victim_name = victim_id == -1 || server_id.isEmpty() || server_id == null?"":nicknameService.grabNickname(victim_id, server_id); - this.assister_name = assister_id == -1 || server_id.isEmpty() || server_id == null?"":nicknameService.grabNickname(assister_id, server_id); + this.attacker_name = attacker_id == -1 || server_id.isEmpty() || server_id == null?"":nicknameService.grabNickname(attacker_id); + this.victim_name = victim_id == -1 || server_id.isEmpty() || server_id == null?"":nicknameService.grabNickname(victim_id); + this.assister_name = assister_id == -1 || server_id.isEmpty() || server_id == null?"":nicknameService.grabNickname(assister_id); return this; } diff --git a/src/main/java/app/services/db/NicknameService.java b/src/main/java/app/services/db/NicknameService.java index cb25af5..f127134 100644 --- a/src/main/java/app/services/db/NicknameService.java +++ b/src/main/java/app/services/db/NicknameService.java @@ -28,24 +28,20 @@ public class NicknameService { this.stats = stats; } - public String getDB4Server(String server_id) { - return stats.getServers().entrySet().stream().filter(ent -> ent.getKey().equals(server_id)).map(ent -> ent.getValue().getDb()).findFirst().orElse(null); - } - - public String grabNickname(int account_id, String server_id) { + public String grabNickname(int account_id) { if (nickname_cache.containsKey(account_id)) { if (Instant.now().getEpochSecond() - nickname_cache.get(account_id).getValue() < refresh_after) { return nickname_cache.get(account_id).getKey(); } } - String nickname = grabNicknameFromDb(account_id, getDB4Server(server_id)); + String nickname = grabNicknameFromDb(account_id); nickname_cache.put(account_id, Pair.create(nickname, Instant.now().getEpochSecond())); return nickname; } - private String grabNicknameFromDb(int account_id, String db) { + private String grabNicknameFromDb(int account_id) { try { - return jdbcTemplate.query("SELECT player_name FROM " + db + ".user_connections WHERE account_id = ? ORDER BY user_connections.id DESC LIMIT 1;", + return jdbcTemplate.query("SELECT player_name FROM user_connections WHERE account_id = ? ORDER BY user_connections.id DESC LIMIT 1;", new Object[]{account_id}, (rs, n) -> rs.getString("player_name")) .stream().findFirst().orElse("Unknown"); diff --git a/src/main/java/app/services/db/UsertimeService.java b/src/main/java/app/services/db/UsertimeService.java index 806a4a7..f69314e 100644 --- a/src/main/java/app/services/db/UsertimeService.java +++ b/src/main/java/app/services/db/UsertimeService.java @@ -33,21 +33,22 @@ public class UsertimeService { this.stats = stats; } + //todo rework public HashMap> getPlayerLastplay(SteamID steamID) { List maps; HashMap> result = new HashMap<>(); for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { if(last_map) { - maps = getMap(stringServerEntry.getValue().getDb(), stringServerEntry.getValue().getGrab_maps_limit()); + maps = getMap(stringServerEntry.getValue().getServer_id(), stringServerEntry.getValue().getGrab_maps_limit()); } else { - maps = getMaps(stringServerEntry.getValue().getDb()); + maps = getMaps(stringServerEntry.getValue().getServer_id()); } if(maps.size() == 0) {continue;} for(String map: maps){ - Timestamp lastplay = getLastplay(stringServerEntry.getValue().getDb(), steamID, map); + Timestamp lastplay = getLastplay(stringServerEntry.getValue().getServer_id(), steamID, map); if(lastplay != null) { if(!result.containsKey(stringServerEntry.getKey())){ result.put(stringServerEntry.getKey(), new HashMap<>()); @@ -59,21 +60,22 @@ public class UsertimeService { return result; } + //todo rework public HashMap> getPlayerUsertime(SteamID steamID) { List maps; HashMap> result = new HashMap<>(); for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { if(last_map) { - maps = getMap(stringServerEntry.getValue().getDb(), stringServerEntry.getValue().getGrab_maps_limit()); + maps = getMap(stringServerEntry.getValue().getServer_id(), stringServerEntry.getValue().getGrab_maps_limit()); } else { - maps = getMaps(stringServerEntry.getValue().getDb()); + maps = getMaps(stringServerEntry.getValue().getServer_id()); } if(maps.size() == 0) {continue;} for(String map: maps){ - BigDecimal usertime = getTotalPlaytime(stringServerEntry.getValue().getDb(), steamID, map); + BigDecimal usertime = getTotalPlaytime(stringServerEntry.getValue().getServer_id(), steamID, map); if(usertime != null) { if(!result.containsKey(stringServerEntry.getKey())){ result.put(stringServerEntry.getKey(), new HashMap<>()); @@ -85,58 +87,49 @@ public class UsertimeService { return result; } - public Timestamp getLastplay(String db, SteamID steamID, String map) { - return jdbcTemplate.query("select timestamp from "+db+".user_connections WHERE map LIKE ? and connection_type LIKE ? AND account_id = ? ORDER BY user_connections.id DESC LIMIT 1", - new Object[]{ map, "disconnect", steamID.account_id}, + public Timestamp getLastplay(String server_id, SteamID steamID, String map) { + return jdbcTemplate.query("select timestamp from user_connections WHERE srv_id like ? AND map LIKE ? and connection_type LIKE ? AND account_id = ? ORDER BY user_connections.id DESC LIMIT 1", + new Object[]{ server_id, map, "disconnect", steamID.account_id}, (rs, n) -> rs.getTimestamp("timestamp")) .stream().findFirst().orElse(null); } - public BigDecimal getTotalPlaytime(String db, SteamID steamID, String map) { - List l = jdbcTemplate.query("select sum(connect_duration) as total from "+db+".user_connections WHERE map LIKE ? and connection_type LIKE ? AND account_id = ?", - new Object[]{ map, "disconnect", steamID.account_id }, + public BigDecimal getTotalPlaytime(String server_id, SteamID steamID, String map) { + List l = jdbcTemplate.query("select sum(connect_duration) as total from user_connections WHERE srv_id like ? AND map LIKE ? and connection_type LIKE ? AND account_id = ?", + new Object[]{ server_id, map, "disconnect", steamID.account_id }, (rs, n) -> rs.getBigDecimal("total")); //optional if has error return l.size() > 0?l.get(0):null; } - public List getMap(String db, int limit) { - return jdbcTemplate.query("select map from "+db+".user_connections WHERE true group by map, id ORDER BY user_connections.id DESC LIMIT ?", - new Object[]{ limit }, + public List getMap(String server_id, int limit) { + return jdbcTemplate.query("select map from user_connections WHERE srv_id like ? group by map, id ORDER BY user_connections.id DESC LIMIT ?", + new Object[]{ server_id, limit }, (rs, n) -> rs.getString("map")); } - public List getMaps(String db) { - return jdbcTemplate.query("select map from "+db+".user_connections group by map", + public List getMaps(String server_id) { + return jdbcTemplate.query("select map from user_connections where srv_id like ? group by map", + new Object[]{server_id}, (rs, n) -> rs.getString("map")); } public SteamID getSteamOnUsername(String username) { - SteamID steamID; - for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { - steamID = jdbcTemplate.query("SELECT account_id FROM "+stringServerEntry.getValue().getDb()+".user_connections WHERE player_name LIKE ? ORDER BY id DESC LIMIT 1", - new Object[]{ username }, - (rs, n) -> SteamIDConverter.getSteamID("[U:1:%d]".formatted(rs.getInt("account_id")))) - .stream().findFirst().orElse(null); - if (steamID != null) return steamID; - } - return null; + return jdbcTemplate.query("SELECT account_id FROM user_connections WHERE player_name LIKE ? ORDER BY id DESC LIMIT 1", + new Object[]{ username }, + (rs, n) -> SteamIDConverter.getSteamID("[U:1:%d]".formatted(rs.getInt("account_id")))) + .stream().findFirst().orElse(null); } public List getGametimeOnServer(SteamID steamID, String server, Integer limit, Integer offset) { - String db = stats.getServers().entrySet().stream().filter(s -> s.getKey().equals(server)).map(s -> s.getValue().getDb()).findFirst().orElse(null); - if (db == null) return new ArrayList<>(); - - return jdbcTemplate.query("SELECT * FROM " + db + ".user_connections WHERE account_id = ? AND connection_type LIKE ? ORDER BY " + db + ".user_connections.id DESC LIMIT ? OFFSET ?", - new Object[]{steamID.account_id, "disconnect", limit, offset}, + return jdbcTemplate.query("SELECT * FROM user_connections WHERE srv_id like ? and account_id = ? AND connection_type LIKE ? ORDER BY user_connections.id DESC LIMIT ? OFFSET ?", + new Object[]{ server, steamID.account_id, "disconnect", limit, offset}, (rs, n) -> new Gametime(rs)); } public Long getTotalGametimeOnServer(SteamID steamID, String server) { - String db = stats.getServers().entrySet().stream().filter(s -> s.getKey().equals(server)).map(s -> s.getValue().getDb()).findFirst().orElse(null); - if (db == null) return 0L; - return jdbcTemplate.query("SELECT count(id) FROM "+db+".user_connections WHERE account_id = ? AND connection_type LIKE ?", - new Object[]{ steamID.account_id, "disconnect" }, + return jdbcTemplate.query("SELECT count(id) FROM user_connections WHERE srv_id like ? and account_id = ? AND connection_type LIKE ?", + new Object[]{ server, steamID.account_id, "disconnect" }, (rs, n) -> rs.getLong(1)) .stream().findFirst().orElse(0L); } diff --git a/src/main/java/app/services/io/readers/ServersReader.java b/src/main/java/app/services/io/readers/ServersReader.java index 3f39a37..94ce89e 100644 --- a/src/main/java/app/services/io/readers/ServersReader.java +++ b/src/main/java/app/services/io/readers/ServersReader.java @@ -29,7 +29,9 @@ public class ServersReader extends BaseReader { Iterator> iterator = loadConfiguration().fields(); while (iterator.hasNext()) { Map.Entry server = iterator.next(); - stats.getServers().put(server.getKey(), objectMapper.treeToValue(server.getValue(), Server.class)); + Server server_obj = objectMapper.treeToValue(server.getValue(), Server.class); + server_obj.setServer_id(server.getKey()); + stats.getServers().put(server.getKey(), server_obj); logger.info("{}\n{}",server.getKey() ,stats.getServers().get(server.getKey())); } return true; diff --git a/src/main/java/app/updates/CountriesUpdater.java b/src/main/java/app/updates/CountriesUpdater.java index af21b7a..af73f89 100644 --- a/src/main/java/app/updates/CountriesUpdater.java +++ b/src/main/java/app/updates/CountriesUpdater.java @@ -31,7 +31,7 @@ public class CountriesUpdater extends BaseUpdater{ @Value("${backend.updates.countries}") private boolean update; - private final String countries_in_current_year = "SELECT DISTINCT connect_ip FROM %s.user_connections WHERE connection_type LIKE 'connect' AND timestamp > date_trunc('YEAR',now())::DATE UNION\n"; + private final String countries_in_current_year = "SELECT connect_ip FROM user_connections WHERE connection_type LIKE 'connect' AND timestamp > date_trunc('YEAR',now())::DATE group by connect_ip"; @Autowired public CountriesUpdater(Stats stats, @@ -52,12 +52,7 @@ public class CountriesUpdater extends BaseUpdater{ logger.info("Update countries statistic"); stats.getCountries().clear(); - String query = ""; - for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { - query += countries_in_current_year.formatted(stringServerEntry.getValue().getDb()); - } - query = query.substring(0, query.length()-7) + ";"; - jdbcTemplate.query(query, (rs, n) -> rs.getString("connect_ip")).forEach(ip -> { + jdbcTemplate.query(countries_in_current_year, (rs, n) -> rs.getString("connect_ip")).forEach(ip -> { try { stats.getCountries().merge(geoIP.GetCountry(ip), 1, (x, y) -> x+y); } catch (UnknownHostException e) { diff --git a/src/main/java/app/updates/UniqueUpdater.java b/src/main/java/app/updates/UniqueUpdater.java index aed3812..27371cf 100644 --- a/src/main/java/app/updates/UniqueUpdater.java +++ b/src/main/java/app/updates/UniqueUpdater.java @@ -24,10 +24,10 @@ public class UniqueUpdater extends BaseUpdater{ @Qualifier("jt_ro") private JdbcTemplate jdbcTemplate; - private final String query_total = "SELECT DISTINCT account_id FROM %s.user_connections WHERE connection_type LIKE 'disconnect' AND connect_duration > 300 UNION\n"; - private final String query_day = "SELECT DISTINCT account_id FROM %s.user_connections WHERE connection_type LIKE 'disconnect' AND connect_duration > 300 AND timestamp > date_trunc('DAY',now())::DATE UNION\n"; - private final String query_month = "SELECT DISTINCT account_id FROM %s.user_connections WHERE connection_type LIKE 'disconnect' AND connect_duration > 300 AND timestamp > date_trunc('MONTH',now())::DATE UNION\n"; - private final String query_year = "SELECT DISTINCT account_id FROM %s.user_connections WHERE connection_type LIKE 'disconnect' AND connect_duration > 300 AND timestamp > date_trunc('YEAR',now())::DATE UNION\n"; + private final String query_total = "SELECT account_id FROM user_connections WHERE srv_id like '%s' AND connection_type LIKE 'disconnect' AND connect_duration > 300 group by account_id"; + private final String query_day = "SELECT account_id FROM user_connections WHERE srv_id like '%s' AND connection_type LIKE 'disconnect' AND connect_duration > 300 AND timestamp > date_trunc('DAY',now())::DATE group by account_id"; + private final String query_month = "SELECT account_id FROM user_connections WHERE srv_id like '%s' AND connection_type LIKE 'disconnect' AND connect_duration > 300 AND timestamp > date_trunc('MONTH',now())::DATE group by account_id"; + private final String query_year = "SELECT account_id FROM user_connections WHERE srv_id like '%s' AND connection_type LIKE 'disconnect' AND connect_duration > 300 AND timestamp > date_trunc('YEAR',now())::DATE group by account_id"; private final Logger logger = LoggerFactory.getLogger(UniqueUpdater.class); @@ -55,32 +55,29 @@ public class UniqueUpdater extends BaseUpdater{ if(server_update) { logger.warn("Per server updater enabled"); stats.getServers().forEach((server_name, server) -> { - CreateTaskUpdater(() -> getServerUnique(server_name, server.getDb()), 5 * 60 * 1000, getClass().getName() + "_" + server_name); + CreateTaskUpdater(() -> getServerUnique(server_name, server.getServer_id()), 5 * 60 * 1000, getClass().getName() + "_" + server_name); }); } } /////////////////////////////////////////////////////////////////////////////////////////// - public Long getServerUniqueFromQuery(String query, String db) { - query = String.format(query, db); - query = "SELECT COUNT(*) as count FROM (" + query.substring(0, query.length()-7) + ") x;"; + public Long getServerUniqueFromQuery(String query, String server_id) { + query = String.format(query, server_id); + query = "SELECT COUNT(*) as count FROM (" + query + ") x;"; return jdbcTemplate.query(query, (rs, n) -> rs.getLong("count")).stream().findFirst().orElse(0L); } public Long getServerUniqueFromQuery(String query) { String final_query = "SELECT COUNT(*) as count FROM ("; - for(Server server: stats.getServers().values()){ - final_query += query.formatted(server.getDb()); - } - final_query = final_query.substring(0, final_query.length()-7) + ") x;"; + final_query += query.formatted("%") + ") x;"; return jdbcTemplate.query(final_query, (rs, n) -> rs.getLong("count")).stream().findFirst().orElse(0L); } /////////////////////////////////////////////////////////////////////////////////////////// - public boolean getServerUnique(String server_name, String db) { - stats.getServers().get(server_name).UpdateUniq("total", getServerUniqueFromQuery(query_total, db)); - stats.getServers().get(server_name).UpdateUniq("day", getServerUniqueFromQuery(query_day, db)); - stats.getServers().get(server_name).UpdateUniq("month", getServerUniqueFromQuery(query_month, db)); - stats.getServers().get(server_name).UpdateUniq("year", getServerUniqueFromQuery(query_year, db)); + public boolean getServerUnique(String server_name, String server_id) { + stats.getServers().get(server_name).UpdateUniq("total", getServerUniqueFromQuery(query_total, server_id)); + stats.getServers().get(server_name).UpdateUniq("day", getServerUniqueFromQuery(query_day, server_id)); + stats.getServers().get(server_name).UpdateUniq("month", getServerUniqueFromQuery(query_month, server_id)); + stats.getServers().get(server_name).UpdateUniq("year", getServerUniqueFromQuery(query_year, server_id)); return true; } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index c3bc2a8..128abd1 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -15,6 +15,9 @@ spring: hibernate: ddl-auto: ${DDL_AUTO:none} show-sql: ${SHOW_SQL:false} + properties: + hibernate: + format_sql: ${SHOW_SQL:false} org: jobrunr: