Skip to content

Commit

Permalink
fix(SILVA-515): adding count of status
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Dec 6, 2024
1 parent 668ad12 commit 32269ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -45,27 +46,27 @@ public List<OpeningsPerYearDto> getOpeningSubmissionTrends(
orgUnits == null ? List.of("NOVALUE") : orgUnits
);

entities
.forEach(
entity -> log.info(
"Opening Submission Trends data found: {} {} {}",
entity.getOpeningId(),
entity.getOrgUnitCode(),
entity.getOrgUnitName()
)
);

if (entities.isEmpty()) {
log.info("No Opening Submission Trends data found!");
return List.of();
}

// Map to count entries grouped by month
Map<Integer, Long> monthToCountMap = entities.stream()
// Group by month and status
Map<Integer, Map<String, Long>> monthToStatusCountMap = entities.stream()
.filter(entity -> entity.getEntryTimestamp() != null) // Ensure timestamp is not null
.collect(Collectors.groupingBy(
entity -> entity.getEntryTimestamp().getMonthValue(), // Extract month value
Collectors.counting() // Count occurrences
Collectors.groupingBy(
OpeningTrendsProjection::getStatus, // Group by status
Collectors.counting() // Count occurrences
)
));

// Map to count total entries grouped by month
Map<Integer, Long> monthToCountMap = monthToStatusCountMap.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey, // Month
entry -> entry.getValue().values().stream().mapToLong(Long::longValue).sum() // Sum counts per status
));

// Generate a 12-month sequence starting from the start date
Expand All @@ -78,7 +79,8 @@ public List<OpeningsPerYearDto> getOpeningSubmissionTrends(
.map(month -> new OpeningsPerYearDto(
month,
getMonthName(month),
monthToCountMap.getOrDefault(month, 0L) // Use 0 if no entries for this month
monthToCountMap.getOrDefault(month, 0L), // Total count for the month
monthToStatusCountMap.getOrDefault(month, Collections.emptyMap()) // Status counts map
))
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.restapi.results.postgres.dto;

import java.util.Map;
import lombok.Builder;
import lombok.With;

Expand All @@ -11,7 +12,8 @@
public record OpeningsPerYearDto(
Integer month,
String monthName,
Long amount
Long amount,
Map<String,Long> statusCounts
) {

}

0 comments on commit 32269ad

Please sign in to comment.