Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import static org.jooq.impl.DSL.partitionBy;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.selectDistinct;
import static org.jooq.impl.DSL.selectOne;
import static org.jooq.impl.DSL.table;
import static usace.cwms.db.jooq.codegen.tables.AV_CWMS_TS_ID2.AV_CWMS_TS_ID2;
import static usace.cwms.db.jooq.codegen.tables.AV_TS_EXTENTS_UTC.AV_TS_EXTENTS_UTC;
Expand Down Expand Up @@ -941,8 +940,6 @@ private TimeSeries getRequestedTimeSeriesDirect(String page, int pageSize,
if (shouldFetchVerticalDatum(parmPart)) {
verticalDatumInfo = fetchVerticalDatumInfoSeparately(locPart, requestedUnits, office);
}
validateRequestedUnits(nativeUnits, metadataUnits);

VersionType finalDateVersionType = getDirectReadVersionType(
metadata.versionFlag, versionDate != null);

Expand Down Expand Up @@ -1000,8 +997,6 @@ private DirectReadMetadata fetchRequestedTimeSeriesMetadataRecord(DSLContext met
final Field<String> tsId = CWMS_TS_PACKAGE.call_GET_TS_ID__2(DSL.val(names), officeId);
final Field<BigDecimal> tsCode = CWMS_TS_PACKAGE.call_GET_TS_CODE__2(DSL.val(names), officeId);

validateUnits(units, tsCode, officeId, names);

Table<Record3<BigDecimal, String, String>> validTs =
select(tsCode.as("tscode"),
tsId.as("tsid"),
Expand Down Expand Up @@ -1049,7 +1044,7 @@ private DirectReadMetadata fetchRequestedTimeSeriesMetadataRecord(DSLContext met

logger.atFine().log("%s", lazy(() -> metadataQuery.getSQL(ParamType.INLINED)));

return metadataQuery.fetchOne(record -> new DirectReadMetadata(
DirectReadMetadata metadata = metadataQuery.fetchOne(record -> new DirectReadMetadata(
record.getValue("tscode", BigDecimal.class).longValue(),
record.getValue("tsid", String.class),
record.getValue("office_id", String.class),
Expand All @@ -1065,32 +1060,10 @@ private DirectReadMetadata fetchRequestedTimeSeriesMetadataRecord(DSLContext met
? UTC
: record.getValue("time_zone_id", String.class),
record.getValue("version_flag", String.class)));
}

private void validateUnits(String units, Field<BigDecimal> tsCode, Field<String> officeId, String name) {
if(!units.equalsIgnoreCase("SI") && !units.equalsIgnoreCase("EN")) {

boolean tsExists = dsl.fetchExists(
selectOne()
.from(AV_TSV_DQU.AV_TSV_DQU)
.where(AV_TSV_DQU.AV_TSV_DQU.TS_CODE.eq(tsCode.cast(Long.class)))
.and(AV_TSV_DQU.AV_TSV_DQU.OFFICE_ID.eq(officeId))
);
if(tsExists) {
boolean unitRequestedExists = dsl.fetchExists(
selectOne()
.from(AV_TSV_DQU.AV_TSV_DQU)
.where(AV_TSV_DQU.AV_TSV_DQU.TS_CODE.eq(tsCode.cast(Long.class)))
.and(AV_TSV_DQU.AV_TSV_DQU.OFFICE_ID.eq(officeId))
.and(AV_TSV_DQU.AV_TSV_DQU.UNIT_ID.equalIgnoreCase(units))
);

if (!unitRequestedExists) {
String msg = sanitizeOrNull(units + " is not a valid unit for time series " + name);
throw new InvalidItemException(msg, new IllegalArgumentException(msg));
}
}
if (metadata != null) {
validateRequestedUnits(metadata.nativeUnits, metadata.units, names);
}
return metadata;
}

private List<TimeSeries.Record> fetchRequestedTimeSeriesRows(long tsCode, String officeId,
Expand Down Expand Up @@ -1187,9 +1160,15 @@ private ResultQuery<Record4<Timestamp, Double, BigDecimal, Timestamp>> buildMaxV
.orderBy(dateTimeCol.asc());
}

private void validateRequestedUnits(String nativeUnits, String requestedUnits) {
private void validateRequestedUnits(String nativeUnits, String requestedUnits, String name) {
if (nativeUnits != null && requestedUnits != null) {
CWMS_UTIL_PACKAGE.call_CONVERT_UNITS(dsl.configuration(), 0.0D, nativeUnits, requestedUnits);
try {
CWMS_UTIL_PACKAGE.call_CONVERT_UNITS(dsl.configuration(), 0.0D, nativeUnits, requestedUnits);
} catch (InvalidItemException ex) {
String message = sanitizeOrNull(requestedUnits
+ " is not a valid unit for time series " + name);
throw new InvalidItemException(message, new IllegalArgumentException(message, ex));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ void denseRegularReadMatchesRetrieveTs() throws Exception {
);
}

@Test
void convertedUnitReadMatchesRetrieveTs() throws Exception {
assertDirectReadMatchesOracle(
"ITPARUNIT",
"ITPARUNIT.Stage.Inst.1Minute.0.BENCH",
"m",
Instant.parse("2024-01-01T00:00:00Z"),
Instant.parse("2024-01-01T00:05:00Z"),
denseRows(),
false,
false,
VersionType.UNVERSIONED,
Duration.ofMinutes(1),
0L,
null
);
}

@Test
void denseRegularEntryDateReadMatchesRetrieveTs() throws Exception {
assertDirectReadMatchesOracle(
Expand Down
Loading