diff --git a/src/main/java/app/annotations/impl/WebAccessAspect.java b/src/main/java/app/annotations/impl/WebAccessAspect.java index 37d61ec..41cd81e 100644 --- a/src/main/java/app/annotations/impl/WebAccessAspect.java +++ b/src/main/java/app/annotations/impl/WebAccessAspect.java @@ -34,6 +34,9 @@ public class WebAccessAspect { private final Logger logger = LoggerFactory.getLogger(WebAccessAspect.class); private boolean enabled = true; + @Autowired + private HttpServletRequest request; + @Autowired public WebAccessAspect(SaltedCookie saltedCookie) { this.saltedCookie = saltedCookie; @@ -79,11 +82,22 @@ public class WebAccessAspect { } } + @Before("@annotation(app.annotations.interfaces.CheckWebAccess)") + public void before(JoinPoint joinPoint) { + if (!enabled) return; + AuthMethod auth_method = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(CheckWebAccess.class).auth_method(); + checkWebAccess(auth_method, this.request); + } + + @Before("@annotation(app.annotations.interfaces.CheckWebAccess) && args(request,..)") public void before(JoinPoint joinPoint, HttpServletRequest request){ if (!enabled) return; - AuthMethod auth_method = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(CheckWebAccess.class).auth_method(); + checkWebAccess(auth_method, request); + } + + private void checkWebAccess(AuthMethod auth_method, HttpServletRequest request) { logger.info("check web access"); if(!(request instanceof HttpServletRequest)) { logger.error("Invalid request"); diff --git a/src/main/java/app/controllers/admin/BanController.java b/src/main/java/app/controllers/admin/BanController.java index 2bd39d9..9ad7f84 100644 --- a/src/main/java/app/controllers/admin/BanController.java +++ b/src/main/java/app/controllers/admin/BanController.java @@ -87,7 +87,7 @@ public class BanController { public ResponseEntity banList( HttpServletRequest request, @RequestParam(required = false, defaultValue = "10") Integer limit, - @RequestParam(required = false, defaultValue = "0") Integer offset + @RequestParam(required = false, defaultValue = "0") Long offset ){ return new ResponseEntity(banService.getLastBans(limit, offset), HttpStatus.OK); } diff --git a/src/main/java/app/controllers/user/MessagesController.java b/src/main/java/app/controllers/user/MessagesController.java index 0529c45..a36b658 100644 --- a/src/main/java/app/controllers/user/MessagesController.java +++ b/src/main/java/app/controllers/user/MessagesController.java @@ -9,11 +9,17 @@ import app.services.db.MessageService; import app.utils.SteamIDConverter; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.List; +import java.util.Map; + /** * котроллер для пользователя, чтоб смотреть сообщения */ @@ -31,19 +37,37 @@ public class MessagesController { @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "messages") @CollectStatistic + @Deprecated public ResponseEntity getMessages(HttpServletRequest request, @RequestBody(required = false) Payload filter, @RequestParam(required = false) String srv, - @RequestParam(required = false, defaultValue = "0") Integer offset, + @RequestParam(required = false, defaultValue = "0") Long offset, @RequestParam(required = false, defaultValue = "20") Integer limit) { if (limit > 20) return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); return new ResponseEntity(messageService.getAllMessages(srv, filter==null?null:filter.getMessage(), offset, limit), HttpStatus.OK); } + @PostMapping(value = "/pages", consumes = {MediaType.APPLICATION_JSON_VALUE}) + @CheckWebAccess(auth_method = AuthMethod.STEAM64) + @WaitAfterNext(order = "messages") + @CollectStatistic + public ResponseEntity> getMessagesWithFilters(Pageable pageable, + @RequestBody(required = false) Payload filter, + @RequestParam(required = false) String srv) { + if (pageable.getPageSize() > 20) return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); + + Map result = messageService.getAllMessages(srv, filter==null?null:filter.getMessage(), pageable.getOffset(), pageable.getPageSize()); + return new ResponseEntity<>( + new PageImpl((List) result.get("result"), pageable, (Long) result.get("count")), + HttpStatus.OK + ); + } + @PostMapping(value = "/account", consumes = {MediaType.APPLICATION_JSON_VALUE}) @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "messages") @CollectStatistic + @Deprecated public ResponseEntity getAccountMessages(HttpServletRequest request, @CookieValue(value = "steam64", defaultValue = "") String mysteam64, @RequestBody(required = false) Payload filter, diff --git a/src/main/java/app/controllers/user/PublicController.java b/src/main/java/app/controllers/user/PublicController.java index 73c4644..9e73b6c 100644 --- a/src/main/java/app/controllers/user/PublicController.java +++ b/src/main/java/app/controllers/user/PublicController.java @@ -4,9 +4,13 @@ import app.annotations.enums.AuthMethod; import app.annotations.interfaces.CheckWebAccess; import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.WaitAfterNext; +import app.entities.db.Ban; import app.services.db.BanService; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -34,10 +38,11 @@ public class PublicController { @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "banlist") @CollectStatistic + @Deprecated public ResponseEntity getBanList( HttpServletRequest request, @RequestParam(required = false, defaultValue = "20") Integer limit, - @RequestParam(required = false, defaultValue = "0") Integer offset) { + @RequestParam(required = false, defaultValue = "0") Long offset) { if (limit > 20) return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE); return new ResponseEntity<>(new HashMap<>(){{ @@ -45,4 +50,17 @@ public class PublicController { put("count", banService.getBansCount()); }}, HttpStatus.OK); } + + @GetMapping("/banlist/pages") + @CheckWebAccess(auth_method = AuthMethod.STEAM64) + @WaitAfterNext(order = "banlist") + @CollectStatistic + public ResponseEntity> getBanListWithFilters(Pageable pageable) { + return new ResponseEntity<>( + new PageImpl<>( + banService.getLastBans(pageable.getPageSize(), pageable.getOffset()), + pageable, + banService.getBansCount()) + , HttpStatus.OK); + } } diff --git a/src/main/java/app/services/db/BanService.java b/src/main/java/app/services/db/BanService.java index 24fe79e..de17b64 100644 --- a/src/main/java/app/services/db/BanService.java +++ b/src/main/java/app/services/db/BanService.java @@ -64,7 +64,7 @@ public class BanService { .toList(); } - public List getLastBans(Integer limit, Integer offset) { + public List getLastBans(Integer limit, Long offset) { return jdbcTemplate.query("SELECT * FROM light_bans WHERE active = ? ORDER BY light_bans.id DESC LIMIT ? OFFSET ?", new Object[]{true, limit, offset}, (rs, n) -> new Ban(rs)) diff --git a/src/main/java/app/services/db/KillfeedService.java b/src/main/java/app/services/db/KillfeedService.java index cc4384d..dc85252 100644 --- a/src/main/java/app/services/db/KillfeedService.java +++ b/src/main/java/app/services/db/KillfeedService.java @@ -35,48 +35,94 @@ public class KillfeedService { } public Long getKills(SteamID steamID, String server_id, Long utime) { - return jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND attacker_id = ? AND attacker_id != victim_id AND server_id like ?", - new Object[]{ utime==null?0:utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + return jdbcTemplate.query("SELECT COUNT(*) as c " + + "FROM user_killfeed " + + "WHERE utime > ? AND attacker_id = ? AND attacker_id != victim_id AND (? is null or server_id like ?)", + new Object[]{ + utime==null?0:utime, + steamID.account_id, + server_id==null||server_id.isEmpty()?null:server_id, + server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } public List getTopKills(String server_id) { - return jdbcTemplate.query("SELECT attacker_id, count(*) as c, server_id FROM user_killfeed WHERE victim_id != attacker_id AND attacker_id != 0 AND server_id LIKE ? GROUP BY attacker_id, server_id ORDER BY c DESC LIMIT 10", - new Object[]{server_id == null || server_id.isEmpty() ? "%" : server_id}, + return jdbcTemplate.query("SELECT attacker_id, count(*) as c, server_id " + + "FROM user_killfeed " + + "WHERE victim_id != attacker_id AND attacker_id != 0 AND (? is null or server_id LIKE ?) " + + "GROUP BY attacker_id, server_id " + + "ORDER BY c DESC LIMIT 10", + new Object[]{ + server_id == null || server_id.isEmpty() ? null : server_id, + server_id == null || server_id.isEmpty() ? "%" : server_id}, (rs, n) -> new TopInFeed(rs).setNicknames(nicknameService)); } public Long getDeads(SteamID steamID, String server_id, Long utime) { - return jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND victim_id = ? AND attacker_id != victim_id AND server_id like ?", - new Object[]{ utime==null?0:utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + return jdbcTemplate.query("SELECT COUNT(*) as c " + + "FROM user_killfeed " + + "WHERE utime > ? AND victim_id = ? AND attacker_id != victim_id AND (? is null or server_id like ?)", + new Object[]{ + utime==null?0:utime, + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } public List getTopDeads(String server_id) { - return jdbcTemplate.query("SELECT victim_id, count(id) as c, server_id FROM user_killfeed WHERE victim_id != attacker_id AND victim_id != 0 AND server_id LIKE ? GROUP BY victim_id, server_id ORDER BY c DESC LIMIT 10", - new Object[]{ server_id == null || server_id.isEmpty() ? "%" : server_id}, + return jdbcTemplate.query("SELECT victim_id, count(id) as c, server_id " + + "FROM user_killfeed " + + "WHERE victim_id != attacker_id AND victim_id != 0 AND (? is null or server_id LIKE ?) " + + "GROUP BY victim_id, server_id " + + "ORDER BY c DESC LIMIT 10", + new Object[]{ + server_id == null || server_id.isEmpty() ? null : server_id, + server_id == null || server_id.isEmpty() ? "%" : server_id}, (rs, n) -> new TopInFeed(rs).setNicknames(nicknameService)); } public Long getSuicides(SteamID steamID, String server_id, Long utime) { - return jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND victim_id = ? AND attacker_id = victim_id AND server_id like ?", - new Object[]{ utime==null?0:utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + return jdbcTemplate.query("SELECT COUNT(*) as c " + + "FROM user_killfeed " + + "WHERE utime > ? AND victim_id = ? AND attacker_id = victim_id AND (? is null or server_id like ?)", + new Object[]{ + utime==null?0:utime, + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } public Long getAssists(SteamID steamID, String server_id, Long utime) { - return jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND assister_id = ? AND server_id like ?", - new Object[]{ utime==null?0:utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id}, + return jdbcTemplate.query("SELECT COUNT(*) as c " + + "FROM user_killfeed " + + "WHERE utime > ? AND assister_id = ? AND (? is null or server_id like ?)", + new Object[]{ + utime==null?0:utime, + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id==null||server_id.isEmpty()?"%":server_id}, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } public Map getPopulateWeapons(SteamID steamID, String server_id, int offset, int limit) { - List result = jdbcTemplate.query("SELECT COUNT(u.weapon_index) as c, i.name, u.server_id, u.weapon_classname FROM user_killfeed as u INNER JOIN tf2idb.tf2idb_item as i ON u.weapon_index = i.id WHERE u.attacker_id = ? AND attacker_id != victim_id AND u.server_id like ? GROUP BY u.weapon_index, i.name, u.server_id, u.weapon_classname ORDER BY c DESC LIMIT ? OFFSET ?", - new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id, limit, offset }, + List result = jdbcTemplate.query("SELECT COUNT(u.weapon_index) as c, i.name, u.server_id, u.weapon_classname " + + "FROM user_killfeed as u " + + "INNER JOIN tf2idb.tf2idb_item as i ON u.weapon_index = i.id " + + "WHERE u.attacker_id = ? AND attacker_id != victim_id AND (? is null or u.server_id like ?) " + + "GROUP BY u.weapon_index, i.name, u.server_id, u.weapon_classname " + + "ORDER BY c DESC LIMIT ? OFFSET ?", + new Object[]{ + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id==null||server_id.isEmpty()?"%":server_id, + limit, + offset }, (rs, n) -> new HypeWeapons(rs)); /*Long count = (Long) createNativeQuery("SELECT COUNT(weapon_index) FROM user_killfeed WHERE attacker_id = ? AND server_id like ? GROUP BY weapon_index") @@ -94,12 +140,28 @@ public class KillfeedService { else current_utime = Instant.now().getEpochSecond(); } - List result = jdbcTemplate.query("SELECT -1, u.victim_id, u.assister_id, u.utime, i.name, u.server_id, u.weapon_classname FROM user_killfeed as u INNER JOIN tf2idb.tf2idb_item as i ON u.weapon_index = i.id WHERE u.utime > ? AND u.attacker_id = ? AND u.attacker_id != u.victim_id AND u.server_id like ? ORDER BY u.id DESC LIMIT ? OFFSET ?", - new Object[]{ current_utime, steamID.account_id, server_id == null || server_id.isEmpty() ? "%" : server_id, limit, offset}, + List result = jdbcTemplate.query("SELECT -1, u.victim_id, u.assister_id, u.utime, i.name, u.server_id, u.weapon_classname " + + "FROM user_killfeed as u " + + "INNER JOIN tf2idb.tf2idb_item as i ON u.weapon_index = i.id " + + "WHERE u.utime > ? AND u.attacker_id = ? AND u.attacker_id != u.victim_id AND (? is null or u.server_id like ?) " + + "ORDER BY u.id DESC LIMIT ? OFFSET ?", + new Object[]{ + current_utime, + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id == null || server_id.isEmpty() ? "%" : server_id, + limit, + offset}, (rs, n) -> new KillsInFeed(rs).setNicknames(nicknameService)); - Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND attacker_id = ? AND attacker_id != victim_id AND server_id like ?", - new Object[]{ current_utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + Long count = jdbcTemplate.query("SELECT COUNT(*) as c " + + "FROM user_killfeed " + + "WHERE utime > ? AND attacker_id = ? AND attacker_id != victim_id AND (? is null or server_id like ?)", + new Object[]{ + current_utime, + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); @@ -114,12 +176,28 @@ public class KillfeedService { else current_utime = Instant.now().getEpochSecond(); } - List result = jdbcTemplate.query("SELECT u.attacker_id, -1, u.assister_id, u.utime, i.name, u.server_id, u.weapon_classname FROM user_killfeed as u INNER JOIN tf2idb.tf2idb_item as i ON u.weapon_index = i.id WHERE u.utime > ? AND u.victim_id = ? AND u.attacker_id != u.victim_id AND u.server_id like ? ORDER BY u.id DESC LIMIT ? OFFSET ?", - new Object[]{ current_utime, steamID.account_id, server_id == null || server_id.isEmpty() ? "%" : server_id, limit, offset}, + List result = jdbcTemplate.query("SELECT u.attacker_id, -1, u.assister_id, u.utime, i.name, u.server_id, u.weapon_classname " + + "FROM user_killfeed as u " + + "INNER JOIN tf2idb.tf2idb_item as i ON u.weapon_index = i.id " + + "WHERE u.utime > ? AND u.victim_id = ? AND u.attacker_id != u.victim_id AND (? is null or u.server_id like ?) " + + "ORDER BY u.id DESC LIMIT ? OFFSET ?", + new Object[]{ + current_utime, + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id == null || server_id.isEmpty() ? "%" : server_id, + limit, + offset}, (rs, n) -> new KillsInFeed(rs).setNicknames(nicknameService)); - Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND victim_id = ? AND attacker_id != victim_id AND server_id like ?", - new Object[]{ current_utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + Long count = jdbcTemplate.query("SELECT COUNT(*) as c " + + "FROM user_killfeed " + + "WHERE utime > ? AND victim_id = ? AND attacker_id != victim_id AND (? is null or server_id like ?)", + new Object[]{ + current_utime, + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")).stream().findFirst().orElse(0L); return Map.of("result", result, "count", count, "type", FeedType.DEAD.ordinal()); @@ -133,12 +211,28 @@ public class KillfeedService { else current_utime = Instant.now().getEpochSecond(); } - List result = jdbcTemplate.query("SELECT u.attacker_id, u.victim_id, -1, u.utime, i.name, u.server_id, u.weapon_classname FROM user_killfeed as u INNER JOIN tf2idb.tf2idb_item as i ON u.weapon_index = i.id WHERE u.utime > ? AND u.assister_id = ? AND u.server_id like ? ORDER BY u.id DESC LIMIT ? OFFSET ?", - new Object[]{ current_utime, steamID.account_id, server_id == null || server_id.isEmpty() ? "%" : server_id, limit, offset}, + List result = jdbcTemplate.query("SELECT u.attacker_id, u.victim_id, -1, u.utime, i.name, u.server_id, u.weapon_classname " + + "FROM user_killfeed as u " + + "INNER JOIN tf2idb.tf2idb_item as i ON u.weapon_index = i.id " + + "WHERE u.utime > ? AND u.assister_id = ? AND (? is null or u.server_id like ?) " + + "ORDER BY u.id DESC LIMIT ? OFFSET ?", + new Object[]{ + current_utime, + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id == null || server_id.isEmpty() ? "%" : server_id, + limit, + offset}, (rs, n) -> new KillsInFeed(rs).setNicknames(nicknameService)); - Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND assister_id = ? AND server_id like ?", - new Object[]{ current_utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + Long count = jdbcTemplate.query("SELECT COUNT(*) as c " + + "FROM user_killfeed " + + "WHERE utime > ? AND assister_id = ? AND (? is null or server_id like ?)", + new Object[]{ + current_utime, + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); diff --git a/src/main/java/app/services/db/MessageService.java b/src/main/java/app/services/db/MessageService.java index 7f80213..6c9e56f 100644 --- a/src/main/java/app/services/db/MessageService.java +++ b/src/main/java/app/services/db/MessageService.java @@ -31,13 +31,26 @@ public class MessageService { this.nicknameService = nicknameService; } - public Map getAllMessages(String server_id, String filter, int offset, int limit) { - List result = jdbcTemplate.query("SELECT account_id,utime,message,server_id FROM user_messages WHERE server_id like ? AND message LIKE ? ORDER BY user_messages.id DESC LIMIT ? OFFSET ?", - new Object[]{ server_id == null || server_id.isEmpty() ? "%" : server_id, filter == null || filter.isEmpty() ? "%" : "%" + filter + "%", limit, offset}, + public Map getAllMessages(String server_id, String filter, long offset, int limit) { + List result = jdbcTemplate.query("SELECT account_id,utime,message,server_id " + + "FROM user_messages " + + "WHERE (? is null or server_id like ?) AND message LIKE ? " + + "ORDER BY user_messages.id DESC LIMIT ? OFFSET ?", + new Object[]{ + server_id == null || server_id.isEmpty() ? null : server_id, + server_id == null || server_id.isEmpty() ? "%" : server_id, + filter == null || filter.isEmpty() ? "%" : "%" + filter + "%", + limit, + offset}, (rs, n) -> new Message(rs)); - Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM user_messages WHERE server_id like ? AND message LIKE ?", - new Object[]{ server_id==null||server_id.isEmpty()?"%":server_id, filter==null||filter.isEmpty()?"%":"%" + filter + "%" }, + Long count = jdbcTemplate.query("SELECT COUNT(*) as c " + + "FROM user_messages " + + "WHERE (? is null or server_id like ?) AND message LIKE ?", + new Object[]{ + server_id == null || server_id.isEmpty() ? null : server_id, + server_id==null||server_id.isEmpty()?"%":server_id, + filter==null||filter.isEmpty()?"%":"%" + filter + "%" }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); @@ -45,12 +58,27 @@ public class MessageService { } public Map getAccountMessages(SteamID steamID, String server_id, String filter, int offset, int limit) { - List result = jdbcTemplate.query("SELECT account_id,utime,message,server_id FROM user_messages WHERE account_id = ? AND server_id like ? AND message LIKE ? ORDER BY user_messages.id DESC LIMIT ? OFFSET ?", - new Object[]{steamID.account_id, server_id == null || server_id.isEmpty() ? "%" : server_id, filter == null || filter.isEmpty() ? "%" : "%" + filter + "%", limit, offset}, + List result = jdbcTemplate.query("SELECT account_id,utime,message,server_id " + + "FROM user_messages " + + "WHERE account_id = ? AND (? is null or server_id like ?) AND message LIKE ? " + + "ORDER BY user_messages.id DESC LIMIT ? OFFSET ?", + new Object[]{ + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id == null || server_id.isEmpty() ? "%" : server_id, + filter == null || filter.isEmpty() ? "%" : "%" + filter + "%", + limit, + offset}, (rs, n) -> new Message(rs)); - Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM user_messages WHERE account_id = ? AND server_id like ? AND message LIKE ?", - new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id, filter==null||filter.isEmpty()?"%":"%" + filter + "%"}, + Long count = jdbcTemplate.query("SELECT COUNT(*) as c " + + "FROM user_messages " + + "WHERE account_id = ? AND (? is null or server_id like ?) AND message LIKE ?", + new Object[]{ + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id==null||server_id.isEmpty()?"%":server_id, + filter==null||filter.isEmpty()?"%":"%" + filter + "%"}, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); @@ -58,8 +86,13 @@ public class MessageService { } public Long getMessageCount(SteamID steamID, String server_id) { - return jdbcTemplate.query("SELECT COUNT(*) as c FROM user_messages WHERE account_id = ? AND server_id like ?", - new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + return jdbcTemplate.query("SELECT COUNT(*) as c " + + "FROM user_messages " + + "WHERE account_id = ? AND (? is null or server_id like ?)", + new Object[]{ + steamID.account_id, + server_id == null || server_id.isEmpty() ? null : server_id, + server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } @@ -85,7 +118,7 @@ public class MessageService { ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; */ @Data - class Message { + public class Message { int account_id; long utime; String message; diff --git a/src/main/java/app/updates/OnlineUpdater.java b/src/main/java/app/updates/OnlineUpdater.java index c033cc1..e8c4565 100644 --- a/src/main/java/app/updates/OnlineUpdater.java +++ b/src/main/java/app/updates/OnlineUpdater.java @@ -67,14 +67,24 @@ public class OnlineUpdater extends BaseUpdater { //todo abstract on all public List getStatsOnPerDay(String server_id) { - return jdbcTemplate_ro.query("SELECT avg(player_count) as player_count, cast(to_timestamp(utime) as date) as fulldate, utime as ts FROM servers_online WHERE player_count != 0 and server_id like ? GROUP BY cast(to_timestamp(utime) as date) / ? ORDER BY ts ASC", - new Object[]{server_id == null || server_id.isEmpty() ? "%": server_id, PerDayStats.period}, + return jdbcTemplate_ro.query("SELECT avg(player_count) as player_count, cast(to_timestamp(utime) as date) as fulldate, utime as ts " + + "FROM servers_online WHERE player_count != 0 and (? is null or server_id like ?) " + + "GROUP BY cast(to_timestamp(utime) as date) / ? ORDER BY ts ASC", + new Object[]{ + server_id == null || server_id.isEmpty() ? null: server_id, + server_id == null || server_id.isEmpty() ? "%": server_id, + PerDayStats.period}, (rs, rowNum) -> new PerDayStats(rs)); } public List getStatsOnPerFiveMinutes(String server_id) { - return jdbcTemplate_ro.query("SELECT avg(player_count) as player_count, cast(to_timestamp(utime) as date) as fulldate, utime as ts FROM servers_online WHERE player_count != 0 and server_id like ? GROUP BY cast(to_timestamp(utime) as date) / ? ORDER BY ts ASC", - new Object[]{server_id == null || server_id.isEmpty() ? "%": server_id, PerFiveMinutesStats.period}, + return jdbcTemplate_ro.query("SELECT avg(player_count) as player_count, cast(to_timestamp(utime) as date) as fulldate, utime as ts " + + "FROM servers_online WHERE player_count != 0 and (? is null or server_id like ?) " + + "GROUP BY cast(to_timestamp(utime) as date) / ? ORDER BY ts ASC", + new Object[]{ + server_id == null || server_id.isEmpty() ? null: server_id, + server_id == null || server_id.isEmpty() ? "%": server_id, + PerFiveMinutesStats.period}, (rs, rowNum) -> new PerFiveMinutesStats(rs)); } @@ -94,10 +104,13 @@ public class OnlineUpdater extends BaseUpdater { SELECT round(sum(s.player_count))::int as player_count, s.ts as fl FROM ( SELECT round(max(player_count), 4) as player_count, to_timestamp(utime)::date as ts FROM servers_online -WHERE server_id like ? and player_count != 0 +WHERE (? is null or server_id like ?) and player_count != 0 GROUP BY ts, server_id) as s group by s.ts order by s.ts desc limit ? """, - new Object[]{server_id==null||server_id.isEmpty()?"%":server_id, limit}, + new Object[]{ + server_id==null||server_id.isEmpty()?null:server_id, + server_id==null||server_id.isEmpty()?"%":server_id, + limit}, (rs, rowNum) -> new MaxPeakOfDay(rs)); } @@ -106,10 +119,13 @@ GROUP BY ts, server_id) as s group by s.ts order by s.ts desc limit ? SELECT round(sum(s.player_count))::int as player_count, s.ts as fl FROM ( SELECT round(avg(player_count), 4) as player_count, to_timestamp(utime)::date as ts FROM servers_online -WHERE server_id like ? and player_count != 0 +WHERE (? is null or server_id like ?) and player_count != 0 GROUP BY ts, server_id) as s group by s.ts order by s.ts desc limit ? """, - new Object[]{server_id==null||server_id.isEmpty()?"%":server_id, limit}, + new Object[]{ + server_id==null||server_id.isEmpty()?null:server_id, + server_id==null||server_id.isEmpty()?"%":server_id, + limit}, (rs, rowNum) -> new AvgPeakOfDay(rs)); } @@ -118,9 +134,12 @@ GROUP BY ts, server_id) as s group by s.ts order by s.ts desc limit ? return jdbcTemplate_ro.query("select round(sum(sub.player_count))::int as player_count, sub.ts as fulldate from ("+ "SELECT round(avg(player_count), 4) as player_count, date_bin('"+minutes+" minutes'"+",to_timestamp(utime)::timestamp, '2001-01-01 00:02:30') as ts "+ "FROM servers_online " + -"WHERE utime >= ? and server_id like ? "+ +"WHERE utime >= ? and (? is null or server_id like ?) "+ "GROUP BY ts, server_id) sub GROUP BY sub.ts ", - new Object[]{utime, server_id==null||server_id.isEmpty()?"%":server_id}, + new Object[]{ + utime, + server_id==null||server_id.isEmpty()?null:server_id, + server_id==null||server_id.isEmpty()?"%":server_id}, (rs, rowNum) -> new StatsOfPeakOfPerFiveMinutes(rs)); }