From 62390fd4a04d20ba2f012f8c79220da30d86ca28 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sun, 21 Feb 2021 00:51:58 +0100 Subject: [PATCH] Added host chart. --- src/fr/devinsy/statoolinfos/core/Service.java | 15 ++ .../statoolinfos/core/ServiceComparator.java | 17 +- .../htmlize/FederationStatsPage.java | 2 + .../statoolinfos/htmlize/Htmlizer.java | 82 ++++++++- .../htmlize/federationStats.xhtml | 5 +- .../statoolinfos/metrics/StringCounter.java | 73 ++++++++ .../metrics/StringCounterComparator.java | 158 ++++++++++++++++++ .../metrics/StringCounterList.java | 104 ++++++++++++ .../statoolinfos/metrics/StringCounters.java | 122 ++++++++++++++ 9 files changed, 575 insertions(+), 3 deletions(-) create mode 100644 src/fr/devinsy/statoolinfos/metrics/StringCounter.java create mode 100644 src/fr/devinsy/statoolinfos/metrics/StringCounterComparator.java create mode 100644 src/fr/devinsy/statoolinfos/metrics/StringCounterList.java create mode 100644 src/fr/devinsy/statoolinfos/metrics/StringCounters.java diff --git a/src/fr/devinsy/statoolinfos/core/Service.java b/src/fr/devinsy/statoolinfos/core/Service.java index fda82d0..4cfd9bb 100644 --- a/src/fr/devinsy/statoolinfos/core/Service.java +++ b/src/fr/devinsy/statoolinfos/core/Service.java @@ -211,6 +211,21 @@ public class Service extends PathPropertyList return result; } + /** + * Gets the host name. + * + * @return the host name + */ + public String getHostName() + { + String result; + + result = get("host.name"); + + // + return result; + } + /** * Gets the host provider type. * diff --git a/src/fr/devinsy/statoolinfos/core/ServiceComparator.java b/src/fr/devinsy/statoolinfos/core/ServiceComparator.java index b297dd6..688fb50 100644 --- a/src/fr/devinsy/statoolinfos/core/ServiceComparator.java +++ b/src/fr/devinsy/statoolinfos/core/ServiceComparator.java @@ -1,5 +1,20 @@ /* + * Copyright (C) 2020-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.core; @@ -8,7 +23,7 @@ import java.util.Comparator; import fr.devinsy.statoolinfos.util.CompareUtils; /** - * The Class CategoryStatComparator. + * The Class ServiceComparator. */ public class ServiceComparator implements Comparator { diff --git a/src/fr/devinsy/statoolinfos/htmlize/FederationStatsPage.java b/src/fr/devinsy/statoolinfos/htmlize/FederationStatsPage.java index 787d910..6d0d32b 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/FederationStatsPage.java +++ b/src/fr/devinsy/statoolinfos/htmlize/FederationStatsPage.java @@ -118,6 +118,8 @@ public class FederationStatsPage data.setContent("categoryDistributionChart", Htmlizer.htmlizeCategoryDistributionChart()); data.setContent("categoryDistributionPieChart", Htmlizer.htmlizeCatergoryDistributionPieChart(federation.getAllServices())); + data.setContent("hostNameChart", Htmlizer.htmlizeHostNamePieChart(federation.getAllServices())); + // String content = PresenterUtils.dynamize("/fr/devinsy/statoolinfos/htmlize/federationStats.xhtml", data).toString(); diff --git a/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java b/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java index 3dba3e5..9b4a80a 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java +++ b/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java @@ -47,6 +47,9 @@ import fr.devinsy.statoolinfos.htmlize.charts.DoughnutChartView; import fr.devinsy.statoolinfos.htmlize.charts.PieChart; import fr.devinsy.statoolinfos.htmlize.charts.PieChart.Position; import fr.devinsy.statoolinfos.htmlize.charts.PieChartView; +import fr.devinsy.statoolinfos.metrics.StringCounter; +import fr.devinsy.statoolinfos.metrics.StringCounterList; +import fr.devinsy.statoolinfos.metrics.StringCounters; import fr.devinsy.statoolinfos.properties.PathProperty; import fr.devinsy.statoolinfos.properties.PathPropertyList; import fr.devinsy.statoolinfos.stats.StatAgent; @@ -292,6 +295,83 @@ public class Htmlizer return result; } + /** + * Htmlize host name pie chart. + * + * @param services + * the services + * @return the string + * @throws StatoolInfosException + * the statool infos exception + */ + public static String htmlizeHostNamePieChart(final Services services) throws StatoolInfosException + { + String result; + + ChartColors colors = ChartColor.valueList(); + colors.remove(ChartColor.BLUE); + + StringCounters counters = new StringCounters(); + long unknowns = 0; + for (Service service : services) + { + if (service.getHostProviderType() == null) + { + unknowns += 1; + } + else + { + switch (service.getHostProviderType()) + { + case HOME: + counters.inc("Auto-hébergé"); + break; + + case HOSTEDBAY: + case HOSTEDSERVER: + case OUTSOURCED: + counters.inc(service.getHostName()); + break; + + case UNKNOWN: + unknowns += 1; + default: + } + } + } + + StringCounterList list = counters.toList().sortByCounter().reverse(); + + PieChart pie = new PieChart("Hébergeurs des services"); + pie.setLegendPosition(Position.RIGHT); + + int index = 0; + while ((index < list.size() && (index < 9))) + { + ChartColor color = colors.get(index); + StringCounter counter = list.get(index); + pie.add(counter.getString(), counter.getCounter(), color); + + index += 1; + } + + int others = 0; + while (index < list.size()) + { + StringCounter counter = list.get(index); + others += counter.getCounter(); + + index += 1; + } + pie.add("Autres", others, ChartColor.GREY); + pie.add("Inconnus", unknowns, ChartColor.BLUE); + + result = DoughnutChartView.build(pie); + + // + return result; + } + /** * Htmlize host provider type chart. * @@ -656,7 +736,7 @@ public class Htmlizer * the services * @return the string * @throws StatoolInfosException - * the statool infos exception + * )* the statool infos exception */ public static String htmlizeServiceCountryChart(final Services services) throws StatoolInfosException { diff --git a/src/fr/devinsy/statoolinfos/htmlize/federationStats.xhtml b/src/fr/devinsy/statoolinfos/htmlize/federationStats.xhtml index b1bc8dd..c757a77 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/federationStats.xhtml +++ b/src/fr/devinsy/statoolinfos/htmlize/federationStats.xhtml @@ -24,9 +24,12 @@
-
+
+
+
+
diff --git a/src/fr/devinsy/statoolinfos/metrics/StringCounter.java b/src/fr/devinsy/statoolinfos/metrics/StringCounter.java new file mode 100644 index 0000000..5b20caa --- /dev/null +++ b/src/fr/devinsy/statoolinfos/metrics/StringCounter.java @@ -0,0 +1,73 @@ +/* + * 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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class StringCounter. + */ +public class StringCounter +{ + private static Logger logger = LoggerFactory.getLogger(StringCounter.class); + + private String string; + private long counter; + + /** + * Instantiates a new string counter. + * + * @param string + * the value + */ + public StringCounter(final String string) + { + this.string = string; + this.counter = 0; + } + + public long getCounter() + { + return this.counter; + } + + public String getString() + { + return this.string; + } + + /** + * Inc. + */ + public void inc() + { + this.counter += 1; + } + + public void setCounter(final long counter) + { + this.counter = counter; + } + + public void setString(final String string) + { + this.string = string; + } +} diff --git a/src/fr/devinsy/statoolinfos/metrics/StringCounterComparator.java b/src/fr/devinsy/statoolinfos/metrics/StringCounterComparator.java new file mode 100644 index 0000000..f36081d --- /dev/null +++ b/src/fr/devinsy/statoolinfos/metrics/StringCounterComparator.java @@ -0,0 +1,158 @@ +/* + * 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.Comparator; + +import fr.devinsy.statoolinfos.util.CompareUtils; + +/** + * The Class StringCounterComparator. + */ +public class StringCounterComparator implements Comparator +{ + public enum Sorting + { + STRING, + COUNTER + } + + private Sorting sorting; + + /** + * Instantiates a new string counter comparator. + * + * @param sorting + * the sorting + */ + public StringCounterComparator(final Sorting sorting) + { + this.sorting = sorting; + } + + /** + * Compare. + * + * @param alpha + * the alpha + * @param bravo + * the bravo + * @return the int + */ + @Override + public int compare(final StringCounter alpha, final StringCounter bravo) + { + int result; + + result = compare(alpha, bravo, this.sorting); + + // + return result; + } + + /** + * Compare. + * + * @param alpha + * the alpha + * @param bravo + * the bravo + * @param sorting + * the sorting + * @return the int + */ + public static int compare(final StringCounter alpha, final StringCounter bravo, final Sorting sorting) + { + int result; + + if (sorting == null) + { + result = 0; + } + else + { + switch (sorting) + { + default: + case STRING: + result = CompareUtils.compareIgnoreCase(getString(alpha), getString(bravo)); + break; + + case COUNTER: + result = CompareUtils.compare(getCounter(alpha), getCounter(bravo)); + if (result == 0) + { + result = CompareUtils.compareIgnoreCase(getString(alpha), getString(bravo)); + } + break; + } + } + + // + return result; + } + + /** + * Gets the counter. + * + * @param source + * the source + * @return the counter + */ + public static Long getCounter(final StringCounter source) + { + Long result; + + if (source == null) + { + result = null; + } + else + { + result = (long) source.getCounter(); + } + + // + return result; + } + + /** + * Gets the string. + * + * @param source + * the source + * @return the string + */ + public static String getString(final StringCounter source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = source.getString(); + } + + // + return result; + } +} diff --git a/src/fr/devinsy/statoolinfos/metrics/StringCounterList.java b/src/fr/devinsy/statoolinfos/metrics/StringCounterList.java new file mode 100644 index 0000000..be0faf0 --- /dev/null +++ b/src/fr/devinsy/statoolinfos/metrics/StringCounterList.java @@ -0,0 +1,104 @@ +/* + * 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.ArrayList; +import java.util.Collections; + +/** + * The Class StringCounterList. + */ +public class StringCounterList extends ArrayList +{ + private static final long serialVersionUID = 6392647621400816570L; + + /** + * Instantiates a new string counter list. + */ + public StringCounterList() + { + super(); + } + + /** + * Reverse. + * + * @return the categories + */ + public StringCounterList reverse() + { + StringCounterList result; + + Collections.reverse(this); + + result = this; + + // + return result; + } + + /** + * Sort. + * + * @param sorting + * the sorting + * @return the string counter list + */ + public StringCounterList sort(final StringCounterComparator.Sorting sorting) + { + StringCounterList result; + + sort(new StringCounterComparator(sorting)); + + result = this; + + // + return result; + } + + /** + * Sort by counter. + * + * @return the string counter list + */ + public StringCounterList sortByCounter() + { + StringCounterList result; + + result = sort(StringCounterComparator.Sorting.COUNTER); + + // + return result; + } + + /** + * Sort by name. + * + * @return the services + */ + public StringCounterList sortByString() + { + StringCounterList result; + + result = sort(StringCounterComparator.Sorting.STRING); + + // + return result; + } +} diff --git a/src/fr/devinsy/statoolinfos/metrics/StringCounters.java b/src/fr/devinsy/statoolinfos/metrics/StringCounters.java new file mode 100644 index 0000000..511ddcb --- /dev/null +++ b/src/fr/devinsy/statoolinfos/metrics/StringCounters.java @@ -0,0 +1,122 @@ +/* + * 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; + +import org.apache.commons.lang3.StringUtils; + +/** + * The Class StringCounters. + */ +public class StringCounters extends HashMap +{ + private static final long serialVersionUID = -4771969211702752306L; + + /** + * Instantiates a new string counters. + */ + public StringCounters() + { + super(); + } + + /** + * Compute key. + * + * @param string + * the string + * @return the string + */ + public String computeKey(final String string) + { + String result; + + result = StringUtils.toRootLowerCase(string); + + // + return result; + } + + /** + * Gets the. + * + * @param string + * the string + * @return the long + */ + public long get(final String string) + { + long result; + + String key = computeKey(string); + + StringCounter counter = super.get(key); + + if (counter == null) + { + result = 0L; + } + else + { + result = counter.getCounter(); + } + + // + return result; + } + + /** + * Inc. + * + * @param string + * the key + */ + public void inc(final String string) + { + String key = computeKey(string); + + StringCounter counter = super.get(key); + + if (counter == null) + { + counter = new StringCounter(string); + put(key, counter); + } + + counter.inc(); + } + + /** + * To list. + * + * @return the string counter list + */ + public StringCounterList toList() + { + StringCounterList result; + + result = new StringCounterList(); + + result.addAll(values()); + + // + return result; + } +}