Skip to content

Commit

Permalink
Add hooks to generate CSV stats summary
Browse files Browse the repository at this point in the history
  • Loading branch information
vish-cs committed Dec 1, 2024
1 parent 98cd40c commit 490368a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
56 changes: 56 additions & 0 deletions util/src/main/java/org/datacommons/util/CSVReportWriter.java
Original file line number Diff line number Diff line change
@@ -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<String, StatVarSummary> 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<String>) (summary.getUniqueDates())).first()
: "",
!summary.dates.isEmpty()
? ((TreeSet<String>) (summary.getUniqueDates())).last()
: "",
summary.mMethods.toString(),
summary.units.toString(),
summary.scalingFactors.toString(),
summary.observationPeriods.toString());
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
51 changes: 51 additions & 0 deletions util/src/test/java/org/datacommons/util/CSVReportWriterTest.java
Original file line number Diff line number Diff line change
@@ -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<String, StatVarSummary> 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<String, StatVarSummary>() {
{
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());
}
}

0 comments on commit 490368a

Please sign in to comment.