Skip to content

Commit

Permalink
Merge branch 'origin-indexer-module-updates' into 'master'
Browse files Browse the repository at this point in the history
Origin indexer module updates

See merge request ghsc/hazdev/pdl!154
  • Loading branch information
jmfee-usgs committed Sep 17, 2022
2 parents e595abb + 9845a4c commit c9a520b
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 123 deletions.
4 changes: 3 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ stages:
- ProductClient.jar
- ProductClient.zip
reports:
cobertura: build/reports/cobertura/cobertura.xml
coverage_report:
coverage_format: cobertura
path: build/reports/cobertura/cobertura.xml
junit: build/test-results/test/TEST-*.xml
cache:
paths:
Expand Down
42 changes: 0 additions & 42 deletions .travis.yml

This file was deleted.

24 changes: 2 additions & 22 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ plugins {
id "java"
id "jacoco" // code coverage
id "pmd" // static code analysis
id "org.ajoberstar.grgit" version "4.1.0" // git repo information
id "org.ajoberstar.git-publish" version "3.0.0" // publish gh-pages branch
id "org.ajoberstar.grgit" version "4.1.1" // git repo information
id "org.ajoberstar.git-publish" version "3.0.1" // publish gh-pages branch
id "com.kageiit.jacobo" version "2.1.0"
}

Expand All @@ -25,8 +25,6 @@ configurations {
}

dependencies {
codacy "com.codacy:codacy-coverage-reporter:4.0+"

// for explanation of dependency types, see
// https://docs.gradle.org/current/userguide/building_java_projects.html

Expand Down Expand Up @@ -189,9 +187,6 @@ gitPublish {
}
}


// Tasks for TravisCI

// convert jacoco to cobertura
import com.kageiit.jacobo.JacoboTask
tasks.create("jacobo", JacoboTask, {
Expand All @@ -200,18 +195,3 @@ tasks.create("jacobo", JacoboTask, {
it.srcDirs = sourceSets.main.java.srcDirs
}).dependsOn(jacocoTestReport)
check.dependsOn jacobo


// .travis.yml uses this to upload coverage
task sendCoverageToCodacy(type: JavaExec, dependsOn: jacocoTestReport) {
description = "Upload coverage to codacy (used by TravisCI)"
main = "com.codacy.CodacyCoverageReporter"
classpath = configurations.codacy
args = [
"report",
"-l",
"Java",
"-r",
"${buildDir}/reports/jacoco/test/jacocoTestReport.xml"
]
}
4 changes: 2 additions & 2 deletions code.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Product Distribution Layer",
"organization": "U.S. Geological Survey",
"description": "Distribution system used for derived earthquake information",
"version": "v2.7.11",
"version": "v2.7.12",
"status": "Production",
"permissions": {
"usageType": "openSource",
Expand All @@ -27,7 +27,7 @@
"email": "[email protected]"
},
"date": {
"metadataLastUpdated": "2021-12-16"
"metadataLastUpdated": "2022-09-16"
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class ProductClient extends DefaultConfigurable implements
ProductClientMBean, Bootstrappable {

/** The "release" version number. */
public static final String RELEASE_VERSION = "Version 2.7.11 2021-12-16";
public static final String RELEASE_VERSION = "Version 2.7.12 2022-09-16";

/** Property name used on products for current RELEASE_VERSION. */
public static final String PDL_CLIENT_VERSION_PROPERTY = "pdl-client-version";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.charset.StandardCharsets;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;

Expand Down Expand Up @@ -98,19 +99,55 @@ public JsonObject getEventPlaces(BigDecimal latitude, BigDecimal longitude)
}
}


/**
* Get nearest place to a latitude and longitude
* @param latitude of place
* @param longitude of place
* @return JSONObject of place
* @throws IndexOutOfBoundsException on no places returned
* @throws IOException on IO error
* @throws MalformedURLException on URL error
* @deprecated
*/
public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude) throws IOException, MalformedURLException {
JsonObject places = this.getEventPlaces(latitude, longitude);
JsonObject feature = places.getJsonArray("features").getJsonObject(0);
public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude)
throws IndexOutOfBoundsException, IOException, MalformedURLException {
return this.getNearestPlace(latitude, longitude, 300);
}

return feature;
/**
* Get nearest place to a latitude and longitude
* @param latitude of place
* @param longitude of place
* @param maxradiuskm around place
* @return JSONObject of place
* @throws IOException on IO error
* @throws MalformedURLException on URL error
*/
public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude,
int maxradiuskm) throws IOException, MalformedURLException {
final URL url = new URL(this.endpointUrl +
"?type=geonames" +
"&latitude=" + URLEncoder.encode(latitude.toString(), StandardCharsets.UTF_8.toString()) +
"&longitude=" + URLEncoder.encode(longitude.toString(), StandardCharsets.UTF_8.toString()) +
"&maxradiuskm=" + URLEncoder.encode(String.valueOf(maxradiuskm), StandardCharsets.UTF_8.toString()) +
"&limit=1"
);

try (
InputStream in = StreamUtils.getURLInputStream(url, this.connectTimeout, this.readTimeout);
JsonReader reader = Json.createReader(in)
) {
JsonObject json = reader.readObject();
JsonObject places = json.getJsonObject("geonames");
JsonArray features = places.getJsonArray("features");

if (features.size() > 0) {
return features.getJsonObject(0);
} else {
return null;
}
}
}

/** @return readTimeout */
Expand All @@ -134,4 +171,4 @@ public void setReadTimeout(final int readTimeout) {
}

// as needed, implement full GeoServe places API options
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ public void setReadTimeout(final int readTimeout) {
}

// as needed, implement full GeoServe places API options
}
}
68 changes: 60 additions & 8 deletions src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public ProductSummary getProductSummary(Product product) throws Exception {
summaryProperties.put("title", StringUtils.encodeAsUtf8(title));
} catch (Exception ex) {
LOGGER
.fine(String.format("[%s] %s for product %s", this.getName(), ex.getMessage(), product.getId().toString()));
.warning(String.format("[%s] %s for product %s", this.getName(), ex.getMessage(), product.getId().toString()));
// Do nothing, value-added failed. Move on.
}
}
Expand Down Expand Up @@ -250,19 +250,44 @@ public void configure(Config config) throws Exception {
*
* @throws IOException if IO error occurs
*/
public String getEventTitle(BigDecimal latitude, BigDecimal longitude) throws IOException {
public String getEventTitle(BigDecimal latitude, BigDecimal longitude) throws Exception, IOException {
StringBuffer messages = new StringBuffer();
String message = null;

try {
JsonObject feature = this.geoservePlaces.getNearestPlace(latitude, longitude);
double distance = feature.getJsonObject("properties").getJsonNumber("distance").doubleValue();
final JsonObject feature = this.geoservePlaces.getNearestPlace(
latitude,
longitude,
this.distanceThreshold
);

if (distance <= (double) this.distanceThreshold) {
if (feature != null) {
return this.formatEventTitle(feature);
} else {
message = "Places service returned no places within distance threshold";
messages.append(message + ". ");
LOGGER.log(Level.INFO, "[" + this.getName() + "] " + message);
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, "[" + this.getName() + "] failed to get nearest place from geoserve places service.");
message = "Failed to get nearest place from geoserve places service";
messages.append(message + ". ");
messages.append(e.getMessage() + ". ");
LOGGER.log(Level.INFO, "[" + this.getName() + "] " + message);
}

try {
return this.geoserveRegions.getFeRegionName(latitude, longitude);
} catch (Exception e) {
message = "Failed to get FE region name";
messages.append(message + ". ");
messages.append(e.getMessage() + ". ");
LOGGER.log(Level.INFO, "[" + this.getName() + "] .");
}

return this.geoserveRegions.getFeRegionName(latitude, longitude);
// If we get this far, things failed spectacularly, report the error
Exception e = new Exception(messages.toString());
e.fillInStackTrace();
throw e;
}

/**
Expand Down Expand Up @@ -311,4 +336,31 @@ public String azimuthToDirection(double azimuth) {
return directions[(int) Math.round((azimuth % 360.0) / fullwind)];
}

}
public static void main(String[] args) throws Exception {
BigDecimal latitude = new BigDecimal("0.0");
BigDecimal longitude = new BigDecimal("0.0");
int maxradiuskm = DEFAULT_GEOSERVE_DISTANCE_THRESHOLD;
final OriginIndexerModule module = new OriginIndexerModule(
new GeoservePlacesService(),
new GeoserveRegionsService()
);
module.setName("TestModule");

for (String arg : args) {
if (arg.startsWith("--latitude=")) {
latitude = new BigDecimal(arg.replace("--latitude=", ""));
} else if (arg.startsWith("--longitude=")) {
longitude = new BigDecimal(arg.replace("--longitude=", ""));
} else if (arg.startsWith("--maxradiuskm=")) {
maxradiuskm = Integer.parseInt(arg.replace("--maxradiuskm=", ""));
}
}

module.setDistanceThreshold(maxradiuskm);

System.out.printf("Title[%s, %s] = `%s`\n",
latitude.doubleValue(),
longitude.doubleValue(),
module.getEventTitle(latitude, longitude));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;

import javax.json.Json;
import javax.json.JsonObject;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class GeoservePlacesServiceTest {

private GeoservePlacesService service = null;
Expand All @@ -25,29 +22,5 @@ public class GeoservePlacesServiceTest {
)
).build();

@Before
public void setUpTestEnvironment() {
this.service = new DummyPlacesService();
}

@Test
public void getNearestPlace() throws Exception {
BigDecimal latitude = new BigDecimal("0.0");
BigDecimal longitude = new BigDecimal("0.0");

// nearest feature from feature collection
JsonObject expectation = this.feature;
Assert.assertEquals(expectation, this.service.getNearestPlace(latitude, longitude));
}

protected class DummyPlacesService extends GeoservePlacesService {
@Override
public JsonObject getEventPlaces(BigDecimal latitude, BigDecimal longitude) throws IOException, MalformedURLException {
JsonObject eventCollection = Json.createObjectBuilder().add("features",
Json.createArrayBuilder().add(0, feature)
).build();

return eventCollection;
}
}
}
// TODO Test this class, consider Mockito
}
Loading

0 comments on commit c9a520b

Please sign in to comment.