diff --git a/util/src/main/java/org/datacommons/util/CSVReportWriter.java b/util/src/main/java/org/datacommons/util/CSVReportWriter.java new file mode 100644 index 00000000..de3a6209 --- /dev/null +++ b/util/src/main/java/org/datacommons/util/CSVReportWriter.java @@ -0,0 +1,56 @@ +package org.datacommons.util; + +import java.io.IOException; +import java.io.Writer; +import java.util.Map; +import java.util.TreeSet; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import org.datacommons.util.SummaryReportGenerator.StatVarSummary; + +class CSVReportWriter { + + enum ReportHeaders { + StatVar, + NumPlaces, + NumObservations, + NumObservationsDates, + MinDate, + MaxDate, + MeasurementMethods, + Units, + ScalingFactors, + observationPeriods + } + + public static void writeRecords(Map records, Writer sw) + throws IOException { + CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(ReportHeaders.class); + + try (final CSVPrinter printer = new CSVPrinter(sw, csvFormat)) { + records.forEach( + (sv, summary) -> { + try { + printer.printRecord( + sv, + summary.places.size(), + summary.numObservations, + summary.dates.size(), + !summary.dates.isEmpty() + ? ((TreeSet) (summary.getUniqueDates())).first() + : "", + !summary.dates.isEmpty() + ? ((TreeSet) (summary.getUniqueDates())).last() + : "", + summary.mMethods.toString(), + summary.units.toString(), + summary.scalingFactors.toString(), + summary.observationPeriods.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + } +} + diff --git a/util/src/main/java/org/datacommons/util/SummaryReportGenerator.java b/util/src/main/java/org/datacommons/util/SummaryReportGenerator.java index 269b2060..162263ac 100644 --- a/util/src/main/java/org/datacommons/util/SummaryReportGenerator.java +++ b/util/src/main/java/org/datacommons/util/SummaryReportGenerator.java @@ -31,6 +31,7 @@ public class SummaryReportGenerator { public static boolean TEST_mode = false; public static final String SUMMARY_REPORT_HTML = "summary_report.html"; +public static final String SUMMARY_REPORT_CSV = "summary_report.csv"; // An object to save the information about a stat var. This contains all the necessary getters to // access the information in this object from SummaryReport.ftl @@ -190,6 +191,8 @@ public static void generateReportSummary( data.put("placeSeriesSummaryMap", placeSeriesSummaryMap); Writer file = new FileWriter(Paths.get(outputDir.toString(), SUMMARY_REPORT_HTML).toString()); template.process(data, file); +Writer csvFile = new FileWriter(Paths.get(outputDir.toString(), SUMMARY_REPORT_CSV).toString()); + CSVReportWriter.writeRecords(svSummaryMap, csvFile); } private static Configuration getConfiguration() throws IOException { diff --git a/util/src/test/java/org/datacommons/util/CSVReportWriterTest.java b/util/src/test/java/org/datacommons/util/CSVReportWriterTest.java new file mode 100644 index 00000000..c51343f1 --- /dev/null +++ b/util/src/test/java/org/datacommons/util/CSVReportWriterTest.java @@ -0,0 +1,51 @@ +package org.datacommons.util; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import org.datacommons.util.SummaryReportGenerator.StatVarSummary; +import org.junit.Before; +import org.junit.Test; + +public class CSVReportWriterTest { + + public static final String[] HEADERS = {"author", "title"}; + public static final String EXPECTED_FILESTREAM = + "StatVar,NumPlaces,NumObservations,NumObservationsDates,MinDate,MaxDate,MeasurementMethods,Units,ScalingFactors,observationPeriods\r\n" + + "Var1,0,6,0,,,[CensusACS5YrSurvey],[],[],[]\r\n" + + "Var2,0,2,2,2020,2025,[CensusACS5YrSurvey],[],[],[]"; + Map records; + + @Before + public void setUp() { + StatVarSummary countPersonExpectedSummary = new StatVarSummary(); + countPersonExpectedSummary.numObservations = 6; + countPersonExpectedSummary.mMethods = Set.of("CensusACS5YrSurvey"); + StatVarSummary countFemaleExpectedSummary = new StatVarSummary(); + countFemaleExpectedSummary.numObservations = 2; + countFemaleExpectedSummary.mMethods = Set.of("CensusACS5YrSurvey"); + countFemaleExpectedSummary.dates = Set.of("2020", "2025"); + + records = + Collections.unmodifiableMap( + new LinkedHashMap() { + { + put("Var1", countPersonExpectedSummary); + put("Var2", countFemaleExpectedSummary); + } + }); + } + + @Test + public void csvWriterSuccess() throws IOException { + StringWriter sw = new StringWriter(); + CSVReportWriter.writeRecords(records, sw); + assertEquals(EXPECTED_FILESTREAM, sw.toString().trim()); + } +} +