/* * 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; import java.io.File; import java.net.URL; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.YearMonth; import org.apache.commons.codec.digest.DigestUtils; 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.htmlize.charts.MonthValues; import fr.devinsy.statoolinfos.properties.PathProperties; import fr.devinsy.statoolinfos.properties.PathPropertyList; import fr.devinsy.statoolinfos.uptime.URLSet; import fr.devinsy.statoolinfos.util.URLUtils; /** * The Class Service. */ public class Service extends PathPropertyList { private static final long serialVersionUID = 3629841771102288863L; private static Logger logger = LoggerFactory.getLogger(Service.class); public enum HostProviderType { HOME, HOSTEDBAY, HOSTEDSERVER, OUTSOURCED, UNKNOWN } public enum HostServerType { NANO, PHYSICAL, VIRTUAL, SHARED, CLOUD, UNKNOWN } public enum RegistrationType { NONE, FREE, MEMBER, CLIENT, UNKNOWN } public enum ServiceInstallType { DISTRIBUTION, PROVIDER, TOOLING, PACKAGE, CLONEREPO, ARCHIVE, SOURCES, CONTAINER, UNKNOWN } public enum Status { OK, WARNING, ALERT, ERROR, OVER, VOID } private Organization organization; private File inputFile; private URL inputURL; private String logoFileName; private MetricsList metricsList; private PropertyChecks inputChecks; private CrawlJournal crawlJournal; /** * Instantiates a new service. */ public Service() { this(null); } /** * Instantiates a new service. * * @param properties * the properties */ public Service(final PathProperties properties) { super(properties); this.inputChecks = new PropertyChecks(); this.crawlJournal = new CrawlJournal(); this.metricsList = new MetricsList(); } /** * Gets the age. * * @return the age */ public String getAge() { String result; LocalDate startDate = StatoolInfosUtils.parseDate(getStartDate()); LocalDate endDate = StatoolInfosUtils.parseDate(getEndDate()); result = StatoolInfosUtils.toHumanDuration(startDate, endDate); // return result; } /** * Gets the contact email. * * @return the contact email */ public String getContactEmail() { String result; result = get("service.contact.email"); // return result; } /** * Gets the contact website. * * @return the contact website */ public URL getContactURL() { URL result; result = getURL("service.contact.url"); // return result; } /** * Gets the country code. * * @return the country code */ public String getCountryCode() { String result; result = StringUtils.toRootUpperCase(get("host.country.code")); // return result; } /** * Gets the crawl date. * * @return the crawl date */ public LocalDateTime getCrawlDate() { LocalDateTime result; result = LocalDateTime.parse(get("crawl.datetime")); // return result; } /** * Gets the crawled date. * * @return the crawled date */ public LocalDateTime getCrawledDate() { LocalDateTime result; result = LocalDateTime.parse(get("crawl.file.datetime")); if (result.getYear() == 1970) { result = getCrawlDate(); } // return result; } public CrawlJournal getCrawlJournal() { return this.crawlJournal; } /** * Gets the description. * * @return the description */ public String getDescription() { String result; result = get("service.description"); // return result; } /** * Gets the end date. * * @return the end date */ public String getEndDate() { String result; result = get("service.enddate"); // 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. * * @return the host provider type */ public HostProviderType getHostProviderType() { HostProviderType result; try { String value = StringUtils.toRootUpperCase(get("host.provider.type")); result = HostProviderType.valueOf(value); } catch (IllegalArgumentException | NullPointerException exception) { result = HostProviderType.UNKNOWN; } // return result; } /** * Gets the host server type. * * @return the host server type */ public HostServerType getHostServerType() { HostServerType result; try { String value = StringUtils.toRootUpperCase(get("host.server.type")); result = HostServerType.valueOf(value); } catch (IllegalArgumentException | NullPointerException exception) { result = HostServerType.UNKNOWN; } // return result; } public PropertyChecks getInputChecks() { return this.inputChecks; } /** * Gets the input checks all. * * @return the input checks all */ public PropertyChecks getInputChecksAll() { PropertyChecks result; result = new PropertyChecks(); result.addAll(this.inputChecks); for (Metrics metrics : getMetrics()) { result.addAll(metrics.getInputChecks()); } // return result; } public File getInputFile() { return this.inputFile; } public URL getInputURL() { return this.inputURL; } /** * Gets the legal website. * * @return the legal website */ public URL getLegalURL() { URL result; String value = get("service.legal.url", "service.legal"); result = getURL(value); // return result; } /** * Gets the license name. * * @return the license name */ public String getLicenseName() { String result; result = get("software.license.name"); // return result; } /** * Gets the local name. * * @return the local name */ public String getLocalFileBaseName() { String result; result = this.organization.getTechnicalName() + "-" + getTechnicalName(); // return result; } /** * Gets the local name. * * @return the local name */ public String getLocalFileName() { String result; result = getLocalFileBaseName() + ".properties"; // return result; } public String getLogoFileName() { return this.logoFileName; } /** * Gets the logo URL. * * @return the logo URL */ public URL getLogoURL() { URL result; result = getURL("service.logo"); // return result; } public MetricsList getMetrics() { return this.metricsList; } /** * Gets the name. * * @return the name */ public String getName() { String result; result = get("service.name"); if (StringUtils.isBlank(result)) { String seed = get("crawl.url"); result = DigestUtils.md5Hex(seed).substring(0, 8); } // return result; } /** * Gets the organization. * * @return the organization */ public Organization getOrganization() { return this.organization; } /** * Gets the previous month user count. * * @return the previous month user count */ public long getPreviousMonthUserCount() { long result; MonthValues values = getMetricMonthValues("metrics.users.count"); values = values.extract(YearMonth.now().minusMonths(1), YearMonth.now().minusMonths(1)); result = (long) values.sum(); // return result; } /** * Gets the previous month visit count. * * @return the previous month visit count */ public long getPreviousMonthVisitCount() { long result; MonthValues values = getMetricMonthValues("metrics.http.visits.visitors"); values = values.extract(YearMonth.now().minusMonths(1), YearMonth.now().minusMonths(1)); result = (long) values.sum(); // return result; } /** * Gets the registration load. * * @return the registration load */ public String getRegistrationLoad() { String result; result = get("service.registration.load"); if (result == null) { result = "open"; } // return result; } /** * Gets the service install type. * * @return the service install type */ public ServiceInstallType getServiceInstallType() { ServiceInstallType result; try { String value = StringUtils.toRootUpperCase(get("service.install.type")); result = ServiceInstallType.valueOf(value); } catch (IllegalArgumentException | NullPointerException exception) { result = ServiceInstallType.UNKNOWN; } // return result; } /** * Gets the software description. * * @return the software description */ public String getSoftwareDescription() { String result; result = get("software.description"); // return result; } /** * Gets the software license name. * * @return the software license name */ public String getSoftwareLicenseName() { String result; result = get("software.license.name"); // return result; } /** * Gets the software license webpage. * * @return the software license webpage */ public URL getSoftwareLicenseURL() { URL result; result = getURL("software.license.url"); // return result; } /** * Gets the software name. * * @return the software name */ public String getSoftwareName() { String result; result = get("software.name"); // return result; } /** * Gets the software source URL. * * @return the software source URL */ public URL getSoftwareSourceURL() { URL result; result = getURL("software.source.url"); // return result; } /** * Gets the software technical name. * * @return the software technical name */ public String getSoftwareTechnicalName() { String result; result = StatoolInfosUtils.toTechnicalName(getSoftwareName()); // return result; } /** * Gets the software version. * * @return the software version */ public String getSoftwareVersion() { String result; result = get("software.version"); // return result; } /** * Gets the software website. * * @return the software website */ public URL getSoftwareWebsite() { URL result; result = getURL("software.website"); // return result; } /** * Gets the start date. * * @return the start date */ public String getStartDate() { String result; result = get("service.startdate"); // return result; } /** * Gets the status. * * @return the status */ public Status getStatus() { Status result; String value = get("service.status", "service.status.level"); if ((StringUtils.isBlank(value)) || (StringUtils.equalsAnyIgnoreCase(value, "unknown", "void"))) { result = Status.VOID; } else if (StringUtils.equalsAnyIgnoreCase(value, "ON", "OK")) { result = Status.OK; } else if (StringUtils.equalsAnyIgnoreCase(value, "alert")) { result = Status.ALERT; } else if (StringUtils.equalsAnyIgnoreCase(value, "error", "ko", "broken", "off")) { result = Status.ERROR; } else if (StringUtils.equalsAnyIgnoreCase(value, "over", "terminated", "closed", "ended")) { result = Status.OVER; } else { result = Status.VOID; } // return result; } /** * Gets the status description. * * @return the status description */ public String getStatusDescription() { String result; result = get("service.status.description"); // return result; } /** * @return */ public URL getTechnicalGuideURL() { URL result; String value = get("service.guide.technical", "service.guide.technical.url"); result = URLUtils.of(value); // return result; } /** * Gets the technical name. * * @return the technical name */ public String getTechnicalName() { String result; result = StatoolInfosUtils.toTechnicalName(getName()); // return result; } public URLSet getURLAll() { URLSet result; result = new URLSet(); result.add(getContactURL()); result.add(getLegalURL()); result.add(getLogoURL()); result.add(getSoftwareWebsite()); result.add(getSoftwareLicenseURL()); result.add(getSoftwareSourceURL()); result.add(getTechnicalGuideURL()); result.add(getUserGuideURL()); result.add(getWebsiteURL()); // return result; } /** * User count. * * @return the int */ public int getUserCount() { int result; result = 0; // return result; } /** * Gets the user doc. * * @return the user doc */ public URL getUserGuideURL() { URL result; String value = get("service.documentation", "service.documentation.url", "service.documentation.user", "service.documentation.user.url", "service.documentation.tutorial", "service.documentation.tutorial.url", "service.guide.user", "service.guide.user.url"); result = URLUtils.of(value); // return result; } /** * Gets the website. * * @return the website */ public URL getWebsiteURL() { URL result; result = getURL("service.website"); // return result; } /** * Checks if is registration client. * * @return true, if is registration client */ public boolean isRegistrationClient() { boolean result; result = StringUtils.containsIgnoreCase(get("service.registration"), "Client"); // return result; } /** * Checks if is registration free. * * @return true, if is registration free */ public boolean isRegistrationFree() { boolean result; result = StringUtils.containsIgnoreCase(get("service.registration"), "Free"); // return result; } /** * Checks if is registration load full. * * @return true, if is registration load full */ public boolean isRegistrationLoadFull() { boolean result; result = StringUtils.containsIgnoreCase(get("service.registration.load"), "full"); // return result; } /** * Checks if is registration load open. * * @return true, if is registration load open */ public boolean isRegistrationLoadOpen() { boolean result; result = StringUtils.containsIgnoreCase(get("service.registration.load"), "open"); // return result; } /** * Checks if is registration member. * * @return true, if is registration member */ public boolean isRegistrationMember() { boolean result; result = StringUtils.containsIgnoreCase(get("service.registration"), "Member"); // return result; } /** * Checks if is registration none. * * @return true, if is registration none */ public boolean isRegistrationNone() { boolean result; result = StringUtils.containsIgnoreCase(get("service.registration"), "None"); // return result; } /** * Checks if is registration unknown. * * @return true, if is registration unknown */ public boolean isRegistrationUnknown() { boolean result; if (isRegistrationNone() || isRegistrationFree() || isRegistrationMember() || isRegistrationClient()) { result = false; } else { result = true; } // return result; } public void setInputFile(final File inputFile) { this.inputFile = inputFile; } public void setInputURL(final URL inputURL) { this.inputURL = inputURL; } public void setLogoFileName(final String logoFileName) { this.logoFileName = logoFileName; } public void setOrganization(final Organization organization) { this.organization = organization; } }