Skip to content

Commit

Permalink
fix sextet report perf
Browse files Browse the repository at this point in the history
  • Loading branch information
Joosakur committed Feb 20, 2025
1 parent 1d197c4 commit 2da4aa7
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions service/src/main/kotlin/fi/espoo/evaka/reports/SextetReport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fi.espoo.evaka.placement.PlacementType
import fi.espoo.evaka.shared.DaycareId
import fi.espoo.evaka.shared.auth.AuthenticatedUser
import fi.espoo.evaka.shared.db.Database
import fi.espoo.evaka.shared.db.Predicate
import fi.espoo.evaka.shared.domain.EvakaClock
import fi.espoo.evaka.shared.domain.FiniteDateRange
import fi.espoo.evaka.shared.domain.getHolidays
Expand Down Expand Up @@ -61,6 +62,25 @@ fun Database.Read.sextetReport(
placementType: PlacementType,
): List<SextetReportRow> {
val holidays = getHolidays(FiniteDateRange(from, to))

val absencePredicate =
if (placementType.absenceCategories().size == 1) {
Predicate {
where(
"NOT EXISTS (SELECT FROM absence WHERE child_id = $it.child_id AND date = $it.date)"
)
}
} else {
Predicate {
where(
"""
NOT EXISTS (SELECT FROM absence WHERE child_id = $it.child_id AND date = $it.date AND category = 'BILLABLE') OR
NOT EXISTS (SELECT FROM absence WHERE child_id = $it.child_id AND date = $it.date AND category = 'NONBILLABLE')
"""
)
}
}

return createQuery {
sql(
"""
Expand Down Expand Up @@ -100,22 +120,17 @@ WITH operational_days AS (
SELECT
ep.unit_id,
d.name AS unit_name,
ep.placement_type,
${bind(placementType)} AS placement_type,
count(ep.date) AS attendance_days
FROM effective_placements ep
JOIN daycare d ON d.id = ep.unit_id
WHERE NOT EXISTS (
SELECT 1
FROM absence
WHERE child_id = ep.child_id AND date = ep.date
HAVING count(category) >= cardinality(absence_categories(ep.placement_type))
) AND (
WHERE ${predicate(absencePredicate.forTable("ep"))} AND (
ep.has_shift_care OR extract(isodow FROM ep.date) = ANY(d.operation_days)
) AND (
(ep.has_shift_care AND d.shift_care_open_on_holidays) OR ep.date != ALL (${bind(holidays)})
)
AND ep.placement_type = ${bind(placementType)}
GROUP BY ep.unit_id, d.name, ep.placement_type
GROUP BY ep.unit_id, d.name
ORDER BY d.name
"""
)
Expand Down

0 comments on commit 2da4aa7

Please sign in to comment.