From ed2590b88b9a90f6c75a644211ce6a39fa742e21 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Tue, 4 Jul 2023 03:20:28 +0200 Subject: [PATCH] Added configuration file for stat visitors cli command. --- .../statoolinfos/cli/StatoolInfosCLI.java | 15 ++++- .../statoolinfos/core/StatoolInfos.java | 63 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java b/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java index db69407..ef9a0f2 100644 --- a/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java +++ b/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java @@ -123,7 +123,7 @@ public final class StatoolInfosCLI message.appendln(" statoolinfos list visitor [-bot|-nobot] generate visitors (ip+ua) list from http log file"); message.appendln(" statoolinfos stat ip [-bot|-nobot] generate stats about ip from http log file"); message.appendln(" statoolinfos stat ua [-bot|-nobot] generate stats about user agent from http log file"); - message.appendln(" statoolinfos stat visitor [-bot|-nobot] generate stats about visitor (ip+ua) from http log file"); + message.appendln(" statoolinfos stat visitor [-bot|-nobot] generate stats about visitor (ip+ua) from http log file"); System.out.print(message.toString()); } @@ -586,6 +586,19 @@ public final class StatoolInfosCLI StatoolInfos.statUserAgents(source, BotFilter.ALL); } + else if (CLIUtils.isMatching(args, "stat", "(visitor|visitors)", "(-all|-bot|-nobot)", ".+\\.conf*")) + { + BotFilter filter = parseLogFilterOption(args[2]); + File configurationFile = new File(StringUtils.trim(args[3])); + + StatoolInfos.statVisitors(configurationFile, filter); + } + else if (CLIUtils.isMatching(args, "stat", "(visitor|visitors)", ".+\\.conf")) + { + File configurationFile = new File(StringUtils.trim(args[2])); + + StatoolInfos.statVisitors(configurationFile, BotFilter.ALL); + } else if (CLIUtils.isMatchingEllipsis(args, "stat", "(visitor|visitors)", "(-all|-bot|-nobot)", ".+")) { BotFilter filter = parseLogFilterOption(args[2]); diff --git a/src/fr/devinsy/statoolinfos/core/StatoolInfos.java b/src/fr/devinsy/statoolinfos/core/StatoolInfos.java index 944f2ea..67ce9ad 100644 --- a/src/fr/devinsy/statoolinfos/core/StatoolInfos.java +++ b/src/fr/devinsy/statoolinfos/core/StatoolInfos.java @@ -865,6 +865,69 @@ public class StatoolInfos } } + /** + * Stat visitors. + * + * @param configurationFile + * the configuration file + * @param filter + * the filter + */ + public static void statVisitors(final File configurationFile, final BotFilter filter) + { + try + { + if ((configurationFile == null) || (!configurationFile.exists())) + { + System.out.println("No configuration file found."); + } + else + { + System.out.println("Testing HttpAccesLog lines from [" + configurationFile.toString() + "]"); + Configuration configuration = Factory.loadConfiguration(configurationFile); + + logger.info("== Testing HttpAccessLog lines."); + String source = configuration.getProbeHttpAccessLogSource(); + String dateTimePattern = configuration.getProbeHttpAccessLogDateTimePattern(); + String pattern = configuration.getProbeHttpAccessLogPattern(); + String pathFilter = configuration.getProbeHttpAccessLogPathFilter(); + logger.info("source=[{}]", source); + logger.info("pattern=[{}]", pattern); + logger.info("dateTimePattern=[{}]", dateTimePattern); + logger.info("pathFilter=[{}]", pathFilter); + + Chrono chrono = new Chrono().start(); + VisitorStator stator = new VisitorStator(); + HttpAccessLogs logs = new HttpAccessLogs(FilesUtils.searchByWildcard(source), pattern, dateTimePattern, pathFilter); + for (HttpAccessLog log : logs) + { + if (filter.matches(log)) + { + stator.putLog(log); + } + } + + System.err.println("VisitCount LogCount VisitDuration VisitDuration Ip UserAgent"); + for (VisitorStat stat : stator.getVisitorStats().sortByVisitCount().reverse()) + { + System.out.println( + String.format("%d %d %d %s %s %s", stat.getVisits().size(), stat.getLogCount(), stat.getVisits().getDurationSum().toSeconds(), + Chrono.format(stat.getVisits().getDurationSum()), + stat.getIp(), stat.getUserAgent())); + } + + System.err.println(String.format("%s %10d", "Visitor count: ", stator.getVisitorStats().size())); + System.err.println(String.format("%s %10d", "Log count: ", stator.getLogCount())); + System.err.println(chrono.format()); + } + } + catch (Exception exception) + { + logger.error("Error: {}", exception.getMessage()); + exception.printStackTrace(); + } + } + /** * Stat visitors. *