use of cwms.radar.data.dao.TimeSeriesDao in project cwms-radar-api by USACE.
the class TimeSeriesController method getAll.
@OpenApi(queryParams = { @OpenApiParam(name = "name", required = true, description = "Specifies the name(s) of the time series whose data is to be included in the response. A case insensitive comparison is used to match names."), @OpenApiParam(name = "office", required = false, description = "Specifies the owning office of the time series(s) whose data is to be included in the response. If this field is not specified, matching location level information from all offices shall be returned."), @OpenApiParam(name = "unit", required = false, description = "Specifies the unit or unit system of the response. Valid values for the unit field are:\r\n 1. EN. (default) Specifies English unit system. Location level values will be in the default English units for their parameters.\r\n2. SI. Specifies the SI unit system. Location level values will be in the default SI units for their parameters.\r\n3. Other. Any unit returned in the response to the units URI request that is appropriate for the requested parameters."), @OpenApiParam(name = "datum", required = false, description = "Specifies the elevation datum of the response. This field affects only elevation location levels. Valid values for this field are:\r\n1. NAVD88. The elevation values will in the specified or default units above the NAVD-88 datum.\r\n2. NGVD29. The elevation values will be in the specified or default units above the NGVD-29 datum."), @OpenApiParam(name = "begin", required = false, description = "Specifies the start of the time window for data to be included in the response. If this field is not specified, any required time window begins 24 hours prior to the specified or default end time. The format for this field is ISO 8601 extended, with optional offset and timezone, i.e., 'YYYY-MM-dd'T'hh:mm:ss[Z'['VV']']', e.g., '2021-06-10T13:00:00-0700[PST8PDT]'."), @OpenApiParam(name = "end", required = false, description = "Specifies the end of the time window for data to be included in the response. If this field is not specified, any required time window ends at the current time. The format for this field is ISO 8601 extended, with optional timezone, i.e., 'YYYY-MM-dd'T'hh:mm:ss[Z'['VV']']', e.g., '2021-06-10T13:00:00-0700[PST8PDT]'."), @OpenApiParam(name = "timezone", required = false, description = "Specifies the time zone of the values of the begin and end fields (unless otherwise specified), as well as the time zone of any times in the response. If this field is not specified, the default time zone of UTC shall be used.\r\nIgnored if begin was specified with offset and timezone."), @OpenApiParam(name = "format", required = false, description = "Specifies the encoding format of the response. Valid values for the format field for this URI are:\r\n1. tab\r\n2. csv\r\n3. xml\r\n4. wml2 (only if name field is specified)\r\n5. json (default)"), @OpenApiParam(name = "page", required = false, description = "This end point can return a lot of data, this identifies where in the request you are. This is an opaque value, and can be obtained from the 'next-page' value in the response."), @OpenApiParam(name = "pageSize", required = false, type = Integer.class, description = "How many entries per page returned. Default " + defaultPageSize + ".") }, responses = { @OpenApiResponse(status = "200", description = "A list of elements of the data set you've selected.", content = { @OpenApiContent(from = TimeSeries.class, type = Formats.JSONV2), @OpenApiContent(from = TimeSeries.class, type = Formats.XML) }), @OpenApiResponse(status = "400", description = "Invalid parameter combination"), @OpenApiResponse(status = "404", description = "The provided combination of parameters did not find a timeseries."), @OpenApiResponse(status = "501", description = "Requested format is not implemented") }, method = HttpMethod.GET, tags = { "TimeSeries" })
@Override
public void getAll(Context ctx) {
getAllRequests.mark();
try (final Timer.Context timeContext = getAllRequestsTime.time();
DSLContext dsl = getDslContext(ctx)) {
TimeSeriesDao dao = getTimeSeriesDao(dsl);
String format = ctx.queryParamAsClass("format", String.class).getOrDefault("");
String names = ctx.queryParam("name");
String office = ctx.queryParam("office");
String unit = ctx.queryParamAsClass("unit", String.class).getOrDefault(UnitSystem.EN.getValue());
String datum = ctx.queryParam("datum");
String begin = ctx.queryParam("begin");
String end = ctx.queryParam("end");
String timezone = ctx.queryParamAsClass("timezone", String.class).getOrDefault("UTC");
// The following parameters are only used for jsonv2 and xmlv2
String cursor = ctx.queryParamAsClass("cursor", String.class).getOrDefault(ctx.queryParamAsClass("page", String.class).getOrDefault(""));
int pageSize = ctx.queryParamAsClass("pageSize", Integer.class).getOrDefault(ctx.queryParamAsClass("pagesize", Integer.class).getOrDefault(defaultPageSize));
String acceptHeader = ctx.header(Header.ACCEPT);
ContentType contentType = Formats.parseHeaderAndQueryParm(acceptHeader, format);
String results;
String version = contentType.getParameters().get("version");
ZoneId tz = ZoneId.of(timezone, ZoneId.SHORT_IDS);
begin = begin != null ? begin : "PT-24H";
ZonedDateTime beginZdt = DateUtils.parseUserDate(begin, timezone);
ZonedDateTime endZdt = end != null ? DateUtils.parseUserDate(end, timezone) : ZonedDateTime.now(tz);
if (version != null && version.equals("2")) {
TimeSeries ts = dao.getTimeseries(cursor, pageSize, names, office, unit, datum, beginZdt, endZdt, tz);
results = Formats.format(contentType, ts);
ctx.status(HttpServletResponse.SC_OK);
// Send back the link to the next page in the response header
StringBuffer linkValue = new StringBuffer(600);
linkValue.append(String.format("<%s>; rel=self; type=\"%s\"", buildRequestUrl(ctx, ts, ts.getPage()), contentType));
if (ts.getNextPage() != null) {
linkValue.append(",");
linkValue.append(String.format("<%s>; rel=next; type=\"%s\"", buildRequestUrl(ctx, ts, ts.getNextPage()), contentType));
}
ctx.header("Link", linkValue.toString());
ctx.result(results).contentType(contentType.toString());
} else {
if (format == null || format.isEmpty()) {
format = "json";
}
results = dao.getTimeseries(format, names, office, unit, datum, beginZdt, endZdt, tz);
ctx.status(HttpServletResponse.SC_OK);
ctx.result(results);
}
requestResultSize.update(results.length());
} catch (NotFoundException e) {
RadarError re = new RadarError("Not found.");
logger.log(Level.WARNING, re.toString(), e);
ctx.status(HttpServletResponse.SC_NOT_FOUND);
ctx.json(re);
} catch (IllegalArgumentException ex) {
RadarError re = new RadarError("Invalid arguments supplied");
logger.log(Level.SEVERE, re.toString(), ex);
ctx.status(HttpServletResponse.SC_BAD_REQUEST);
ctx.json(re);
}
}
use of cwms.radar.data.dao.TimeSeriesDao in project cwms-radar-api by USACE.
the class TimeSeriesController method update.
@OpenApi(description = "Update a TimeSeries", requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = TimeSeries.class, type = Formats.JSON), @OpenApiContent(from = TimeSeries.class, type = Formats.XML) }, required = true), method = HttpMethod.PATCH, path = "/timeseries", tags = { "TimeSeries" })
@Override
public void update(Context ctx, String id) {
updateRequests.mark();
try (final Timer.Context timeContext = updateRequestsTime.time();
DSLContext dsl = getDslContext(ctx)) {
TimeSeriesDao dao = getTimeSeriesDao(dsl);
TimeSeries timeSeries = deserializeTimeSeries(ctx);
dao.store(timeSeries, TimeSeriesDao.NON_VERSIONED);
ctx.status(HttpServletResponse.SC_OK);
} catch (IOException | DataAccessException ex) {
RadarError re = new RadarError("Internal Error");
logger.log(Level.SEVERE, re.toString(), ex);
ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
}
}
use of cwms.radar.data.dao.TimeSeriesDao in project cwms-radar-api by USACE.
the class TimeSeriesController method create.
@OpenApi(description = "Create new TimeSeries", requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = TimeSeries.class, type = Formats.JSON), @OpenApiContent(from = TimeSeries.class, type = Formats.XML) }, required = true), method = HttpMethod.POST, path = "/timeseries", tags = { "TimeSeries" })
@Override
public void create(Context ctx) {
createRequests.mark();
try (final Timer.Context timeContext = createRequestsTime.time();
DSLContext dsl = getDslContext(ctx)) {
TimeSeriesDao dao = getTimeSeriesDao(dsl);
TimeSeries timeSeries = deserializeTimeSeries(ctx);
dao.create(timeSeries);
ctx.status(HttpServletResponse.SC_OK);
} catch (IOException | DataAccessException ex) {
RadarError re = new RadarError("Internal Error");
logger.log(Level.SEVERE, re.toString(), ex);
ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
}
}
use of cwms.radar.data.dao.TimeSeriesDao in project cwms-radar-api by USACE.
the class TimeSeriesControllerTest method testDaoMock.
@Test
public void testDaoMock() throws JsonProcessingException {
String officeId = "LRL";
String tsId = "RYAN3.Stage.Inst.5Minutes.0.ZSTORE_TS_TEST";
TimeSeries expected = buildTimeSeries(officeId, tsId);
// build a mock dao that returns a pre-built ts when called a certain way
TimeSeriesDao dao = mock(TimeSeriesDao.class);
when(dao.getTimeseries(eq(""), eq(500), eq(tsId), eq(officeId), eq("EN"), isNull(), isNotNull(), isNotNull(), isNotNull())).thenReturn(expected);
// build mock request and response
final HttpServletRequest request = mock(HttpServletRequest.class);
final HttpServletResponse response = mock(HttpServletResponse.class);
final Map<String, ?> map = new LinkedHashMap<>();
when(request.getAttribute("office-id")).thenReturn(officeId);
when(request.getAttribute("database")).thenReturn(null);
when(request.getHeader(Header.ACCEPT)).thenReturn(Formats.JSONV2);
Map<String, String> urlParams = new LinkedHashMap<>();
urlParams.put("office", officeId);
urlParams.put("name", tsId);
String paramStr = buildParamStr(urlParams);
when(request.getQueryString()).thenReturn(paramStr);
when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1:7001/timeseries"));
// build real context that uses the mock request/response
Context ctx = new Context(request, response, map);
// Build a controller that doesn't actually talk to database
TimeSeriesController controller = new TimeSeriesController(new MetricRegistry()) {
@Override
protected DSLContext getDslContext(Context ctx) {
return null;
}
@NotNull
@Override
protected TimeSeriesDao getTimeSeriesDao(DSLContext dsl) {
return dao;
}
};
// make controller use our mock dao
// Do a controller getAll with our context
controller.getAll(ctx);
// Check that the controller accessed our mock dao in the expected way
verify(dao, times(1)).getTimeseries(eq(""), eq(500), eq(tsId), eq(officeId), eq("EN"), isNull(), isNotNull(), isNotNull(), isNotNull());
// Make sure controller thought it was happy
verify(response).setStatus(200);
// And make sure controller returned json
verify(response).setContentType(Formats.JSONV2);
String result = ctx.resultString();
// MAke sure we got some sort of response
assertNotNull(result);
// Turn json response back into a TimeSeries object
ObjectMapper om = JsonV2.buildObjectMapper();
TimeSeries actual = om.readValue(result, TimeSeries.class);
assertSimilar(expected, actual);
}
use of cwms.radar.data.dao.TimeSeriesDao in project cwms-radar-api by USACE.
the class TimeSeriesController method delete.
@OpenApi(queryParams = { @OpenApiParam(name = "office", required = true, description = "Specifies the owning office of the timeseries to be deleted.") }, method = HttpMethod.DELETE, tags = { "TimeSeries" })
@Override
public void delete(Context ctx, String tsId) {
deleteRequests.mark();
String office = ctx.queryParam("office");
try (final Timer.Context timeContext = deleteRequestsTime.time();
DSLContext dsl = getDslContext(ctx)) {
TimeSeriesDao dao = getTimeSeriesDao(dsl);
dao.delete(office, tsId);
} catch (DataAccessException ex) {
RadarError re = new RadarError("Internal Error");
logger.log(Level.SEVERE, re.toString(), ex);
ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
}
ctx.status(HttpServletResponse.SC_OK);
}
Aggregations