diff --git a/src/main/java/app/entities/db/CollectableStatistic.java b/src/main/java/app/entities/db/CollectableStatistic.java index f556bc8..386da0b 100644 --- a/src/main/java/app/entities/db/CollectableStatistic.java +++ b/src/main/java/app/entities/db/CollectableStatistic.java @@ -50,4 +50,12 @@ public class CollectableStatistic { 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); } + + public boolean isServerRequest() { + return getClient_ip() != null && getClient_ip().equals("192.168.3.3") && getPath() != null && getPath().equals("/api/stats"); + } + + public boolean isIgnoreRule() { + return getPath() != null && getPath().equals("/api/stats") && getQuery() != null && !getQuery().contains("filter=servers"); + } } diff --git a/src/main/java/app/services/db/CollectStatisticService.java b/src/main/java/app/services/db/CollectStatisticService.java index 7f039fb..d3e6862 100644 --- a/src/main/java/app/services/db/CollectStatisticService.java +++ b/src/main/java/app/services/db/CollectStatisticService.java @@ -4,6 +4,8 @@ import app.entities.db.CollectableStatistic; import app.updates.BaseUpdater; import jakarta.annotation.PostConstruct; import jakarta.persistence.EntityManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @@ -19,6 +21,8 @@ public class CollectStatisticService extends BaseUpdater { private EntityManager entityManager; + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + @Autowired public CollectStatisticService(@Qualifier(value = "RwEntityManager") EntityManager entityManager) { this.entityManager = entityManager; @@ -26,14 +30,19 @@ public class CollectStatisticService extends BaseUpdater { @Transactional("RwTransactionManager") public void add(CollectableStatistic collectableStatistic) { - if (collectableStatistic.getClient_ip() != null && collectableStatistic.getClient_ip().equals("192.168.3.3") && collectableStatistic.getPath().equals("/api/stats")) return; - 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(); + if (collectableStatistic.isServerRequest()) return; + if (collectableStatistic.isIgnoreRule()) return; + try { + 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(); + } catch (Exception err) { + logger.error("Ignoring add stats"); + } } }