Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change nihms-loader startDate arg to harvestMonths #101

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
package org.eclipse.pass.loader.nihms;

import java.net.URL;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.pass.loader.nihms.model.NihmsStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -35,6 +37,8 @@ public class NihmsHarvester {

private static final Logger LOG = LoggerFactory.getLogger(NihmsHarvester.class);

static final int DEFAULT_HARVEST_MONTHS = 12;

private final UrlBuilder urlBuilder;
private final NihmsHarvesterDownloader nihmsHarvesterDownloader;

Expand All @@ -51,22 +55,19 @@ public NihmsHarvester(UrlBuilder urlBuilder,
* Retrieve files from NIHMS based on status list and startDate provided
*
* @param statusesToDownload list of {@code NihmsStatus} types to download from the NIHMS website
* @param startDate formatted as {@code yyyy-mm}, can be null to default to 1 year prior to harvest date
* @param harvestPeriodMonths number of months of data to query
*/
public void harvest(Set<NihmsStatus> statusesToDownload, String startDate) {
public void harvest(Set<NihmsStatus> statusesToDownload, int harvestPeriodMonths) {
if (CollectionUtils.isEmpty(statusesToDownload)) {
throw new RuntimeException("statusesToDownload list cannot be empty");
}
if (!validStartDate(startDate)) {
throw new RuntimeException(
String.format("The startDate %s is not valid. The date must be formatted as mm-yyyy", startDate));
}

try {
Map<String, String> params = new HashMap<>();

if (StringUtils.isNotEmpty(startDate)) {
startDate = startDate.replace("-", "/");
if (harvestPeriodMonths != DEFAULT_HARVEST_MONTHS) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/uuuu");
String startDate = LocalDate.now().minus(Period.ofMonths(harvestPeriodMonths)).format(formatter);
LOG.info("Filtering with Start Date " + startDate);
params.put("pdf", startDate);
}
Expand Down Expand Up @@ -94,15 +95,4 @@ public void harvest(Set<NihmsStatus> statusesToDownload, String startDate) {
}
}

/**
* null or empty are OK for start date, but a badly formatted date that does not have the format mm-yyyy should
* return false
*
* @param startDate true if valid start date (empty or formatted mm-yyyy)
* @return true if valid start date (empty or formatted mm-yyyy)
*/
public static boolean validStartDate(String startDate) {
return (StringUtils.isEmpty(startDate) || startDate.matches("^(0?[1-9]|1[012])-(\\d{4})$"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,13 @@ public class NihmsHarvesterCLIRunner implements CommandLineRunner {
private boolean inProcess = false;

/**
* The start date from which to load .
* The number of months of data to nihms harvest.
*/
@Option(name = "-s", aliases = {"-startDate", "--startDate"},
usage = "DateTime to start the query against NIHMS data. This will cause "
+ "a return of all records published since the date provided. Syntax must be mm-yyyy. This value " +
"will override the "
+ "NIHMS system default which is one year before the current month")
private String startDate = "";
@Option(name = "-m", aliases = {"-harvestMonths", "--harvestMonths"},
usage = "Period of time by month to query against NIHMS data. For example, to query for the past 3 " +
"months of nihms data, the argument would be -harvestMonths=3. This value will override the NIHMS " +
"system default which is one year before the current month.")
private int harvestMonths = NihmsHarvester.DEFAULT_HARVEST_MONTHS;

private final NihmsHarvester nihmsHarvester;

Expand All @@ -97,7 +96,7 @@ public void run(String... args) {
}

Set<NihmsStatus> statusesToProcess = new HashSet<>();
String startDateFilter = this.startDate;
int harvestPeriodMonths = this.harvestMonths;

//select statuses to process
if (this.compliant) {
Expand All @@ -114,7 +113,7 @@ public void run(String... args) {
}

/* Run the package generation application proper */
nihmsHarvester.harvest(statusesToProcess, startDateFilter);
nihmsHarvester.harvest(statusesToProcess, harvestPeriodMonths);

} catch (CmdLineException e) {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
*
* * Copyright 2024 Johns Hopkins University
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package org.eclipse.pass.loader.nihms;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;

import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.EnumSet;

import org.eclipse.pass.loader.nihms.model.NihmsStatus;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.TestPropertySource;

/**
* @author Russ Poetker ([email protected])
*/
@SpringBootTest(classes = NihmsHarvesterCLI.class, args = {"--harvestMonths=3"})
@TestPropertySource(
locations = "classpath:test-application.properties",
properties = {
"nihmsetl.api.url.param.pdf=",
"nihmsetl.api.url.param.pdt="
})
public class NihmsHarvesterCLIHarvestMonthsTest {

@SpyBean NihmsHarvester nihmsHarvester;
@MockBean NihmsHarvesterDownloader nihmsHarvesterDownloader;

@Test
public void testHarvesterCLI_WithHarvestMonths() throws IOException, InterruptedException {
// GIVEN/WHEN
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/uuuu");
// 3 months passed in args up top in @SpringBootTest
String expectedPdf = LocalDate.now().minus(Period.ofMonths(3)).format(formatter)
.replace("/", "%2F");
// THEN
verify(nihmsHarvester).harvest(eq(EnumSet.allOf(NihmsStatus.class)), anyInt());
verify(nihmsHarvesterDownloader).download(
eq(new URL("https://www.ncbi.nlm.nih.gov/pmc/utils/pacm/c?pdf=" + expectedPdf +
"&api-token=test-token&inst=JOHNS-HOPKINS-TEST&format=csv&ipf=4134401-TEST")),
eq(NihmsStatus.COMPLIANT));
verify(nihmsHarvesterDownloader).download(
eq(new URL("https://www.ncbi.nlm.nih.gov/pmc/utils/pacm/p?pdf=" + expectedPdf +
"&api-token=test-token&inst=JOHNS-HOPKINS-TEST&format=csv&ipf=4134401-TEST")),
eq(NihmsStatus.IN_PROCESS));
verify(nihmsHarvesterDownloader).download(
eq(new URL("https://www.ncbi.nlm.nih.gov/pmc/utils/pacm/n?pdf=" + expectedPdf +
"&api-token=test-token&inst=JOHNS-HOPKINS-TEST&format=csv&ipf=4134401-TEST")),
eq(NihmsStatus.NON_COMPLIANT));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.eclipse.pass.loader.nihms;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;

Expand Down Expand Up @@ -46,7 +46,7 @@ public class NihmsHarvesterCLITest {
public void testHarvesterCLI() throws IOException, InterruptedException {
// GIVEN/WHEN
// THEN
verify(nihmsHarvester).harvest(eq(EnumSet.allOf(NihmsStatus.class)), any());
verify(nihmsHarvester).harvest(eq(EnumSet.allOf(NihmsStatus.class)), anyInt());
verify(nihmsHarvesterDownloader).download(
eq(new URL("https://www.ncbi.nlm.nih.gov/pmc/utils/pacm/c?pdt=07%2F2019&pdf=07%2F2018" +
"&api-token=test-token&inst=JOHNS-HOPKINS-TEST&format=csv&ipf=4134401-TEST")),
Expand Down
2 changes: 1 addition & 1 deletion pass-nihms-loader/nihms-docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

# Execute NIHMS harvest
java -jar nihms-data-harvest-cli-exec.jar
java -jar nihms-data-harvest-cli-exec.jar "$@"

# Execute NIHMS transform and load into PASS
java -jar nihms-data-transform-load-exec.jar
Loading