Skip to content

Commit

Permalink
Pull request #329: BUG: Export Measurable ratings seems to overfetch …
Browse files Browse the repository at this point in the history
…data

Merge in WALTZ/waltz from WALTZ/waltz-dw:CTCTOWALTZ-2964-bug-measurable-export-6870 to db-feature/waltz-6870-bug-alloc-export

* commit '5af3791854a0453d3f5cda4670658e211c5ca9ac':
  BUG: Export Measurable ratings seems to overfetch data
  • Loading branch information
db-waltz committed Nov 21, 2023
2 parents 5c66eac + 5af3791 commit 9dbb507
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
<waltz-data-extract-link name="Export"
styling="link"
filename="ratings.csv"
extract="allocations/measurable-category/{{$ctrl.measurableCategory.id}}"
method="POST"
extract="allocations/measurable/{{$ctrl.parentEntityRef.id}}"
method="GET"
request-body="$ctrl.selector">
</waltz-data-extract-link>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.Record2;
import org.jooq.Select;
import org.jooq.SelectConditionStep;
import org.jooq.SelectSelectStep;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static org.finos.waltz.schema.Tables.*;
import java.util.Optional;

import static org.finos.waltz.schema.Tables.ALLOCATION;
import static org.finos.waltz.schema.Tables.ALLOCATION_SCHEME;
import static org.finos.waltz.schema.Tables.APPLICATION;
import static org.finos.waltz.schema.Tables.ENTITY_HIERARCHY;
import static org.finos.waltz.schema.Tables.MEASURABLE;
import static org.finos.waltz.schema.Tables.MEASURABLE_CATEGORY;
import static org.finos.waltz.schema.Tables.MEASURABLE_RATING;
import static org.finos.waltz.schema.Tables.ORGANISATIONAL_UNIT;
import static org.finos.waltz.schema.Tables.RATING_SCHEME_ITEM;
import static org.finos.waltz.web.WebUtilities.mkPath;
import static spark.Spark.get;
import static spark.Spark.post;


Expand All @@ -54,9 +65,10 @@ public AllocationsExtractor(DSLContext dsl) {

@Override
public void register() {
registerExtractForAll( WebUtilities.mkPath("data-extract", "allocations", "all"));
registerExtractForCategory( WebUtilities.mkPath("data-extract", "allocations", "measurable-category", ":measurableCategoryId"));
registerExtractForScheme( WebUtilities.mkPath("data-extract", "allocations", "allocation-scheme", ":schemeId"));
registerExtractForAll(mkPath("data-extract", "allocations", "all"));
registerExtractForCategory(mkPath("data-extract", "allocations", "measurable-category", ":measurableCategoryId"));
registerExtractForMeasurable(mkPath("data-extract", "allocations", "measurable", ":id"));
registerExtractForScheme(mkPath("data-extract", "allocations", "allocation-scheme", ":schemeId"));
}


Expand All @@ -65,7 +77,7 @@ private void registerExtractForAll(String path) {
IdSelectionOptions idSelectionOptions = WebUtilities.readIdSelectionOptionsFromBody(request);
SelectConditionStep<Record> qry = prepareQuery(
DSL.trueCondition(),
idSelectionOptions);
Optional.of(idSelectionOptions));

return writeExtract(
"all_allocations",
Expand All @@ -76,6 +88,34 @@ private void registerExtractForAll(String path) {
}


private void registerExtractForMeasurable(String path) {
get(path, (request, response) -> {

long measurableId = WebUtilities.getId(request);

Record1<String> fileName = dsl
.select(DSL.concat(MEASURABLE.NAME, "_all_allocations"))
.from(MEASURABLE)
.where(MEASURABLE.ID.eq(measurableId))
.fetchOne();

SelectConditionStep<Record> qry = prepareQuery(
MEASURABLE.ID.in(DSL
.select(ENTITY_HIERARCHY.ID)
.from(ENTITY_HIERARCHY)
.where(ENTITY_HIERARCHY.KIND.eq(EntityKind.MEASURABLE.name())
.and(ENTITY_HIERARCHY.ANCESTOR_ID.eq(measurableId)))),
Optional.empty());

return writeExtract(
fileName.value1(),
qry,
request,
response);
});
}


private void registerExtractForCategory(String path) {
post(path, (request, response) -> {

Expand All @@ -91,7 +131,7 @@ private void registerExtractForCategory(String path) {

SelectConditionStep<Record> qry = prepareQuery(
MEASURABLE.MEASURABLE_CATEGORY_ID.eq(measurableCategoryId),
applicationIdSelectionOptions);
Optional.of(applicationIdSelectionOptions));

return writeExtract(
fileName.value1(),
Expand All @@ -118,7 +158,7 @@ private void registerExtractForScheme(String path) {

SelectConditionStep<Record> qry = prepareQuery(
ALLOCATION_SCHEME.ID.eq(schemeId),
applicationIdSelectionOptions);
Optional.of(applicationIdSelectionOptions));

String filename = fileNameInfoRow.value1() + "_" + fileNameInfoRow.value2();

Expand All @@ -134,8 +174,7 @@ private void registerExtractForScheme(String path) {
// -- HELPER ----

private SelectConditionStep<Record> prepareQuery(Condition additionalCondition,
IdSelectionOptions idSelectionOptions) {
Select<Record1<Long>> appSelector = applicationIdSelectorFactory.apply(idSelectionOptions);
Optional<IdSelectionOptions> idSelectionOptions) {
SelectSelectStep<Record> reportColumns = dsl
.select(APPLICATION.NAME.as("Application Name"),
APPLICATION.ID.as("Application Waltz Id"),
Expand All @@ -162,8 +201,13 @@ private SelectConditionStep<Record> prepareQuery(Condition additionalCondition,
DSL.coalesce(ALLOCATION.LAST_UPDATED_BY, "").as("Allocation Last Updated By"),
DSL.coalesce(ALLOCATION.PROVENANCE, "").as("Allocation Provenance"));

Condition condition = MEASURABLE_RATING.ENTITY_ID.in(appSelector)
.and(MEASURABLE_RATING.ENTITY_KIND.eq(EntityKind.APPLICATION.name()))
Condition appCondition = idSelectionOptions
.map(applicationIdSelectorFactory)
.map(selector -> MEASURABLE_RATING.ENTITY_ID.in(selector)
.and(MEASURABLE_RATING.ENTITY_KIND.eq(EntityKind.APPLICATION.name())))
.orElse(DSL.trueCondition());

Condition condition = appCondition
.and(ENTITY_HIERARCHY.ID.eq(ENTITY_HIERARCHY.ANCESTOR_ID))
.and(ENTITY_HIERARCHY.KIND.eq(EntityKind.MEASURABLE.name()))
.and(ENTITY_HIERARCHY.ID.eq(MEASURABLE_RATING.MEASURABLE_ID))
Expand Down

0 comments on commit 9dbb507

Please sign in to comment.