Skip to content

Commit

Permalink
Merge pull request #6516 from davidwatkins73/waltz-6508-cost-years
Browse files Browse the repository at this point in the history
Waltz 6508 cost years
  • Loading branch information
davidwatkins73 authored Mar 26, 2023
2 parents 12131b6 + 2e3a742 commit fd15704
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 124 deletions.
34 changes: 5 additions & 29 deletions waltz-data/src/main/java/org/finos/waltz/data/cost/CostDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,39 +88,28 @@ public Set<EntityCost> findByEntityReference(EntityReference ref){
}


public Set<EntityCost> findBySelector(GenericSelector genericSelector){

SelectHavingStep<Record2<Long, Integer>> latestYearForCostKindSelector = DSL
.select(COST.COST_KIND_ID, DSL.max(COST.YEAR).as("latest_year"))
.from(COST)
.where(COST.ENTITY_ID.in(genericSelector.selector())
.and(COST.ENTITY_KIND.eq(genericSelector.kind().name())))
.groupBy(COST.COST_KIND_ID);

Condition latestYearForCostKind = COST.COST_KIND_ID.eq(latestYearForCostKindSelector.field(COST.COST_KIND_ID))
.and(COST.YEAR.eq(latestYearForCostKindSelector.field("latest_year", Integer.class)));

public Set<EntityCost> findBySelector(GenericSelector genericSelector,
int year){
return dsl
.select(ENTITY_NAME_FIELD)
.select(COST.fields())
.from(COST)
.innerJoin(latestYearForCostKindSelector).on(dsl.renderInlined(latestYearForCostKind))
.where(COST.ENTITY_ID.in(genericSelector.selector())
.and(COST.ENTITY_KIND.eq(genericSelector.kind().name())))
.and(COST.YEAR.eq(year))
.fetchSet(TO_COST_MAPPER);
}


public Set<EntityCost> findTopCostsForCostKindAndSelector(long costKindId,
int year,
GenericSelector genericSelector,
int limit){

SelectConditionStep<Record1<Integer>> latestYear = mkLatestYearSelector(costKindId, genericSelector);

Condition cond = COST.ENTITY_ID.in(genericSelector.selector())
.and(COST.ENTITY_KIND.eq(genericSelector.kind().name()))
.and(COST.COST_KIND_ID.eq(costKindId))
.and(COST.YEAR.eq(latestYear));
.and(COST.YEAR.eq(year));

SelectSeekStep1<Record, BigDecimal> qry = dsl
.select(ENTITY_NAME_FIELD)
Expand Down Expand Up @@ -203,17 +192,4 @@ public Tuple2<Integer, Integer> getMappedAndMissingCountsForKindAndYearBySelecto
r.get(appCount) - r.get(appsWithCostsCount)));
}


// -- HELPERS -------------

private SelectConditionStep<Record1<Integer>> mkLatestYearSelector(long costKindId,
GenericSelector genericSelector) {
return DSL
.select(DSL.max(COST.YEAR).as("latest_year"))
.from(COST)
.where(COST.COST_KIND_ID.eq(costKindId))
.and(COST.ENTITY_ID.in(genericSelector.selector())
.and(COST.ENTITY_KIND.eq(genericSelector.kind().name())));
}

}
52 changes: 35 additions & 17 deletions waltz-data/src/main/java/org/finos/waltz/data/cost/CostKindDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@

package org.finos.waltz.data.cost;

import org.finos.waltz.model.cost.CostKindWithYears;
import org.finos.waltz.model.cost.ImmutableCostKindWithYears;
import org.finos.waltz.schema.tables.records.CostKindRecord;
import org.finos.waltz.data.GenericSelector;
import org.finos.waltz.model.cost.EntityCostKind;
import org.finos.waltz.model.cost.ImmutableEntityCostKind;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.DSL;
import org.jooq.lambda.tuple.Tuple2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Set;

import static java.util.stream.Collectors.toSet;
import static org.finos.waltz.schema.Tables.COST;
import static org.finos.waltz.schema.Tables.COST_KIND;
import static org.jooq.lambda.tuple.Tuple.tuple;


@Repository
public class CostKindDao {

private final DSLContext dsl;

private RecordMapper<Record, EntityCostKind> TO_COST_KIND_MAPPER = r -> {
private static final RecordMapper<Record, EntityCostKind> TO_COST_KIND_MAPPER = r -> {
CostKindRecord record = r.into(COST_KIND);
return ImmutableEntityCostKind.builder()
.id(record.getId())
Expand All @@ -54,30 +54,48 @@ public class CostKindDao {
};


private final DSLContext dsl;


@Autowired
public CostKindDao(DSLContext dsl) {
this.dsl = dsl;
}


public Set<EntityCostKind> findAll(){
return dsl
.select(COST_KIND.fields())
.from(COST_KIND)
.fetchSet(TO_COST_KIND_MAPPER);
public Set<CostKindWithYears> findAll(){
return findCostKindsByCondition(DSL.trueCondition());
}


public Set<Tuple2<EntityCostKind, Integer>> findCostKindsBySelector(GenericSelector genericSelector){
public Set<CostKindWithYears> findCostKindsBySelector(GenericSelector genericSelector) {

Condition cond = COST.ENTITY_ID.in(genericSelector.selector())
.and(COST.ENTITY_KIND.eq(genericSelector.kind().name()));

return findCostKindsByCondition(cond);
}


private Set<CostKindWithYears> findCostKindsByCondition(Condition cond){
return dsl
.select(COST_KIND.fields())
.select(DSL.max(COST.YEAR))
.selectDistinct(COST_KIND.fields())
.select(COST.YEAR)
.from(COST_KIND)
.innerJoin(COST).on(COST_KIND.ID.eq(COST.COST_KIND_ID))
.where(COST.ENTITY_ID.in(genericSelector.selector())
.and(COST.ENTITY_KIND.eq(genericSelector.kind().name())))
.groupBy(COST_KIND.fields())
.fetchSet(r -> tuple(TO_COST_KIND_MAPPER.map(r), r.get(DSL.max(COST.YEAR))));
.where(cond)
.orderBy(COST.YEAR.desc())
.fetchGroups(
TO_COST_KIND_MAPPER,
r -> r.get(COST.YEAR))
.entrySet()
.stream()
.map(kv -> ImmutableCostKindWithYears
.builder()
.costKind(kv.getKey())
.years(kv.getValue())
.build())
.collect(toSet());
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.finos.waltz.model.cost;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;

import java.util.List;

@Value.Immutable
@JsonSerialize(as = ImmutableCostKindWithYears.class)
public abstract class CostKindWithYears {

public abstract EntityCostKind costKind();

public abstract List<Integer> years();

}
18 changes: 10 additions & 8 deletions waltz-ng/client/common/svelte/entity-pickers/CostKindPicker.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@
import {costKindStore} from "../../../svelte-stores/cost-kind-store";
import _ from "lodash";
const columnDefs = [
{ field: "name", name: "Cost Kind", width: "30%"},
{ field: "description", name: "Description", width: "70%"},
];
export let onSelect = () => console.log("Selecting cost kind");
export let selectionFilter = () => true;
let rowData = [];
$: costKindCall = costKindStore.findAll();
$: costKinds = $costKindCall.data;
$: rowData = _
.chain(costKinds)
.map(d => d.costKind)
.filter(selectionFilter)
.orderBy(d => d.name)
.value();
const columnDefs = [
{ field: "name", name: "Cost Kind", width: "30%"},
{ field: "description", name: "Description", width: "70%"},
];
</script>

<div class="help-block small">
<Icon name="info-circle"/>Select a cost kind from the list below, you can filter the list using the search bar.
<Icon name="info-circle"/>
Select a cost kind from the list below, you can filter the list using the search bar.
</div>
<br>
<Grid {columnDefs}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,56 @@
<div class="col-sm-12">
<waltz-no-data>
<message>
<strong>No {{$ctrl.targetEntityKind | toDisplayName:'entity'}} costs found.</strong>
<strong>
No
<span ng-bind="$ctrl.targetEntityKind | toDisplayName:'entity'"></span>
costs found
</strong>
</message>
</waltz-no-data>
</div>
</div>

<div class="row" ng-if="$ctrl.costKinds.length > 0">
<div class="row"
ng-if="$ctrl.costKinds.length > 0">
<div class="col-sm-12"
ng-if="$ctrl.selectedKind">
<h4 style="min-height: 2em; vertical-align: center">
<h4>
<span ng-bind="$ctrl.targetEntityKind | toDisplayName:'entity'"></span>
<span ng-bind="$ctrl.selectedKind.name"></span>
costs -
<span ng-bind="$ctrl.latestYearByKindId[$ctrl.selectedKind.id]"></span>
:
<small>
<a class="clickable"
ng-if="!$ctrl.visibility.selectKind"
ng-click="$ctrl.visibility.selectKind = true">Change cost kind
</a>

<span ng-if="$ctrl.visibility.selectKind">
<select ng-model="$ctrl.selectedKind"
ng-change="$ctrl.refresh()"
ng-options="kind as kind.name for kind in $ctrl.costKinds"
id="selectedKind"
class="form-control wacss-form-control-inline">
</select>
</span>
</small>
<span ng-bind="$ctrl.selectedYear"></span>
<waltz-icon ng-if="$ctrl.visibility.loading"
name="spinner"
spin="true">
</waltz-icon>
</h4>
<div>
<label for="selectedKind">
Change Cost Kind:
</label>
<select ng-model="$ctrl.selectedKind"
ng-change="$ctrl.onKindChange()"
ng-options="kind as kind.name for kind in $ctrl.costKinds"
id="selectedKind"
class="form-control wacss-form-control-inline">
</select>

&nbsp;
&nbsp;

<label for="selectedYear">
Change Year:
</label>
<select ng-model="$ctrl.selectedYear"
ng-change="$ctrl.onYearChange()"
ng-options="year for year in $ctrl.costYears"
id="selectedYear"
class="form-control wacss-form-control-inline">
</select>
</div>

<br>
</div>
</div>

Expand Down
Loading

0 comments on commit fd15704

Please sign in to comment.