diff --git a/ext/sourcepawn-client/Facti13BackendIntegration.sp b/ext/sourcepawn-client/Facti13BackendIntegration.sp index 4aabdb7..51d52dc 100644 --- a/ext/sourcepawn-client/Facti13BackendIntegration.sp +++ b/ext/sourcepawn-client/Facti13BackendIntegration.sp @@ -148,7 +148,7 @@ stock JSONObject createPayload() { stock UpdateStatus(){ if (!g_setuped) return; if (g_updating) return; - if (GetTime() - g_lastupdate < 10) return; + if (GetTime() - g_lastupdate < 5) return; g_updating = true; JSONObject payload = createPayload(); @@ -187,11 +187,12 @@ stock IsValidClient(int client){ public OnPluginStart() { SetupConVar(); RegAdminCmd("fbi_test", TestUpdate, ADMFLAG_ROOT); - g_timer = CreateTimer(15, timerCall, 0, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE); + g_timer = CreateTimer(15, timerCall, 0, TIMER_REPEAT); } public Action timerCall(Handle:t, any:d) { UpdateStatus(); + return Plugin_Continue; } public OnPluginEnd() { @@ -203,7 +204,7 @@ public OnPluginEnd() { public Action TestUpdate(int client, int args){ UpdateStatus(); - ReplyToCommand(client, "OK!") + ReplyToCommand(client, "OK! Last update: %d", g_lastupdate) return Plugin_Handled; } diff --git a/src/main/java/app/annotations/impl/CollectStatisticAspect.java b/src/main/java/app/annotations/impl/CollectStatisticAspect.java new file mode 100644 index 0000000..24b9d09 --- /dev/null +++ b/src/main/java/app/annotations/impl/CollectStatisticAspect.java @@ -0,0 +1,41 @@ +package app.annotations.impl; + +import app.entities.db.CollectableStatistic; +import app.services.db.CollectStatisticService; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.AfterReturning; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; + +import java.util.Arrays; + +@Aspect +@Configuration +public class CollectStatisticAspect { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + private CollectStatisticService collectStatisticService; + + private HttpServletRequest request; + + @Autowired + public CollectStatisticAspect(CollectStatisticService collectStatisticService, HttpServletRequest request) { + this.collectStatisticService = collectStatisticService; + this.request = request; + } + + @Before("@annotation(app.annotations.interfaces.CollectStatistic)") + public void before() { + CollectableStatistic st = new CollectableStatistic(request); + collectStatisticService.add(st); + logger.info(st.toString()); + } +} diff --git a/src/main/java/app/annotations/interfaces/CollectStatistic.java b/src/main/java/app/annotations/interfaces/CollectStatistic.java new file mode 100644 index 0000000..45a3303 --- /dev/null +++ b/src/main/java/app/annotations/interfaces/CollectStatistic.java @@ -0,0 +1,11 @@ +package app.annotations.interfaces; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface CollectStatistic { +} diff --git a/src/main/java/app/controllers/StatsController.java b/src/main/java/app/controllers/StatsController.java index 029ec51..e087e18 100644 --- a/src/main/java/app/controllers/StatsController.java +++ b/src/main/java/app/controllers/StatsController.java @@ -1,6 +1,7 @@ package app.controllers; import app.annotations.interfaces.BurstUpdatePlayers; +import app.annotations.interfaces.CollectStatistic; import app.entities.Stats; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -24,6 +25,7 @@ public class StatsController { @GetMapping @BurstUpdatePlayers + @CollectStatistic public ResponseEntity GetStats(@RequestParam(name = "filter", required = false) String filter){ if (filter != null && !filter.isEmpty()) { return new ResponseEntity(stats.getOnlyThis(filter), HttpStatus.OK); diff --git a/src/main/java/app/controllers/admin/BanController.java b/src/main/java/app/controllers/admin/BanController.java index 84ca4dc..d8a98cc 100644 --- a/src/main/java/app/controllers/admin/BanController.java +++ b/src/main/java/app/controllers/admin/BanController.java @@ -1,10 +1,7 @@ package app.controllers.admin; import app.annotations.enums.AuthMethod; -import app.annotations.interfaces.BurstUpdatePlayers; -import app.annotations.interfaces.CheckPermitionFlag; -import app.annotations.interfaces.CheckWebAccess; -import app.annotations.interfaces.WaitAfterNext; +import app.annotations.interfaces.*; import app.entities.db.Ban; import app.services.ProfileService; import app.services.db.BanService; @@ -39,6 +36,7 @@ public class BanController { @CheckPermitionFlag(flag = "d") @BurstUpdatePlayers @WaitAfterNext(order = "ban") + @CollectStatistic public ResponseEntity banPlayer( HttpServletRequest request, @CookieValue(value = "steam64") String admin_steam64, @@ -64,6 +62,7 @@ public class BanController { @CheckWebAccess @CheckPermitionFlag(flag = "e") @WaitAfterNext(order = "unban") + @CollectStatistic public ResponseEntity unbanPlayer( HttpServletRequest request, @CookieValue(value = "steam64") String admin_steam64, diff --git a/src/main/java/app/controllers/admin/DBController.java b/src/main/java/app/controllers/admin/DBController.java index dc80ccb..a4d06ec 100644 --- a/src/main/java/app/controllers/admin/DBController.java +++ b/src/main/java/app/controllers/admin/DBController.java @@ -3,6 +3,7 @@ package app.controllers.admin; import app.annotations.enums.AuthMethod; import app.annotations.interfaces.CheckPermitionFlag; import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.CollectStatistic; import app.services.db.DetectService; import app.utils.SteamIDConverter; import jakarta.servlet.http.HttpServletRequest; @@ -26,6 +27,7 @@ public class DBController { @GetMapping(value = "/alt") @CheckWebAccess @CheckPermitionFlag(flag = "d") + @CollectStatistic public List getAltAccountPerAccount(HttpServletRequest request, @RequestParam(value = "steam64", required = false, defaultValue = "") String steam64) { return detectService.getAccountsPerSteamID(SteamIDConverter.getSteamID(steam64)).stream().map(s -> s.community_url).toList(); diff --git a/src/main/java/app/controllers/admin/KickController.java b/src/main/java/app/controllers/admin/KickController.java index 8cb2c99..7581033 100644 --- a/src/main/java/app/controllers/admin/KickController.java +++ b/src/main/java/app/controllers/admin/KickController.java @@ -1,9 +1,6 @@ package app.controllers.admin; -import app.annotations.interfaces.BurstUpdatePlayers; -import app.annotations.interfaces.CheckPermitionFlag; -import app.annotations.interfaces.CheckWebAccess; -import app.annotations.interfaces.WaitAfterNext; +import app.annotations.interfaces.*; import app.entities.PlayerProfile; import app.services.ProfileService; import app.services.ServerService; @@ -38,6 +35,7 @@ public class KickController { @CheckPermitionFlag(flag = "c") @BurstUpdatePlayers @WaitAfterNext(order = "kick") + @CollectStatistic public ResponseEntity kickPlayer( HttpServletRequest request, @CookieValue(value = "steam64") String steam64, diff --git a/src/main/java/app/controllers/admin/MuteContoller.java b/src/main/java/app/controllers/admin/MuteContoller.java index 792ce75..7286acd 100644 --- a/src/main/java/app/controllers/admin/MuteContoller.java +++ b/src/main/java/app/controllers/admin/MuteContoller.java @@ -1,9 +1,6 @@ package app.controllers.admin; -import app.annotations.interfaces.BurstUpdatePlayers; -import app.annotations.interfaces.CheckPermitionFlag; -import app.annotations.interfaces.CheckWebAccess; -import app.annotations.interfaces.WaitAfterNext; +import app.annotations.interfaces.*; import app.entities.PlayerProfile; import app.services.ProfileService; import app.services.ServerService; @@ -38,6 +35,7 @@ public class MuteContoller { @CheckPermitionFlag(flag = "c") @BurstUpdatePlayers @WaitAfterNext(order = "mute") + @CollectStatistic public ResponseEntity mutePlayer( HttpServletRequest request, @CookieValue(value = "steam64") String steam64, @@ -56,6 +54,7 @@ public class MuteContoller { @CheckPermitionFlag(flag = "c") @BurstUpdatePlayers @WaitAfterNext(order = "unmute") + @CollectStatistic public ResponseEntity unmutePlayer( HttpServletRequest request, @CookieValue(value = "steam64") String steam64, diff --git a/src/main/java/app/controllers/admin/RconController.java b/src/main/java/app/controllers/admin/RconController.java index 68b5f80..ea7927b 100644 --- a/src/main/java/app/controllers/admin/RconController.java +++ b/src/main/java/app/controllers/admin/RconController.java @@ -2,6 +2,7 @@ package app.controllers.admin; import app.annotations.interfaces.CheckPermitionFlag; import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.WaitAfterNext; import app.services.StatsService; import jakarta.servlet.http.HttpServletRequest; @@ -25,6 +26,7 @@ public class RconController { @CheckWebAccess @CheckPermitionFlag(flag = "m") @WaitAfterNext(order = "rcon") + @CollectStatistic public ResponseEntity rcon(HttpServletRequest request, @RequestParam String srv, @RequestParam String command) { diff --git a/src/main/java/app/controllers/admin/VIPController.java b/src/main/java/app/controllers/admin/VIPController.java index 49fc8e4..cfcb506 100644 --- a/src/main/java/app/controllers/admin/VIPController.java +++ b/src/main/java/app/controllers/admin/VIPController.java @@ -2,6 +2,7 @@ package app.controllers.admin; import app.annotations.interfaces.CheckPermitionFlag; import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.WaitAfterNext; import app.entities.VipGiveMethod; import app.services.db.VIPService; @@ -26,6 +27,7 @@ public class VIPController { @CheckWebAccess @CheckPermitionFlag(flag = "z") @WaitAfterNext(order = "givevip") + @CollectStatistic public ResponseEntity giveVIP( HttpServletRequest request, @CookieValue(value = "steam64") String admin_steam64, @@ -47,6 +49,7 @@ public class VIPController { @CheckWebAccess @CheckPermitionFlag(flag = "z") @WaitAfterNext(order = "removevip") + @CollectStatistic public ResponseEntity removeVIP( HttpServletRequest request, @CookieValue(value = "steam64") String admin_steam64, diff --git a/src/main/java/app/controllers/auth/AuthDiscordController.java b/src/main/java/app/controllers/auth/AuthDiscordController.java index e7474c4..a4aa17e 100644 --- a/src/main/java/app/controllers/auth/AuthDiscordController.java +++ b/src/main/java/app/controllers/auth/AuthDiscordController.java @@ -4,6 +4,7 @@ import app.annotations.enums.AuthMethod; import app.annotations.exceptions.InvalidCookie; import app.annotations.exceptions.NeedCookie; import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.CollectStatistic; import app.entities.other.SteamID; import app.services.db.DiscordAuthService; import app.utils.CryptedCookie; @@ -50,6 +51,7 @@ public class AuthDiscordController { } @GetMapping("login") + @CollectStatistic public ResponseEntity Login() { return ResponseEntity.status(HttpStatus.SEE_OTHER). header("Content-Type", "application/x-www-form-urlencoded"). @@ -58,6 +60,7 @@ public class AuthDiscordController { } @GetMapping("logout") + @CollectStatistic public ResponseEntity Logout(HttpServletResponse response) { Cookie cookie_discord = new Cookie("discord", ""); cookie_discord.setMaxAge(0); @@ -70,6 +73,7 @@ public class AuthDiscordController { } @GetMapping("processlogin") + @CollectStatistic public ResponseEntity ProcessLogin(HttpServletResponse response, @RequestParam Map auth_result){ if (auth_result.isEmpty()) { String html = """ @@ -106,6 +110,7 @@ public class AuthDiscordController { } @GetMapping + @CollectStatistic public ResponseEntity aboutMe(@CookieValue(value = "discord", defaultValue = "") String discord_token) { if (discord_token.isEmpty()) return ResponseEntity.status(401).build(); if (!cryptedCookie.Validate(discord_token)) return ResponseEntity.status(401).build(); @@ -121,6 +126,7 @@ public class AuthDiscordController { } @PostMapping + @CollectStatistic public ResponseEntity registerDiscordUser(@CookieValue(value = "discord", defaultValue = "") String discord_token, @CookieValue(value = "steam64", defaultValue = "") String steam64, @CookieValue(value = "steam64_secured", defaultValue = "") String steam64_secured){ diff --git a/src/main/java/app/controllers/other/CryptoController.java b/src/main/java/app/controllers/other/CryptoController.java index 9c35efd..3b030b5 100644 --- a/src/main/java/app/controllers/other/CryptoController.java +++ b/src/main/java/app/controllers/other/CryptoController.java @@ -2,6 +2,7 @@ package app.controllers.other; import app.annotations.interfaces.CheckPermitionFlag; import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.CollectStatistic; import app.utils.CryptedCookie; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; @@ -31,6 +32,7 @@ public class CryptoController { @PostMapping("/crypt") @CheckWebAccess @CheckPermitionFlag + @CollectStatistic public HashMap crypt(HttpServletRequest httpServletRequest, @RequestBody HashMap need_crypt) { return new HashMap<>() {{ need_crypt.entrySet().stream() @@ -43,6 +45,7 @@ public class CryptoController { @PostMapping("/decrypt") @CheckWebAccess @CheckPermitionFlag + @CollectStatistic public HashMap decrypt(HttpServletRequest httpServletRequest, @RequestBody HashMap need_decrypt) { return new HashMap<>() {{ need_decrypt.entrySet().stream() diff --git a/src/main/java/app/controllers/user/DetailController.java b/src/main/java/app/controllers/user/DetailController.java index 7155ac3..1d1766e 100644 --- a/src/main/java/app/controllers/user/DetailController.java +++ b/src/main/java/app/controllers/user/DetailController.java @@ -3,6 +3,7 @@ package app.controllers.user; import app.annotations.enums.AuthMethod; import app.annotations.interfaces.BurstUpdatePlayers; import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.WaitAfterNext; import app.entities.other.SteamID; import app.services.ProfileService; @@ -37,6 +38,7 @@ public class DetailController { @CheckWebAccess(auth_method = AuthMethod.STEAM64) @BurstUpdatePlayers @WaitAfterNext(order = "getprofile") + @CollectStatistic public ResponseEntity GetUser4Web(HttpServletRequest request, @RequestParam String steam64, @RequestParam(required = false, defaultValue = "") String requests) { @@ -53,6 +55,7 @@ public class DetailController { @GetMapping("/steam/web") @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "search") + @CollectStatistic public ResponseEntity GetSteam4Web(HttpServletRequest request, @RequestParam String any) { return new ResponseEntity<>(profileService.GetSteamIDFromAnyData(any), HttpStatus.OK); @@ -69,6 +72,7 @@ public class DetailController { @PostMapping("/steam/web") @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "search") + @CollectStatistic public ResponseEntity GetSteamOnHashMap4Web(HttpServletRequest request, @RequestBody HashMap container) { if (!container.containsKey("any")) return new ResponseEntity(HttpStatus.BAD_REQUEST); @@ -78,6 +82,7 @@ public class DetailController { @GetMapping("/gametime/web") @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "gametime") + @CollectStatistic public ResponseEntity GetGametime4Web(HttpServletRequest request, @RequestParam String steam64, @RequestParam String srv, diff --git a/src/main/java/app/controllers/user/KillFeedController.java b/src/main/java/app/controllers/user/KillFeedController.java index ef2ce27..cf11899 100644 --- a/src/main/java/app/controllers/user/KillFeedController.java +++ b/src/main/java/app/controllers/user/KillFeedController.java @@ -2,6 +2,7 @@ package app.controllers.user; import app.annotations.enums.AuthMethod; import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.WaitAfterNext; import app.entities.other.SteamID; import app.services.db.KillfeedService; @@ -25,6 +26,7 @@ public class KillFeedController { @GetMapping("/weapons") @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "weapons") + @CollectStatistic public ResponseEntity getPopulateWeapons(HttpServletRequest request, @CookieValue(value = "steam64", defaultValue = "") String mysteam64, @RequestParam(required = false) String steam64, @@ -40,6 +42,7 @@ public class KillFeedController { @GetMapping("/top") @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "top") + @CollectStatistic public ResponseEntity getTopKills(HttpServletRequest request, @RequestParam(required = false) String srv) { return new ResponseEntity(killfeedService.getTopKills(srv), HttpStatus.OK); @@ -48,6 +51,7 @@ public class KillFeedController { @GetMapping @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "kills") + @CollectStatistic public ResponseEntity getKills(HttpServletRequest request, @CookieValue(value = "steam64", defaultValue = "") String mysteam64, @RequestParam(required = false) String steam64, @@ -63,6 +67,7 @@ public class KillFeedController { @PutMapping @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "assists") + @CollectStatistic public ResponseEntity getAssists(HttpServletRequest request, @CookieValue(value = "steam64", defaultValue = "") String mysteam64, @RequestParam(required = false) String steam64, @@ -78,6 +83,7 @@ public class KillFeedController { @DeleteMapping @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "deads") + @CollectStatistic public ResponseEntity getDeads(HttpServletRequest request, @CookieValue(value = "steam64", defaultValue = "") String mysteam64, @RequestParam(required = false) String steam64, @@ -93,6 +99,7 @@ public class KillFeedController { @DeleteMapping("/top") @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "top") + @CollectStatistic public ResponseEntity getTopDeads(HttpServletRequest request, @RequestParam(required = false) String srv) { return new ResponseEntity(killfeedService.getTopDeads(srv), HttpStatus.OK); diff --git a/src/main/java/app/controllers/user/MessagesController.java b/src/main/java/app/controllers/user/MessagesController.java index 333f4ea..04b8388 100644 --- a/src/main/java/app/controllers/user/MessagesController.java +++ b/src/main/java/app/controllers/user/MessagesController.java @@ -2,6 +2,7 @@ package app.controllers.user; import app.annotations.enums.AuthMethod; import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.WaitAfterNext; import app.entities.other.SteamID; import app.services.db.MessageService; @@ -26,6 +27,7 @@ public class MessagesController { @PostMapping(consumes = {MediaType.APPLICATION_JSON_VALUE}) @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "messages") + @CollectStatistic public ResponseEntity getMessages(HttpServletRequest request, @RequestBody(required = false) Payload filter, @RequestParam(required = false) String srv, @@ -38,6 +40,7 @@ public class MessagesController { @PostMapping(value = "/account", consumes = {MediaType.APPLICATION_JSON_VALUE}) @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "messages") + @CollectStatistic 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/ProfileController.java b/src/main/java/app/controllers/user/ProfileController.java index 62c28bb..30a1f67 100644 --- a/src/main/java/app/controllers/user/ProfileController.java +++ b/src/main/java/app/controllers/user/ProfileController.java @@ -1,10 +1,7 @@ package app.controllers.user; import app.annotations.enums.AuthMethod; -import app.annotations.interfaces.BurstUpdatePlayers; -import app.annotations.interfaces.CheckWebAccess; -import app.annotations.interfaces.ShowClientIP; -import app.annotations.interfaces.WaitAfterNext; +import app.annotations.interfaces.*; import app.entities.SocialAuth; import app.services.ProfileService; import app.services.ReportService; @@ -43,6 +40,7 @@ public class ProfileController { @BurstUpdatePlayers @WaitAfterNext(order = "currentuser") @ShowClientIP + @CollectStatistic public ResponseEntity GetCurrentUser(HttpServletRequest request, @CookieValue(value = "steam64", defaultValue = "") String steam64, @RequestParam(value = "requests", defaultValue = "") String requests @@ -57,6 +55,7 @@ public class ProfileController { @PostMapping("/freevip") @CheckWebAccess @WaitAfterNext(order = "freevip") + @CollectStatistic public ResponseEntity GetFreeVIP(HttpServletRequest request, @CookieValue(value = "steam64", defaultValue = "") String steam64, @RequestBody(required = false) SocialAuth socialAuth, @@ -72,6 +71,7 @@ public class ProfileController { @CheckWebAccess @BurstUpdatePlayers @WaitAfterNext(order = "report") + @CollectStatistic public ResponseEntity ReportUser(HttpServletRequest request, @CookieValue(value = "steam64", defaultValue = "") String steam64, @RequestParam(value = "steam64", defaultValue = "") String reported_steam64, @@ -87,6 +87,7 @@ public class ProfileController { } @GetMapping("/buyvip") + @CollectStatistic public ResponseEntity BuyVIP(HttpServletRequest request, @RequestParam(value = "steam64", defaultValue = "") String steam64, @RequestParam String buy_type, diff --git a/src/main/java/app/controllers/user/PublicController.java b/src/main/java/app/controllers/user/PublicController.java index 4a8d5ff..dc8905f 100644 --- a/src/main/java/app/controllers/user/PublicController.java +++ b/src/main/java/app/controllers/user/PublicController.java @@ -2,6 +2,7 @@ package app.controllers.user; import app.annotations.enums.AuthMethod; import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.WaitAfterNext; import app.services.db.BanService; import jakarta.servlet.http.HttpServletRequest; @@ -29,6 +30,7 @@ public class PublicController { @GetMapping("/banlist") @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "banlist") + @CollectStatistic public ResponseEntity getBanList( HttpServletRequest request, @RequestParam(required = false, defaultValue = "20") Integer limit, diff --git a/src/main/java/app/entities/db/CollectableStatistic.java b/src/main/java/app/entities/db/CollectableStatistic.java new file mode 100644 index 0000000..6e3de3c --- /dev/null +++ b/src/main/java/app/entities/db/CollectableStatistic.java @@ -0,0 +1,52 @@ +package app.entities.db; + +import jakarta.servlet.http.HttpServletRequest; +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.sql.Timestamp; +import java.time.Instant; +import java.util.Arrays; + +@Data +public class CollectableStatistic { + Long id; + Timestamp timestamp; + /// + String client_ip; + String steam64; + /// + String method; + String path; + String query; + /// + String useragent; + + public CollectableStatistic(HttpServletRequest request) { + this.client_ip = request.getHeader("X-Forwarded-For"); + this.method = request.getMethod(); + this.path = request.getRequestURI(); + this.query = request.getQueryString(); + this.steam64 = getCookie(request, "steam64"); + this.useragent = request.getHeader("User-Agent"); + this.timestamp = Timestamp.from(Instant.now()); + } + + @Override + public String toString() { + return + "Time: " + timestamp.toString() + "\n" + + "IP: " + client_ip + "\n" + + "Steam64: " + steam64 + "\n" + + "Method: " + method + "\n" + + "Path: " + path + "\n" + + "Query: " + query + "\n" + + "UA: " + useragent + "\n"; + } + + private String getCookie(HttpServletRequest request, String cookie_name) { + if (request.getHeader("Cookie") == null) return null; + String[] rawCookieParams = request.getHeader("Cookie").split(";"); + return Arrays.stream(rawCookieParams).filter(s -> s.contains(cookie_name + "=")).map(s -> s.split(cookie_name + "=")[1]).findFirst().orElse(null); + } +} diff --git a/src/main/java/app/services/db/CollectStatisticService.java b/src/main/java/app/services/db/CollectStatisticService.java new file mode 100644 index 0000000..c1b9252 --- /dev/null +++ b/src/main/java/app/services/db/CollectStatisticService.java @@ -0,0 +1,38 @@ +package app.services.db; + +import app.entities.db.CollectableStatistic; +import app.updates.BaseUpdater; +import jakarta.annotation.PostConstruct; +import jakarta.persistence.EntityManager; +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.ArrayList; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; + +@Service +@Transactional("RwTransactionManager") +public class CollectStatisticService extends BaseUpdater { + + private EntityManager entityManager; + + @Autowired + public CollectStatisticService(@Qualifier(value = "RwEntityManager") EntityManager entityManager) { + this.entityManager = entityManager; + } + + @Transactional("RwTransactionManager") + public void add(CollectableStatistic collectableStatistic) { + entityManager.createNativeQuery("INSERT INTO `web_statistic` (`steam64`, `client_ip`, `method`, `path`, `query`, `useragent`) VALUES (?1, ?2, ?3, ?4, ?5, ?6)") + .setParameter(1, collectableStatistic.getSteam64()) + .setParameter(2, collectableStatistic.getClient_ip()) + .setParameter(3, collectableStatistic.getMethod()) + .setParameter(4, collectableStatistic.getPath()) + .setParameter(5, collectableStatistic.getQuery()) + .setParameter(6, collectableStatistic.getUseragent()) + .executeUpdate(); + } +}