Skip to content

Commit

Permalink
Fix map keys when adding entries to an existing matching stats instance
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsrnhld committed Jan 10, 2025
1 parent d96e881 commit 87d3060
Showing 1 changed file with 25 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.jooq.impl.DSL.asterisk;
import static org.jooq.impl.DSL.count;
import static org.jooq.impl.DSL.countDistinct;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.max;
import static org.jooq.impl.DSL.min;
Expand Down Expand Up @@ -101,22 +100,6 @@ public UpdateMatchingStatsSqlJob(
this.executors = MoreExecutors.listeningDecorator(executors);
}

private static boolean isTreeConcept(final Concept<?> concept) {
if (!(concept instanceof TreeConcept)) {
log.error("Collecting MatchingStats is currently only supported for TreeConcepts.");
return false;
}
return true;
}

private static void addEntryToConceptElement(final ConceptTreeNode<?> mostSpecificChild, final String columnKey, final MatchingStats.Entry entry) {
if (mostSpecificChild.getMatchingStats() == null) {
((ConceptElement<?>) mostSpecificChild).setMatchingStats(new MatchingStats());
}

mostSpecificChild.getMatchingStats().putEntry(columnKey, entry);
}

@Override
public void execute() throws Exception {

Expand Down Expand Up @@ -161,6 +144,14 @@ public String getLabel() {
return "Calculating Matching Stats for %s.".formatted(executionService);
}

private static boolean isTreeConcept(final Concept<?> concept) {
if (!(concept instanceof TreeConcept)) {
log.error("Collecting MatchingStats is currently only supported for TreeConcepts.");
return false;
}
return true;
}

private void calculateMatchingStats(final TreeConcept treeConcept) {

final Map<Connector, Set<Field<?>>> relevantColumns = collectRelevantColumns(treeConcept);
Expand All @@ -187,8 +178,8 @@ private void calculateMatchingStats(final TreeConcept treeConcept) {

final SelectHavingStep<Record> query = dslContext.select(relevantColumnsAliased)
.select(
field(ENTITIES),
count(asterisk()).as(EVENTS),
countDistinct(field(ENTITIES)).as(ENTITIES),
validityDateExpression.as(DATES)
)
.from(unioned)
Expand Down Expand Up @@ -328,10 +319,16 @@ private Field<String> toValidityDateExpression(final Map<Connector, List<ColumnD

private void mapRecordToConceptElements(final TreeConcept treeConcept, final Record record, final ConceptTreeCache treeCache) {
final CalculatedValue<Map<String, Object>> rowMap = new CalculatedValue<>(record::intoMap);
final MatchingStats.Entry entry = toMatchingStatsEntry(record);

// as we group by primary id, a record contains the matching stats for a single entity
final long events = record.get(EVENTS, Integer.class).longValue();
final String entity = record.get(ENTITIES, String.class);
final CDateRange dateSpan = toDateRange(record.get(DATES, String.class));

final MatchingStats.Entry entry = new MatchingStats.Entry(events, 1, dateSpan.getMinValue(), dateSpan.getMaxValue());

if (treeConcept.getChildren().isEmpty()) {
addEntryToConceptElement(treeConcept, treeConcept.getName(), entry);
addEntryToConceptElement(treeConcept, entity, entry);
return;
}

Expand All @@ -352,7 +349,7 @@ private void mapRecordToConceptElements(final TreeConcept treeConcept, final Rec
// add child stats to all parents till concept root
ConceptTreeNode<?> current = mostSpecificChild;
while (current != null) {
addEntryToConceptElement(current, columnValue, entry);
addEntryToConceptElement(current, entity, entry);
current = current.getParent();
}
}
Expand All @@ -361,14 +358,6 @@ private void mapRecordToConceptElements(final TreeConcept treeConcept, final Rec
}
}

private MatchingStats.Entry toMatchingStatsEntry(Record record) {
final long events = record.get(EVENTS, Integer.class).longValue();
final long entities = record.get(ENTITIES, Integer.class).longValue();
final CDateRange dateSpan = toDateRange(record.get(DATES, String.class));

return new MatchingStats.Entry(events, entities, dateSpan.getMinValue(), dateSpan.getMaxValue());
}

private CDateRange toDateRange(final String validityDateExpression) {
final List<Integer> dateRange = executionService.getResultSetProcessor().getCDateSetParser().toEpochDayRange(validityDateExpression);

Expand All @@ -379,4 +368,11 @@ private CDateRange toDateRange(final String validityDateExpression) {
return CDateRange.fromList(dateRange);
}

private static void addEntryToConceptElement(final ConceptTreeNode<?> mostSpecificChild, final String columnKey, final MatchingStats.Entry entry) {
if (mostSpecificChild.getMatchingStats() == null) {
((ConceptElement<?>) mostSpecificChild).setMatchingStats(new MatchingStats());
}
mostSpecificChild.getMatchingStats().putEntry(columnKey, entry);
}

}

0 comments on commit 87d3060

Please sign in to comment.