-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hooks to generate CSV stats summary
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
util/src/main/java/org/datacommons/util/CSVReportWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
util/src/test/java/org/datacommons/util/CSVReportWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|