Added CSV export.

This commit is contained in:
Christian P. MOMON 2021-01-23 20:06:42 +01:00
parent adc7146644
commit 9e53a184e6
7 changed files with 523 additions and 163 deletions

View file

@ -22,6 +22,8 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import fr.devinsy.strings.StringList;
/** /**
* The Class Categories. * The Class Categories.
*/ */
@ -149,4 +151,31 @@ public class Categories extends ArrayList<Category>
// //
return result; return result;
} }
/**
* To string.
*
* @return the string
*/
@Override
public String toString()
{
String result;
StringList buffer = new StringList();
for (Category category : this)
{
buffer.append(category.getName()).append(",");
}
if (!buffer.isEmpty())
{
buffer.removeLast();
}
result = buffer.toString();
//
return result;
}
} }

View file

@ -19,22 +19,20 @@
package fr.devinsy.statoolinfos.htmlize; package fr.devinsy.statoolinfos.htmlize;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.HtmlizerContext; import fr.devinsy.statoolinfos.HtmlizerContext;
import fr.devinsy.statoolinfos.core.Federation; import fr.devinsy.statoolinfos.core.Federation;
import fr.devinsy.statoolinfos.core.Organization;
import fr.devinsy.statoolinfos.core.Service;
import fr.devinsy.statoolinfos.core.StatoolInfosException; import fr.devinsy.statoolinfos.core.StatoolInfosException;
import fr.devinsy.statoolinfos.properties.PathPropertyUtils; import fr.devinsy.statoolinfos.io.CSVFile;
import fr.devinsy.strings.StringList; import fr.devinsy.statoolinfos.io.JSONFile;
import fr.devinsy.strings.StringsUtils;
import fr.devinsy.xidyn.XidynException; import fr.devinsy.xidyn.XidynException;
import fr.devinsy.xidyn.presenters.PresenterUtils; import fr.devinsy.xidyn.presenters.PresenterUtils;
@ -53,162 +51,53 @@ public class ExportsPage
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public static void build() throws StatoolInfosException, IOException public static void build()
{ {
Federation federation = HtmlizerContext.instance().getFederation();
File htmlizeDirectory = HtmlizerContext.instance().getHtmlizeDirectory(); File htmlizeDirectory = HtmlizerContext.instance().getHtmlizeDirectory();
// //
logger.info("EXPORTS JSON."); try
ExportsPage.buildFederationExport(); {
ExportsPage.buildOrganizationsExport(); logger.info("EXPORTS CSV.");
ExportsPage.buildServicesExport(); CSVFile.save(new File(htmlizeDirectory, "organizations.csv"), federation.getOrganizations());
CSVFile.save(new File(htmlizeDirectory, "services.csv"), federation.getAllServices());
}
catch (UnsupportedEncodingException | FileNotFoundException exception)
{
logger.error("Error during CSV export: " + exception.getMessage(), exception);
}
// //
try
{
logger.info("EXPORTS JSON.");
JSONFile.save(new File(htmlizeDirectory, "federation.json"), federation);
JSONFile.save(new File(htmlizeDirectory, "organizations.json"), federation.getOrganizations());
JSONFile.save(new File(htmlizeDirectory, "services.json"), federation.getAllServices());
}
catch (UnsupportedEncodingException | FileNotFoundException exception)
{
logger.error("Error during JSON export: " + exception.getMessage(), exception);
}
//
logger.info("EXPORTS ODS.");
// ODSFile.save(new File(htmlizeDirectory, "organizations.ods"),
// federation.getOrganizations());
// ODSFile.save(new File(htmlizeDirectory, "services.ods"),
// federation.getAllServices());
//
try
{
String page = htmlize(); String page = htmlize();
FileUtils.write(new File(htmlizeDirectory, "exports.xhtml"), page, StandardCharsets.UTF_8); FileUtils.write(new File(htmlizeDirectory, "exports.xhtml"), page, StandardCharsets.UTF_8);
} }
catch (StatoolInfosException | IOException exception)
/**
* Builds the federation export.
*
* @throws StatoolInfosException
* the statool infos exception
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void buildFederationExport() throws StatoolInfosException, IOException
{ {
Federation federation = HtmlizerContext.instance().getFederation(); logger.error("Error building export page: " + exception.getMessage(), exception);
File htmlizeDirectory = HtmlizerContext.instance().getHtmlizeDirectory();
StringList lines = new StringList();
lines.appendln("{ \"federation\" : ");
lines.addAll(PathPropertyUtils.toJSON(federation));
lines.removeLast();
lines.appendln(",");
lines.appendln("\"organizations\" : [");
for (Organization organization : federation.getOrganizations())
{
lines.addAll(PathPropertyUtils.toJSON(organization));
lines.removeLast();
lines.appendln(",");
lines.append("\"services\" : [");
for (Service service : organization.getServices())
{
lines.addAll(PathPropertyUtils.toJSON(service));
lines.appendln(",");
} }
if (!organization.getServices().isEmpty())
{
lines.removeLast(2);
}
lines.appendln();
lines.appendln("]");
lines.append("}");
lines.appendln(",");
}
if (!federation.getOrganizations().isEmpty())
{
lines.removeLast(2);
lines.appendln();
}
lines.appendln("]");
lines.appendln("}");
lines.appendln("}");
logger.info("Htmlize federation JSON Export pages.");
StringsUtils.writeToFile(new File(htmlizeDirectory, "federation.json"), lines);
}
/**
* Builds the organizations export.
*
* @throws StatoolInfosException
* the statool infos exception
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void buildOrganizationsExport() throws StatoolInfosException, IOException
{
Federation federation = HtmlizerContext.instance().getFederation();
File htmlizeDirectory = HtmlizerContext.instance().getHtmlizeDirectory();
StringList lines = new StringList();
lines.appendln("{ \"organizations\" : [");
for (Organization organization : federation.getOrganizations())
{
lines.addAll(PathPropertyUtils.toJSON(organization));
lines.append(",");
}
if (!federation.getOrganizations().isEmpty())
{
lines.removeLast();
}
lines.appendln("] }");
logger.info("Htmlize organizations JSON Export pages.");
StringsUtils.writeToFile(new File(htmlizeDirectory, "organizations.json"), lines);
}
/**
* Builds the services export.
*
* @throws StatoolInfosException
* the statool infos exception
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void buildServicesExport() throws StatoolInfosException, IOException
{
Federation federation = HtmlizerContext.instance().getFederation();
File htmlizeDirectory = HtmlizerContext.instance().getHtmlizeDirectory();
StringList lines = new StringList();
lines.appendln("{ \"services\" : [");
for (Service service : federation.getAllServices())
{
lines.addAll(PathPropertyUtils.toJSON(service));
lines.append(",");
}
if (!federation.getAllServices().isEmpty())
{
lines.removeLast();
}
lines.appendln("] }");
logger.info("Htmlize services JSON Export pages.");
StringsUtils.writeToFile(new File(htmlizeDirectory, "services.json"), lines);
}
/**
* Escape.
*
* @param source
* the source
* @return the string
*/
public static String escapeJSON(final String source)
{
String result;
result = StringEscapeUtils.escapeJson(source);
//
return result;
} }
/** /**

View file

@ -180,22 +180,22 @@ public class Htmlizer
copyCategoriesStuff(HtmlizerContext.instance().getConfiguration().get("conf.htmlize.categories.icons"), htmlizeDirectory); copyCategoriesStuff(HtmlizerContext.instance().getConfiguration().get("conf.htmlize.categories.icons"), htmlizeDirectory);
AboutPage.build(); AboutPage.build();
CategoriesPage.build();
CategoryPage.buildAll();
EditoPage.build(); EditoPage.build();
ExportsPage.build();
FederationPage.build(); FederationPage.build();
FederationStatsPage.build(); FederationStatsPage.build();
OrganizationPage.buildAll(); OrganizationPage.buildAll();
OrganizationStatsPage.buildAll(); OrganizationStatsPage.buildAll();
ServicePage.buildAll();
ServicesPage.build();
PropertyFileCheckPage.buildAll(); PropertyFileCheckPage.buildAll();
PropertiesFilesPage.build(); PropertiesFilesPage.build();
PropertyStatsPage.buildAll(); PropertyStatsPage.buildAll();
CategoriesPage.build(); ServicePage.buildAll();
CategoryPage.buildAll(); ServicesPage.build();
SoftwaresPage.build(); SoftwaresPage.build();
SoftwarePage.buildAll(); SoftwarePage.buildAll();
SocialNetworksPage.buildAll(); SocialNetworksPage.buildAll();
ExportsPage.build();
} }
/** /**

View file

@ -24,16 +24,16 @@
<hr/> <hr/>
<div class="row exportsView"> <div class="row exportsView">
<div class="column">Les organisations :</div> <div class="column">Les organisations :</div>
<div class="column"><a href="#"><img src="csv-icon.svg" title="CSV" style="opacity: 0.2"/></a></div> <div class="column"><a href="organizations.csv"><img src="csv-icon.svg" title="CSV"/></a></div>
<div class="column"><a href="organizations.json"><img src="json-icon.svg" title="JSON"/></a></div> <div class="column"><a href="organizations.json"><img src="json-icon.svg" title="JSON"/></a></div>
<div class="column"><a href="#"><img src="ods-icon.svg" title="ODS" style="opacity: 0.2"/></a></div> <div class="column"><a href="organizations.ods"><img src="ods-icon.svg" title="ODS"/></a></div>
</div> </div>
<hr/> <hr/>
<div class="row exportsView"> <div class="row exportsView">
<div class="column">Les services :</div> <div class="column">Les services :</div>
<div class="column"><a href="#"><img src="csv-icon.svg" title="CSV" style="opacity: 0.2"/></a></div> <div class="column"><a href="services.csv"><img src="csv-icon.svg" title="CSV"/></a></div>
<div class="column"><a href="services.json"><img src="json-icon.svg" title="JSON"/></a></div> <div class="column"><a href="services.json"><img src="json-icon.svg" title="JSON"/></a></div>
<div class="column"><a href="#"><img src="ods-icon.svg" title="ODS" style="opacity: 0.2"/></a></div> <div class="column"><a href="services.ods"><img src="ods-icon.svg" title="ODS"/></a></div>
</div> </div>
</div> </div>
</body> </body>

View file

@ -0,0 +1,250 @@
/*
* 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.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.HtmlizerContext;
import fr.devinsy.statoolinfos.core.Categories;
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.properties.PathProperty;
import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringSet;
/**
* The Class CSVFile.
*/
public class CSVFile
{
private static final Logger logger = LoggerFactory.getLogger(CSVFile.class);
public static final String DEFAULT_CHARSET_NAME = "UTF-8";
public static final int MAX_LINE_SIZE = 4096;
public static final String SEPARATOR = ";";
/**
* Escape.
*
* @param string
* the string
* @return the string
*/
public static String escape(final String string)
{
String result;
result = StringEscapeUtils.escapeCsv(string);
//
return result;
}
/**
* Save.
*
* @param file
* the file
* @param source
* the source
* @throws UnsupportedEncodingException
* the unsupported encoding exception
* @throws FileNotFoundException
* the file not found exception
*/
public static void save(final File file, final Organizations source) throws UnsupportedEncodingException, FileNotFoundException
{
PrintWriter out = null;
try
{
out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
write(out, source);
}
finally
{
IOUtils.closeQuietly(out);
}
}
/**
* Save.
*
* @param file
* the file
* @param source
* the source
* @throws UnsupportedEncodingException
* the unsupported encoding exception
* @throws FileNotFoundException
* the file not found exception
*/
public static void save(final File file, final Services source) throws UnsupportedEncodingException, FileNotFoundException
{
PrintWriter out = null;
try
{
out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
write(out, source);
}
finally
{
IOUtils.closeQuietly(out);
}
}
/**
* Write.
*
* @param out
* the out
* @param source
* the source
*/
public static void write(final PrintWriter out, final Organizations organizations)
{
// Build label list.
StringList labels = new StringList(100);
StringSet dejavu = new StringSet(100);
StringList prefixes = new StringList("file", "organization", "crawl");
for (String prefix : prefixes)
{
for (Organization organization : organizations)
{
for (PathProperty property : organization.getByPrefix(prefix))
{
if (!dejavu.contains(property.getPath()))
{
labels.add(property.getPath());
dejavu.add(property.getPath());
}
}
}
}
dejavu.clear();
// Write label line.
StringList line = new StringList();
for (String label : labels)
{
line.append(escape(label)).append(SEPARATOR);
}
line.removeLast();
out.println(line.toString());
// Write organization lines.
for (Organization organization : organizations)
{
line = new StringList(100);
for (String label : labels)
{
String value = organization.get(label);
line.append(escape(value)).append(SEPARATOR);
}
line.removeLast();
out.println(line.toString());
}
}
/**
* Write.
*
* @param out
* the out
* @param source
* the source
*/
public static void write(final PrintWriter out, final Services services)
{
// Build label list.
StringList labels = new StringList(100);
StringSet dejavu = new StringSet(100);
StringList prefixes = new StringList("file", "service", "host", "software", "crawl");
for (String prefix : prefixes)
{
for (Service service : services)
{
for (PathProperty property : service.getByPrefix(prefix))
{
if (!dejavu.contains(property.getPath()))
{
labels.add(property.getPath());
dejavu.add(property.getPath());
}
}
}
}
dejavu.clear();
int index = labels.indexOf("service.name");
labels.add(index, "organization.name");
index = labels.indexOf("software.name");
labels.add(index, "software.categories");
// Write label line.
StringList line = new StringList();
for (String label : labels)
{
line.append(escape(label)).append(SEPARATOR);
}
line.removeLast();
out.println(line.toString());
Categories categories = HtmlizerContext.instance().getCategories();
// Write service lines.
for (Service service : services)
{
line = new StringList(100);
for (String label : labels)
{
String value;
if (StringUtils.equals(label, "organization.name"))
{
value = service.getOrganization().getName();
}
else if (StringUtils.equals(label, "software.categories"))
{
value = categories.findBySoftware(service.getSoftwareName()).toString();
}
else
{
value = service.get(label);
}
line.append(escape(value)).append(SEPARATOR);
}
line.removeLast();
out.println(line.toString());
}
}
}

View file

@ -0,0 +1,192 @@
/*
* 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.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.properties.PathPropertyUtils;
import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringsUtils;
/**
* The Class JSONFile.
*/
public class JSONFile
{
private static final Logger logger = LoggerFactory.getLogger(JSONFile.class);
public static final String DEFAULT_CHARSET_NAME = "UTF-8";
/**
* Escape.
*
* @param source
* the source
* @return the string
*/
public static String escapeJSON(final String source)
{
String result;
result = StringEscapeUtils.escapeJson(source);
//
return result;
}
/**
* Save.
*
* @param file
* the file
* @param federation
* the federation
* @throws UnsupportedEncodingException
* the unsupported encoding exception
* @throws FileNotFoundException
* the file not found exception
*/
public static void save(final File file, final Federation federation) throws UnsupportedEncodingException, FileNotFoundException
{
StringList lines = new StringList();
lines.appendln("{ \"federation\" : ");
lines.addAll(PathPropertyUtils.toJSON(federation));
lines.removeLast();
lines.appendln(",");
lines.appendln("\"organizations\" : [");
for (Organization organization : federation.getOrganizations())
{
lines.addAll(PathPropertyUtils.toJSON(organization));
lines.removeLast();
lines.appendln(",");
lines.append("\"services\" : [");
for (Service service : organization.getServices())
{
lines.addAll(PathPropertyUtils.toJSON(service));
lines.appendln(",");
}
if (!organization.getServices().isEmpty())
{
lines.removeLast(2);
}
lines.appendln();
lines.appendln("]");
lines.append("}");
lines.appendln(",");
}
if (!federation.getOrganizations().isEmpty())
{
lines.removeLast(2);
lines.appendln();
}
lines.appendln("]");
lines.appendln("}");
lines.appendln("}");
logger.info("Htmlize federation JSON Export pages.");
StringsUtils.writeToFile(file, lines);
}
/**
* Save.
*
* @param file
* the file
* @param organizations
* the organizations
* @throws UnsupportedEncodingException
* the unsupported encoding exception
* @throws FileNotFoundException
* the file not found exception
*/
public static void save(final File file, final Organizations organizations) throws UnsupportedEncodingException, FileNotFoundException
{
StringList lines = new StringList();
lines.appendln("{ \"organizations\" : [");
for (Organization organization : organizations)
{
lines.addAll(PathPropertyUtils.toJSON(organization));
lines.append(",");
}
if (!organizations.isEmpty())
{
lines.removeLast();
}
lines.appendln("] }");
logger.info("Htmlize organizations JSON Export pages.");
StringsUtils.writeToFile(file, lines);
}
/**
* Save.
*
* @param file
* the file
* @param services
* the services
* @throws UnsupportedEncodingException
* the unsupported encoding exception
* @throws FileNotFoundException
* the file not found exception
*/
public static void save(final File file, final Services services) throws UnsupportedEncodingException, FileNotFoundException
{
StringList lines = new StringList();
lines.appendln("{ \"services\" : [");
for (Service service : services)
{
lines.addAll(PathPropertyUtils.toJSON(service));
lines.append(",");
}
if (!services.isEmpty())
{
lines.removeLast();
}
lines.appendln("] }");
logger.info("Htmlize services JSON Export pages.");
StringsUtils.writeToFile(file, lines);
}
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2020 Christian Pierre MOMON <christian@momon.org> * Copyright (C) 2020-2021 Christian Pierre MOMON <christian@momon.org>
* *
* This file is part of StatoolInfos, simple service statistics tool. * This file is part of StatoolInfos, simple service statistics tool.
* *
@ -35,7 +35,7 @@ public interface PathProperties extends Iterable<PathProperty>
boolean add(PathProperty property); boolean add(PathProperty property);
/** /**
* Gets the. * h Gets the.
* *
* @param path * @param path
* the path * the path