-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding endpoints to list download statistics and export them as CSV a…
…nd TSV files
- Loading branch information
Showing
8 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
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
89 changes: 89 additions & 0 deletions
89
registry-ws/src/main/java/org/gbif/registry/ws/export/CsvWriter.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,89 @@ | ||
package org.gbif.registry.ws.export; | ||
|
||
import org.gbif.api.model.common.export.ExportFormat; | ||
import org.gbif.api.model.common.paging.Pageable; | ||
import org.gbif.api.model.occurrence.DownloadStatistics; | ||
import org.gbif.api.service.registry.OccurrenceDownloadService; | ||
import org.gbif.api.vocabulary.Country; | ||
|
||
import java.io.Writer; | ||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.SneakyThrows; | ||
import org.supercsv.cellprocessor.Optional; | ||
import org.supercsv.cellprocessor.ParseInt; | ||
import org.supercsv.cellprocessor.ift.CellProcessor; | ||
import org.supercsv.io.CsvBeanWriter; | ||
import org.supercsv.io.ICsvBeanWriter; | ||
import org.supercsv.prefs.CsvPreference; | ||
import org.supercsv.util.CsvContext; | ||
|
||
@Data | ||
@Builder | ||
public class CsvWriter<T> { | ||
|
||
private final String[] header; | ||
|
||
private final String[] fields; | ||
|
||
private final CellProcessor[] processors; | ||
|
||
private final Iterable<T> pager; | ||
|
||
private final ExportFormat preference; | ||
|
||
|
||
private CsvPreference csvPreference() { | ||
if (ExportFormat.CSV == preference) { | ||
return CsvPreference.STANDARD_PREFERENCE; | ||
} else if (ExportFormat.TSV == preference) { | ||
return CsvPreference.TAB_PREFERENCE; | ||
} | ||
throw new IllegalArgumentException("Export format not supported " + preference); | ||
} | ||
|
||
@SneakyThrows | ||
public void export(Writer writer) { | ||
try (ICsvBeanWriter beanWriter = new CsvBeanWriter(writer, csvPreference())) { | ||
beanWriter.writeHeader(header); | ||
for (T o : pager) { | ||
beanWriter.write(o, fields, processors); | ||
} | ||
writer.flush(); | ||
} | ||
} | ||
|
||
/** | ||
* Creates and CsvWriter/exporter DownloadStatistics. | ||
*/ | ||
public static CsvWriter<DownloadStatistics> downloadStatisticsTsvWriter(Iterable<DownloadStatistics> pager, | ||
ExportFormat preference) { | ||
|
||
return CsvWriter.<DownloadStatistics>builder() | ||
.fields(new String[]{"datasetKey", "totalRecords", "numberDownloads", "year", "month"}) | ||
.header(new String[]{"dataset_key", "total_records", "number_downloads", "year", "month"}) | ||
.processors(new CellProcessor[]{new UUIDProcessor(), | ||
new Optional(new ParseInt()), | ||
new Optional(new ParseInt()), | ||
new Optional(new ParseInt()), | ||
new Optional(new ParseInt())}) | ||
.preference(preference) | ||
.pager(pager) | ||
.build(); | ||
} | ||
|
||
/** | ||
* Null aware UUID processor. | ||
*/ | ||
private static class UUIDProcessor implements CellProcessor { | ||
@Override | ||
public String execute(Object value, CsvContext csvContext) { | ||
return value != null ? ((UUID) value).toString() : ""; | ||
} | ||
} | ||
|
||
} |
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
47 changes: 47 additions & 0 deletions
47
registry-ws/src/test/java/org/gbif/registry/ws/export/CsvWriterTest.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,47 @@ | ||
package org.gbif.registry.ws.export; | ||
|
||
import org.gbif.api.model.common.export.ExportFormat; | ||
import org.gbif.api.model.occurrence.DownloadStatistics; | ||
|
||
import java.io.StringWriter; | ||
import java.time.LocalDate; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class CsvWriterTest { | ||
|
||
@Test | ||
public void downloadStatisticsTest() { | ||
|
||
List<DownloadStatistics> stats = Arrays.asList( | ||
new DownloadStatistics(UUID.randomUUID(), 10, 10, LocalDate.of(2020,1,1)), | ||
new DownloadStatistics(UUID.randomUUID(), 10, 10, LocalDate.of(2021, 2,1))); | ||
|
||
StringWriter writer = new StringWriter(); | ||
|
||
CsvWriter.downloadStatisticsTsvWriter(stats, ExportFormat.TSV) | ||
.export(writer); | ||
|
||
String export = writer.toString(); | ||
String[] lines = export.split("\\n"); | ||
|
||
//Number of lines is header + list.size | ||
assertEquals(stats.size() + 1, lines.length); | ||
|
||
//Each line has 4 tabs | ||
assertEquals((stats.size() + 1) * 4, export.chars().filter(ch -> ch == '\t').count()); | ||
|
||
//Year test | ||
assertEquals("2020", lines[1].split("\\t")[3]); | ||
assertEquals("2021", lines[2].split("\\t")[3]); | ||
|
||
//Month test | ||
assertEquals("1", lines[1].split("\\t")[4]); | ||
assertEquals("2", lines[2].split("\\t")[4]); | ||
} | ||
} |