Search in sources :

Example 1 with TimeSeries

use of cwms.radar.data.dto.TimeSeries 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);
    }
}
Also used : TimeSeries(cwms.radar.data.dto.TimeSeries) ContentType(cwms.radar.formatters.ContentType) ZoneId(java.time.ZoneId) DSLContext(org.jooq.DSLContext) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) ZonedDateTime(java.time.ZonedDateTime) TimeSeriesDao(cwms.radar.data.dao.TimeSeriesDao) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 2 with TimeSeries

use of cwms.radar.data.dto.TimeSeries in project cwms-radar-api by USACE.

the class TimeSeriesController method deserializeJaxb.

public static TimeSeries deserializeJaxb(String body) throws IOException {
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(TimeSeries.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        return (TimeSeries) unmarshaller.unmarshal(new StringReader(body));
    } catch (JAXBException e) {
        throw new IOException(e);
    }
}
Also used : TimeSeries(cwms.radar.data.dto.TimeSeries) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 3 with TimeSeries

use of cwms.radar.data.dto.TimeSeries 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);
    }
}
Also used : RadarError(cwms.radar.api.errors.RadarError) TimeSeries(cwms.radar.data.dto.TimeSeries) Timer(com.codahale.metrics.Timer) DSLContext(org.jooq.DSLContext) IOException(java.io.IOException) TimeSeriesDao(cwms.radar.data.dao.TimeSeriesDao) DataAccessException(org.jooq.exception.DataAccessException) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 4 with TimeSeries

use of cwms.radar.data.dto.TimeSeries 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);
    }
}
Also used : RadarError(cwms.radar.api.errors.RadarError) TimeSeries(cwms.radar.data.dto.TimeSeries) Timer(com.codahale.metrics.Timer) DSLContext(org.jooq.DSLContext) IOException(java.io.IOException) TimeSeriesDao(cwms.radar.data.dao.TimeSeriesDao) DataAccessException(org.jooq.exception.DataAccessException) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 5 with TimeSeries

use of cwms.radar.data.dto.TimeSeries in project cwms-radar-api by USACE.

the class TimeSeriesControllerTest method testDeserializeTimeSeriesXmlUTC.

@Test
public void testDeserializeTimeSeriesXmlUTC() throws IOException {
    TimeZone aDefault = TimeZone.getDefault();
    try {
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        String xml = loadResourceAsString("cwms/radar/api/timeseries_create.xml");
        assertNotNull(xml);
        // Should this be XMLv2?
        TimeSeries ts = TimeSeriesController.deserializeTimeSeries(xml, Formats.XMLV2);
        assertNotNull(ts);
        TimeSeries fakeTs = buildTimeSeries("LRL", "RYAN3.Stage.Inst.5Minutes.0.ZSTORE_TS_TEST");
        assertSimilar(fakeTs, ts);
    } finally {
        TimeZone.setDefault(aDefault);
    }
}
Also used : TimeZone(java.util.TimeZone) TimeSeries(cwms.radar.data.dto.TimeSeries) Test(org.junit.jupiter.api.Test)

Aggregations

TimeSeries (cwms.radar.data.dto.TimeSeries)15 Test (org.junit.jupiter.api.Test)7 DSLContext (org.jooq.DSLContext)6 TimeSeriesDao (cwms.radar.data.dao.TimeSeriesDao)4 ZonedDateTime (java.time.ZonedDateTime)4 Timer (com.codahale.metrics.Timer)3 RadarError (cwms.radar.api.errors.RadarError)3 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)3 IOException (java.io.IOException)3 Timestamp (java.sql.Timestamp)3 DaoTest.getConnection (cwms.radar.data.dao.DaoTest.getConnection)2 Connection (java.sql.Connection)2 DataAccessException (org.jooq.exception.DataAccessException)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 NotFoundException (cwms.radar.api.NotFoundException)1 VerticalDatumInfo (cwms.radar.data.dto.VerticalDatumInfo)1 ContentType (cwms.radar.formatters.ContentType)1 XMLv2 (cwms.radar.formatters.xml.XMLv2)1 Context (io.javalin.http.Context)1