From 1d5179a5c3582650d2050563c5f85fdbc3973397 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sun, 6 Jun 2021 03:32:26 +0200 Subject: [PATCH] Added column in stat ua command. --- .../statoolinfos/core/StatoolInfos.java | 7 +- .../statoolinfos/stats/useragent/IpSet.java | 41 ++++++++++++ .../stats/useragent/UserAgentStat.java | 66 ++++++++++++++----- .../useragent/UserAgentStatComparator.java | 4 +- .../stats/useragent/UserAgentStatSet.java | 30 +++++---- .../stats/useragent/UserAgentStator.java | 4 +- .../stats/visitor/VisitorStatSet.java | 22 ++++++- 7 files changed, 136 insertions(+), 38 deletions(-) create mode 100644 src/fr/devinsy/statoolinfos/stats/useragent/IpSet.java diff --git a/src/fr/devinsy/statoolinfos/core/StatoolInfos.java b/src/fr/devinsy/statoolinfos/core/StatoolInfos.java index d7657cb..8e711bb 100644 --- a/src/fr/devinsy/statoolinfos/core/StatoolInfos.java +++ b/src/fr/devinsy/statoolinfos/core/StatoolInfos.java @@ -237,7 +237,7 @@ public class StatoolInfos for (UserAgentStat stat : stator.getUserAgentStats().sortByLabel()) { - System.out.println(stat.getValue()); + System.out.println(stat.getUserAgent()); } System.err.println(String.format("%s %10d", "UserAgent count: ", stator.getUserAgentStats().size())); @@ -351,6 +351,7 @@ public class StatoolInfos } } + System.err.println("IpCount Ip"); for (IpStat stat : stator.getIps().sortByCount().reverse()) { System.out.println(stat.getCount() + " " + stat.getValue()); @@ -401,9 +402,10 @@ public class StatoolInfos } } + System.err.println("LogCount IpCount VisitCount UserAgent"); for (UserAgentStat stat : stator.getUserAgentStats().sortByCount().reverse()) { - System.out.println(stat.getCount() + " " + stat.getValue()); + System.out.println(String.format("%d %d %d %s", stat.getLogCount(), stat.getIpCount(), stat.getVisitCount(), stat.getUserAgent())); } System.err.println(String.format("%s %10d", "User Agent count: ", stator.getUserAgentStats().size())); @@ -451,6 +453,7 @@ public class StatoolInfos } } + System.err.println("VisitCount LogCount VisitDuration VisitDuration Ip UserAgent"); for (VisitorStat stat : stator.getVisitorStats().sortByVisitCount().reverse()) { System.out.println( diff --git a/src/fr/devinsy/statoolinfos/stats/useragent/IpSet.java b/src/fr/devinsy/statoolinfos/stats/useragent/IpSet.java new file mode 100644 index 0000000..7f408b6 --- /dev/null +++ b/src/fr/devinsy/statoolinfos/stats/useragent/IpSet.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2021 Christian Pierre MOMON + * + * This file is part of StatoolInfos, simple service statistics tool. + * + * StatoolInfos is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * StatoolInfos is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with StatoolInfos. If not, see . + */ +package fr.devinsy.statoolinfos.stats.useragent; + +import java.util.HashSet; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class IpSet. + */ +public final class IpSet extends HashSet +{ + private static final long serialVersionUID = 8883849089810229045L; + private static Logger logger = LoggerFactory.getLogger(IpSet.class); + + /** + * Instantiates a new ip set. + */ + public IpSet() + { + super(); + } +} \ No newline at end of file diff --git a/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStat.java b/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStat.java index 0b25b36..5db49e8 100644 --- a/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStat.java +++ b/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStat.java @@ -21,6 +21,11 @@ package fr.devinsy.statoolinfos.stats.useragent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import fr.devinsy.statoolinfos.metrics.http.HttpAccessLog; +import fr.devinsy.statoolinfos.metrics.http.HttpStatusCategory; +import fr.devinsy.statoolinfos.stats.visitor.VisitorStat; +import fr.devinsy.statoolinfos.stats.visitor.VisitorStatSet; + /** * The Class UserAgentStat. */ @@ -28,9 +33,10 @@ public final class UserAgentStat { private static Logger logger = LoggerFactory.getLogger(UserAgentStat.class); - private String value; - private long count; - private long ipLinkCount; + private String userAgent; + private long logCount; + private IpSet ips; + private VisitorStatSet visitors; /** * Instantiates a new user agent stat. @@ -42,34 +48,62 @@ public final class UserAgentStat */ public UserAgentStat(final String userAgent) { - this.value = userAgent; - this.count = 0; - this.ipLinkCount = 0; + this.userAgent = userAgent; + this.logCount = 0; + this.ips = new IpSet(); + this.visitors = new VisitorStatSet(); } - public long getCount() + public long getIpCount() { - return this.count; + return this.ips.size(); } - public long getIpLinkCount() + public long getLogCount() { - return this.ipLinkCount; + return this.logCount; } - public String getValue() + public String getUserAgent() { - return this.value; + return this.userAgent; } - public void inc() + /** + * Gets the visit count. + * + * @return the visit count + */ + public long getVisitCount() { - this.count += 1; + long result; + + result = this.visitors.getVisitCount(); + + // + return result; } - public void setIpLinkCount(final long ipLinkCount) + public void incLogCount() { - this.ipLinkCount = ipLinkCount; + this.logCount += 1; } + /** + * Put log. + * + * @param log + * the log + */ + public void putLog(final HttpAccessLog log) + { + incLogCount(); + this.ips.add(log.getIp()); + + if (log.getStatus().getCategory() == HttpStatusCategory.SUCCESS) + { + VisitorStat stat = this.visitors.put(log.getIp(), log.getUserAgent().toString()); + stat.getVisits().add(log.getTime()); + } + } } \ No newline at end of file diff --git a/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStatComparator.java b/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStatComparator.java index 5cfe770..b7ee275 100644 --- a/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStatComparator.java +++ b/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStatComparator.java @@ -106,7 +106,7 @@ public class UserAgentStatComparator implements Comparator } else { - result = (long) source.getCount(); + result = (long) source.getLogCount(); } // @@ -130,7 +130,7 @@ public class UserAgentStatComparator implements Comparator } else { - result = source.getValue(); + result = source.getUserAgent(); } // diff --git a/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStatSet.java b/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStatSet.java index 22e3fff..bdb08be 100644 --- a/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStatSet.java +++ b/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStatSet.java @@ -23,6 +23,9 @@ import java.util.HashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import fr.devinsy.statoolinfos.metrics.http.HttpAccessLog; + +// TODO: Auto-generated Javadoc /** * The Class UserAgents. */ @@ -40,27 +43,26 @@ public final class UserAgentStatSet extends HashMap super(); } - public void put(final String userAgent) - { - UserAgentStat stat = get(userAgent); - if (stat == null) - { - stat = new UserAgentStat(userAgent); - this.put(userAgent, stat); - } - - stat.inc(); - } - /** * Put. * + * @param ip + * the ip * @param userAgent * the user agent */ - public void put(final UserAgentStat userAgent) + public void put(final HttpAccessLog log) { - this.put(userAgent.getValue(), userAgent); + String key = log.getUserAgent().toString().trim(); + + UserAgentStat stat = get(key); + if (stat == null) + { + stat = new UserAgentStat(log.getUserAgent().toString()); + this.put(key, stat); + } + + stat.putLog(log); } /** diff --git a/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStator.java b/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStator.java index da9879e..91b660b 100644 --- a/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStator.java +++ b/src/fr/devinsy/statoolinfos/stats/useragent/UserAgentStator.java @@ -70,9 +70,7 @@ public final class UserAgentStator */ public void putLog(final HttpAccessLog log) { - String userAgent = log.getUserAgent().toString().trim(); - this.logCount += 1; - this.userAgents.put(userAgent); + this.userAgents.put(log); } } diff --git a/src/fr/devinsy/statoolinfos/stats/visitor/VisitorStatSet.java b/src/fr/devinsy/statoolinfos/stats/visitor/VisitorStatSet.java index 9d5bd16..c3032b1 100644 --- a/src/fr/devinsy/statoolinfos/stats/visitor/VisitorStatSet.java +++ b/src/fr/devinsy/statoolinfos/stats/visitor/VisitorStatSet.java @@ -60,6 +60,26 @@ public final class VisitorStatSet extends HashMap return result; } + /** + * Gets the visit count. + * + * @return the visit count + */ + public long getVisitCount() + { + long result; + + result = 0; + + for (VisitorStat stat : this.values()) + { + result += stat.getVisitCount(); + } + + // + return result; + } + /** * Put. * @@ -93,7 +113,7 @@ public final class VisitorStatSet extends HashMap */ public void put(final String ip, final UserAgentStat userAgent) { - this.put(userAgent.getValue(), userAgent); + this.put(userAgent.getUserAgent(), userAgent); } /**