Search in sources :

Example 1 with TimeSeriesGroup

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

the class TimeSeriesGroupDao method getTimeSeriesGroupsWhere.

@NotNull
private List<TimeSeriesGroup> getTimeSeriesGroupsWhere(Condition whereCond) {
    List<TimeSeriesGroup> retval = new ArrayList<>();
    AV_TS_CAT_GRP catGrp = AV_TS_CAT_GRP.AV_TS_CAT_GRP;
    AV_TS_GRP_ASSGN grpAssgn = AV_TS_GRP_ASSGN.AV_TS_GRP_ASSGN;
    final RecordMapper<Record, Pair<TimeSeriesGroup, AssignedTimeSeries>> mapper = queryRecord -> {
        TimeSeriesGroup group = buildTimeSeriesGroup(queryRecord);
        AssignedTimeSeries loc = buildAssignedTimeSeries(queryRecord);
        return new Pair<>(group, loc);
    };
    SelectOnConditionStep<?> selectOn = dsl.select(catGrp.CAT_DB_OFFICE_ID, catGrp.TS_CATEGORY_ID, catGrp.TS_CATEGORY_DESC, catGrp.GRP_DB_OFFICE_ID, catGrp.TS_GROUP_ID, catGrp.TS_GROUP_DESC, catGrp.SHARED_TS_ALIAS_ID, catGrp.SHARED_REF_TS_ID, grpAssgn.CATEGORY_ID, grpAssgn.DB_OFFICE_ID, grpAssgn.GROUP_ID, grpAssgn.TS_ID, grpAssgn.TS_CODE, grpAssgn.ATTRIBUTE, grpAssgn.ALIAS_ID, grpAssgn.REF_TS_ID).from(catGrp).leftOuterJoin(grpAssgn).on(catGrp.TS_CATEGORY_ID.eq(grpAssgn.CATEGORY_ID).and(catGrp.TS_GROUP_ID.eq(grpAssgn.GROUP_ID)).and(catGrp.GRP_DB_OFFICE_ID.eq(grpAssgn.DB_OFFICE_ID)));
    SelectOrderByStep<?> select = selectOn;
    if (whereCond != null) {
        select = selectOn.where(whereCond);
    }
    List<Pair<TimeSeriesGroup, AssignedTimeSeries>> assignments = select.orderBy(grpAssgn.ATTRIBUTE).fetch(mapper);
    Map<TimeSeriesGroup, List<AssignedTimeSeries>> map = new LinkedHashMap<>();
    for (Pair<TimeSeriesGroup, AssignedTimeSeries> pair : assignments) {
        List<AssignedTimeSeries> list = map.computeIfAbsent(pair.component1(), k -> new ArrayList<>());
        AssignedTimeSeries assignedTimeSeries = pair.component2();
        if (assignedTimeSeries != null) {
            list.add(assignedTimeSeries);
        }
    }
    for (final Map.Entry<TimeSeriesGroup, List<AssignedTimeSeries>> entry : map.entrySet()) {
        List<AssignedTimeSeries> assigned = entry.getValue();
        retval.add(new TimeSeriesGroup(entry.getKey(), assigned));
    }
    return retval;
}
Also used : TimeSeriesGroup(cwms.radar.data.dto.TimeSeriesGroup) Record(org.jooq.Record) RecordMapper(org.jooq.RecordMapper) SelectOrderByStep(org.jooq.SelectOrderByStep) Pair(kotlin.Pair) AV_TS_GRP_ASSGN(usace.cwms.db.jooq.codegen.tables.AV_TS_GRP_ASSGN) AssignedTimeSeries(cwms.radar.data.dto.AssignedTimeSeries) Condition(org.jooq.Condition) ArrayList(java.util.ArrayList) TimeSeriesCategory(cwms.radar.data.dto.TimeSeriesCategory) LinkedHashMap(java.util.LinkedHashMap) BigDecimal(java.math.BigDecimal) SelectOnConditionStep(org.jooq.SelectOnConditionStep) List(java.util.List) AV_TS_CAT_GRP(usace.cwms.db.jooq.codegen.tables.AV_TS_CAT_GRP) Map(java.util.Map) DSLContext(org.jooq.DSLContext) NotNull(org.jetbrains.annotations.NotNull) TimeSeriesGroup(cwms.radar.data.dto.TimeSeriesGroup) AV_TS_CAT_GRP(usace.cwms.db.jooq.codegen.tables.AV_TS_CAT_GRP) ArrayList(java.util.ArrayList) AssignedTimeSeries(cwms.radar.data.dto.AssignedTimeSeries) LinkedHashMap(java.util.LinkedHashMap) AV_TS_GRP_ASSGN(usace.cwms.db.jooq.codegen.tables.AV_TS_GRP_ASSGN) Record(org.jooq.Record) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(kotlin.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with TimeSeriesGroup

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

the class TimeSeriesGroupDao method buildTimeSeriesGroup.

private TimeSeriesGroup buildTimeSeriesGroup(Record queryRecord) {
    TimeSeriesCategory cat = buildTimeSeriesCategory(queryRecord);
    String grpOfficeId = queryRecord.get(AV_TS_CAT_GRP.AV_TS_CAT_GRP.GRP_DB_OFFICE_ID);
    String grpId = queryRecord.get(AV_TS_CAT_GRP.AV_TS_CAT_GRP.TS_GROUP_ID);
    String grpDesc = queryRecord.get(AV_TS_CAT_GRP.AV_TS_CAT_GRP.TS_GROUP_DESC);
    String sharedAliasId = queryRecord.get(AV_TS_CAT_GRP.AV_TS_CAT_GRP.SHARED_TS_ALIAS_ID);
    String sharedRefTsId = queryRecord.get(AV_TS_CAT_GRP.AV_TS_CAT_GRP.SHARED_REF_TS_ID);
    return new TimeSeriesGroup(cat, grpOfficeId, grpId, grpDesc, sharedAliasId, sharedRefTsId);
}
Also used : TimeSeriesGroup(cwms.radar.data.dto.TimeSeriesGroup) TimeSeriesCategory(cwms.radar.data.dto.TimeSeriesCategory)

Example 3 with TimeSeriesGroup

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

the class TimeSeriesGroupController method getOne.

@OpenApi(pathParams = { @OpenApiParam(name = "group-id", required = true, description = "Specifies the timeseries group whose data is to be included in the response") }, queryParams = { @OpenApiParam(name = "office", required = true, description = "Specifies the owning office of the timeseries group whose data is to be included in the response."), @OpenApiParam(name = "category-id", required = true, description = "Specifies the category containing the timeseries group whose data is to be included in the response.") }, responses = { @OpenApiResponse(status = "200", content = { @OpenApiContent(from = TimeSeriesGroup.class, type = Formats.JSON) }) }, description = "Retrieves requested timeseries group", tags = { "Timeseries Groups" })
@Override
public void getOne(Context ctx, String groupId) {
    getOneRequest.mark();
    try (final Timer.Context timeContext = getOneRequestTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        TimeSeriesGroupDao dao = new TimeSeriesGroupDao(dsl);
        String office = ctx.queryParam("office");
        String categoryId = ctx.queryParam("category-id");
        String formatHeader = ctx.header(Header.ACCEPT);
        ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null);
        TimeSeriesGroup group = null;
        List<TimeSeriesGroup> timeSeriesGroups = dao.getTimeSeriesGroups(office, categoryId, groupId);
        if (timeSeriesGroups != null && !timeSeriesGroups.isEmpty()) {
            if (timeSeriesGroups.size() == 1) {
                group = timeSeriesGroups.get(0);
            } else {
                // An error. [office, categoryId, groupId] should have, at most, one match
                String message = String.format("Multiple TimeSeriesGroups returned from getTimeSeriesGroups " + "for:%s category:%s groupId:%s At most one match was expected. Found:%s", office, categoryId, groupId, timeSeriesGroups);
                throw new IllegalArgumentException(message);
            }
        }
        if (group != null) {
            String result = Formats.format(contentType, group);
            ctx.result(result);
            ctx.contentType(contentType.toString());
            requestResultSize.update(result.length());
            ctx.status(HttpServletResponse.SC_OK);
        } else {
            RadarError re = new RadarError("Unable to find group based on parameters given");
            logger.info(() -> new StringBuilder().append(re.toString()).append(System.lineSeparator()).append("for request ").append(ctx.fullUrl()).toString());
            ctx.status(HttpServletResponse.SC_NOT_FOUND).json(re);
        }
    }
}
Also used : TimeSeriesGroup(cwms.radar.data.dto.TimeSeriesGroup) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) DSLContext(org.jooq.DSLContext) TimeSeriesGroupDao(cwms.radar.data.dao.TimeSeriesGroupDao) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 4 with TimeSeriesGroup

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

the class TimeSeriesGroupDaoTest method getTimeSeriesGroups.

@Test
void getTimeSeriesGroups() throws SQLException {
    try (DSLContext lrl = getDslContext(getConnection(), "LRL")) {
        TimeSeriesGroupDao dao = new TimeSeriesGroupDao(lrl);
        List<TimeSeriesGroup> groups = dao.getTimeSeriesGroups();
        assertNotNull(groups);
        assertFalse(groups.isEmpty());
    }
}
Also used : TimeSeriesGroup(cwms.radar.data.dto.TimeSeriesGroup) DSLContext(org.jooq.DSLContext) Test(org.junit.jupiter.api.Test)

Example 5 with TimeSeriesGroup

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

the class TimeSeriesGroupController method getAll.

@OpenApi(queryParams = { @OpenApiParam(name = "office", description = "Specifies the owning office of the timeseries group(s) whose data is to be included in the response. If this field is not specified, matching timeseries groups information from all offices shall be returned.") }, responses = { @OpenApiResponse(status = "200", content = { @OpenApiContent(isArray = true, from = TimeSeriesGroup.class, type = Formats.JSON) // @OpenApiContent(isArray = true, from = TabV1TimeseriesGroup.class, type = Formats.TAB ),
// @OpenApiContent(isArray = true, from = CsvV1TimeseriesGroup.class, type = Formats.CSV )
}), @OpenApiResponse(status = "404", description = "Based on the combination of inputs provided the timeseries group(s) were not found."), @OpenApiResponse(status = "501", description = "request format is not implemented") }, description = "Returns CWMS Timeseries Groups Data", tags = { TAG })
@Override
public void getAll(Context ctx) {
    getAllRequests.mark();
    try (final Timer.Context timeContext = getAllRequestsTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        TimeSeriesGroupDao dao = new TimeSeriesGroupDao(dsl);
        String office = ctx.queryParam("office");
        List<TimeSeriesGroup> grps = dao.getTimeSeriesGroups(office);
        if (grps.isEmpty()) {
            RadarError re = new RadarError("No data found for The provided office");
            logger.info(() -> re.toString() + " for request " + ctx.fullUrl());
            ctx.status(HttpCode.NOT_FOUND).json(re);
        } else {
            String formatHeader = ctx.header(Header.ACCEPT);
            ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null);
            String result = Formats.format(contentType, grps, TimeSeriesGroup.class);
            ctx.result(result).contentType(contentType.toString());
            requestResultSize.update(result.length());
            ctx.status(HttpServletResponse.SC_OK);
        }
    }
}
Also used : TimeSeriesGroup(cwms.radar.data.dto.TimeSeriesGroup) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) DSLContext(org.jooq.DSLContext) TimeSeriesGroupDao(cwms.radar.data.dao.TimeSeriesGroupDao) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Aggregations

TimeSeriesGroup (cwms.radar.data.dto.TimeSeriesGroup)5 DSLContext (org.jooq.DSLContext)4 Timer (com.codahale.metrics.Timer)2 RadarError (cwms.radar.api.errors.RadarError)2 TimeSeriesGroupDao (cwms.radar.data.dao.TimeSeriesGroupDao)2 TimeSeriesCategory (cwms.radar.data.dto.TimeSeriesCategory)2 ContentType (cwms.radar.formatters.ContentType)2 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)2 AssignedTimeSeries (cwms.radar.data.dto.AssignedTimeSeries)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Pair (kotlin.Pair)1 NotNull (org.jetbrains.annotations.NotNull)1 Condition (org.jooq.Condition)1 Record (org.jooq.Record)1 RecordMapper (org.jooq.RecordMapper)1 SelectOnConditionStep (org.jooq.SelectOnConditionStep)1