From 062e7886b33de7319e39f4142d975ac8c3c3362d Mon Sep 17 00:00:00 2001 From: Eric Martinez Date: Wed, 14 Sep 2022 11:22:13 -0600 Subject: [PATCH 01/12] Updating logic for getting nearest place --- .../geoserve/GeoservePlacesService.java | 43 ++++++++++++++++--- .../origin/OriginIndexerModule.java | 40 ++++++++++++++--- 2 files changed, 71 insertions(+), 12 deletions(-) diff --git a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java index 35505b9e..f3ca0a0e 100644 --- a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java +++ b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; +import java.math.BigInteger; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; @@ -98,6 +99,7 @@ public JsonObject getEventPlaces(BigDecimal latitude, BigDecimal longitude) } } + /** * Get nearest place to a latitude and longitude * @param latitude of place @@ -106,11 +108,42 @@ public JsonObject getEventPlaces(BigDecimal latitude, BigDecimal longitude) * @throws IOException on IO error * @throws MalformedURLException on URL error */ - 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 IOException, MalformedURLException { + return this.getNearestPlace(latitude, longitude, null); + } - 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, + BigInteger maxradiuskm) throws IOException, MalformedURLException { + // JsonObject places = this.getEventPlaces(latitude, longitude); + // JsonObject feature = places.getJsonArray("features").getJsonObject(0); + if (maxradiuskm == null) { + maxradiuskm = new BigInteger("300"); + } + + 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(maxradiuskm.toString(), StandardCharsets.UTF_8.toString()) + ); + + try (InputStream in = StreamUtils.getURLInputStream(url, this.connectTimeout, this.readTimeout)) { + JsonReader reader = Json.createReader(in); + JsonObject json = reader.readObject(); + reader.close(); + JsonObject places = json.getJsonObject("event"); + return places.getJsonArray("features").getJsonObject(0); + } } /** @return readTimeout */ @@ -134,4 +167,4 @@ public void setReadTimeout(final int readTimeout) { } // as needed, implement full GeoServe places API options -} \ No newline at end of file +} diff --git a/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java b/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java index 7ccfb35b..1ed4e7ee 100644 --- a/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java +++ b/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java @@ -2,6 +2,8 @@ import java.io.IOException; import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.ArrayList; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -250,19 +252,43 @@ 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, + new BigInteger(String.valueOf(this.distanceThreshold)) + ); - if (distance <= (double) this.distanceThreshold) { + if (feature != null) { return this.formatEventTitle(feature); + } else { + message = "Nearest place exceeded 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.WARNING, "[" + 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.WARNING, "[" + this.getName() + "] ."); } - return this.geoserveRegions.getFeRegionName(latitude, longitude); + Exception e = new Exception(messages.toString()); + e.fillInStackTrace(); + throw e; } /** @@ -311,4 +337,4 @@ public String azimuthToDirection(double azimuth) { return directions[(int) Math.round((azimuth % 360.0) / fullwind)]; } -} \ No newline at end of file +} From 9372ae2d802adafc3a4c09b34964d98875e3ec93 Mon Sep 17 00:00:00 2001 From: Eric Martinez Date: Wed, 14 Sep 2022 11:22:42 -0600 Subject: [PATCH 02/12] Auto-formatting --- .../gov/usgs/earthquake/geoserve/GeoserveRegionsService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/gov/usgs/earthquake/geoserve/GeoserveRegionsService.java b/src/main/java/gov/usgs/earthquake/geoserve/GeoserveRegionsService.java index e6a4d8c4..e5be1590 100644 --- a/src/main/java/gov/usgs/earthquake/geoserve/GeoserveRegionsService.java +++ b/src/main/java/gov/usgs/earthquake/geoserve/GeoserveRegionsService.java @@ -138,4 +138,4 @@ public void setReadTimeout(final int readTimeout) { } // as needed, implement full GeoServe places API options -} \ No newline at end of file +} From f1b2a261c435c31249088920d85ad3721b03d167 Mon Sep 17 00:00:00 2001 From: Eric Martinez Date: Wed, 14 Sep 2022 11:45:06 -0600 Subject: [PATCH 03/12] Updating tests for getEventTitle --- .../geoserve/GeoservePlacesService.java | 7 +++-- .../origin/OriginIndexerModule.java | 19 ++++++------- .../origin/OriginIndexerModuleTest.java | 28 +++++++++++-------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java index f3ca0a0e..5bf9a91f 100644 --- a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java +++ b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java @@ -105,11 +105,13 @@ public JsonObject getEventPlaces(BigDecimal latitude, BigDecimal 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 { + throws IndexOutOfBoundsException, IOException, MalformedURLException { return this.getNearestPlace(latitude, longitude, null); } @@ -119,11 +121,12 @@ public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude) * @param longitude of place * @param maxradiuskm around place * @return JSONObject of place + * @throws IndexOutOfBoundsException on no places returned * @throws IOException on IO error * @throws MalformedURLException on URL error */ public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, - BigInteger maxradiuskm) throws IOException, MalformedURLException { + BigInteger maxradiuskm) throws IndexOutOfBoundsException, IOException, MalformedURLException { // JsonObject places = this.getEventPlaces(latitude, longitude); // JsonObject feature = places.getJsonArray("features").getJsonObject(0); if (maxradiuskm == null) { diff --git a/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java b/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java index 1ed4e7ee..e1282bff 100644 --- a/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java +++ b/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java @@ -113,7 +113,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. } } @@ -263,18 +263,16 @@ public String getEventTitle(BigDecimal latitude, BigDecimal longitude) throws Ex new BigInteger(String.valueOf(this.distanceThreshold)) ); - if (feature != null) { - return this.formatEventTitle(feature); - } else { - message = "Nearest place exceeded distance threshold"; - messages.append(message + ". "); - LOGGER.log(Level.INFO, "[" + this.getName() + "] " + message); - } + return this.formatEventTitle(feature); + } catch (IndexOutOfBoundsException ibx) { + message = "Places service returned no places within distance threshold"; + messages.append(message + ". "); + LOGGER.log(Level.INFO, "[" + this.getName() + "] " + message); } catch (Exception e) { message = "Failed to get nearest place from geoserve places service"; messages.append(message + ". "); messages.append(e.getMessage() + ". "); - LOGGER.log(Level.WARNING, "[" + this.getName() + "] " + message); + LOGGER.log(Level.INFO, "[" + this.getName() + "] " + message); } try { @@ -283,9 +281,10 @@ public String getEventTitle(BigDecimal latitude, BigDecimal longitude) throws Ex message = "Failed to get FE region name"; messages.append(message + ". "); messages.append(e.getMessage() + ". "); - LOGGER.log(Level.WARNING, "[" + this.getName() + "] ."); + LOGGER.log(Level.INFO, "[" + this.getName() + "] ."); } + // If we get this far, things failed spectacularly, report the error Exception e = new Exception(messages.toString()); e.fillInStackTrace(); throw e; diff --git a/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java b/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java index 7dc74fe2..5bc72d1b 100644 --- a/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java +++ b/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.math.BigDecimal; +import java.math.BigInteger; import java.net.MalformedURLException; import javax.json.Json; @@ -140,7 +141,7 @@ public void formatEventTitleTest() { @Test - public void getEventTitleTest() throws IOException { + public void getEventTitleTest() throws Exception, IOException { BigDecimal latitude = new BigDecimal("0.0"); BigDecimal longitude = new BigDecimal("0.0"); String title = ""; @@ -171,16 +172,21 @@ public void azimuthToDirectionTest() { protected class DummyPlacesService extends GeoservePlacesService { @Override - public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude) throws IOException, MalformedURLException { - return Json.createObjectBuilder().add("properties", - Json.createObjectBuilder() - .add("admin1_name", "admin1_name") - .add("azimuth", 60.1) - .add("country_code", "country_code") - .add("country_name", "country_name") - .add("distance", distance) - .add("name", "name") - ).build(); + public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, BigInteger maxradiuskm) + throws IndexOutOfBoundsException, IOException, MalformedURLException { + if (distance <= maxradiuskm.doubleValue()) { + return Json.createObjectBuilder().add("properties", + Json.createObjectBuilder() + .add("admin1_name", "admin1_name") + .add("azimuth", 60.1) + .add("country_code", "country_code") + .add("country_name", "country_name") + .add("distance", distance) + .add("name", "name")) + .build(); + } else { + throw new IndexOutOfBoundsException("Index out of bounds"); + } } } protected class DummyRegionsService extends GeoserveRegionsService { From 88ced976bc5dd48a148edb6aa59978dd95121a33 Mon Sep 17 00:00:00 2001 From: Eric Martinez Date: Wed, 14 Sep 2022 11:55:38 -0600 Subject: [PATCH 04/12] Removing tests for getNearestPlace, need mocking framework --- .../geoserve/GeoservePlacesServiceTest.java | 33 ++----------------- 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/src/test/java/gov/usgs/earthquake/geoserve/GeoservePlacesServiceTest.java b/src/test/java/gov/usgs/earthquake/geoserve/GeoservePlacesServiceTest.java index fea3fed5..7829f4a0 100644 --- a/src/test/java/gov/usgs/earthquake/geoserve/GeoservePlacesServiceTest.java +++ b/src/test/java/gov/usgs/earthquake/geoserve/GeoservePlacesServiceTest.java @@ -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; @@ -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; - } - } -} \ No newline at end of file + // TODO Test this class, consider Mockito +} From 34d635a511ce53d282be3b7cc0f79dc769a022b5 Mon Sep 17 00:00:00 2001 From: Eric Martinez Date: Fri, 16 Sep 2022 11:03:08 -0600 Subject: [PATCH 05/12] Add limit to query for performance --- .../gov/usgs/earthquake/geoserve/GeoservePlacesService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java index 5bf9a91f..f73b0994 100644 --- a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java +++ b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java @@ -137,7 +137,8 @@ public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, "?type=geonames" + "&latitude=" + URLEncoder.encode(latitude.toString(), StandardCharsets.UTF_8.toString()) + "&longitude=" + URLEncoder.encode(longitude.toString(), StandardCharsets.UTF_8.toString()) + - "&maxradiuskm=" + URLEncoder.encode(maxradiuskm.toString(), StandardCharsets.UTF_8.toString()) + "&maxradiuskm=" + URLEncoder.encode(maxradiuskm.toString(), StandardCharsets.UTF_8.toString()) + + "&limit=1" ); try (InputStream in = StreamUtils.getURLInputStream(url, this.connectTimeout, this.readTimeout)) { From 3fa8642125ae3bf078eb1e812c1ef796e174bbaf Mon Sep 17 00:00:00 2001 From: Eric Martinez Date: Fri, 16 Sep 2022 11:07:09 -0600 Subject: [PATCH 06/12] Using try-with-resources --- .../usgs/earthquake/geoserve/GeoservePlacesService.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java index f73b0994..ac3d95b4 100644 --- a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java +++ b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java @@ -141,10 +141,11 @@ public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, "&limit=1" ); - try (InputStream in = StreamUtils.getURLInputStream(url, this.connectTimeout, this.readTimeout)) { - JsonReader reader = Json.createReader(in); + try ( + InputStream in = StreamUtils.getURLInputStream(url, this.connectTimeout, this.readTimeout); + JsonReader reader = Json.createReader(in) + ) { JsonObject json = reader.readObject(); - reader.close(); JsonObject places = json.getJsonObject("event"); return places.getJsonArray("features").getJsonObject(0); } From c8ce8d9be85151a058490257f3601a9e7b8003ae Mon Sep 17 00:00:00 2001 From: Eric Martinez Date: Fri, 16 Sep 2022 11:07:25 -0600 Subject: [PATCH 07/12] Pulling off geonames instead of event --- .../gov/usgs/earthquake/geoserve/GeoservePlacesService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java index ac3d95b4..b17370d2 100644 --- a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java +++ b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java @@ -146,7 +146,7 @@ public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, JsonReader reader = Json.createReader(in) ) { JsonObject json = reader.readObject(); - JsonObject places = json.getJsonObject("event"); + JsonObject places = json.getJsonObject("geonames"); return places.getJsonArray("features").getJsonObject(0); } } From 0f20d2747c19aa1c98b9e20327e32cd7133db784 Mon Sep 17 00:00:00 2001 From: Eric Martinez Date: Fri, 16 Sep 2022 11:13:38 -0600 Subject: [PATCH 08/12] Checking length rather than throwing exception --- .../geoserve/GeoservePlacesService.java | 12 +++++++++--- .../earthquake/origin/OriginIndexerModule.java | 16 +++++++++------- .../origin/OriginIndexerModuleTest.java | 4 ++-- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java index b17370d2..f3fc5f3a 100644 --- a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java +++ b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java @@ -10,6 +10,7 @@ import java.nio.charset.StandardCharsets; import javax.json.Json; +import javax.json.JsonArray; import javax.json.JsonObject; import javax.json.JsonReader; @@ -121,12 +122,11 @@ public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude) * @param longitude of place * @param maxradiuskm around place * @return JSONObject of place - * @throws IndexOutOfBoundsException on no places returned * @throws IOException on IO error * @throws MalformedURLException on URL error */ public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, - BigInteger maxradiuskm) throws IndexOutOfBoundsException, IOException, MalformedURLException { + BigInteger maxradiuskm) throws IOException, MalformedURLException { // JsonObject places = this.getEventPlaces(latitude, longitude); // JsonObject feature = places.getJsonArray("features").getJsonObject(0); if (maxradiuskm == null) { @@ -147,7 +147,13 @@ public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, ) { JsonObject json = reader.readObject(); JsonObject places = json.getJsonObject("geonames"); - return places.getJsonArray("features").getJsonObject(0); + JsonArray features = places.getJsonArray("features"); + + if (features.size() > 0) { + return features.getJsonObject(0); + } else { + return null; + } } } diff --git a/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java b/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java index e1282bff..c9a4823a 100644 --- a/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java +++ b/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java @@ -261,13 +261,15 @@ public String getEventTitle(BigDecimal latitude, BigDecimal longitude) throws Ex latitude, longitude, new BigInteger(String.valueOf(this.distanceThreshold)) - ); - - return this.formatEventTitle(feature); - } catch (IndexOutOfBoundsException ibx) { - message = "Places service returned no places within distance threshold"; - messages.append(message + ". "); - LOGGER.log(Level.INFO, "[" + this.getName() + "] " + message); + ); + + 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) { message = "Failed to get nearest place from geoserve places service"; messages.append(message + ". "); diff --git a/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java b/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java index 5bc72d1b..1b776135 100644 --- a/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java +++ b/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java @@ -173,7 +173,7 @@ public void azimuthToDirectionTest() { protected class DummyPlacesService extends GeoservePlacesService { @Override public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, BigInteger maxradiuskm) - throws IndexOutOfBoundsException, IOException, MalformedURLException { + throws IOException, MalformedURLException { if (distance <= maxradiuskm.doubleValue()) { return Json.createObjectBuilder().add("properties", Json.createObjectBuilder() @@ -185,7 +185,7 @@ public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, Big .add("name", "name")) .build(); } else { - throw new IndexOutOfBoundsException("Index out of bounds"); + return null; } } } From a1b2c5397b11b6ad7d512a7b71a3b4359f354021 Mon Sep 17 00:00:00 2001 From: Eric Martinez Date: Fri, 16 Sep 2022 11:22:09 -0600 Subject: [PATCH 09/12] Using maxradiuskm as integer instead of bigint --- .../earthquake/geoserve/GeoservePlacesService.java | 12 +++--------- .../usgs/earthquake/origin/OriginIndexerModule.java | 2 +- .../earthquake/origin/OriginIndexerModuleTest.java | 4 ++-- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java index f3fc5f3a..572789ee 100644 --- a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java +++ b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java @@ -113,7 +113,7 @@ public JsonObject getEventPlaces(BigDecimal latitude, BigDecimal longitude) */ public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude) throws IndexOutOfBoundsException, IOException, MalformedURLException { - return this.getNearestPlace(latitude, longitude, null); + return this.getNearestPlace(latitude, longitude, 300); } /** @@ -126,18 +126,12 @@ public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude) * @throws MalformedURLException on URL error */ public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, - BigInteger maxradiuskm) throws IOException, MalformedURLException { - // JsonObject places = this.getEventPlaces(latitude, longitude); - // JsonObject feature = places.getJsonArray("features").getJsonObject(0); - if (maxradiuskm == null) { - maxradiuskm = new BigInteger("300"); - } - + 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(maxradiuskm.toString(), StandardCharsets.UTF_8.toString()) + + "&maxradiuskm=" + URLEncoder.encode(String.valueOf(maxradiuskm), StandardCharsets.UTF_8.toString()) + "&limit=1" ); diff --git a/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java b/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java index c9a4823a..3d3a7dcc 100644 --- a/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java +++ b/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java @@ -260,7 +260,7 @@ public String getEventTitle(BigDecimal latitude, BigDecimal longitude) throws Ex final JsonObject feature = this.geoservePlaces.getNearestPlace( latitude, longitude, - new BigInteger(String.valueOf(this.distanceThreshold)) + this.distanceThreshold ); if (feature != null) { diff --git a/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java b/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java index 1b776135..4e7c5805 100644 --- a/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java +++ b/src/test/java/gov/usgs/earthquake/origin/OriginIndexerModuleTest.java @@ -172,9 +172,9 @@ public void azimuthToDirectionTest() { protected class DummyPlacesService extends GeoservePlacesService { @Override - public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, BigInteger maxradiuskm) + public JsonObject getNearestPlace(BigDecimal latitude, BigDecimal longitude, int maxradiuskm) throws IOException, MalformedURLException { - if (distance <= maxradiuskm.doubleValue()) { + if (distance <= maxradiuskm) { return Json.createObjectBuilder().add("properties", Json.createObjectBuilder() .add("admin1_name", "admin1_name") From c267f1b1c3480eead8da4a7ee0b1ff508900ee58 Mon Sep 17 00:00:00 2001 From: Eric Martinez Date: Fri, 16 Sep 2022 11:26:04 -0600 Subject: [PATCH 10/12] Version bump --- code.json | 4 ++-- .../java/gov/usgs/earthquake/distribution/ProductClient.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/code.json b/code.json index 6d1e9644..e455fd80 100644 --- a/code.json +++ b/code.json @@ -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", @@ -27,7 +27,7 @@ "email": "jmfee@usgs.gov" }, "date": { - "metadataLastUpdated": "2021-12-16" + "metadataLastUpdated": "2022-09-16" } } ] diff --git a/src/main/java/gov/usgs/earthquake/distribution/ProductClient.java b/src/main/java/gov/usgs/earthquake/distribution/ProductClient.java index 43be1bae..e2763f09 100644 --- a/src/main/java/gov/usgs/earthquake/distribution/ProductClient.java +++ b/src/main/java/gov/usgs/earthquake/distribution/ProductClient.java @@ -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"; From 88841e1c1f2722a37c4020326a9ab7f4631b681f Mon Sep 17 00:00:00 2001 From: Eric Martinez Date: Fri, 16 Sep 2022 11:56:45 -0600 Subject: [PATCH 11/12] Adding CLI to quick-check event names functionality --- .../origin/OriginIndexerModule.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java b/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java index 3d3a7dcc..10522eb7 100644 --- a/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java +++ b/src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java @@ -2,8 +2,6 @@ import java.io.IOException; import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.ArrayList; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -338,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)); + } } From 9845a4c90e9e3c3f4a771a104a7507b6fca23e9d Mon Sep 17 00:00:00 2001 From: Jeremy Fee Date: Fri, 16 Sep 2022 20:31:11 -0600 Subject: [PATCH 12/12] Fix gitlab pipeline, gradle build for java 8, and remove travis. --- .gitlab-ci.yml | 4 +- .travis.yml | 42 ------------------- build.gradle | 24 +---------- .../geoserve/GeoservePlacesService.java | 1 - 4 files changed, 5 insertions(+), 66 deletions(-) delete mode 100644 .travis.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c3795fcb..33ba8186 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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: diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index e95c59c0..00000000 --- a/.travis.yml +++ /dev/null @@ -1,42 +0,0 @@ -language: java -# local ssh seems to require sudo -sudo: required - -jdk: - - openjdk8 - - openjdk11 - # - oraclejdk8 - not available in travis anymore - - oraclejdk11 - -# gradle caching -before_cache: - - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock - - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ -cache: - directories: - - "$HOME/.gradle/caches/" - - "$HOME/.gradle/wrapper/" - -# tests depend on local ssh -install: - - ssh-keygen -f ~/.ssh/id_rsa -t rsa -N '' -b 4096 - - cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys - - chmod 700 ~/.ssh/authorized_keys -addons: - ssh_known_hosts: - - 127.0.0.1 - - localhost - -script: - - ./gradlew --info build - # use --scan to post output online - # - ./gradlew build --scan - -# upload coverage -after_success: - - bash <(curl -s https://codecov.io/bash) - - "./gradlew sendCoverageToCodacy" - -env: - # CODACY_PROJECT_TOKEN - secure: PO6Zdc7e/x021brgk55kG1zTUPSNnvQ8BKNDJJK8cZJ4dgLgzbvxW5eoNPSn2CW5/C5YqkLgUVPkVy15zUVNpdM/RXYgzPiFTyg2GFXLQL9DbfQOz2zD8/iSySxTjUGfd7VNJZPG8XlFmzUIvY+4Eykj4UBsTFknZmro1kC/PS5E/bTtOrYGUKg9mfQz3t4/Tx7n8mw5CO5IVaHvklpbUTgaW0d8Et0qxQZ/Ks2xnlEP/4OcmSLMDeeyeYKVJc9KajgU27r1qtSoWtqL5InyW+L/8MU0soFDPO71KCphB0dbLJ6sdkzCWPIWfA/zeVlf+54K9zveXdbtdJs1Btzw73sTruoq2z/YTUZVIUdLG5yenMLPX0ayWpWc7AT4dDkSNXrwy+wWvjcF7mMddffvvGH//38FtkQ8fvm2o2tq/PIua2mQb8otsS7IKhOgmDFV+C2ygsMtTq/Wn7ny/0Yb+vUswNZUEZiK5IMEGF73OMIriDFKIvc4tJnJ2xKw2SY5cnratxeWzpsPH3IRIIG+bIfAGvN+g6/f97+xemR5YVO9FuZKR8IEBPl0jM+s8G2T1PpSG9LsfDPYSLIYC95GUEi9dFcFX7FP5y3a0rxyCyCcu+YoSBQXTsb/mCcb+IKoMugSh88hdUhqbDWbHA252ryBCmW7lTQI8C4S3B0NKBA= diff --git a/build.gradle b/build.gradle index 1f1feaab..8ce487a1 100644 --- a/build.gradle +++ b/build.gradle @@ -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" } @@ -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 @@ -189,9 +187,6 @@ gitPublish { } } - -// Tasks for TravisCI - // convert jacoco to cobertura import com.kageiit.jacobo.JacoboTask tasks.create("jacobo", JacoboTask, { @@ -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" - ] -} diff --git a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java index 572789ee..814a374e 100644 --- a/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java +++ b/src/main/java/gov/usgs/earthquake/geoserve/GeoservePlacesService.java @@ -3,7 +3,6 @@ import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; -import java.math.BigInteger; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder;