-
Notifications
You must be signed in to change notification settings - Fork 14
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
Issue 693 - Rating curve retrieval #909
base: develop
Are you sure you want to change the base?
Changes from 4 commits
142fc4f
1f3a956
6d92679
e300f64
412716e
7b0124e
b64a83a
edc5efb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,17 +29,20 @@ | |
import com.fasterxml.jackson.dataformat.xml.XmlMapper; | ||
import hec.data.RatingException; | ||
import hec.data.cwmsRating.RatingSet; | ||
import java.io.IOException; | ||
import java.sql.Connection; | ||
import java.sql.Timestamp; | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.util.List; | ||
import mil.army.usace.hec.cwms.rating.io.jdbc.ConnectionProvider; | ||
import mil.army.usace.hec.cwms.rating.io.jdbc.RatingJdbcFactory; | ||
import org.jooq.DSLContext; | ||
import org.jooq.Result; | ||
import org.jooq.exception.DataAccessException; | ||
import usace.cwms.db.jooq.codegen.packages.CWMS_RATING_PACKAGE; | ||
import usace.cwms.db.jooq.codegen.tables.AV_RATING_LOCAL; | ||
|
||
import java.io.IOException; | ||
import java.sql.Connection; | ||
import java.sql.Timestamp; | ||
import java.time.Instant; | ||
import java.util.List; | ||
|
||
public class RatingSetDao extends JooqDao<RatingSet> implements RatingDao { | ||
|
||
|
@@ -57,8 +60,7 @@ public void create(String ratingSetXml, boolean storeTemplate) throws IOExceptio | |
DSLContext context = getDslContext(c, office); | ||
String errs = CWMS_RATING_PACKAGE.call_STORE_RATINGS_XML__5(context.configuration(), | ||
ratingSetXml, "T", storeTemplate ? "T" : "F"); | ||
if (errs != null && !errs.isEmpty()) | ||
{ | ||
if (errs != null && !errs.isEmpty()) { | ||
throw new DataAccessException(errs); | ||
} | ||
}); | ||
|
@@ -85,8 +87,9 @@ private static String extractOfficeId(String ratingSet) throws JsonProcessingExc | |
|
||
@Override | ||
public RatingSet retrieve(RatingSet.DatabaseLoadMethod method, String officeId, | ||
String specificationId, Instant startZdt, Instant endZdt | ||
String specificationId, Instant startZdt, Instant endZdt, Instant effectiveDateParam | ||
) throws IOException, RatingException { | ||
AV_RATING_LOCAL view = AV_RATING_LOCAL.AV_RATING_LOCAL; | ||
|
||
final RatingSet[] retval = new RatingSet[1]; | ||
try { | ||
|
@@ -110,9 +113,43 @@ public RatingSet retrieve(RatingSet.DatabaseLoadMethod method, String officeId, | |
|
||
RatingSet.DatabaseLoadMethod finalMethod = method; | ||
|
||
connection(dsl, c -> retval[0] = | ||
RatingJdbcFactory.ratingSet(finalMethod, new RatingConnectionProvider(c), officeId, | ||
specificationId, start, end, false)); | ||
if (effectiveDateParam != null) { | ||
|
||
String ratingId = null; | ||
Long difference = null; | ||
Instant startInstant = null; | ||
Instant endInstant = null; | ||
Result<?> results = connectionResult(dsl, c -> | ||
dsl.select(view.RATING_ID, view.EFFECTIVE_DATE, view.RATING_CODE).from(view) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be possible to have this query do the limit rather than pulling all records and do tinghe limit in the CDA code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rewrote the query and removed the difference logic from the DAO |
||
.where(view.OFFICE_ID.eq(officeId) | ||
.and(view.RATING_ID.eq(specificationId)) | ||
).fetch() | ||
); | ||
for (int i = 0; i < results.size(); i++) { | ||
Timestamp effectiveDate = results.getValue(i, view.EFFECTIVE_DATE); | ||
long effectiveDifference = Math.abs(effectiveDate.toInstant().toEpochMilli() - effectiveDateParam.toEpochMilli()); | ||
if (difference == null || effectiveDifference < difference) { | ||
difference = effectiveDifference; | ||
ratingId = results.getValue(i, view.RATING_ID); | ||
startInstant = effectiveDate.toInstant().atZone(ZoneId.of("UTC")).toInstant(); | ||
endInstant = effectiveDate.toInstant().atZone(ZoneId.of("UTC")).toInstant(); | ||
} | ||
} | ||
if (ratingId == null) { | ||
return null; | ||
} | ||
final Instant finalStartInstant = startInstant; | ||
final String ratingIdFinal = ratingId; | ||
final Instant finalEndInstant = endInstant; | ||
connection(dsl, c -> retval[0] = | ||
RatingJdbcFactory.ratingSet(finalMethod, new RatingConnectionProvider(c), officeId, | ||
ratingIdFinal, finalStartInstant.toEpochMilli(), | ||
finalEndInstant.toEpochMilli(), false)); | ||
} else { | ||
connection(dsl, c -> retval[0] = | ||
RatingJdbcFactory.ratingSet(finalMethod, new RatingConnectionProvider(c), officeId, | ||
specificationId, start, end, false)); | ||
} | ||
|
||
} catch (DataAccessException ex) { | ||
Throwable cause = ex.getCause(); | ||
|
@@ -151,7 +188,7 @@ public void store(String ratingSetXml, boolean includeTemplate) throws IOExcepti | |
public void delete(String officeId, String specificationId, Instant start, Instant end) { | ||
Timestamp startDate = new Timestamp(start.toEpochMilli()); | ||
Timestamp endDate = new Timestamp(end.toEpochMilli()); | ||
dsl.connection(c-> | ||
dsl.connection(c -> | ||
CWMS_RATING_PACKAGE.call_DELETE_RATINGS( | ||
getDslContext(c,officeId).configuration(), specificationId, startDate, | ||
endDate, "UTC", officeId | ||
|
@@ -170,15 +207,15 @@ public String retrieveRatings(String format, String names, String unit, String d | |
} | ||
|
||
private static final class RatingConnectionProvider implements ConnectionProvider { | ||
private final Connection c; | ||
private final Connection conn; | ||
|
||
private RatingConnectionProvider(Connection c) { | ||
this.c = c; | ||
private RatingConnectionProvider(Connection conn) { | ||
this.conn = conn; | ||
} | ||
|
||
@Override | ||
public Connection getConnection() { | ||
return c; | ||
return conn; | ||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very confusing parameter.
Additionally, you are passing this confusion into the DAO by expanding the existing DAO method instead of creating a new one, so now you have three dates in the DAO function. This makes test cases far more complicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved functionality to its own endpoint to reduce confusion. New endpoint returns closest effective date rating where the date is not beyond the current date.