Improved probe code. Managed the 53th week case.

This commit is contained in:
Christian P. MOMON 2021-03-11 13:25:31 +01:00
parent eb7adda168
commit a18fab99dc
7 changed files with 159 additions and 41 deletions

View file

@ -146,9 +146,20 @@ public class PathCounters extends HashMap<String, PathCounter>
ended = true; ended = true;
} }
} }
line.removeLast();
while ((!line.isEmpty()) && (line.getLast().equals(",")))
{
line.removeLast();
}
if (line.isEmpty())
{
result = "";
}
else
{
result = line.toString(); result = line.toString();
}
// //
return result; return result;
@ -178,9 +189,20 @@ public class PathCounters extends HashMap<String, PathCounter>
} }
line.append(','); line.append(',');
} }
line.removeLast();
while ((!line.isEmpty()) && (line.getLast().equals(",")))
{
line.removeLast();
}
if (line.isEmpty())
{
result = "";
}
else
{
result = line.toString(); result = line.toString();
}
// //
return result; return result;
@ -234,10 +256,20 @@ public class PathCounters extends HashMap<String, PathCounter>
} }
line.append(','); line.append(',');
} }
while ((!line.isEmpty()) && (line.getLast().equals(",")))
{
line.removeLast(); line.removeLast();
}
if (line.isEmpty())
{
result = "";
}
else
{
result = line.toString(); result = line.toString();
}
// //
return result; return result;
} }
@ -254,9 +286,12 @@ public class PathCounters extends HashMap<String, PathCounter>
StringSet years = new StringSet(); StringSet years = new StringSet();
for (PathCounter counter : this.values()) 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);
} }
} }

View file

@ -23,6 +23,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -164,31 +165,46 @@ public class Prober
{ {
// Year. // Year.
PathCounter yearCounter = prefixCounters.get(prefix, year); PathCounter yearCounter = prefixCounters.get(prefix, year);
if (yearCounter != null)
{
String line = String.format("%s.%s=%s", yearCounter.getPath(), yearCounter.getTimeMark(), yearCounter.getCounter()); String line = String.format("%s.%s=%s", yearCounter.getPath(), yearCounter.getTimeMark(), yearCounter.getCounter());
metrics.appendln(line); metrics.appendln(line);
} }
}
{ {
// Months. // Months.
StringList line = new StringList(); StringList line = new StringList();
line.append(prefix).append('.').append(year).append(".months=").append(prefixCounters.getMonthsValuesLine(prefix, year)); String values = prefixCounters.getMonthsValuesLine(prefix, year);
if (!StringUtils.isBlank(values))
{
line.append(prefix).append('.').append(year).append(".months=").append(values);
metrics.appendln(line); metrics.appendln(line);
} }
}
{ {
// Weeks. // Weeks.
StringList line = new StringList(); StringList line = new StringList();
line.append(prefix).append('.').append(year).append(".weeks=").append(prefixCounters.getWeeksValuesLine(prefix, year)); String values = prefixCounters.getWeeksValuesLine(prefix, year);
if (!StringUtils.isBlank(values))
{
line.append(prefix).append('.').append(year).append(".weeks=").append(values);
metrics.appendln(line); metrics.appendln(line);
} }
}
{ {
// Days. // Days.
StringList line = new StringList(); StringList line = new StringList();
line.append(prefix).append('.').append(year).append(".days=").append(prefixCounters.getDaysValuesLine(prefix, year)); String values = prefixCounters.getDaysValuesLine(prefix, year);
if (!StringUtils.isBlank(values))
{
line.append(prefix).append('.').append(year).append(".days=").append(values);
metrics.appendln(line); metrics.appendln(line);
} }
} }
}
metrics.appendln(); metrics.appendln();
} }

View file

@ -23,7 +23,7 @@ import java.time.LocalDateTime;
import java.time.YearMonth; import java.time.YearMonth;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Pattern; import java.util.regex.Matcher;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -48,6 +48,64 @@ public class TimeMark
this.value = value; 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. * Checks if is date.
* *
@ -57,9 +115,7 @@ public class TimeMark
{ {
boolean result; boolean result;
Pattern pattern = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$"); result = TimeMarkUtils.isDate(this.value);
result = (pattern.matcher(this.value).matches());
// //
return result; return result;
@ -74,9 +130,22 @@ public class TimeMark
{ {
boolean result; 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; return result;
@ -91,9 +160,7 @@ public class TimeMark
{ {
boolean result; boolean result;
Pattern pattern = Pattern.compile("^\\d{4}W\\d{2}$"); result = TimeMarkUtils.isYearWeek(this.value);
result = (pattern.matcher(this.value).matches());
// //
return result; return result;

View file

@ -36,10 +36,10 @@ public class TimeMarkUtils
{ {
private static Logger logger = LoggerFactory.getLogger(TimeMarkUtils.class); private static Logger logger = LoggerFactory.getLogger(TimeMarkUtils.class);
private static final Pattern YEAR_PATTERN = Pattern.compile("^(?<year>\\d{4})$"); public static final Pattern YEAR_PATTERN = Pattern.compile("^(?<year>\\d{4})$");
private static final Pattern YEAR_MONTH_PATTERN = Pattern.compile("^(?<year>\\d{4})-(?<month>\\d{2})$"); public static final Pattern YEAR_MONTH_PATTERN = Pattern.compile("^(?<year>\\d{4})-(?<month>\\d{2})$");
private static final Pattern YEAR_WEEK_PATTERN = Pattern.compile("^(?<year>\\d{4})-W(?<week>\\d{2})$"); public static final Pattern YEAR_WEEK_PATTERN = Pattern.compile("^(?<year>\\d{4})-W(?<week>\\d{2})$");
private static final Pattern DATE_PATTERN = Pattern.compile("^(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})$"); public static final Pattern DATE_PATTERN = Pattern.compile("^(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})$");
/** /**
* Instantiates a new time mark utils. * Instantiates a new time mark utils.
@ -246,7 +246,7 @@ public class TimeMarkUtils
{ {
String result; String result;
result = date.format(DateTimeFormatter.ofPattern("yyyy'-W'ww", Locale.FRANCE)); result = YearWeek.from(date).toString();
// //
return result; return result;

View file

@ -26,6 +26,7 @@ import java.util.Locale;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -290,10 +291,10 @@ public class HttpAccessLogAnalyzer
{ {
if (source.endsWith("*")) 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().startsWith(FilenameUtils.getBaseName(prefix)))
if (file.getName().equals(source))
{ {
analyzer.probe(file); analyzer.probe(file);
} }

View file

@ -215,10 +215,10 @@ public class HttpErrorLogAnalyzer
{ {
if (source.endsWith("*")) 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().startsWith(prefix))
if (file.getName().equals(source))
{ {
analyzer.probe(file); analyzer.probe(file);
} }

View file

@ -21,7 +21,6 @@ package fr.devinsy.statoolinfos.metrics.http;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.YearMonth; import java.time.YearMonth;
import java.time.temporal.WeekFields;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -189,8 +188,8 @@ public class Visit
} }
else else
{ {
YearMonth startYearMonth = YearMonth.of(this.start.getYear(), this.start.getMonth()); YearMonth startYearMonth = YearMonth.from(this.start);
YearMonth endYearMonth = YearMonth.of(this.end.getYear(), this.end.getMonth()); YearMonth endYearMonth = YearMonth.from(this.end);
if ((yearMonth.isBefore(startYearMonth)) || (yearMonth.isAfter(endYearMonth))) if ((yearMonth.isBefore(startYearMonth)) || (yearMonth.isAfter(endYearMonth)))
{ {
@ -217,8 +216,8 @@ public class Visit
{ {
boolean result; boolean result;
YearWeek startYearWeek = YearWeek.of(this.start.getYear(), this.start.get(WeekFields.ISO.weekOfYear())); YearWeek startYearWeek = YearWeek.from(this.start);
YearWeek endYearWeek = YearWeek.of(this.end.getYear(), this.end.get(WeekFields.ISO.weekOfYear())); YearWeek endYearWeek = YearWeek.from(this.end);
if ((yearWeek.isBefore(startYearWeek)) || (yearWeek.isAfter(endYearWeek))) if ((yearWeek.isBefore(startYearWeek)) || (yearWeek.isAfter(endYearWeek)))
{ {