From e8be6ba8d19e2670a1578cb52c02ccf27177ed9e Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sat, 20 Feb 2021 03:57:21 +0100 Subject: [PATCH] Added service count chart and service date status chart. --- .../statoolinfos/core/StatoolInfosUtils.java | 2 +- .../htmlize/FederationStatsPage.java | 2 + .../statoolinfos/htmlize/Htmlizer.java | 102 ++++++++++++++++ .../htmlize/federationStats.xhtml | 4 + .../statoolinfos/metrics/TimeMark.java | 115 ++++++++++++++++++ .../metrics/TimeMarkCounters.java | 71 +++++++++++ .../metrics/YearMonthCounters.java | 107 ++++++++++++++++ 7 files changed, 402 insertions(+), 1 deletion(-) create mode 100644 src/fr/devinsy/statoolinfos/metrics/TimeMarkCounters.java create mode 100644 src/fr/devinsy/statoolinfos/metrics/YearMonthCounters.java diff --git a/src/fr/devinsy/statoolinfos/core/StatoolInfosUtils.java b/src/fr/devinsy/statoolinfos/core/StatoolInfosUtils.java index 4c50a47..b6e8e29 100644 --- a/src/fr/devinsy/statoolinfos/core/StatoolInfosUtils.java +++ b/src/fr/devinsy/statoolinfos/core/StatoolInfosUtils.java @@ -192,7 +192,7 @@ public class StatoolInfosUtils { result = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd/MM/yyyy")); } - else if (date.matches("^\\d{4}-\\d{1,2}-\\d{1,2}/$")) + else if (date.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) { result = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd")); } diff --git a/src/fr/devinsy/statoolinfos/htmlize/FederationStatsPage.java b/src/fr/devinsy/statoolinfos/htmlize/FederationStatsPage.java index 3e6d4f0..dc7b7d8 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/FederationStatsPage.java +++ b/src/fr/devinsy/statoolinfos/htmlize/FederationStatsPage.java @@ -66,6 +66,8 @@ public class FederationStatsPage File htmlizeDirectory = HtmlizerContext.instance().getHtmlizeDirectory(); TagDataManager data = new TagDataManager(); + data.setContent("serviceDateStatusChart", Htmlizer.htmlizeServiceDateStatusChart(federation.getAllServices())); + data.setContent("serviceCountChart", Htmlizer.htmlizeServiceCountChart(federation)); data.setContent("turnoutChart", Htmlizer.htmlizeOrganizationTurnoutChart(federation.getOrganizations())); data.setContent("organizationCountryChart", Htmlizer.htmlizeOrganizationCountryChart(federation.getOrganizations())); diff --git a/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java b/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java index 0b05ebe..8303c93 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java +++ b/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java @@ -20,6 +20,8 @@ package fr.devinsy.statoolinfos.htmlize; import java.io.File; import java.io.IOException; +import java.time.LocalDate; +import java.time.YearMonth; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; @@ -32,6 +34,7 @@ import fr.devinsy.statoolinfos.core.Configuration; import fr.devinsy.statoolinfos.core.Federation; import fr.devinsy.statoolinfos.core.Organization; import fr.devinsy.statoolinfos.core.Organizations; +import fr.devinsy.statoolinfos.core.Service; import fr.devinsy.statoolinfos.core.Services; import fr.devinsy.statoolinfos.core.StatoolInfosException; import fr.devinsy.statoolinfos.core.StatoolInfosUtils; @@ -413,6 +416,65 @@ public class Htmlizer return result; } + /** + * Htmlize service count chart. + * + * @param services + * the services + * @return the string + * @throws StatoolInfosException + * the statool infos exception + */ + public static String htmlizeServiceCountChart(final Federation federation) throws StatoolInfosException + { + String result; + + BarChart chart; + + chart = new BarChart("Nombre des services"); + chart.addDataset("Services"); + + YearMonth now = YearMonth.now(); + YearMonth current = YearMonth.from(StatoolInfosUtils.parseDate(federation.getStartDate())); + while (current.compareTo(now) <= 0) + { + long count = 0; + for (Service service : federation.getAllServices()) + { + LocalDate startDate = StatoolInfosUtils.parseDate(service.getStartDate()); + LocalDate endDate = StatoolInfosUtils.parseDate(service.getEndDate()); + + if (startDate != null) + { + YearMonth start = YearMonth.from(startDate); + YearMonth end; + if (endDate == null) + { + end = now; + } + else + { + end = YearMonth.of(endDate.getYear(), endDate.getMonth()); + } + + if ((current.compareTo(start) >= 0) && (current.compareTo(end) <= 0)) + { + count += 1; + } + } + } + + chart.add(current.toString(), count, ChartColor.VIOLET); + + current = current.plusMonths(1); + } + + result = BarChartView.build(chart); + + // + return result; + } + /** * Htmlize service country chart. * @@ -457,6 +519,46 @@ public class Htmlizer return result; } + /** + * Htmlize service date status chart. + * + * @param services + * the services + * @return the string + * @throws StatoolInfosException + * the statool infos exception + */ + public static String htmlizeServiceDateStatusChart(final Services services) throws StatoolInfosException + { + String result; + + PieChart pie = new PieChart("Services avec dates"); + + long filled = 0; + long unfilled = 0; + for (Service service : services) + { + if (StatoolInfosUtils.parseDate(service.getStartDate()) == null) + { + unfilled += 1; + } + else + { + filled += 1; + } + } + + pie.add("Avec", filled, ChartColor.VIOLET); + pie.add("Sans", unfilled, ChartColor.BLUE); + + pie.setLegendPosition(Position.RIGHT); + + result = PieChartView.build(pie); + + // + return result; + } + /** * Htmlize service install type chart. * diff --git a/src/fr/devinsy/statoolinfos/htmlize/federationStats.xhtml b/src/fr/devinsy/statoolinfos/htmlize/federationStats.xhtml index 8170fa5..17e95e3 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/federationStats.xhtml +++ b/src/fr/devinsy/statoolinfos/htmlize/federationStats.xhtml @@ -15,6 +15,10 @@

Statistiques

+
+
+
+
diff --git a/src/fr/devinsy/statoolinfos/metrics/TimeMark.java b/src/fr/devinsy/statoolinfos/metrics/TimeMark.java index 8bb989c..073bf30 100644 --- a/src/fr/devinsy/statoolinfos/metrics/TimeMark.java +++ b/src/fr/devinsy/statoolinfos/metrics/TimeMark.java @@ -18,7 +18,9 @@ */ package fr.devinsy.statoolinfos.metrics; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.YearMonth; import java.time.format.DateTimeFormatter; import java.util.Locale; import java.util.regex.Pattern; @@ -104,6 +106,47 @@ public class TimeMark this.value = value; } + /** + * To string. + * + * @return the string + */ + @Override + public String toString() + { + String result; + + result = this.value; + + // + return result; + } + + /** + * Day of. + * + * @param date + * the date + * @return the time mark + */ + public static TimeMark dayOf(final LocalDate date) + { + TimeMark result; + + String day = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.FRANCE)); + result = new TimeMark(day); + + // + return result; + } + + /** + * Day of. + * + * @param date + * the date + * @return the time mark + */ public static TimeMark dayOf(final LocalDateTime date) { TimeMark result; @@ -115,6 +158,24 @@ public class TimeMark return result; } + /** + * Month of. + * + * @param date + * the date + * @return the time mark + */ + public static TimeMark monthOf(final LocalDate date) + { + TimeMark result; + + String month = date.format(DateTimeFormatter.ofPattern("yyyy-MM", Locale.FRANCE)); + result = new TimeMark(month); + + // + return result; + } + /** * Month of. * @@ -133,6 +194,42 @@ public class TimeMark return result; } + /** + * Month of. + * + * @param date + * the date + * @return the time mark + */ + public static TimeMark monthOf(final YearMonth date) + { + TimeMark result; + + String month = date.format(DateTimeFormatter.ofPattern("yyyy-MM", Locale.FRANCE)); + result = new TimeMark(month); + + // + return result; + } + + /** + * Year of. + * + * @param date + * the date + * @return the time mark + */ + public static TimeMark yearOf(final LocalDate date) + { + TimeMark result; + + String year = date.format(DateTimeFormatter.ofPattern("yyyy", Locale.FRANCE)); + result = new TimeMark(year); + + // + return result; + } + /** * Year of. * @@ -151,6 +248,24 @@ public class TimeMark return result; } + /** + * Year week of. + * + * @param date + * the date + * @return the time mark + */ + public static TimeMark yearWeekOf(final LocalDate date) + { + TimeMark result; + + String yearWeek = date.format(DateTimeFormatter.ofPattern("yyyyWW", Locale.FRANCE)); + result = new TimeMark(yearWeek); + + // + return result; + } + /** * Year week of. * diff --git a/src/fr/devinsy/statoolinfos/metrics/TimeMarkCounters.java b/src/fr/devinsy/statoolinfos/metrics/TimeMarkCounters.java new file mode 100644 index 0000000..c85c5cc --- /dev/null +++ b/src/fr/devinsy/statoolinfos/metrics/TimeMarkCounters.java @@ -0,0 +1,71 @@ +/* + * 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.metrics; + +import java.util.HashMap; + +/** + * The Class TimeMarkCounters. + */ +public class TimeMarkCounters extends HashMap +{ + private static final long serialVersionUID = -3437543089769867597L; + + /** + * Instantiates a new time mark counters. + */ + public TimeMarkCounters() + { + super(); + } + + /** + * Gets the. + * + * @param timeMark + * the time mark + * @return the long + */ + public Long get(final String timeMark) + { + Long result; + + result = super.get(timeMark); + + // + return result; + } + + /** + * Inc. + * + * @param timemark + * the timemark + */ + public void inc(final String timemark) + { + Long value = super.get(timemark); + if (value == null) + { + value = 0L; + } + + put(timemark, value + 1); + } +} diff --git a/src/fr/devinsy/statoolinfos/metrics/YearMonthCounters.java b/src/fr/devinsy/statoolinfos/metrics/YearMonthCounters.java new file mode 100644 index 0000000..4239264 --- /dev/null +++ b/src/fr/devinsy/statoolinfos/metrics/YearMonthCounters.java @@ -0,0 +1,107 @@ +/* + * 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.metrics; + +import java.time.YearMonth; +import java.util.HashMap; +import java.util.Iterator; + +/** + * The Class YearMonthCounters. + */ +public class YearMonthCounters extends HashMap +{ + private static final long serialVersionUID = 2707786945149971666L; + + /** + * Instantiates a new year month counters. + */ + public YearMonthCounters() + { + super(); + } + + /** + * Gets the. + * + * @param timemark + * the timemark + * @return the long + */ + public long get(final YearMonth timemark) + { + long result; + + Long value = super.get(timemark); + + if (value == null) + { + result = 0; + } + else + { + result = value; + } + + // + return result; + } + + /** + * Gets the first. + * + * @return the first + */ + public YearMonth getFirstTimeMark() + { + YearMonth result; + + result = null; + Iterator iterator = keySet().iterator(); + while (iterator.hasNext()) + { + YearMonth current = iterator.next(); + + if ((result == null) || (current.isBefore(result))) + { + result = current; + } + } + + // + return result; + } + + /** + * Inc. + * + * @param timemark + * the timemark + */ + public void inc(final YearMonth timemark) + { + Long value = super.get(timemark); + if (value == null) + { + value = 0L; + } + + put(timemark, value + 1); + } +}