From bce3715fb087d946549d95fb40d9c286f4745889 Mon Sep 17 00:00:00 2001 From: gsd Date: Sun, 7 Jul 2024 21:14:14 +0300 Subject: [PATCH] fix --- .../impl/CollectStatisticAspect.java | 2 +- .../interfaces/CollectStatistic.java | 2 +- .../app/entities/db/CollectableStatistic.java | 8 ------- .../db/web/CollectStatisticFilter.java | 21 +++++++++++++++++++ .../db/{ => web}/CollectStatisticService.java | 10 ++++++--- 5 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 src/main/java/app/services/db/web/CollectStatisticFilter.java rename src/main/java/app/services/db/{ => web}/CollectStatisticService.java (86%) diff --git a/src/main/java/app/annotations/impl/CollectStatisticAspect.java b/src/main/java/app/annotations/impl/CollectStatisticAspect.java index 7baaeb2..1e1887b 100644 --- a/src/main/java/app/annotations/impl/CollectStatisticAspect.java +++ b/src/main/java/app/annotations/impl/CollectStatisticAspect.java @@ -3,7 +3,7 @@ package app.annotations.impl; import app.annotations.enums.CollectStages; import app.annotations.interfaces.CollectStatistic; import app.entities.db.CollectableStatistic; -import app.services.db.CollectStatisticService; +import app.services.db.web.CollectStatisticService; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.aspectj.lang.JoinPoint; diff --git a/src/main/java/app/annotations/interfaces/CollectStatistic.java b/src/main/java/app/annotations/interfaces/CollectStatistic.java index e8bc1c5..95cdcd3 100644 --- a/src/main/java/app/annotations/interfaces/CollectStatistic.java +++ b/src/main/java/app/annotations/interfaces/CollectStatistic.java @@ -14,5 +14,5 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface CollectStatistic { - public CollectStages stage() default CollectStages.BEFORE; + CollectStages stage() default CollectStages.BEFORE; } diff --git a/src/main/java/app/entities/db/CollectableStatistic.java b/src/main/java/app/entities/db/CollectableStatistic.java index 706b499..f1cac37 100644 --- a/src/main/java/app/entities/db/CollectableStatistic.java +++ b/src/main/java/app/entities/db/CollectableStatistic.java @@ -59,14 +59,6 @@ public class CollectableStatistic { 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"); - } - public boolean isStatsRequests() { return getPath().equals("/api/stats"); } diff --git a/src/main/java/app/services/db/web/CollectStatisticFilter.java b/src/main/java/app/services/db/web/CollectStatisticFilter.java new file mode 100644 index 0000000..ec7fc1e --- /dev/null +++ b/src/main/java/app/services/db/web/CollectStatisticFilter.java @@ -0,0 +1,21 @@ +package app.services.db.web; + +import app.entities.db.CollectableStatistic; + +import java.util.function.Predicate; + +enum CollectStatisticFilter { + SERVER_REQUEST((c) -> c.getClient_ip() != null && c.getClient_ip().equals("192.168.3.3") && c.getPath() != null && c.getPath().equals("/api/stats")), + API_IGNORE_RULE((c) -> c.getPath() != null && c.getPath().equals("/api/stats") && c.getQuery() != null && !c.getQuery().contains("filter=servers")), + DISCORD_WEB_BOT((c) -> c.getUseragent().contains("Discordbot/2.0; +https://discordapp.com")); + + private final Predicate rule; + + CollectStatisticFilter(Predicate rule) { + this.rule = rule; + } + + public boolean check(CollectableStatistic collectableStatistic) { + return rule.test(collectableStatistic); + } +} diff --git a/src/main/java/app/services/db/CollectStatisticService.java b/src/main/java/app/services/db/web/CollectStatisticService.java similarity index 86% rename from src/main/java/app/services/db/CollectStatisticService.java rename to src/main/java/app/services/db/web/CollectStatisticService.java index fa1272c..5540378 100644 --- a/src/main/java/app/services/db/CollectStatisticService.java +++ b/src/main/java/app/services/db/web/CollectStatisticService.java @@ -1,4 +1,4 @@ -package app.services.db; +package app.services.db.web; import app.entities.db.CollectableStatistic; import app.updates.BaseUpdater; @@ -20,8 +20,12 @@ public class CollectStatisticService extends BaseUpdater { private final Logger logger = LoggerFactory.getLogger(this.getClass()); public void add(CollectableStatistic collectableStatistic) { - if (collectableStatistic.isServerRequest()) return; - if (collectableStatistic.isIgnoreRule()) return; + for(CollectStatisticFilter f:CollectStatisticFilter.values()) { + if (f.check(collectableStatistic)) { + return; + } + } + try { if (!collectableStatistic.isStatsRequests()) logger.info(collectableStatistic.toString());