use of cwms.radar.data.dto.Catalog in project cwms-radar-api by USACE.
the class TimeSeriesDaoImpl method getTimeSeriesCatalog.
@Override
public Catalog getTimeSeriesCatalog(String page, int pageSize, Optional<String> office, String idLike, String locCategoryLike, String locGroupLike, String tsCategoryLike, String tsGroupLike) {
int total = 0;
String tsCursor = "*";
if (page == null || page.isEmpty()) {
Condition condition = AV_CWMS_TS_ID2.CWMS_TS_ID.upper().likeRegex(idLike.toUpperCase()).and(AV_CWMS_TS_ID2.ALIASED_ITEM.isNull());
if (office.isPresent()) {
condition = condition.and(AV_CWMS_TS_ID2.DB_OFFICE_ID.upper().eq(office.get().toUpperCase()));
}
if (locCategoryLike != null) {
condition.and(AV_CWMS_TS_ID2.LOC_ALIAS_CATEGORY.upper().likeRegex(locCategoryLike.toUpperCase()));
}
if (locGroupLike != null) {
condition.and(AV_CWMS_TS_ID2.LOC_ALIAS_GROUP.upper().likeRegex(locGroupLike.toUpperCase()));
}
if (tsCategoryLike != null) {
condition.and(AV_CWMS_TS_ID2.TS_ALIAS_CATEGORY.upper().likeRegex(tsCategoryLike.toUpperCase()));
}
if (tsGroupLike != null) {
condition.and(AV_CWMS_TS_ID2.TS_ALIAS_GROUP.upper().likeRegex(tsGroupLike.toUpperCase()));
}
SelectConditionStep<Record1<Integer>> count = dsl.select(count(asterisk())).from(AV_CWMS_TS_ID2).where(condition);
total = count.fetchOne().value1();
} else {
logger.fine("getting non-default page");
// get totally from page
String[] parts = CwmsDTOPaginated.decodeCursor(page, "|||");
logger.fine("decoded cursor: " + String.join("|||", parts));
for (String p : parts) {
logger.finest(p);
}
if (parts.length > 1) {
tsCursor = parts[0].split("/")[1];
total = Integer.parseInt(parts[1]);
}
}
SelectQuery<?> primaryDataQuery = dsl.selectQuery();
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.DB_OFFICE_ID);
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.CWMS_TS_ID);
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.TS_CODE);
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.UNIT_ID);
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.INTERVAL_ID);
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.INTERVAL_UTC_OFFSET);
if (this.getDbVersion() >= Dao.CWMS_21_1_1) {
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.TIME_ZONE_ID);
}
primaryDataQuery.addFrom(AV_CWMS_TS_ID2);
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.ALIASED_ITEM.isNull());
// add the regexp_like clause.
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.CWMS_TS_ID.upper().likeRegex(idLike.toUpperCase()));
if (office.isPresent()) {
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.DB_OFFICE_ID.upper().eq(office.get().toUpperCase()));
}
if (locCategoryLike != null) {
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.LOC_ALIAS_CATEGORY.upper().likeRegex(locCategoryLike.toUpperCase()));
}
if (locGroupLike != null) {
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.LOC_ALIAS_GROUP.upper().likeRegex(locGroupLike.toUpperCase()));
}
if (tsCategoryLike != null) {
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.TS_ALIAS_CATEGORY.upper().likeRegex(tsCategoryLike.toUpperCase()));
}
if (tsGroupLike != null) {
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.TS_ALIAS_GROUP.upper().likeRegex(tsGroupLike.toUpperCase()));
}
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.CWMS_TS_ID.upper().gt(tsCursor));
primaryDataQuery.addOrderBy(AV_CWMS_TS_ID2.CWMS_TS_ID);
Table<?> dataTable = primaryDataQuery.asTable("data");
// query.addConditions(field("rownum").lessOrEqual(pageSize));
// query.addConditions(condition("rownum < 500"));
SelectQuery<?> limitQuery = dsl.selectQuery();
// limitQuery.addSelect(field("rownum"));
limitQuery.addSelect(dataTable.fields());
// .limit(pageSize);
limitQuery.addFrom(dataTable);
limitQuery.addConditions(field("rownum").lessOrEqual(pageSize));
Table<?> limitTable = limitQuery.asTable("limiter");
SelectQuery<?> overallQuery = dsl.selectQuery();
overallQuery.addSelect(limitTable.fields());
overallQuery.addSelect(AV_TS_EXTENTS_UTC.VERSION_TIME);
overallQuery.addSelect(AV_TS_EXTENTS_UTC.EARLIEST_TIME);
overallQuery.addSelect(AV_TS_EXTENTS_UTC.LATEST_TIME);
overallQuery.addFrom(limitTable);
overallQuery.addJoin(AV_TS_EXTENTS_UTC, org.jooq.JoinType.LEFT_OUTER_JOIN, condition("\"CWMS_20\".\"AV_TS_EXTENTS_UTC\".\"TS_CODE\" = " + field("\"limiter\".\"TS_CODE\"")));
logger.info(() -> overallQuery.getSQL(ParamType.INLINED));
Result<?> result = overallQuery.fetch();
HashMap<String, TimeseriesCatalogEntry.Builder> tsIdExtentMap = new HashMap<>();
result.forEach(row -> {
String tsId = row.get(AV_CWMS_TS_ID2.CWMS_TS_ID);
if (!tsIdExtentMap.containsKey(tsId)) {
TimeseriesCatalogEntry.Builder builder = new TimeseriesCatalogEntry.Builder().officeId(row.get(AV_CWMS_TS_ID2.DB_OFFICE_ID)).cwmsTsId(row.get(AV_CWMS_TS_ID2.CWMS_TS_ID)).units(row.get(AV_CWMS_TS_ID2.UNIT_ID)).interval(row.get(AV_CWMS_TS_ID2.INTERVAL_ID)).intervalOffset(row.get(AV_CWMS_TS_ID2.INTERVAL_UTC_OFFSET));
if (this.getDbVersion() > Dao.CWMS_21_1_1) {
builder.timeZone(row.get("TIME_ZONE_ID", String.class));
}
tsIdExtentMap.put(tsId, builder);
}
if (row.get(AV_TS_EXTENTS_UTC.EARLIEST_TIME) != null) {
// tsIdExtentMap.get(tsId)
TimeSeriesExtents extents = new TimeSeriesExtents(row.get(AV_TS_EXTENTS_UTC.VERSION_TIME), row.get(AV_TS_EXTENTS_UTC.EARLIEST_TIME), row.get(AV_TS_EXTENTS_UTC.LATEST_TIME));
tsIdExtentMap.get(tsId).withExtent(extents);
}
});
List<? extends CatalogEntry> entries = tsIdExtentMap.entrySet().stream().sorted((left, right) -> left.getKey().compareTo(right.getKey())).map(e -> {
return e.getValue().build();
}).collect(Collectors.toList());
return new Catalog(tsCursor, total, pageSize, entries);
}
use of cwms.radar.data.dto.Catalog in project cwms-radar-api by USACE.
the class LocationsDaoImpl method getLocationCatalog.
@Override
public Catalog getLocationCatalog(String cursor, int pageSize, String unitSystem, Optional<String> office, String idLike, String categoryLike, String groupLike) {
int total = 0;
String locCursor = "*";
if (cursor == null || cursor.isEmpty()) {
Condition condition = buildCatalogWhere(unitSystem, office, idLike, categoryLike, groupLike);
SelectConditionStep<Record1<Integer>> count = dsl.select(count(asterisk())).from(AV_LOC).innerJoin(AV_LOC_GRP_ASSGN).on(AV_LOC.LOCATION_ID.eq(AV_LOC_GRP_ASSGN.LOCATION_ID)).where(condition);
total = count.fetchOne().value1().intValue();
} else {
// get totally from page
String[] parts = CwmsDTOPaginated.decodeCursor(cursor, "|||");
if (parts.length > 1) {
locCursor = parts[0].split("\\/")[1];
total = Integer.parseInt(parts[1]);
}
}
Condition condition = buildCatalogWhere(unitSystem, office, idLike, categoryLike, groupLike).and(AV_LOC.LOCATION_ID.upper().greaterThan(locCursor));
SelectConditionStep<Record1<String>> tmp = dsl.select(AV_LOC.LOCATION_ID).from(AV_LOC).innerJoin(AV_LOC_GRP_ASSGN).on(AV_LOC.LOCATION_ID.eq(AV_LOC_GRP_ASSGN.LOCATION_ID)).where(condition);
Table<?> forLimit = tmp.orderBy(AV_LOC.LOCATION_ID).limit(pageSize).asTable();
SelectConditionStep<Record> query = dsl.select(AV_LOC.asterisk(), AV_LOC_GRP_ASSGN.asterisk()).from(AV_LOC).innerJoin(forLimit).on(forLimit.field(AV_LOC.LOCATION_ID).eq(AV_LOC.LOCATION_ID)).leftJoin(AV_LOC_GRP_ASSGN).on(AV_LOC_GRP_ASSGN.LOCATION_ID.eq(AV_LOC.LOCATION_ID)).where(condition);
query.orderBy(AV_LOC.LOCATION_ID);
// logger.info( () -> query.getSQL(ParamType.INLINED));
HashMap<usace.cwms.db.jooq.codegen.tables.records.AV_LOC, ArrayList<usace.cwms.db.jooq.codegen.tables.records.AV_LOC_ALIAS>> theMap = new HashMap<>();
query.fetch().forEach(row -> {
usace.cwms.db.jooq.codegen.tables.records.AV_LOC loc = row.into(AV_LOC);
if (!theMap.containsKey(loc)) {
theMap.put(loc, new ArrayList<>());
}
usace.cwms.db.jooq.codegen.tables.records.AV_LOC_ALIAS alias = row.into(AV_LOC_ALIAS);
usace.cwms.db.jooq.codegen.tables.records.AV_LOC_GRP_ASSGN group = row.into(AV_LOC_GRP_ASSGN);
if (group.getALIAS_ID() != null) {
theMap.get(loc).add(alias);
}
});
List<? extends CatalogEntry> entries = theMap.entrySet().stream().sorted((left, right) -> (left.getKey().getLOCATION_ID().compareTo(right.getKey().getBASE_LOCATION_ID()))).map(e -> new LocationCatalogEntry(e.getKey().getDB_OFFICE_ID(), e.getKey().getLOCATION_ID(), e.getKey().getNEAREST_CITY(), e.getKey().getPUBLIC_NAME(), e.getKey().getLONG_NAME(), e.getKey().getDESCRIPTION(), e.getKey().getLOCATION_KIND_ID(), e.getKey().getLOCATION_TYPE(), e.getKey().getTIME_ZONE_NAME(), e.getKey().getLATITUDE() != null ? e.getKey().getLATITUDE().doubleValue() : null, e.getKey().getLONGITUDE() != null ? e.getKey().getLONGITUDE().doubleValue() : null, e.getKey().getPUBLISHED_LATITUDE() != null ? e.getKey().getPUBLISHED_LATITUDE().doubleValue() : null, e.getKey().getPUBLISHED_LONGITUDE() != null ? e.getKey().getPUBLISHED_LONGITUDE().doubleValue() : null, e.getKey().getHORIZONTAL_DATUM(), e.getKey().getELEVATION(), e.getKey().getUNIT_ID(), e.getKey().getVERTICAL_DATUM(), e.getKey().getNATION_ID(), e.getKey().getSTATE_INITIAL(), e.getKey().getCOUNTY_NAME(), e.getKey().getBOUNDING_OFFICE_ID(), e.getKey().getMAP_LABEL(), e.getKey().getACTIVE_FLAG().equalsIgnoreCase("T"), e.getValue().stream().map(a -> new LocationAlias(a.getCATEGORY_ID() + "-" + a.getGROUP_ID(), a.getALIAS_ID())).collect(Collectors.toList()))).collect(Collectors.toList());
return new Catalog(locCursor, total, pageSize, entries);
}
use of cwms.radar.data.dto.Catalog in project cwms-radar-api by USACE.
the class TimeseriesCatalogEntryTest method test_xml_serialization_earliest.
@Test
void test_xml_serialization_earliest() {
CatalogEntry entry = buildEntry();
Catalog cat = new Catalog(null, 1, 10, new ArrayList<CatalogEntry>() {
{
add(entry);
}
});
ContentType contentType = Formats.parseHeader(Formats.XML);
String xml = Formats.format(contentType, cat);
assertNotNull(xml);
assertFalse(xml.isEmpty());
XmlPath path = XmlPath.from(xml);
assertThat(path.getString("catalog.entries.entry.@name"), equalTo("Barren-Lake.Elev.Inst.0.0.USGS-raw"));
assertThat(path.getString("catalog.entries.entry.units"), equalTo("m"));
assertThat(path.getInt("catalog.entries.entry.interval"), equalTo(0));
assertThat(path.getLong("catalog.entries.entry.interval-offset"), equalTo(-2147483648L));
assertThat(path.getString("catalog.entries.entry.time-zone"), equalTo("US/Central"));
Object tmp = path.get("catalog.entries.entry.extents");
System.out.println(tmp.toString());
assertThat(path.getString("catalog.entries.entry.extents.extents.earliest-time"), equalTo("2017-07-27T05:00:00Z"));
assertThat(path.getString("catalog.entries.entry.extents.extents.latest-time"), equalTo("2017-11-24T22:30:00Z"));
}
use of cwms.radar.data.dto.Catalog in project cwms-radar-api by USACE.
the class CatalogController method getOne.
@OpenApi(queryParams = { @OpenApiParam(name = "page", description = "This end point can return a lot of data, this identifies where in the request you are."), @OpenApiParam(name = "pageSize", type = Integer.class, description = "How many entires per page returned. Default 500."), @OpenApiParam(name = "unitSystem", type = UnitSystem.class, description = UnitSystem.DESCRIPTION), @OpenApiParam(name = "office", description = "3-4 letter office name representing the district you want to isolate data to."), @OpenApiParam(name = "like", description = "Posix regular expression matching against the id"), @OpenApiParam(name = "timeseriesCategoryLike", description = "Posix regular expression matching against the timeseries category id"), @OpenApiParam(name = "timeseriesGroupLike", description = "Posix regular expression matching against the timeseries group id"), @OpenApiParam(name = "locationCategoryLike", description = "Posix regular expression matching against the location category id"), @OpenApiParam(name = "locationGroupLike", description = "Posix regular expression matching against the location group id") }, pathParams = { @OpenApiParam(name = "dataSet", required = false, type = CatalogableEndpoint.class, description = "A list of what data? E.g. Timeseries, Locations, Ratings, etc") }, responses = { @OpenApiResponse(status = "200", description = "A list of elements the data set you've selected.", content = { @OpenApiContent(from = Catalog.class, type = Formats.JSONV2), @OpenApiContent(from = Catalog.class, type = Formats.XML) }) }, tags = { TAG })
@Override
public void getOne(Context ctx, String dataSet) {
getOneRequest.mark();
try (final Timer.Context timeContext = getOneRequestTime.time();
DSLContext dsl = JooqDao.getDslContext(ctx)) {
String valDataSet = ((PolicyFactory) ctx.appAttribute("PolicyFactory")).sanitize(dataSet);
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 unitSystem = ctx.queryParamAsClass("unitSystem", String.class).getOrDefault(UnitSystem.SI.getValue());
Optional<String> office = Optional.ofNullable(ctx.queryParamAsClass("office", String.class).allowNullable().check(Office::validOfficeCanNull, "Invalid office provided").get());
String like = ctx.queryParamAsClass("like", String.class).getOrDefault(".*");
String tsCategoryLike = ctx.queryParamAsClass("timeseriesCategoryLike", String.class).getOrDefault(null);
String tsGroupLike = ctx.queryParamAsClass("timeseriesGroupLike", String.class).getOrDefault(null);
String locCategoryLike = ctx.queryParamAsClass("locationCategoryLike", String.class).getOrDefault(null);
String locGroupLike = ctx.queryParamAsClass("locationGroupLike", String.class).getOrDefault(null);
String acceptHeader = ctx.header("Accept");
ContentType contentType = Formats.parseHeaderAndQueryParm(acceptHeader, null);
Catalog cat = null;
if ("timeseries".equalsIgnoreCase(valDataSet)) {
TimeSeriesDao tsDao = new TimeSeriesDaoImpl(dsl);
cat = tsDao.getTimeSeriesCatalog(cursor, pageSize, office, like, locCategoryLike, locGroupLike, tsCategoryLike, tsGroupLike);
} else if ("locations".equalsIgnoreCase(valDataSet)) {
LocationsDao dao = new LocationsDaoImpl(dsl);
cat = dao.getLocationCatalog(cursor, pageSize, unitSystem, office, like, locCategoryLike, locGroupLike);
}
if (cat != null) {
String data = Formats.format(contentType, cat);
ctx.result(data).contentType(contentType.toString());
requestResultSize.update(data.length());
} else {
final RadarError re = new RadarError("Cannot create catalog of requested information");
logger.info(() -> re.toString() + "with url:" + ctx.fullUrl());
ctx.json(re).status(HttpCode.NOT_FOUND);
}
}
}
use of cwms.radar.data.dto.Catalog in project cwms-radar-api by USACE.
the class TimeseriesCatalogEntryTest method test_json_serialization_earliest.
@Test
void test_json_serialization_earliest() {
CatalogEntry entry = buildEntry();
Catalog cat = new Catalog(null, 1, 10, new ArrayList<CatalogEntry>() {
{
add(entry);
}
});
ContentType contentType = Formats.parseHeader(Formats.JSONV2);
String json = Formats.format(contentType, cat);
assertNotNull(json);
assertFalse(json.isEmpty());
JsonPath path = JsonPath.from(json);
assertThat(path.getString("entries[0].name"), equalTo("Barren-Lake.Elev.Inst.0.0.USGS-raw"));
assertThat(path.getString("entries[0].units"), equalTo("m"));
assertThat(path.getInt("entries[0].interval"), equalTo(0));
assertThat(path.getLong("entries[0].interval-offset"), equalTo(-2147483648L));
assertThat(path.getString("entries[0].time-zone"), equalTo("US/Central"));
assertThat(path.getString("entries[0].extents[0].earliest-time"), equalTo("2017-07-27T05:00:00Z"));
assertThat(path.getString("entries[0].extents[0].latest-time"), equalTo("2017-11-24T22:30:00Z"));
}
Aggregations