Improved metric bar chart computation.

This commit is contained in:
Christian P. MOMON 2021-05-23 02:03:40 +02:00
parent fb4ff4483f
commit 0bd23e09ab
8 changed files with 1694 additions and 147 deletions

View file

@ -23,23 +23,16 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.checker.PropertyChecks;
import fr.devinsy.statoolinfos.crawl.CrawlJournal;
import fr.devinsy.statoolinfos.metrics.Metric;
import fr.devinsy.statoolinfos.properties.PathProperties;
import fr.devinsy.statoolinfos.properties.PathProperty;
import fr.devinsy.statoolinfos.properties.PathPropertyList;
import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringSet;
/**
* The Class Service.
@ -454,82 +447,11 @@ public class Service extends PathPropertyList
return result;
}
/**
* Gets the metric.
*
* @param path
* the path
* @return the metric
*/
public Metric getMetric(final String path)
{
Metric result;
String metricName = StringUtils.defaultIfBlank(get(path + ".name"), RegExUtils.removeFirst(path, "^metrics\\."));
String metricDescription = StringUtils.defaultIfBlank(get(path + ".description"), metricName);
StringList years = getMetricYears(path).sort();
if (years.isEmpty())
{
result = null;
}
else
{
result = new Metric(path, metricName, metricDescription, years.get(0));
for (String year : years)
{
result.getYearValues().add(get(path + "." + year));
result.getMonthValues().addAll(StatoolInfosUtils.splitMonthValues(get(path + "." + year + ".months")));
result.getWeekValues().addAll(StatoolInfosUtils.splitWeekValues(get(path + "." + year + ".weeks")));
result.getDayValues().addAll(StatoolInfosUtils.splitDayValues(get(path + "." + year + ".weeks")));
}
}
//
return result;
}
public MetricsList getMetrics()
{
return this.metricsList;
}
/**
* Gets the metric years.
*
* @param path
* the path
* @return the metric years
*/
public StringList getMetricYears(final String path)
{
StringList result;
StringSet years = new StringSet();
Pattern pattern = Pattern.compile("^" + path + "\\.(?<year>\\d{4}).*$");
for (PathProperty property : getByPrefix(path))
{
String subPath = property.getPath();
Matcher matcher = pattern.matcher(subPath);
if (matcher.matches())
{
if (matcher.start("year") != -1)
{
years.add(matcher.group("year"));
}
}
}
result = new StringList(years);
//
return result;
}
/**
* Gets the name.
*

View file

@ -199,7 +199,7 @@ public class StatoolInfos
if (StringUtils.startsWith(line, "file.datetime="))
{
lines.set(lineIndex, "file.datetime=" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss")));
lines.set(lineIndex, "file.datetime=" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")));
}
}

File diff suppressed because it is too large Load diff

View file

@ -35,9 +35,8 @@ import fr.devinsy.statoolinfos.core.Organization;
import fr.devinsy.statoolinfos.core.Service;
import fr.devinsy.statoolinfos.core.StatoolInfosException;
import fr.devinsy.statoolinfos.crawl.CrawlCache;
import fr.devinsy.statoolinfos.htmlize.charts.BarMonthsChartView;
import fr.devinsy.statoolinfos.htmlize.charts.ChartColor;
import fr.devinsy.statoolinfos.metrics.Metric;
import fr.devinsy.statoolinfos.htmlize.charts.MonthValues;
import fr.devinsy.xidyn.XidynException;
import fr.devinsy.xidyn.data.DisplayMode;
import fr.devinsy.xidyn.data.TagDataManager;
@ -255,83 +254,65 @@ public class ServicePage
// service.getPrefixes();
Metric metric = service.getMetric("metrics.http.hits");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric));
}
MonthValues metric = service.getMetricMonthValues("metrics.http.hits");
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric, ChartColor.BLUE));
metric = service.getMetric("metrics.http.errors");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.RED));
}
MonthValues metric4 = service.getMetricMonthValues("metrics.http.hits.ipv4");
MonthValues metric6 = service.getMetricMonthValues("metrics.http.hits.ipv6");
data.setContent("fooChart", graphicIndex++,
ChartHtmlizer.htmlizeMetricsChart("http.hits (ipv4 + ipv6)", null, null, new ChartColor[] { ChartColor.YELLOW, ChartColor.GREEN }, metric4, metric6));
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric4, ChartColor.YELLOW));
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric6, ChartColor.GREEN));
metric = service.getMetric("metrics.http.errors.php");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.RED));
}
metric4 = service.getMetricMonthValues("metrics.http.hits.bots");
metric6 = service.getMetricMonthValues("metrics.http.hits.visitors");
data.setContent("fooChart", graphicIndex++,
ChartHtmlizer.htmlizeMetricsChart("http.hits (visitors + bots)", null, null, new ChartColor[] { ChartColor.GREEN, ChartColor.YELLOW }, metric6, metric4));
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric4, ChartColor.YELLOW));
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric6, ChartColor.GREEN));
metric = service.getMetric("metrics.http.hits.ipv4");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.BLUE));
}
metric = service.getMetricMonthValues("metrics.http.errors");
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric, ChartColor.RED));
metric = service.getMetric("metrics.http.hits.ipv6");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.BLUE));
}
metric = service.getMetricMonthValues("metrics.http.errors.php");
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric, ChartColor.RED));
metric = service.getMetric("metrics.http.hits.bots");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.BLUE));
}
metric = service.getMetricMonthValues("metrics.http.files");
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric, ChartColor.BLUE));
metric = service.getMetric("metrics.http.files");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.BLUE));
}
metric = service.getMetricMonthValues("metrics.http.pages");
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric, ChartColor.BLUE));
metric = service.getMetric("metrics.http.pages");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.BLUE));
}
metric = service.getMetricMonthValues("metrics.http.bytes");
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric, ChartColor.BLUE));
metric = service.getMetric("metrics.http.bytes");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.BLUE));
}
metric = service.getMetricMonthValues("metrics.http.ip");
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric, ChartColor.BLUE));
metric = service.getMetric("metrics.http.ip");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.BLUE));
}
metric4 = service.getMetricMonthValues("metrics.http.ip.bots");
metric6 = service.getMetricMonthValues("metrics.http.ip.visitors");
data.setContent("fooChart", graphicIndex++,
ChartHtmlizer.htmlizeMetricsChart("http.ip (visitors + bots)", null, null, new ChartColor[] { ChartColor.GREEN, ChartColor.YELLOW }, metric6, metric4));
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric6, ChartColor.GREEN));
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric4, ChartColor.YELLOW));
metric = service.getMetric("metrics.http.ip.ipv4");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.BLUE));
}
metric4 = service.getMetricMonthValues("metrics.http.ip.ipv4");
metric6 = service.getMetricMonthValues("metrics.http.ip.ipv6");
data.setContent("fooChart", graphicIndex++,
ChartHtmlizer.htmlizeMetricsChart("http.ip (ipv4 + ipv6)", null, null, new ChartColor[] { ChartColor.YELLOW, ChartColor.GREEN }, metric4, metric6));
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric4, ChartColor.YELLOW));
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric6, ChartColor.GREEN));
metric = service.getMetric("metrics.http.ip.ipv6");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.RED));
}
metric = service.getMetricMonthValues("metrics.http.visits");
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric, ChartColor.BLUE));
metric = service.getMetric("metrics.http.visits");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.RED));
}
metric4 = service.getMetricMonthValues("metrics.http.visits.bots");
metric6 = service.getMetricMonthValues("metrics.http.visits.visitors");
data.setContent("fooChart", graphicIndex++,
ChartHtmlizer.htmlizeMetricsChart("http.visits (visitors + bots)", null, null, new ChartColor[] { ChartColor.GREEN, ChartColor.YELLOW }, metric6, metric4));
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric6, ChartColor.GREEN));
data.setContent("fooChart", graphicIndex++, ChartHtmlizer.htmlizeMetricsChart(null, null, metric4, ChartColor.YELLOW));
//
String content = PresenterUtils.dynamize("/fr/devinsy/statoolinfos/htmlize/service.xhtml", data).toString();

View file

@ -0,0 +1,309 @@
/*
* Copyright (C) 2021 Christian Pierre MOMON <christian@momon.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package fr.devinsy.statoolinfos.htmlize.charts;
import java.time.YearMonth;
import java.util.HashMap;
import fr.devinsy.strings.StringList;
/**
* The Class MonthValues.
*/
public class MonthValues extends HashMap<YearMonth, Double>
{
private static final long serialVersionUID = 1868850913480770744L;
private String label;
private String description;
/**
* Instantiates a new month values.
*/
public MonthValues()
{
this(null, null);
}
/**
* Instantiates a new month values.
*
* @param label
* the label
* @param description
* the description
*/
public MonthValues(final String label, final String description)
{
super();
this.label = label;
this.description = description;
}
/**
* Adds the.
*
* @param timestamp
* the timestamp
* @param value
* the value
*/
public void add(final YearMonth timestamp, final double value)
{
put(timestamp, getValue(timestamp) + value);
}
/**
* Adds the all.
*
* @param source
* the source
*/
public void addAll(final MonthValues source)
{
if (source != null)
{
for (YearMonth timestamp : source.keySet())
{
add(timestamp, source.getValue(timestamp));
}
}
}
/**
* Dec.
*
* @param timestamp
* the timestamp
*/
public void dec(final YearMonth timestamp)
{
put(timestamp, getValue(timestamp) - 1);
}
/**
* Extract.
*
* @param start
* the start
* @param end
* the end
* @return the month value map
*/
public MonthValues extract(final YearMonth start, final YearMonth end)
{
MonthValues result;
YearMonth startTarget = normalizeStart(start);
YearMonth endTarget = normalizeEnd(end);
result = new MonthValues();
for (YearMonth timestamp : this.keySet())
{
if ((!timestamp.isBefore(startTarget)) &&
(!timestamp.isAfter(endTarget)))
{
result.put(timestamp, get(timestamp));
}
}
//
return result;
}
public String getDescription()
{
return this.description;
}
public String getLabel()
{
return this.label;
}
/**
* Gets the normalized timestamps.
*
* @param start
* the start
* @param end
* the end
* @return the normalized timestamps
*/
public StringList getNormalizedTimestamps(final YearMonth start, final YearMonth end)
{
StringList result;
YearMonth startTarget = normalizeStart(start);
YearMonth endTarget = normalizeEnd(end);
result = new StringList();
for (YearMonth timestamp = startTarget; endTarget.isAfter(timestamp); timestamp = timestamp.plusMonths(1))
{
result.append(timestamp);
}
//
return result;
}
/**
* Gets the oldest timestamp.
*
* @return the oldest timestamp
*/
public YearMonth getOldestTimestamp()
{
YearMonth result;
result = null;
for (YearMonth timestamp : this.keySet())
{
if ((result == null) || (timestamp.isBefore(result)))
{
result = timestamp;
}
}
//
return result;
}
/**
* Gets the value.
*
* @param timestamp
* the timestamp
* @return the value
*/
public double getValue(final YearMonth timestamp)
{
double result;
Double value = get(timestamp);
if (value == null)
{
result = 0.0;
}
else
{
result = value;
}
//
return result;
}
/**
* Inc.
*
* @param timestamp
* the timestamp
*/
public void inc(final YearMonth timestamp)
{
put(timestamp, getValue(timestamp) + 1);
}
/**
* Normalize end.
*
* @param end
* the end
* @return the year month
*/
public YearMonth normalizeEnd(final YearMonth end)
{
YearMonth result;
YearMonth endTarget;
if (end == null)
{
result = YearMonth.now();
}
else
{
result = end;
}
//
return result;
}
/**
* Normalize start.
*
* @param start
* the start
* @return the year month
*/
public YearMonth normalizeStart(final YearMonth start)
{
YearMonth result;
if (start == null)
{
result = getOldestTimestamp();
}
else
{
result = start;
}
//
return result;
}
public void setDescription(final String description)
{
this.description = description;
}
public void setLabel(final String label)
{
this.label = label;
}
/**
* Gets the normalized values.
*
* @param start
* the start
* @param end
* the end
* @return the normalized values
*/
public StringList toNormalizedValues(final YearMonth start, final YearMonth end)
{
StringList result;
YearMonth startTarget = normalizeStart(start);
YearMonth endTarget = normalizeEnd(end);
result = new StringList();
for (YearMonth timestamp = startTarget; endTarget.isAfter(timestamp); timestamp = timestamp.plusMonths(1))
{
result.append(getValue(timestamp));
}
//
return result;
}
}

View file

@ -61,7 +61,7 @@
</div>
<div id="chartZone">
<div id="fooChart" style="width: 450px; height: 300px; display: inline-block;"/>
<div id="fooChart" style="width: 450px; height: 300px; display: inline-block; border: 1px solid #e7e7e7;"/>
</div>
</body>
</html>

View file

@ -18,9 +18,12 @@
*/
package fr.devinsy.statoolinfos.metrics;
import java.time.YearMonth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.htmlize.charts.MonthValues;
import fr.devinsy.strings.StringList;
/**
@ -133,4 +136,26 @@ public class Metric
this.startYear = startYear;
}
/**
* To month values.
*
* @return the month values
*/
public MonthValues toMonthValues()
{
MonthValues result;
result = new MonthValues();
result.setLabel(this.name);
YearMonth timestamp = YearMonth.of(Integer.valueOf(this.startYear), 01);
for (String value : this.monthValues)
{
result.put(timestamp, Double.valueOf(value));
timestamp = timestamp.plusMonths(1);
}
//
return result;
}
}

View file

@ -20,15 +20,20 @@ package fr.devinsy.statoolinfos.properties;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.core.StatoolInfosUtils;
import fr.devinsy.statoolinfos.htmlize.charts.MonthValues;
import fr.devinsy.statoolinfos.metrics.Metric;
import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringSet;
@ -315,6 +320,117 @@ public class PathPropertyList extends ArrayList<PathProperty> implements PathPro
return result;
}
/**
* Gets the metric.
*
* @param path
* the path
* @return the metric
*/
public Metric getMetric(final String path)
{
Metric result;
String metricName = StringUtils.defaultIfBlank(get(path + ".name"), RegExUtils.removeFirst(path, "^metrics\\."));
String metricDescription = StringUtils.defaultIfBlank(get(path + ".description"), metricName);
StringList years = getMetricYears(path).sort();
if (years.isEmpty())
{
result = null;
}
else
{
result = new Metric(path, metricName, metricDescription, years.get(0));
for (String year : years)
{
result.getYearValues().add(get(path + "." + year));
result.getMonthValues().addAll(StatoolInfosUtils.splitMonthValues(get(path + "." + year + ".months")));
result.getWeekValues().addAll(StatoolInfosUtils.splitWeekValues(get(path + "." + year + ".weeks")));
result.getDayValues().addAll(StatoolInfosUtils.splitDayValues(get(path + "." + year + ".weeks")));
}
}
//
return result;
}
/**
* Gets the metric.
*
* @param path
* the path
* @return the metric
*/
public MonthValues getMetricMonthValues(final String path)
{
MonthValues result;
String metricName = StringUtils.defaultIfBlank(get(path + ".name"), RegExUtils.removeFirst(path, "^metrics\\."));
String metricDescription = StringUtils.defaultIfBlank(get(path + ".description"), metricName);
StringList years = getMetricYears(path).sort();
result = new MonthValues(metricName, metricDescription);
for (String year : years)
{
String line = get(path + "." + year + ".months");
StringList values = StatoolInfosUtils.splitMonthValues(line);
int monthIndex = 1;
for (String value : values)
{
if (!StringUtils.isBlank(value))
{
YearMonth timestamp = YearMonth.of(Integer.valueOf(year), monthIndex);
result.put(timestamp, Double.valueOf(value));
}
monthIndex += 1;
}
}
//
return result;
}
/**
* Gets the metric years.
*
* @param path
* the path
* @return the metric years
*/
public StringList getMetricYears(final String path)
{
StringList result;
StringSet years = new StringSet();
Pattern pattern = Pattern.compile("^" + path + "\\.(?<year>\\d{4}).*$");
for (PathProperty property : getByPrefix(path))
{
String subPath = property.getPath();
Matcher matcher = pattern.matcher(subPath);
if (matcher.matches())
{
if (matcher.start("year") != -1)
{
years.add(matcher.group("year"));
}
}
}
result = new StringList(years);
//
return result;
}
/**
* Gets the keys.
*