From a18fab99dc53d56ecd0f1241f57ff8cb591ae958 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 11 Mar 2021 13:25:31 +0100 Subject: [PATCH] Improved probe code. Managed the 53th week case. --- .../statoolinfos/metrics/PathCounters.java | 51 +++++++++-- .../devinsy/statoolinfos/metrics/Prober.java | 32 +++++-- .../statoolinfos/metrics/TimeMark.java | 85 +++++++++++++++++-- .../statoolinfos/metrics/TimeMarkUtils.java | 10 +-- .../metrics/http/HttpAccessLogAnalyzer.java | 7 +- .../metrics/http/HttpErrorLogAnalyzer.java | 6 +- .../statoolinfos/metrics/http/Visit.java | 9 +- 7 files changed, 159 insertions(+), 41 deletions(-) diff --git a/src/fr/devinsy/statoolinfos/metrics/PathCounters.java b/src/fr/devinsy/statoolinfos/metrics/PathCounters.java index 794167a..345d88e 100644 --- a/src/fr/devinsy/statoolinfos/metrics/PathCounters.java +++ b/src/fr/devinsy/statoolinfos/metrics/PathCounters.java @@ -146,9 +146,20 @@ public class PathCounters extends HashMap ended = true; } } - line.removeLast(); - result = line.toString(); + while ((!line.isEmpty()) && (line.getLast().equals(","))) + { + line.removeLast(); + } + + if (line.isEmpty()) + { + result = ""; + } + else + { + result = line.toString(); + } // return result; @@ -178,9 +189,20 @@ public class PathCounters extends HashMap } line.append(','); } - line.removeLast(); - result = line.toString(); + while ((!line.isEmpty()) && (line.getLast().equals(","))) + { + line.removeLast(); + } + + if (line.isEmpty()) + { + result = ""; + } + else + { + result = line.toString(); + } // return result; @@ -234,10 +256,20 @@ public class PathCounters extends HashMap } line.append(','); } - line.removeLast(); - result = line.toString(); + while ((!line.isEmpty()) && (line.getLast().equals(","))) + { + line.removeLast(); + } + if (line.isEmpty()) + { + result = ""; + } + else + { + result = line.toString(); + } // return result; } @@ -254,9 +286,12 @@ public class PathCounters extends HashMap StringSet years = new StringSet(); for (PathCounter counter : this.values()) { - if (counter.getTimeMark().matches("\\d{4}")) + TimeMark timemark = new TimeMark(counter.getTimeMark()); + + Integer year = timemark.getYear(); + if (year != null) { - years.put(counter.getTimeMark()); + years.put(year); } } diff --git a/src/fr/devinsy/statoolinfos/metrics/Prober.java b/src/fr/devinsy/statoolinfos/metrics/Prober.java index 78b7741..17fdd1f 100644 --- a/src/fr/devinsy/statoolinfos/metrics/Prober.java +++ b/src/fr/devinsy/statoolinfos/metrics/Prober.java @@ -23,6 +23,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.UnsupportedEncodingException; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -164,29 +165,44 @@ public class Prober { // Year. PathCounter yearCounter = prefixCounters.get(prefix, year); - String line = String.format("%s.%s=%s", yearCounter.getPath(), yearCounter.getTimeMark(), yearCounter.getCounter()); - metrics.appendln(line); + if (yearCounter != null) + { + String line = String.format("%s.%s=%s", yearCounter.getPath(), yearCounter.getTimeMark(), yearCounter.getCounter()); + metrics.appendln(line); + } } { // Months. StringList line = new StringList(); - line.append(prefix).append('.').append(year).append(".months=").append(prefixCounters.getMonthsValuesLine(prefix, year)); - metrics.appendln(line); + String values = prefixCounters.getMonthsValuesLine(prefix, year); + if (!StringUtils.isBlank(values)) + { + line.append(prefix).append('.').append(year).append(".months=").append(values); + metrics.appendln(line); + } } { // Weeks. StringList line = new StringList(); - line.append(prefix).append('.').append(year).append(".weeks=").append(prefixCounters.getWeeksValuesLine(prefix, year)); - metrics.appendln(line); + String values = prefixCounters.getWeeksValuesLine(prefix, year); + if (!StringUtils.isBlank(values)) + { + line.append(prefix).append('.').append(year).append(".weeks=").append(values); + metrics.appendln(line); + } } { // Days. StringList line = new StringList(); - line.append(prefix).append('.').append(year).append(".days=").append(prefixCounters.getDaysValuesLine(prefix, year)); - metrics.appendln(line); + String values = prefixCounters.getDaysValuesLine(prefix, year); + if (!StringUtils.isBlank(values)) + { + line.append(prefix).append('.').append(year).append(".days=").append(values); + metrics.appendln(line); + } } } diff --git a/src/fr/devinsy/statoolinfos/metrics/TimeMark.java b/src/fr/devinsy/statoolinfos/metrics/TimeMark.java index 14c2480..aac9f9c 100644 --- a/src/fr/devinsy/statoolinfos/metrics/TimeMark.java +++ b/src/fr/devinsy/statoolinfos/metrics/TimeMark.java @@ -23,7 +23,7 @@ import java.time.LocalDateTime; import java.time.YearMonth; import java.time.format.DateTimeFormatter; import java.util.Locale; -import java.util.regex.Pattern; +import java.util.regex.Matcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,6 +48,64 @@ public class TimeMark this.value = value; } + /** + * Gets the year. + * + * @return the year + */ + public Integer getYear() + { + Integer result; + + if (isYearMark()) + { + result = Integer.parseInt(this.value); + } + else if (isYearMonth()) + { + Matcher matcher = TimeMarkUtils.YEAR_MONTH_PATTERN.matcher(this.value); + if (matcher.find()) + { + result = Integer.parseInt(matcher.group("year")); + } + else + { + result = null; + } + } + else if (isYearWeek()) + { + Matcher matcher = TimeMarkUtils.YEAR_WEEK_PATTERN.matcher(this.value); + if (matcher.find()) + { + result = Integer.parseInt(matcher.group("year")); + } + else + { + result = null; + } + } + else if (isDate()) + { + Matcher matcher = TimeMarkUtils.DATE_PATTERN.matcher(this.value); + if (matcher.find()) + { + result = Integer.parseInt(matcher.group("year")); + } + else + { + result = null; + } + } + else + { + result = null; + } + + // + return result; + } + /** * Checks if is date. * @@ -57,9 +115,7 @@ public class TimeMark { boolean result; - Pattern pattern = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$"); - - result = (pattern.matcher(this.value).matches()); + result = TimeMarkUtils.isDate(this.value); // return result; @@ -74,9 +130,22 @@ public class TimeMark { boolean result; - Pattern pattern = Pattern.compile("^\\d{4}$"); + result = TimeMarkUtils.isYear(this.value); - result = (pattern.matcher(this.value).matches()); + // + return result; + } + + /** + * Checks if is year month. + * + * @return true, if is year month + */ + public boolean isYearMonth() + { + boolean result; + + result = TimeMarkUtils.isYearMonth(this.value); // return result; @@ -91,9 +160,7 @@ public class TimeMark { boolean result; - Pattern pattern = Pattern.compile("^\\d{4}W\\d{2}$"); - - result = (pattern.matcher(this.value).matches()); + result = TimeMarkUtils.isYearWeek(this.value); // return result; diff --git a/src/fr/devinsy/statoolinfos/metrics/TimeMarkUtils.java b/src/fr/devinsy/statoolinfos/metrics/TimeMarkUtils.java index 51c3ba3..cc968ba 100644 --- a/src/fr/devinsy/statoolinfos/metrics/TimeMarkUtils.java +++ b/src/fr/devinsy/statoolinfos/metrics/TimeMarkUtils.java @@ -36,10 +36,10 @@ public class TimeMarkUtils { private static Logger logger = LoggerFactory.getLogger(TimeMarkUtils.class); - private static final Pattern YEAR_PATTERN = Pattern.compile("^(?\\d{4})$"); - private static final Pattern YEAR_MONTH_PATTERN = Pattern.compile("^(?\\d{4})-(?\\d{2})$"); - private static final Pattern YEAR_WEEK_PATTERN = Pattern.compile("^(?\\d{4})-W(?\\d{2})$"); - private static final Pattern DATE_PATTERN = Pattern.compile("^(?\\d{4})-(?\\d{2})-(?\\d{2})$"); + public static final Pattern YEAR_PATTERN = Pattern.compile("^(?\\d{4})$"); + public static final Pattern YEAR_MONTH_PATTERN = Pattern.compile("^(?\\d{4})-(?\\d{2})$"); + public static final Pattern YEAR_WEEK_PATTERN = Pattern.compile("^(?\\d{4})-W(?\\d{2})$"); + public static final Pattern DATE_PATTERN = Pattern.compile("^(?\\d{4})-(?\\d{2})-(?\\d{2})$"); /** * Instantiates a new time mark utils. @@ -246,7 +246,7 @@ public class TimeMarkUtils { String result; - result = date.format(DateTimeFormatter.ofPattern("yyyy'-W'ww", Locale.FRANCE)); + result = YearWeek.from(date).toString(); // return result; diff --git a/src/fr/devinsy/statoolinfos/metrics/http/HttpAccessLogAnalyzer.java b/src/fr/devinsy/statoolinfos/metrics/http/HttpAccessLogAnalyzer.java index bc046f8..98a9e3e 100644 --- a/src/fr/devinsy/statoolinfos/metrics/http/HttpAccessLogAnalyzer.java +++ b/src/fr/devinsy/statoolinfos/metrics/http/HttpAccessLogAnalyzer.java @@ -26,6 +26,7 @@ import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -290,10 +291,10 @@ public class HttpAccessLogAnalyzer { if (source.endsWith("*")) { - for (File file : new File(source).getParentFile().listFiles()) + String prefix = source.substring(0, source.length() - 1); + for (File file : new File(prefix).getParentFile().listFiles()) { - // if (file.getName().startsWith(source.getName())) - if (file.getName().equals(source)) + if (file.getName().startsWith(FilenameUtils.getBaseName(prefix))) { analyzer.probe(file); } diff --git a/src/fr/devinsy/statoolinfos/metrics/http/HttpErrorLogAnalyzer.java b/src/fr/devinsy/statoolinfos/metrics/http/HttpErrorLogAnalyzer.java index 1bab342..d8167c9 100644 --- a/src/fr/devinsy/statoolinfos/metrics/http/HttpErrorLogAnalyzer.java +++ b/src/fr/devinsy/statoolinfos/metrics/http/HttpErrorLogAnalyzer.java @@ -215,10 +215,10 @@ public class HttpErrorLogAnalyzer { if (source.endsWith("*")) { - for (File file : new File(source).getParentFile().listFiles()) + String prefix = source.substring(0, source.length() - 1); + for (File file : new File(prefix).getParentFile().listFiles()) { - // if (file.getName().startsWith(source.getName())) - if (file.getName().equals(source)) + if (file.getName().startsWith(prefix)) { analyzer.probe(file); } diff --git a/src/fr/devinsy/statoolinfos/metrics/http/Visit.java b/src/fr/devinsy/statoolinfos/metrics/http/Visit.java index 46dd995..0645299 100644 --- a/src/fr/devinsy/statoolinfos/metrics/http/Visit.java +++ b/src/fr/devinsy/statoolinfos/metrics/http/Visit.java @@ -21,7 +21,6 @@ package fr.devinsy.statoolinfos.metrics.http; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.YearMonth; -import java.time.temporal.WeekFields; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -189,8 +188,8 @@ public class Visit } else { - YearMonth startYearMonth = YearMonth.of(this.start.getYear(), this.start.getMonth()); - YearMonth endYearMonth = YearMonth.of(this.end.getYear(), this.end.getMonth()); + YearMonth startYearMonth = YearMonth.from(this.start); + YearMonth endYearMonth = YearMonth.from(this.end); if ((yearMonth.isBefore(startYearMonth)) || (yearMonth.isAfter(endYearMonth))) { @@ -217,8 +216,8 @@ public class Visit { boolean result; - YearWeek startYearWeek = YearWeek.of(this.start.getYear(), this.start.get(WeekFields.ISO.weekOfYear())); - YearWeek endYearWeek = YearWeek.of(this.end.getYear(), this.end.get(WeekFields.ISO.weekOfYear())); + YearWeek startYearWeek = YearWeek.from(this.start); + YearWeek endYearWeek = YearWeek.from(this.end); if ((yearWeek.isBefore(startYearWeek)) || (yearWeek.isAfter(endYearWeek))) {