From c98fc07e1bcd562775109096f5a767f77347cbba Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 25 Jul 2022 15:32:56 +0200 Subject: [PATCH] Improved federation service count. --- .../devinsy/statoolinfos/core/Federation.java | 51 ++++++ .../statoolinfos/core/Organization.java | 164 +++++++++++++++++- .../statoolinfos/core/Organizations.java | 26 +++ .../devinsy/statoolinfos/core/Services.java | 129 +++++++++++++- .../statoolinfos/core/StatoolInfosUtils.java | 64 ++++++- .../statoolinfos/htmlize/ChartHtmlizer.java | 47 ++--- .../htmlize/OrganizationHeaderView.java | 6 +- .../properties/PathPropertyList.java | 50 +----- 8 files changed, 449 insertions(+), 88 deletions(-) diff --git a/src/fr/devinsy/statoolinfos/core/Federation.java b/src/fr/devinsy/statoolinfos/core/Federation.java index d68e3f4..98405c6 100644 --- a/src/fr/devinsy/statoolinfos/core/Federation.java +++ b/src/fr/devinsy/statoolinfos/core/Federation.java @@ -386,6 +386,23 @@ public class Federation extends PathPropertyList return result; } + /** + * Gets the member organizations. + * + * @param year + * the year + * @return the member organizations + */ + public Organizations getMemberOrganizations(final Year year) + { + Organizations result; + + result = this.organizations.filterMemberFor(year); + + // + return result; + } + /** * Gets the metric month values all. * @@ -506,6 +523,40 @@ public class Federation extends PathPropertyList return result; } + /** + * Gets the service count by. + * + * @param year + * the year + * @return the service count by + */ + public long getServiceCountBy(final Year year) + { + long result; + + Organizations organizations = this.organizations.filterMemberFor(year); + + result = 0; + for (Organization organization : organizations) + { + for (Service service : organization.getServices().getBy(year)) + { + Year memberStart = organization.getMemberStartYear(); + Year memberEnd = organization.getMemberEndYear(); + Year serviceStart = service.getStartYear(); + Year serviceEnd = service.getEndYear(); + + if (StatoolInfosUtils.overlapp(memberStart, memberEnd, serviceStart, serviceEnd)) + { + result += 1; + } + } + } + + // + return result; + } + /** * Gets the all services. * diff --git a/src/fr/devinsy/statoolinfos/core/Organization.java b/src/fr/devinsy/statoolinfos/core/Organization.java index 8d242cd..a14b993 100644 --- a/src/fr/devinsy/statoolinfos/core/Organization.java +++ b/src/fr/devinsy/statoolinfos/core/Organization.java @@ -501,10 +501,7 @@ public class Organization extends PathPropertyList { String result; - LocalDate startDate = StatoolInfosUtils.parseDate(getMemberStartDate()); - LocalDate endDate = StatoolInfosUtils.parseDate(getMemberEndDate()); - - result = StatoolInfosUtils.toHumanDuration(startDate, endDate); + result = StatoolInfosUtils.toHumanDuration(getMemberStartDate(), getMemberEndDate()); // return result; @@ -515,7 +512,39 @@ public class Organization extends PathPropertyList * * @return the member end date */ - public String getMemberEndDate() + public LocalDate getMemberEndDate() + { + LocalDate result; + + result = getDate("organization.memberof." + this.federation.getName() + ".enddate"); + + // + return result; + } + + /** + * Gets the member end date. + * + * @param entityName + * the entity name + * @return the member end date + */ + public LocalDate getMemberEndDate(final String entityName) + { + LocalDate result; + + result = getDate("organization.memberof." + entityName + ".enddate"); + + // + return result; + } + + /** + * Gets the member end date. + * + * @return the member end date + */ + public String getMemberEndDateValue() { String result; @@ -525,12 +554,67 @@ public class Organization extends PathPropertyList return result; } + /** + * Gets the member end year. + * + * @return the member end year + */ + public Year getMemberEndYear() + { + Year result; + + LocalDate date = getMemberEndDate(); + if (date == null) + { + result = null; + } + else + { + result = Year.from(date); + } + + // + return result; + } + /** * Gets the member start date. * * @return the member start date */ - public String getMemberStartDate() + public LocalDate getMemberStartDate() + { + LocalDate result; + + result = getDate("organization.memberof." + this.federation.getName() + ".startdate"); + + // + return result; + } + + /** + * Gets the member start date. + * + * @param entityName + * the entity name + * @return the member start date + */ + public LocalDate getMemberStartDate(final String entityName) + { + LocalDate result; + + result = getDate("organization.memberof." + entityName + ".startdate"); + + // + return result; + } + + /** + * Gets the member start date. + * + * @return the member start date + */ + public String getMemberStartDateValue() { String result; @@ -540,6 +624,29 @@ public class Organization extends PathPropertyList return result; } + /** + * Gets the member start year. + * + * @return the member start year + */ + public Year getMemberStartYear() + { + Year result; + + LocalDate date = getMemberStartDate(); + if (date == null) + { + result = null; + } + else + { + result = Year.from(date); + } + + // + return result; + } + /** * Gets the status member of. * @@ -1224,6 +1331,51 @@ public class Organization extends PathPropertyList return result; } + /** + * Checks if is member. + * + * @param year + * the year + * @return true, if is member + */ + public boolean isMember(final Year year) + { + boolean result; + + if (year == null) + { + result = false; + } + else + { + Year startYear = getMemberStartYear(); + if (startYear == null) + { + result = false; + } + else + { + Year endYear = getMemberEndYear(); + if (endYear == null) + { + endYear = Year.now(); + } + + if ((year.isBefore(startYear)) || (year.isAfter(endYear))) + { + result = false; + } + else + { + result = true; + } + } + } + + // + return result; + } + /** * Checks if is valid. * diff --git a/src/fr/devinsy/statoolinfos/core/Organizations.java b/src/fr/devinsy/statoolinfos/core/Organizations.java index 1f45f8c..d16cf48 100644 --- a/src/fr/devinsy/statoolinfos/core/Organizations.java +++ b/src/fr/devinsy/statoolinfos/core/Organizations.java @@ -18,6 +18,7 @@ */ package fr.devinsy.statoolinfos.core; +import java.time.Year; import java.util.ArrayList; import java.util.Collections; @@ -182,6 +183,31 @@ public class Organizations extends ArrayList return result; } + /** + * Filter member for. + * + * @param year + * the year + * @return the organizations + */ + public Organizations filterMemberFor(final Year year) + { + Organizations result; + + result = new Organizations(); + + for (Organization organization : this) + { + if (organization.isMember(year)) + { + result.add(organization); + } + } + + // + return result; + } + /** * Gets the active service count. * diff --git a/src/fr/devinsy/statoolinfos/core/Services.java b/src/fr/devinsy/statoolinfos/core/Services.java index 6b5da8b..9e868ee 100644 --- a/src/fr/devinsy/statoolinfos/core/Services.java +++ b/src/fr/devinsy/statoolinfos/core/Services.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Christian Pierre MOMON + * Copyright (C) 2020-2022 Christian Pierre MOMON * * This file is part of StatoolInfos, simple service statistics tool. * @@ -18,6 +18,8 @@ */ package fr.devinsy.statoolinfos.core; +import java.time.LocalDate; +import java.time.Year; import java.util.ArrayList; import java.util.Collections; @@ -40,6 +42,42 @@ public class Services extends ArrayList super(); } + /** + * Count by year. + * + * @param year + * the year + * @return the long + */ + public long countBy(final Year year) + { + long result; + + result = 0; + Year now = Year.now(); + for (Service service : this) + { + Year start = service.getStartYear(); + Year end = service.getEndYear(); + + if (start != null) + { + if (end == null) + { + end = now; + } + + if ((!start.isAfter(year) && (!end.isBefore(year)))) + { + result += 1; + } + } + } + + // + return result; + } + /** * Gets the by. * @@ -117,6 +155,95 @@ public class Services extends ArrayList return result; } + /** + * Gets the by. + * + * @param year + * the year + * @return the by + */ + public Services getBy(final Year year) + { + Services result; + + result = new Services(); + + if (year != null) + { + for (Service service : this) + { + Year startYear = service.getStartYear(); + Year endYear = service.getEndYear(); + if (endYear == null) + { + endYear = Year.now(); + } + + if ((startYear != null) && (!year.isBefore(startYear)) && (!year.isAfter(endYear))) + { + result.add(service); + } + } + } + + // + return result; + } + + /** + * Gets the older. + * + * @return the older + */ + public Service getOldestService() + { + Service result; + + result = null; + LocalDate oldestDate = null; + for (Service current : this) + { + LocalDate date = current.getStartDate(); + + if (date != null) + { + LocalDate currentDate = current.getStartDate(); + if ((result == null) || (currentDate.isBefore(oldestDate))) + { + result = current; + oldestDate = currentDate; + } + } + } + + // + return result; + } + + /** + * Gets the oldest year. + * + * @return the oldest year + */ + public Year getOldestStartYear() + { + Year result; + + Service oldestService = getOldestService(); + + if (oldestService == null) + { + result = null; + } + else + { + result = oldestService.getStartYear(); + } + + // + return result; + } + /** * Reverse. * diff --git a/src/fr/devinsy/statoolinfos/core/StatoolInfosUtils.java b/src/fr/devinsy/statoolinfos/core/StatoolInfosUtils.java index 529ed25..aec7603 100644 --- a/src/fr/devinsy/statoolinfos/core/StatoolInfosUtils.java +++ b/src/fr/devinsy/statoolinfos/core/StatoolInfosUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2021 Christian Pierre MOMON + * Copyright (C) 2020-2022 Christian Pierre MOMON * * This file is part of StatoolInfos, simple service statistics tool. * @@ -27,6 +27,7 @@ import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.Period; +import java.time.Year; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; @@ -206,6 +207,63 @@ public class StatoolInfosUtils return new Date().getTime(); } + /** + * Overlapp. + * + * @param start1 + * the start 1 + * @param end1 + * the end 1 + * @param start2 + * the start 2 + * @param end2 + * the end 2 + * @return true, if successful + */ + public static boolean overlapp(final Year start1, final Year end1, final Year start2, final Year end2) + { + boolean result; + + if ((start1 == null) || (start2 == null)) + { + result = false; + } + else + { + Year end11; + if (end1 == null) + { + end11 = Year.now(); + } + else + { + end11 = end1; + } + + Year end22; + if (end2 == null) + { + end22 = Year.now(); + } + else + { + end22 = end2; + } + + if ((end22.isBefore(start1)) || (start2.isAfter(end11))) + { + result = false; + } + else + { + result = true; + } + } + + // + return result; + } + /** * Parses the date. * @@ -235,6 +293,10 @@ public class StatoolInfosUtils { result = LocalDate.parse("01/" + date, DateTimeFormatter.ofPattern("dd/MM/yyyy")); } + else if (date.matches("\\d{4}-\\d{2}")) + { + result = LocalDate.parse(date + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd")); + } else { result = null; diff --git a/src/fr/devinsy/statoolinfos/htmlize/ChartHtmlizer.java b/src/fr/devinsy/statoolinfos/htmlize/ChartHtmlizer.java index cf4d63e..10488c8 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/ChartHtmlizer.java +++ b/src/fr/devinsy/statoolinfos/htmlize/ChartHtmlizer.java @@ -1451,7 +1451,23 @@ public class ChartHtmlizer { String result; - result = htmlizeServiceCountYearChart(federation.getServices(), federation.getStartYear()); + BarChart chart; + + chart = new BarChart("Nombre de services"); + chart.addDataset("Services"); + + Year current = federation.getStartYear(); + Year now = Year.now(); + while (!current.isAfter(now)) + { + long count = federation.getServiceCountBy(current); + + chart.add(String.valueOf(current), count, ChartColor.YELLOW); + + current = current.plusYears(1); + } + + result = BarChartView.build(chart); // return result; @@ -1531,36 +1547,11 @@ public class ChartHtmlizer if (first != null) { - int now = LocalDate.now().getYear(); + int now = Year.now().getValue(); int current = first; while (current <= now) { - long count = 0; - for (Service service : services) - { - LocalDate startDate = service.getStartDate(); - LocalDate endDate = service.getEndDate(); - - if (startDate != null) - { - int start = startDate.getYear(); - int end; - if (endDate == null) - { - end = now; - } - else - { - end = endDate.getYear(); - } - - if ((current >= start) && (current <= end)) - { - count += 1; - } - } - } - + long count = services.countBy(Year.of(current)); chart.add(String.valueOf(current), count, ChartColor.YELLOW); current += 1; diff --git a/src/fr/devinsy/statoolinfos/htmlize/OrganizationHeaderView.java b/src/fr/devinsy/statoolinfos/htmlize/OrganizationHeaderView.java index e822e27..ced2944 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/OrganizationHeaderView.java +++ b/src/fr/devinsy/statoolinfos/htmlize/OrganizationHeaderView.java @@ -84,10 +84,10 @@ public class OrganizationHeaderView data.setContent("organizationMemberOfWord", ":"); } - data.setContent("organizationMemberStartDate", StringUtils.defaultIfBlank(organization.getMemberStartDate(), "n/a")); - data.setContent("organizationMemberEndDate", StringUtils.defaultIfBlank(organization.getMemberEndDate(), "n/a")); + data.setContent("organizationMemberStartDate", StringUtils.defaultIfBlank(organization.getMemberStartDateValue(), "n/a")); + data.setContent("organizationMemberEndDate", StringUtils.defaultIfBlank(organization.getMemberEndDateValue(), "n/a")); data.setContent("organizationMemberAge", StringUtils.defaultIfBlank(organization.getMemberAge(), "n/a")); - if (StringUtils.isBlank(organization.getMemberEndDate())) + if (StringUtils.isBlank(organization.getMemberEndDateValue())) { data.setAttribute("organizationMemberEndDateData", "style", "display: none;"); } diff --git a/src/fr/devinsy/statoolinfos/properties/PathPropertyList.java b/src/fr/devinsy/statoolinfos/properties/PathPropertyList.java index 7ba9079..12cda43 100644 --- a/src/fr/devinsy/statoolinfos/properties/PathPropertyList.java +++ b/src/fr/devinsy/statoolinfos/properties/PathPropertyList.java @@ -24,8 +24,6 @@ import java.net.URL; import java.time.LocalDate; import java.time.Year; import java.time.YearMonth; -import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.Iterator; import java.util.regex.Matcher; @@ -315,53 +313,7 @@ public class PathPropertyList extends ArrayList implements PathPro { LocalDate result; - String value = get(path); - if (value == null) - { - result = null; - } - else - { - String pattern; - if (value.matches("\\d{1,2}/\\d{1,2}/\\d{4}")) - { - pattern = "dd/MM/yyyy"; - } - else if (value.matches("\\d{4}-\\d{2}-\\d{2}")) - { - pattern = "yyyy-MM-dd"; - } - else if (value.matches("\\d{1,2}/\\d{4}")) - { - value = "01/" + value; - pattern = "dd/MM/yyyy"; - } - else if (value.matches("\\d{4}-\\d{2}")) - { - value = value + "-01"; - pattern = "yyyy-MM-dd"; - } - else - { - pattern = null; - } - - if (pattern == null) - { - result = null; - } - else - { - try - { - result = LocalDate.parse(value, DateTimeFormatter.ofPattern(pattern)); - } - catch (DateTimeParseException exception) - { - result = null; - } - } - } + result = StatoolInfosUtils.parseDate(get(path)); // return result;