Skip to content

Commit

Permalink
Merge branch 'develop' into feature/cto-137-project-sub-location
Browse files Browse the repository at this point in the history
  • Loading branch information
rma-rripken authored Jul 24, 2024
2 parents 02467b1 + 9b60d14 commit 0bb9863
Show file tree
Hide file tree
Showing 65 changed files with 1,853 additions and 670 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ RUN apk add --no-cache bash

RUN mkdir /download && \
cd /download && \
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.90/bin/apache-tomcat-9.0.90.tar.gz && \
echo "e77b47d7ded86da81018d38c4f728f5f804c1a65bb941a138a7989b69c859031e88d113ccf4fc3a409062ee24511fa5ccf15dfad333f570838ee2a36dae23e19 *apache-tomcat-9.0.90.tar.gz" > checksum.txt && \
wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.91/bin/apache-tomcat-9.0.91.tar.gz && \
echo "b22054c9141782232a693765d23d944f0f50774af17dd8968331e020b425e71459b5877a7ba8c2121246a5ce47e6b6a31c3f4215ef133e942da45b49cb534948 *apache-tomcat-9.0.91.tar.gz" > checksum.txt && \
sha512sum -c checksum.txt && \
tar xzf apache-tomcat-*tar.gz && \
mv apache-tomcat-9.0.90 /usr/local/tomcat/ && \
mv apache-tomcat-9.0.91 /usr/local/tomcat/ && \
cd / && \
rm -rf /download
CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
Expand Down
11 changes: 11 additions & 0 deletions cwms-data-api/src/main/java/cwms/cda/ApiServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import static cwms.cda.api.Controllers.NAME;
import cwms.cda.api.DownstreamLocationsGetController;
import cwms.cda.api.location.kind.VirtualOutletController;
import cwms.cda.api.LookupTypeController;
import cwms.cda.api.StreamController;
import cwms.cda.api.StreamLocationController;
Expand Down Expand Up @@ -64,6 +65,7 @@
import cwms.cda.api.LocationController;
import cwms.cda.api.LocationGroupController;
import cwms.cda.api.OfficeController;
import cwms.cda.api.location.kind.OutletController;
import cwms.cda.api.ParametersController;
import cwms.cda.api.PoolController;
import cwms.cda.api.ProjectController;
Expand Down Expand Up @@ -99,6 +101,7 @@
import cwms.cda.api.errors.NotFoundException;
import cwms.cda.api.errors.RequiredQueryParameterException;
import cwms.cda.api.project.ProjectChildLocationHandler;
import cwms.cda.api.location.kind.VirtualOutletCreateController;
import cwms.cda.data.dao.JooqDao;
import cwms.cda.formatters.Formats;
import cwms.cda.formatters.FormattingException;
Expand Down Expand Up @@ -509,6 +512,14 @@ protected void configureRoutes() {
post(turbineChanges, new TurbineChangesPostController(metrics), requiredRoles);
delete(turbineChanges, new TurbineChangesDeleteController(metrics), requiredRoles);

String outletPath = format("/projects/outlets/{%s}", NAME);
String virtualOutletPath = format("/projects/{%s}/{%s}/virtual-outlets/{%s}", Controllers.OFFICE,
Controllers.PROJECT_ID, NAME);
String virtualOutletCreatePath = "/projects/virtual-outlets";
cdaCrudCache(outletPath, new OutletController(metrics), requiredRoles, 1, TimeUnit.DAYS);
cdaCrudCache(virtualOutletPath, new VirtualOutletController(metrics), requiredRoles, 1, TimeUnit.DAYS);
post(virtualOutletCreatePath, new VirtualOutletCreateController(metrics));

get("/projects/child-locations/", new ProjectChildLocationHandler(metrics));
cdaCrudCache(format("/projects/{%s}", Controllers.NAME),
new ProjectController(metrics), requiredRoles,5, TimeUnit.MINUTES);
Expand Down
24 changes: 24 additions & 0 deletions cwms-data-api/src/main/java/cwms/cda/api/Controllers.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
import io.javalin.http.Context;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.jetbrains.annotations.Nullable;

public final class Controllers {
Expand Down Expand Up @@ -212,6 +215,27 @@ public static Timer.Context markAndTime(MetricRegistry registry, String classNam
return timer.time();
}

/**
* Returns the first matching query param or the provided default value if no match is found.
*
* @param ctx Request Context
* @param name Name of the query param
* @param aliases Alternative names for the query parameter that could be coming in
* @param clazz Return value type.
* @param defaultValue Value to return if no matching queryParam is found.
* @return value
*/
public static <T> T queryParamAsClass(io.javalin.http.Context ctx,
Class<T> clazz, T defaultValue, String name, String ... aliases) {
List<String> items = new ArrayList<>();
items.add(name);
if (aliases != null) {
items.addAll(Arrays.asList(aliases));
}

return queryParamAsClass(ctx, items.toArray(new String[]{}), clazz, defaultValue);
}

/**
* Returns the first matching query param or the provided default value if no match is found.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import static cwms.cda.api.Controllers.STATUS_200;
import static cwms.cda.api.Controllers.STATUS_204;
import static cwms.cda.api.Controllers.STATUS_404;
import static cwms.cda.api.Controllers.STREAM_ID_MASK;
import static cwms.cda.api.Controllers.UPDATE;
import static cwms.cda.api.Controllers.requiredParam;
import cwms.cda.data.dao.DeleteRule;
Expand Down Expand Up @@ -91,6 +92,8 @@ private Timer.Context markAndTime(String subject) {
queryParams = {
@OpenApiParam(name = OFFICE_MASK, description = "Office id for the reservoir project location " +
"associated with the streams."),
@OpenApiParam(name = STREAM_ID_MASK, description = "Specifies the stream-id of the stream to be " +
"retrieved."),
@OpenApiParam(name = DIVERTS_FROM_STREAM_ID_MASK, description = "Specifies the stream-id of the " +
"stream that the returned streams flow from."),
@OpenApiParam(name = FLOWS_INTO_STREAM_ID_MASK, description = "Specifies the stream-id of the " +
Expand All @@ -110,13 +113,14 @@ private Timer.Context markAndTime(String subject) {
@Override
public void getAll(@NotNull Context ctx) {
String office = ctx.queryParam(OFFICE_MASK);
String streamId = ctx.queryParam(STREAM_ID_MASK);
String divertsFromStream = ctx.queryParam(DIVERTS_FROM_STREAM_ID_MASK);
String flowsIntoStream = ctx.queryParam(FLOWS_INTO_STREAM_ID_MASK);
try (Timer.Context ignored = markAndTime(GET_ALL)) {
DSLContext dsl = getDslContext(ctx);
StreamDao dao = new StreamDao(dsl);
String stationUnits = ctx.queryParamAsClass(STATION_UNIT, String.class).getOrDefault("mi");
List<Stream> streams = dao.retrieveStreams(office, divertsFromStream, flowsIntoStream, stationUnits);
List<Stream> streams = dao.retrieveStreams(office, streamId, divertsFromStream, flowsIntoStream, stationUnits);
String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) :
Formats.JSONV1;
ContentType contentType = Formats.parseHeader(formatHeader, Stream.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import cwms.cda.data.dao.TimeZoneDao;
import cwms.cda.data.dto.TimeZone;
import cwms.cda.data.dto.TimeZones;
import cwms.cda.data.dto.TimeZoneId;
import cwms.cda.data.dto.TimeZoneIds;
import cwms.cda.formatters.ContentType;
import cwms.cda.formatters.Formats;
import io.javalin.apibuilder.CrudHandler;
Expand All @@ -28,7 +28,6 @@
import io.javalin.plugin.openapi.annotations.OpenApiParam;
import io.javalin.plugin.openapi.annotations.OpenApiResponse;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -89,7 +88,7 @@ public void getAll(Context ctx) {
String format = ctx.queryParamAsClass(FORMAT, String.class).getOrDefault("");
String header = ctx.header(ACCEPT);

ContentType contentType = Formats.parseHeaderAndQueryParm(header, format, TimeZone.class);
ContentType contentType = Formats.parseHeaderAndQueryParm(header, format, TimeZoneId.class);
String version = contentType.getParameters()
.getOrDefault(VERSION, "");

Expand All @@ -98,7 +97,7 @@ public void getAll(Context ctx) {
String results;
if (format.isEmpty() && !isLegacyVersion)
{
TimeZones zones = dao.getTimeZones();
TimeZoneIds zones = dao.getTimeZones();
results = Formats.format(contentType, zones);
ctx.contentType(contentType.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import com.codahale.metrics.Timer;
import cwms.cda.api.errors.CdaError;
import cwms.cda.data.dao.UnitsDao;
import cwms.cda.data.dto.TimeZone;
import cwms.cda.data.dto.TimeZones;
import cwms.cda.data.dto.Unit;
import cwms.cda.formatters.ContentType;
import cwms.cda.formatters.Formats;
Expand Down Expand Up @@ -90,7 +88,7 @@ public void getAll(Context ctx) {
String format = ctx.queryParamAsClass(FORMAT, String.class).getOrDefault("");
String header = ctx.header(ACCEPT);

ContentType contentType = Formats.parseHeaderAndQueryParm(header, format, TimeZone.class);
ContentType contentType = Formats.parseHeaderAndQueryParm(header, format, Unit.class);
String version = contentType.getParameters()
.getOrDefault(VERSION, "");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* MIT License
* Copyright (c) 2024 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.location.kind;

import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import cwms.cda.api.Controllers;
import io.javalin.http.Handler;
import static com.codahale.metrics.MetricRegistry.name;
import static cwms.cda.api.Controllers.RESULTS;
import static cwms.cda.api.Controllers.SIZE;

abstract class BaseOutletHandler implements Handler {

private final MetricRegistry metrics;
private final Histogram requestResultSize;

BaseOutletHandler(MetricRegistry metrics) {
this.metrics = metrics;
String className = this.getClass().getName();

requestResultSize = this.metrics.histogram(name(className, RESULTS, SIZE));
}

protected final Timer.Context markAndTime(String subject) {
return Controllers.markAndTime(metrics, getClass().getName(), subject);
}

protected final void addToHistogram(int value) {
requestResultSize.update(value);
}
}
Loading

0 comments on commit 0bb9863

Please sign in to comment.