Search in sources :

Example 1 with TallyPack

use of com.khartec.waltz.model.tally.TallyPack in project waltz by khartec.

the class EntityStatisticHarness method main.

public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
    DSLContext dsl = ctx.getBean(DSLContext.class);
    EntityStatisticValueDao dao = ctx.getBean(EntityStatisticValueDao.class);
    EntityStatisticService service = ctx.getBean(EntityStatisticService.class);
    /*
        select
  outcome,
  sum(CASE ISNUMERIC(value) when 1 then cast(value as BIGINT) else 0 end)
from entity_statistic_value
where statistic_id = 20010
GROUP BY outcome;
         */
    /*
        AggregateFunction<BigDecimal> summer = DSL.sum(DSL.cast(esv.VALUE, Long.class));

        dsl.select(esv.OUTCOME, summer)
                .from(esv)
                .where(esv.STATISTIC_ID.eq(20010L))
                .groupBy(esv.OUTCOME)
                .fetch()
                .forEach(System.out::println);

*/
    IdSelectionOptions selectionOptions = ImmutableIdSelectionOptions.builder().entityReference(EntityReference.mkRef(EntityKind.ORG_UNIT, 70)).scope(HierarchyQueryScope.CHILDREN).build();
    // count by value
    List<TallyPack<String>> countByValueTallyPacks = service.calculateHistoricStatTally(10100L, RollupKind.COUNT_BY_ENTITY, selectionOptions, Duration.WEEK);
    System.out.println(countByValueTallyPacks);
    // sum by value
    List<TallyPack<String>> sumByValueTallyPacks = service.calculateHistoricStatTally(20010L, RollupKind.SUM_BY_VALUE, selectionOptions, Duration.YEAR);
    System.out.println(sumByValueTallyPacks);
    // pre-computed
    List<TallyPack<String>> preComputedTallyPacks = service.calculateHistoricStatTally(11000L, RollupKind.NONE, selectionOptions, Duration.ALL);
    System.out.println(preComputedTallyPacks);
    System.out.println("done");
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) TallyPack(com.khartec.waltz.model.tally.TallyPack) EntityStatisticValueDao(com.khartec.waltz.data.entity_statistic.EntityStatisticValueDao) DSLContext(org.jooq.DSLContext) EntityStatisticService(com.khartec.waltz.service.entity_statistic.EntityStatisticService)

Example 2 with TallyPack

use of com.khartec.waltz.model.tally.TallyPack in project waltz by khartec.

the class EntityStatisticSummaryDao method generateWithNoRollup.

public TallyPack<String> generateWithNoRollup(Long statisticId, EntityReference entityReference) {
    Condition condition = mkNoRollupCondition(newArrayList(statisticId), entityReference, esv.CURRENT.eq(true));
    Result<Record4<Long, String, String, Timestamp>> values = dsl.select(esv.STATISTIC_ID, esv.OUTCOME, esv.VALUE, max(esv.CREATED_AT).as(maxCreatedAtField)).from(esv).where(dsl.renderInlined(condition)).groupBy(esv.STATISTIC_ID, esv.OUTCOME, esv.VALUE).fetch();
    LocalDateTime maxCreatedAt = values.isNotEmpty() ? values.get(0).getValue(maxCreatedAtField).toLocalDateTime() : nowUtc();
    List<Tally<String>> tallies = values.stream().map(r -> ImmutableTally.<String>builder().count(Double.parseDouble(r.getValue(esv.VALUE))).id(r.getValue(esv.OUTCOME)).build()).collect(toList());
    return ImmutableTallyPack.<String>builder().entityReference(EntityReference.mkRef(EntityKind.ENTITY_STATISTIC, statisticId)).tallies(tallies).lastUpdatedAt(maxCreatedAt).build();
}
Also used : LocalDateTime(java.time.LocalDateTime) DSL(org.jooq.impl.DSL) DateTimeUtilities.nowUtc(com.khartec.waltz.common.DateTimeUtilities.nowUtc) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) EntityReference(com.khartec.waltz.model.EntityReference) ListUtilities.newArrayList(com.khartec.waltz.common.ListUtilities.newArrayList) Duration(com.khartec.waltz.model.Duration) Function(java.util.function.Function) EntityKind(com.khartec.waltz.model.EntityKind) BigDecimal(java.math.BigDecimal) Future(java.util.concurrent.Future) org.jooq(org.jooq) ImmutableTally(com.khartec.waltz.model.tally.ImmutableTally) ImmutableTallyPack(com.khartec.waltz.model.tally.ImmutableTallyPack) Repository(org.springframework.stereotype.Repository) Unchecked(org.jooq.lambda.Unchecked) Checks.checkNotNull(com.khartec.waltz.common.Checks.checkNotNull) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) Tally(com.khartec.waltz.model.tally.Tally) Collectors(java.util.stream.Collectors) Date(java.sql.Date) List(java.util.List) Tuple.tuple(org.jooq.lambda.tuple.Tuple.tuple) TallyPack(com.khartec.waltz.model.tally.TallyPack) DBExecutorPoolInterface(com.khartec.waltz.data.DBExecutorPoolInterface) Collections(java.util.Collections) ENTITY_STATISTIC_VALUE(com.khartec.waltz.schema.tables.EntityStatisticValue.ENTITY_STATISTIC_VALUE) ImmutableTally(com.khartec.waltz.model.tally.ImmutableTally) Tally(com.khartec.waltz.model.tally.Tally)

Example 3 with TallyPack

use of com.khartec.waltz.model.tally.TallyPack in project waltz by khartec.

the class EntityStatisticSummaryDao method generateSummary.

private <T> TallyPack<String> generateSummary(Long statisticId, Select<Record1<Long>> appIdSelector, Field<T> aggregateField, Function<T, Double> toTally) {
    Condition condition = mkSummaryCondition(newArrayList(statisticId), appIdSelector, esv.CURRENT.eq(true));
    Result<Record3<String, T, Timestamp>> values = dsl.select(esv.OUTCOME, aggregateField, max(esv.CREATED_AT).as(maxCreatedAtField)).from(esv).where(condition).groupBy(esv.OUTCOME).fetch();
    LocalDateTime maxCreatedAt = values.isNotEmpty() ? values.get(0).getValue(maxCreatedAtField).toLocalDateTime() : nowUtc();
    List<Tally<String>> tallies = values.stream().map(r -> ImmutableTally.<String>builder().count(toTally.apply(r.getValue(aggregateField))).id(r.getValue(esv.OUTCOME)).build()).collect(toList());
    return ImmutableTallyPack.<String>builder().entityReference(EntityReference.mkRef(EntityKind.ENTITY_STATISTIC, statisticId)).tallies(tallies).lastUpdatedAt(maxCreatedAt).build();
}
Also used : LocalDateTime(java.time.LocalDateTime) DSL(org.jooq.impl.DSL) DateTimeUtilities.nowUtc(com.khartec.waltz.common.DateTimeUtilities.nowUtc) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) EntityReference(com.khartec.waltz.model.EntityReference) ListUtilities.newArrayList(com.khartec.waltz.common.ListUtilities.newArrayList) Duration(com.khartec.waltz.model.Duration) Function(java.util.function.Function) EntityKind(com.khartec.waltz.model.EntityKind) BigDecimal(java.math.BigDecimal) Future(java.util.concurrent.Future) org.jooq(org.jooq) ImmutableTally(com.khartec.waltz.model.tally.ImmutableTally) ImmutableTallyPack(com.khartec.waltz.model.tally.ImmutableTallyPack) Repository(org.springframework.stereotype.Repository) Unchecked(org.jooq.lambda.Unchecked) Checks.checkNotNull(com.khartec.waltz.common.Checks.checkNotNull) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) Tally(com.khartec.waltz.model.tally.Tally) Collectors(java.util.stream.Collectors) Date(java.sql.Date) List(java.util.List) Tuple.tuple(org.jooq.lambda.tuple.Tuple.tuple) TallyPack(com.khartec.waltz.model.tally.TallyPack) DBExecutorPoolInterface(com.khartec.waltz.data.DBExecutorPoolInterface) Collections(java.util.Collections) ENTITY_STATISTIC_VALUE(com.khartec.waltz.schema.tables.EntityStatisticValue.ENTITY_STATISTIC_VALUE) ImmutableTally(com.khartec.waltz.model.tally.ImmutableTally) Tally(com.khartec.waltz.model.tally.Tally)

Aggregations

TallyPack (com.khartec.waltz.model.tally.TallyPack)3 Checks.checkNotNull (com.khartec.waltz.common.Checks.checkNotNull)2 DateTimeUtilities.nowUtc (com.khartec.waltz.common.DateTimeUtilities.nowUtc)2 ListUtilities.newArrayList (com.khartec.waltz.common.ListUtilities.newArrayList)2 DBExecutorPoolInterface (com.khartec.waltz.data.DBExecutorPoolInterface)2 Duration (com.khartec.waltz.model.Duration)2 EntityKind (com.khartec.waltz.model.EntityKind)2 EntityReference (com.khartec.waltz.model.EntityReference)2 ImmutableTally (com.khartec.waltz.model.tally.ImmutableTally)2 ImmutableTallyPack (com.khartec.waltz.model.tally.ImmutableTallyPack)2 Tally (com.khartec.waltz.model.tally.Tally)2 ENTITY_STATISTIC_VALUE (com.khartec.waltz.schema.tables.EntityStatisticValue.ENTITY_STATISTIC_VALUE)2 BigDecimal (java.math.BigDecimal)2 Date (java.sql.Date)2 Timestamp (java.sql.Timestamp)2 LocalDateTime (java.time.LocalDateTime)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 Future (java.util.concurrent.Future)2