Search in sources :

Example 1 with NotFoundException

use of cwms.radar.api.NotFoundException in project cwms-radar-api by USACE.

the class TimeSeriesDaoImpl method getTimeseries.

protected TimeSeries getTimeseries(String page, int pageSize, String names, String office, String units, ZonedDateTime beginTime, ZonedDateTime endTime) {
    TimeSeries retval = null;
    String cursor = null;
    Timestamp tsCursor = null;
    Integer total = null;
    if (page != null && !page.isEmpty()) {
        String[] parts = CwmsDTOPaginated.decodeCursor(page);
        logger.fine("Decoded cursor");
        for (String p : parts) {
            logger.finest(p);
        }
        if (parts.length > 1) {
            cursor = parts[0];
            tsCursor = Timestamp.from(Instant.ofEpochMilli(Long.parseLong(parts[0])));
            if (parts.length > 2)
                total = Integer.parseInt(parts[1]);
            // Use the pageSize from the original cursor, for consistent paging
            // Last item is pageSize
            pageSize = Integer.parseInt(parts[parts.length - 1]);
        }
    }
    final String recordCursor = cursor;
    final int recordPageSize = pageSize;
    try {
        Field<String> officeId = CWMS_UTIL_PACKAGE.call_GET_DB_OFFICE_ID(office != null ? DSL.val(office) : CWMS_UTIL_PACKAGE.call_USER_OFFICE_ID());
        Field<String> tsId = CWMS_TS_PACKAGE.call_GET_TS_ID__2(DSL.val(names), officeId);
        Field<BigDecimal> tsCode = CWMS_TS_PACKAGE.call_GET_TS_CODE__2(tsId, officeId);
        Field<String> unit = units.compareToIgnoreCase("SI") == 0 || units.compareToIgnoreCase("EN") == 0 ? CWMS_UTIL_PACKAGE.call_GET_DEFAULT_UNITS(CWMS_TS_PACKAGE.call_GET_BASE_PARAMETER_ID(tsCode), DSL.val(units, String.class)) : DSL.val(units, String.class);
        // This code assumes the database timezone is in UTC (per Oracle recommendation)
        // Wrap in table() so JOOQ can parse the result
        @SuppressWarnings("deprecated") SQL retrieveTable = DSL.sql("table(" + CWMS_TS_PACKAGE.call_RETRIEVE_TS_OUT_TAB(tsId, unit, CWMS_UTIL_PACKAGE.call_TO_TIMESTAMP__2(DSL.val(beginTime.toInstant().toEpochMilli())), CWMS_UTIL_PACKAGE.call_TO_TIMESTAMP__2(DSL.val(endTime.toInstant().toEpochMilli())), DSL.inline("UTC", String.class), // All times are sent as UTC to the database, regardless of requested timezone.
        null, null, null, null, null, null, null, officeId) + ")");
        Field<String> loc = CWMS_UTIL_PACKAGE.call_SPLIT_TEXT(tsId, DSL.val(BigInteger.valueOf(1L)), DSL.val("."), DSL.val(BigInteger.valueOf(6L)));
        Field<String> param = DSL.upper(CWMS_UTIL_PACKAGE.call_SPLIT_TEXT(tsId, DSL.val(BigInteger.valueOf(2L)), DSL.val("."), DSL.val(BigInteger.valueOf(6L))));
        // What is the syntax for selecting tzName and offsetField from the same subquery?
        // It works when each field comes from its own subquery.
        // This didn't work.
        // Field<?>[] fields = DSL.select(AV_CWMS_TS_ID2.INTERVAL_UTC_OFFSET.as("INTERVAL_UTC_OFFSET"),
        // AV_CWMS_TS_ID2.TIME_ZONE_ID.as("TIME_ZONE_ID"))
        // .from(AV_CWMS_TS_ID2).where(AV_CWMS_TS_ID2.CWMS_TS_ID.eq(tsId))
        // .fields();
        // Field<BigDecimal> offsetField = (Field<BigDecimal>) fields[0];
        // Field<String> tzName = (Field<String>) fields[1];
        Field<BigDecimal> offsetField = DSL.select(AV_CWMS_TS_ID2.INTERVAL_UTC_OFFSET.as("INTERVAL_UTC_OFFSET")).from(AV_CWMS_TS_ID2).where(AV_CWMS_TS_ID2.CWMS_TS_ID.eq(tsId).and(AV_CWMS_TS_ID2.ALIASED_ITEM.isNull())).asField();
        Field<String> tzName;
        if (this.getDbVersion() >= Dao.CWMS_21_1_1) {
            tzName = DSL.select(AV_CWMS_TS_ID2.TIME_ZONE_ID).from(AV_CWMS_TS_ID2).where(AV_CWMS_TS_ID2.CWMS_TS_ID.eq(tsId).and(AV_CWMS_TS_ID2.ALIASED_ITEM.isNull())).asField("TIME_ZONE_ID");
        } else {
            tzName = DSL.val((String) null).as("TIME_ZONE_ID");
        }
        SelectSelectStep<? extends Record> metadataQuery = dsl.select(tsId.as("NAME"), officeId.as("OFFICE_ID"), unit.as("UNITS"), CWMS_TS_PACKAGE.call_GET_INTERVAL(tsId).as("INTERVAL"), loc.as("LOC_PART"), param.as("PARM_PART"), DSL.choose(param).when("ELEV", CWMS_LOC_PACKAGE.call_GET_VERTICAL_DATUM_INFO_F__2(loc, unit, officeId)).otherwise("").as("VERTICAL_DATUM"), // Total is only an estimate, as it can change if fetching current data, or the timeseries otherwise changes between queries.
        total != null ? DSL.val(total).as("TOTAL") : DSL.selectCount().from(retrieveTable).asField("TOTAL"), offsetField, tzName);
        logger.finest(() -> metadataQuery.getSQL(ParamType.INLINED));
        TimeSeries timeseries = metadataQuery.fetchOne(tsMetadata -> {
            String vert = (String) tsMetadata.getValue("VERTICAL_DATUM");
            VerticalDatumInfo verticalDatumInfo = parseVerticalDatumInfo(vert);
            return new TimeSeries(recordCursor, recordPageSize, tsMetadata.getValue("TOTAL", Integer.class), tsMetadata.getValue("NAME", String.class), tsMetadata.getValue("OFFICE_ID", String.class), beginTime, endTime, tsMetadata.getValue("UNITS", String.class), Duration.ofMinutes(tsMetadata.get("INTERVAL") == null ? 0 : tsMetadata.getValue("INTERVAL", Long.class)), verticalDatumInfo, tsMetadata.getValue(offsetField).longValue(), tsMetadata.getValue(tzName));
        });
        if (pageSize != 0) {
            SelectConditionStep<Record3<Timestamp, Double, BigDecimal>> query = dsl.select(DSL.field("DATE_TIME", Timestamp.class).as("DATE_TIME"), CWMS_ROUNDING_PACKAGE.call_ROUND_DD_F(DSL.field("VALUE", Double.class), DSL.inline("5567899996"), DSL.inline('T')).as("VALUE"), CWMS_TS_PACKAGE.call_NORMALIZE_QUALITY(DSL.nvl(DSL.field("QUALITY_CODE", Integer.class), DSL.inline(5))).as("QUALITY_CODE")).from(retrieveTable).where(DSL.field("DATE_TIME", Timestamp.class).greaterOrEqual(CWMS_UTIL_PACKAGE.call_TO_TIMESTAMP__2(DSL.nvl(DSL.val(tsCursor == null ? null : tsCursor.toInstant().toEpochMilli()), DSL.val(beginTime.toInstant().toEpochMilli()))))).and(DSL.field("DATE_TIME", Timestamp.class).lessOrEqual(CWMS_UTIL_PACKAGE.call_TO_TIMESTAMP__2(DSL.val(endTime.toInstant().toEpochMilli()))));
            if (pageSize > 0)
                query.limit(DSL.val(pageSize + 1));
            logger.finest(() -> query.getSQL(ParamType.INLINED));
            query.fetchInto(tsRecord -> timeseries.addValue(tsRecord.getValue("DATE_TIME", Timestamp.class), tsRecord.getValue("VALUE", Double.class), tsRecord.getValue("QUALITY_CODE", Integer.class)));
            retval = timeseries;
        }
    } catch (org.jooq.exception.DataAccessException e) {
        if (isNotFound(e.getCause())) {
            throw new NotFoundException(e.getCause());
        }
        throw e;
    }
    return retval;
}
Also used : TimeSeries(cwms.radar.data.dto.TimeSeries) VerticalDatumInfo(cwms.radar.data.dto.VerticalDatumInfo) NotFoundException(cwms.radar.api.NotFoundException) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) SQL(org.jooq.SQL) BigInteger(java.math.BigInteger) Record3(org.jooq.Record3)

Aggregations

NotFoundException (cwms.radar.api.NotFoundException)1 TimeSeries (cwms.radar.data.dto.TimeSeries)1 VerticalDatumInfo (cwms.radar.data.dto.VerticalDatumInfo)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Timestamp (java.sql.Timestamp)1 Record3 (org.jooq.Record3)1 SQL (org.jooq.SQL)1