Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 17 additions & 36 deletions sql/reports/challenges/challenges-history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ SELECT
c.groups as "groupNames",
c.status as "challengeStatus",
c."registrationStartDate" as "registrationStartDate",
CASE WHEN c.status = 'COMPLETED' THEN
(SELECT cp."actualEndDate" FROM challenges."ChallengePhase" cp WHERE cp."challengeId"=c.id ORDER BY cp."scheduledEndDate" DESC LIMIT 1)
ELSE null END as "challengeCompletionDate",
CASE
WHEN c.status = 'COMPLETED' THEN latest_phase."actualEndDate"
ELSE null
END as "challengeCompletionDate",
c.tags as "tags",
c."projectId" as "projectId"
FROM
challenges."Challenge" c
LEFT JOIN LATERAL (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ performance]
The use of LEFT JOIN LATERAL is a good choice for fetching the latest phase's actualEndDate. However, ensure that the database engine being used supports this feature efficiently, as it might have performance implications depending on the size of the ChallengePhase table.

SELECT cp."actualEndDate"
FROM challenges."ChallengePhase" cp
WHERE cp."challengeId" = c.id
ORDER BY cp."scheduledEndDate" DESC
LIMIT 1
) latest_phase ON true
LEFT JOIN
challenges."ChallengeBilling" cb on cb."challengeId"=c.id
-- filter by billing account
Expand All @@ -19,40 +27,13 @@ WHERE ($1::text[] IS NULL OR cb."billingAccountId" = ANY($1::text[]))
AND ($2::text[] IS NULL OR cb."billingAccountId" <> ANY($2::text[]))
-- filter by challenge status
AND ($3::text[] IS NULL OR c.status::text= ANY($3::text[]))
-- -- filter by completion date
-- filter by challengeCompletedDate
AND (c.status!='COMPLETED' OR (
-- filter by completion date bounds on the latest challenge phase end date
AND (
($4::timestamptz IS NULL AND $5::timestamptz IS NULL)
OR (
$5::timestamptz IS NULL AND (
(
SELECT cp."actualEndDate"
FROM challenges."ChallengePhase" cp
WHERE cp."challengeId" = c.id
ORDER BY cp."scheduledEndDate" DESC
LIMIT 1
) >= $4::timestamptz
)
latest_phase."actualEndDate" IS NOT NULL

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ correctness]
The condition latest_phase."actualEndDate" IS NOT NULL is crucial for ensuring that the date filters are applied only when an actual end date exists. This is a good safeguard for correctness, but be aware that it might exclude challenges that are completed but have no recorded end date, depending on the data integrity.

AND ($4::timestamptz IS NULL OR latest_phase."actualEndDate" >= $4::timestamptz)
AND ($5::timestamptz IS NULL OR latest_phase."actualEndDate" <= $5::timestamptz)
)
OR (
$4::timestamptz IS NULL AND $5::timestamptz IS NOT NULL AND (
(
SELECT cp."actualEndDate"
FROM challenges."ChallengePhase" cp
WHERE cp."challengeId" = c.id
ORDER BY cp."scheduledEndDate" DESC
LIMIT 1
) >= $5::timestamptz
)
)
OR (
$4::timestamptz IS NOT NULL AND $5::timestamptz IS NOT NULL AND ((
SELECT cp."actualEndDate"
FROM challenges."ChallengePhase" cp
WHERE cp."challengeId" = c.id
ORDER BY cp."scheduledEndDate" DESC
LIMIT 1
) BETWEEN $4::timestamptz AND $5::timestamptz)
)
))
)
LIMIT 1000;
53 changes: 17 additions & 36 deletions sql/reports/challenges/registrants-history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ SELECT
c.status as "challengeStatus",
cw.handle as "winnerHandle",
CASE WHEN sub.placement = 1 THEN true ELSE false END as "isWinner",
CASE WHEN c.status = 'COMPLETED' THEN
(SELECT cp."actualEndDate" FROM challenges."ChallengePhase" cp WHERE cp."challengeId"=c.id ORDER BY cp."scheduledEndDate" DESC LIMIT 1)
ELSE null END as "challengeCompletedDate",
CASE
WHEN c.status = 'COMPLETED' THEN latest_phase."actualEndDate"
ELSE null
END as "challengeCompletedDate",
res."memberHandle" as "registrantHandle",
ROUND(sub."finalScore", 2) as "registrantFinalScore"
FROM
challenges."Challenge" c
LEFT JOIN LATERAL (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ maintainability]
The use of LEFT JOIN LATERAL is appropriate here to simplify the query logic by removing the repeated subquery. However, ensure that the database engine being used supports LATERAL joins, as not all databases do. This could impact portability if the query is intended to be used across different database systems.

SELECT cp."actualEndDate"
FROM challenges."ChallengePhase" cp
WHERE cp."challengeId" = c.id
ORDER BY cp."scheduledEndDate" DESC
LIMIT 1
) latest_phase ON true
LEFT JOIN
resources."Resource" res ON c.id = res."challengeId"
LEFT JOIN
Expand All @@ -27,40 +35,13 @@ WHERE rr.name = 'Submitter'
AND ($2::text[] IS NULL OR cb."billingAccountId" <> ANY($2::text[]))
-- filter by challenge status
AND ($3::text[] IS NULL OR c.status::text= ANY($3::text[]))
-- -- filter by completion date
-- filter by challengeCompletedDate
AND (c.status!='COMPLETED' OR (
-- filter by completion date bounds on the latest challenge phase end date
AND (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ correctness]
The condition latest_phase."actualEndDate" IS NOT NULL is crucial for ensuring that the subsequent comparisons with $4 and $5 are valid. However, consider whether there should be additional handling or logging for cases where latest_phase."actualEndDate" is NULL, as this could indicate incomplete data for a 'COMPLETED' challenge.

($4::timestamptz IS NULL AND $5::timestamptz IS NULL)
OR (
$5::timestamptz IS NULL AND (
(
SELECT cp."actualEndDate"
FROM challenges."ChallengePhase" cp
WHERE cp."challengeId" = c.id
ORDER BY cp."scheduledEndDate" DESC
LIMIT 1
) >= $4::timestamptz
)
latest_phase."actualEndDate" IS NOT NULL
AND ($4::timestamptz IS NULL OR latest_phase."actualEndDate" >= $4::timestamptz)
AND ($5::timestamptz IS NULL OR latest_phase."actualEndDate" <= $5::timestamptz)
)
OR (
$4::timestamptz IS NULL AND $5::timestamptz IS NOT NULL AND (
(
SELECT cp."actualEndDate"
FROM challenges."ChallengePhase" cp
WHERE cp."challengeId" = c.id
ORDER BY cp."scheduledEndDate" DESC
LIMIT 1
) >= $5::timestamptz
)
)
OR (
$4::timestamptz IS NOT NULL AND $5::timestamptz IS NOT NULL AND ((
SELECT cp."actualEndDate"
FROM challenges."ChallengePhase" cp
WHERE cp."challengeId" = c.id
ORDER BY cp."scheduledEndDate" DESC
LIMIT 1
) BETWEEN $4::timestamptz AND $5::timestamptz)
)
))
)
LIMIT 1000;
Loading