Skip to content

Commit

Permalink
refactor(db): consider null curr_round as lottery round
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiDood committed Jul 12, 2024
1 parent 4076bbc commit 2db4495
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 23 deletions.
4 changes: 2 additions & 2 deletions postgres/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ CREATE SCHEMA drap
)
CREATE TABLE drafts (
draft_id BIGINT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY,
curr_round SMALLINT NOT NULL DEFAULT 0,
curr_round SMALLINT DEFAULT 0,
max_rounds SMALLINT NOT NULL CONSTRAINT max_rounds_above_floor CHECK (max_rounds > 0),
active_period TSTZRANGE NOT NULL DEFAULT TSTZRANGE '[now,)',
CONSTRAINT curr_round_within_bounds CHECK (curr_round BETWEEN 0 AND max_rounds + 1),
CONSTRAINT curr_round_within_bounds CHECK (curr_round BETWEEN 0 AND max_rounds),
CONSTRAINT overlapping_draft_periods EXCLUDE USING gist (active_period WITH &&)
)
CREATE TABLE student_ranks (
Expand Down
2 changes: 1 addition & 1 deletion src/lib/models/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type InferOutput, bigint, date, minValue, nullable, number, object, pip

export const Draft = object({
draft_id: bigint(),
curr_round: pipe(number(), safeInteger(), minValue(0)),
curr_round: nullable(pipe(number(), safeInteger(), minValue(0))),
max_rounds: pipe(number(), safeInteger(), minValue(0)),
active_period_start: date(),
active_period_end: nullable(date()),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/server/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export class Database implements Loggable {
@timed async incrementDraftRound(draft: Draft['draft_id']) {
const sql = this.#sql;
const [first, ...rest] =
await sql`UPDATE drap.drafts SET curr_round = curr_round + 1 WHERE draft_id = ${draft} RETURNING curr_round, max_rounds`;
await sql`UPDATE drap.drafts SET curr_round = curr_round + 1 WHERE draft_id = ${draft} ON CONFLICT ON CONSTRAINT curr_round_within_bounds DO UPDATE SET curr_round = NULL RETURNING curr_round, max_rounds `;
strictEqual(rest.length, 0);
return typeof first === 'undefined' ? null : parse(IncrementedDraftRound, first);
}
Expand Down
4 changes: 2 additions & 2 deletions src/routes/dashboard/(draft)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

<div class="card prose max-w-none p-4 dark:prose-invert">
<p>
{#if curr_round > max_rounds}
{#if curr_round === null}
<strong>Draft &num;{draft_id}</strong> (which opened last <strong>{startDate}</strong> at
<strong>{startTime}</strong>) has recently finished the main drafting process. It is currently in the
lottery rounds.
lottery round.
{:else}
<strong>Draft &num;{draft_id}</strong> is currently on Round <strong>{curr_round}</strong>
of <strong>{max_rounds}</strong>. It opened last <strong>{startDate}</strong> at
Expand Down
12 changes: 4 additions & 8 deletions src/routes/dashboard/(draft)/ranks/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@
} = data);
</script>

{#if curr_round > 0}
<WarningAlert>
{#if curr_round > max_rounds}
A lottery is ongoing. You may join again soon in the next draft.
{:else}
A draft is currently ongoing. You may no longer register.
{/if}
</WarningAlert>
{#if curr_round === null}
<WarningAlert>A lottery is currently ongoing. You may join again soon in the next draft.</WarningAlert>
{:else if curr_round > 0}
<WarningAlert>A draft is currently ongoing. You may no longer register.</WarningAlert>
{:else if Array.isArray(info)}
<SubmitRankings draftId={draft_id} maxRounds={max_rounds} availableLabs={info} />
{:else}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/dashboard/(draft)/students/+page.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const actions = {

const round = await db.incrementDraftRound(draft);
assert(round !== null);
if (round.curr_round > round.max_rounds) break;
if (round.curr_round === null) break;
db.logger.info({ incrementDraftRound: round });

const autoAcknowledgeLabsWithoutPreferences = await db.autoAcknowledgeLabsWithoutPreferences(draft);
Expand Down
5 changes: 2 additions & 3 deletions src/routes/dashboard/(draft)/students/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
lab: { lab_name, quota },
} = data);
$: suffix = getOrdinalSuffix(curr_round);
let draftees: string[] = [];
$: remainingQuota = quota - researchers.length;
$: remainingDraftees = remainingQuota - draftees.length;
</script>

{#if students.length > 0}
{#if curr_round !== null && students.length > 0}
{@const suffix = getOrdinalSuffix(curr_round)}
<div class="grid grid-cols-1 gap-4 sm:grid-cols-[auto_1fr]">
<div class="prose dark:prose-invert">
<h2>Draft Picks</h2>
Expand Down
25 changes: 21 additions & 4 deletions src/routes/dashboard/drafts/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<div class="card prose max-w-none p-4 dark:prose-invert">
<p>
{#if curr_round > max_rounds}
{#if curr_round === null}
<strong>Draft &num;{draft_id}</strong> (which opened last <strong>{startDate}</strong> at
<strong>{startTime}</strong>) has recently finished the main drafting process. It is currently in the
lottery rounds.
Expand All @@ -36,7 +36,7 @@
{/if}
</p>
</div>
{#if curr_round > max_rounds}
{#if curr_round === null}
<div class="grid grid-cols-1 gap-4 md:grid-cols-[auto_1fr]">
<div class="prose dark:prose-invert">
<h3>Lottery</h3>
Expand Down Expand Up @@ -65,7 +65,24 @@
After the randomization stage, the draft process is officially complete. All students, lab heads, and
administrators are notified of the final results.
</p>
<form action="?/conclude" method="post" class="not-prose" use:enhance>
<form
action="?/conclude"
method="post"
class="not-prose"
use:enhance={({ submitter, cancel }) => {
if (!confirm('Are you sure you want to apply these interventions?')) {
cancel();
return;
}
assert(submitter !== null);
assert(submitter instanceof HTMLButtonElement);
submitter.disabled = true;
return async ({ update }) => {
submitter.disabled = false;
await update();
};
}}
>
<button type="submit" class="variant-filled-primary btn btn-lg w-full">
<Icon src={ArrowRight} class="size-8" />
<span>Conclude Draft</span>
Expand All @@ -90,7 +107,7 @@
submitter.disabled = true;
return async ({ update, result }) => {
submitter.disabled = false;
await update({ reset: false });
await update();
switch (result.type) {
case 'success':
toast.trigger({
Expand Down
2 changes: 1 addition & 1 deletion src/routes/dashboard/labs/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
{@const startTime = format(active_period_start, 'pp')}
<WarningAlert>
<span
>{#if curr_round > max_rounds}
>{#if curr_round === null}
<strong>Draft &num;{draft_id}</strong> started last <strong>{startDate}</strong> at
<strong>{startTime}</strong> and is now in lottery mode. It's still unsafe to update the lab quota.
{:else}
Expand Down

0 comments on commit 2db4495

Please sign in to comment.