-
Notifications
You must be signed in to change notification settings - Fork 0
Optimization for registrants history report #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,86 @@ | ||
| WITH submitter_role AS ( | ||
| SELECT rr.id | ||
| FROM resources."ResourceRole" rr | ||
| WHERE rr."nameLower" = 'submitter' | ||
| LIMIT 1 | ||
| ), | ||
| filtered_challenges AS ( | ||
| SELECT | ||
| c.id, | ||
| c.status, | ||
| latest_phase."actualEndDate" as "challengeCompletedDate" | ||
| FROM | ||
| challenges."Challenge" c | ||
| LEFT JOIN LATERAL ( | ||
| SELECT cp."actualEndDate" | ||
| FROM challenges."ChallengePhase" cp | ||
| WHERE cp."challengeId" = c.id | ||
| ORDER BY cp."scheduledEndDate" DESC | ||
| LIMIT 1 | ||
| ) latest_phase ON true | ||
| WHERE | ||
| -- filter by billing account | ||
| ( | ||
| $1::text[] IS NULL | ||
| OR EXISTS ( | ||
| SELECT 1 | ||
| FROM challenges."ChallengeBilling" cb | ||
| WHERE cb."challengeId" = c.id | ||
| AND cb."billingAccountId" = ANY($1::text[]) | ||
| ) | ||
| ) | ||
| -- filter by excluding billing account | ||
| AND ( | ||
| $2::text[] IS NULL | ||
| OR NOT EXISTS ( | ||
| SELECT 1 | ||
| FROM challenges."ChallengeBilling" cb | ||
| WHERE cb."challengeId" = c.id | ||
| AND 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 bounds on the latest challenge phase end date | ||
| AND ( | ||
| ($4::timestamptz IS NULL AND $5::timestamptz IS NULL) | ||
| OR ( | ||
| 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) | ||
| ) | ||
| ) | ||
| ) | ||
| SELECT | ||
| c.id as "challengeId", | ||
| c.status as "challengeStatus", | ||
| registrants."challengeId", | ||
| registrants."challengeStatus", | ||
| cw.handle as "winnerHandle", | ||
| CASE WHEN sub.placement = 1 THEN true ELSE false END as "isWinner", | ||
| CASE | ||
| WHEN c.status = 'COMPLETED' THEN latest_phase."actualEndDate" | ||
| WHEN registrants."challengeStatus" = 'COMPLETED' | ||
| THEN registrants."challengeCompletedDate" | ||
| ELSE null | ||
| END as "challengeCompletedDate", | ||
| res."memberHandle" as "registrantHandle", | ||
| registrants."registrantHandle", | ||
| ROUND(sub."finalScore", 2) as "registrantFinalScore" | ||
| FROM | ||
| challenges."Challenge" c | ||
| LEFT JOIN LATERAL ( | ||
| 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 | ||
| resources."ResourceRole" rr on res."roleId" = rr.id | ||
| LEFT JOIN | ||
| challenges."ChallengeWinner" cw ON c.id = cw."challengeId" AND cw."userId"::text=res."memberId" | ||
| LEFT JOIN | ||
| reviews."submission" sub on sub."memberId"=res."memberId" AND sub."challengeId"=c.id | ||
| LEFT JOIN | ||
| challenges."ChallengeBilling" cb on cb."challengeId"=c.id | ||
| WHERE rr.name = 'Submitter' | ||
| -- filter by billing account | ||
| AND ($1::text[] IS NULL OR cb."billingAccountId" = ANY($1::text[])) | ||
| -- filter by excluding billing account | ||
| 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 bounds on the latest challenge phase end date | ||
| AND ( | ||
| ($4::timestamptz IS NULL AND $5::timestamptz IS NULL) | ||
| OR ( | ||
| 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) | ||
| ) | ||
| ) | ||
| FROM ( | ||
| SELECT | ||
| fc.id as "challengeId", | ||
| fc.status as "challengeStatus", | ||
| fc."challengeCompletedDate", | ||
| res."memberId", | ||
| res."memberHandle" as "registrantHandle" | ||
| FROM filtered_challenges fc | ||
| JOIN submitter_role sr ON true | ||
| JOIN resources."Resource" res | ||
| ON res."challengeId" = fc.id | ||
| AND res."roleId" = sr.id | ||
| LIMIT 1000 | ||
| ) registrants | ||
| LEFT JOIN challenges."ChallengeWinner" cw | ||
| ON cw."challengeId" = registrants."challengeId" | ||
| AND cw."userId"::text = registrants."memberId" | ||
| LEFT JOIN reviews."submission" sub | ||
| ON sub."challengeId" = registrants."challengeId" | ||
| AND sub."memberId" = registrants."memberId" | ||
| LIMIT 1000; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[⚠️
correctness]The
LIMIT 1000clause is applied twice: once in the subquery forregistrantsand again in the main query. This could lead to unexpected results if the intention is to limit the overall result set to 1000 rows. Consider whether both limits are necessary or if only one should be applied to achieve the desired result.