-
Notifications
You must be signed in to change notification settings - Fork 19
Feature/rate function #1082
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
Merged
+2,460
−47
Merged
Feature/rate function #1082
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
21213d4
add rate APIs
adamkorynta 23d11c8
add rate function endpoint
adamkorynta 4f5be59
split rate ts/values since javalin swagger annotations don't support …
adamkorynta 0547bde
updates to OpenAPI descriptions
adamkorynta 1d926cb
add DTO unit tests
adamkorynta 0685fc1
add ratedoutput unit tests
adamkorynta a33a2be
add 401 error code info
adamkorynta b0d4ce8
add in integration testing for rate endpoints
adamkorynta a208011
remove incorrect dev logging
adamkorynta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
34 changes: 34 additions & 0 deletions
34
cwms-data-api/src/main/java/cwms/cda/api/errors/RateException.java
This file contains hidden or 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,34 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2025 Hydrologic Engineering Center | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package cwms.cda.api.errors; | ||
|
||
import java.sql.SQLException; | ||
|
||
public final class RateException extends RuntimeException { | ||
|
||
public RateException(String message, SQLException cause) { | ||
super(message, cause); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
cwms-data-api/src/main/java/cwms/cda/api/rating/RateTimeSeriesController.java
This file contains hidden or 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,115 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2025 Hydrologic Engineering Center | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package cwms.cda.api.rating; | ||
|
||
import static cwms.cda.api.Controllers.OFFICE; | ||
import static cwms.cda.api.Controllers.RATING_ID; | ||
import static cwms.cda.api.Controllers.STATUS_200; | ||
import static cwms.cda.api.Controllers.STATUS_400; | ||
import static cwms.cda.api.Controllers.STATUS_401; | ||
import static cwms.cda.api.Controllers.STATUS_404; | ||
import static cwms.cda.api.Controllers.STATUS_501; | ||
import static cwms.cda.data.dao.JooqDao.getDslContext; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.Timer; | ||
import cwms.cda.api.BaseHandler; | ||
import cwms.cda.data.dao.RateDao; | ||
import cwms.cda.data.dto.rating.RateInputTimeSeries; | ||
import cwms.cda.data.dto.rating.RatedOutput; | ||
import cwms.cda.data.dto.rating.RatedOutputTimeSeries; | ||
import cwms.cda.formatters.ContentType; | ||
import cwms.cda.formatters.Formats; | ||
import io.javalin.core.util.Header; | ||
import io.javalin.http.Context; | ||
import io.javalin.plugin.openapi.annotations.HttpMethod; | ||
import io.javalin.plugin.openapi.annotations.OpenApi; | ||
import io.javalin.plugin.openapi.annotations.OpenApiContent; | ||
import io.javalin.plugin.openapi.annotations.OpenApiParam; | ||
import io.javalin.plugin.openapi.annotations.OpenApiRequestBody; | ||
import io.javalin.plugin.openapi.annotations.OpenApiResponse; | ||
import io.javalin.plugin.openapi.annotations.OpenApiSecurity; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jooq.DSLContext; | ||
|
||
public final class RateTimeSeriesController extends BaseHandler { | ||
|
||
static final String TAG = "Ratings"; | ||
private static final String RATE = "Rate"; | ||
|
||
public RateTimeSeriesController(MetricRegistry metrics) { | ||
super(metrics); | ||
} | ||
|
||
@OpenApi( | ||
pathParams = { | ||
@OpenApiParam(name = OFFICE, description = "Office owning the rating"), | ||
@OpenApiParam(name = RATING_ID, description = "Rating Specification identifier"), | ||
}, | ||
requestBody = @OpenApiRequestBody(content = { | ||
@OpenApiContent(type = Formats.JSON, from = RateInputTimeSeries.class), | ||
@OpenApiContent(type = Formats.JSONV1, from = RateInputTimeSeries.class) | ||
}, required = true), | ||
responses = { | ||
@OpenApiResponse(status = STATUS_200, content = { | ||
@OpenApiContent(type = Formats.JSON, from = RatedOutputTimeSeries.class), | ||
@OpenApiContent(type = Formats.JSONV1, from = RatedOutputTimeSeries.class) | ||
}), | ||
@OpenApiResponse(status = STATUS_400, description = "Invalid input parameters."), | ||
@OpenApiResponse(status = STATUS_401, description = "Client is not authorized."), | ||
@OpenApiResponse(status = STATUS_404, description = "The rating curve was not found."), | ||
@OpenApiResponse(status = STATUS_501, description = "Requested format is not implemented") | ||
}, | ||
security = { | ||
@OpenApiSecurity(name = "gets overridden allows lock icon.") | ||
}, | ||
description = "Rates input values using CWMS ratings. The input format `RateInputTimeSeries` DTO " + | ||
"supports an array of CWMS time series ids, " + | ||
"each corresponding to an independent parameter in the rating curve." + | ||
"The output format `RatedOutputTimeSeries` will contain a singular double array corresponding to the " + | ||
"dependent parameter of the rating curve. ", | ||
method = HttpMethod.POST, | ||
tags = {TAG} | ||
) | ||
@Override | ||
public void handle(@NotNull Context ctx) throws Exception { | ||
try (final Timer.Context ignored = markAndTime(RATE)) { | ||
DSLContext dsl = getDslContext(ctx); | ||
RateDao ratingDao = new RateDao(dsl); | ||
String office = ctx.pathParam(OFFICE); | ||
String ratingId = ctx.pathParam(RATING_ID); | ||
String contentTypeHeader = ctx.req.getContentType(); | ||
String body = ctx.body(); | ||
ContentType contentType = Formats.parseHeader(contentTypeHeader, RateInputTimeSeries.class); | ||
RateInputTimeSeries input = Formats.parseContent(contentType, body, RateInputTimeSeries.class); | ||
RatedOutput output = ratingDao.rate(office, ratingId, input); | ||
String acceptFormatHeader = ctx.header(Header.ACCEPT); | ||
ContentType acceptContentType = Formats.parseHeader(acceptFormatHeader, RatedOutputTimeSeries.class); | ||
String result = Formats.format(acceptContentType, output); | ||
ctx.status(HttpServletResponse.SC_OK).result(result); | ||
} | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
cwms-data-api/src/main/java/cwms/cda/api/rating/RateValuesController.java
This file contains hidden or 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,115 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2025 Hydrologic Engineering Center | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package cwms.cda.api.rating; | ||
|
||
import static cwms.cda.api.Controllers.OFFICE; | ||
import static cwms.cda.api.Controllers.RATING_ID; | ||
import static cwms.cda.api.Controllers.STATUS_200; | ||
import static cwms.cda.api.Controllers.STATUS_400; | ||
import static cwms.cda.api.Controllers.STATUS_401; | ||
import static cwms.cda.api.Controllers.STATUS_404; | ||
import static cwms.cda.api.Controllers.STATUS_501; | ||
import static cwms.cda.data.dao.JooqDao.getDslContext; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.Timer; | ||
import cwms.cda.api.BaseHandler; | ||
import cwms.cda.data.dao.RateDao; | ||
import cwms.cda.data.dto.rating.RateInputValues; | ||
import cwms.cda.data.dto.rating.RatedOutput; | ||
import cwms.cda.data.dto.rating.RatedOutputValues; | ||
import cwms.cda.formatters.ContentType; | ||
import cwms.cda.formatters.Formats; | ||
import io.javalin.core.util.Header; | ||
import io.javalin.http.Context; | ||
import io.javalin.plugin.openapi.annotations.HttpMethod; | ||
import io.javalin.plugin.openapi.annotations.OpenApi; | ||
import io.javalin.plugin.openapi.annotations.OpenApiContent; | ||
import io.javalin.plugin.openapi.annotations.OpenApiParam; | ||
import io.javalin.plugin.openapi.annotations.OpenApiRequestBody; | ||
import io.javalin.plugin.openapi.annotations.OpenApiResponse; | ||
import io.javalin.plugin.openapi.annotations.OpenApiSecurity; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jooq.DSLContext; | ||
|
||
public final class RateValuesController extends BaseHandler { | ||
|
||
static final String TAG = "Ratings"; | ||
private static final String RATE = "Rate"; | ||
|
||
public RateValuesController(MetricRegistry metrics) { | ||
super(metrics); | ||
} | ||
|
||
@OpenApi( | ||
pathParams = { | ||
@OpenApiParam(name = OFFICE, description = "Office owning the rating"), | ||
@OpenApiParam(name = RATING_ID, description = "Rating Specification identifier"), | ||
}, | ||
requestBody = @OpenApiRequestBody(content = { | ||
@OpenApiContent(type = Formats.JSON, from = RateInputValues.class), | ||
@OpenApiContent(type = Formats.JSONV1, from = RateInputValues.class) | ||
}, required = true), | ||
responses = { | ||
@OpenApiResponse(status = STATUS_200, content = { | ||
@OpenApiContent(type = Formats.JSON, from = RatedOutputValues.class), | ||
@OpenApiContent(type = Formats.JSONV1, from = RatedOutputValues.class) | ||
}), | ||
@OpenApiResponse(status = STATUS_400, description = "Invalid input parameters."), | ||
@OpenApiResponse(status = STATUS_401, description = "Client is not authorized."), | ||
@OpenApiResponse(status = STATUS_404, description = "The rating curve was not found."), | ||
@OpenApiResponse(status = STATUS_501, description = "Requested format is not implemented") | ||
}, | ||
security = { | ||
@OpenApiSecurity(name = "gets overridden allows lock icon.") | ||
}, | ||
description = "Rates input values using CWMS ratings. The input format `RatingInputValues` includes " + | ||
"a two dimensional array with each dimension corresponding to an independent parameter " + | ||
"in the rating curve. " + | ||
"The output format `RatedOutputTimeSeries` will contain a singular double array corresponding to the " + | ||
"dependent parameter of the rating curve. ", | ||
method = HttpMethod.POST, | ||
tags = {TAG} | ||
) | ||
@Override | ||
public void handle(@NotNull Context ctx) throws Exception { | ||
try (final Timer.Context ignored = markAndTime(RATE)) { | ||
DSLContext dsl = getDslContext(ctx); | ||
RateDao ratingDao = new RateDao(dsl); | ||
String office = ctx.pathParam(OFFICE); | ||
String ratingId = ctx.pathParam(RATING_ID); | ||
String contentTypeHeader = ctx.req.getContentType(); | ||
String body = ctx.body(); | ||
ContentType contentType = Formats.parseHeader(contentTypeHeader, RateInputValues.class); | ||
RateInputValues input = Formats.parseContent(contentType, body, RateInputValues.class); | ||
RatedOutput output = ratingDao.rate(office, ratingId, input); | ||
String acceptFormatHeader = ctx.header(Header.ACCEPT); | ||
ContentType acceptContentType = Formats.parseHeader(acceptFormatHeader, RatedOutputValues.class); | ||
String result = Formats.format(acceptContentType, output); | ||
ctx.status(HttpServletResponse.SC_OK).result(result); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There is no parent class that RateException can extend? Otherwise what keeps ApiServlet from filling up with implementation specific exception types? I would think that if we really can't think of anything more generic at the very least we should be able to make a class for Controller exceptions and another for DaoExceptions depending on where the issue is. I don't see ApiServlet knowing how to handle RateException in any helpful or meaningfully different way than other exceptions.
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.
Though I didn't see it before. I completely agree with Ryan on this one.
Though based on the suggestion of DAO vs Controller exception, would it be better to accept for now and create a follow up issue to review the exception handling.
This particular block has already been getting a bit large and it's possible that just a few marker interfaces may help sort that out.
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.
I agree, I'd like to move that refactor into a subsequent effort as my ideas for implementing something standard are either insufficient or too large of a change for this PR.