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
63 changes: 52 additions & 11 deletions src/routes/dashboard/admin/print/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { db } from '$lib/server/db/index.js';
import { project, user, devlog, legionReview } from '$lib/server/db/schema.js';
import { error } from '@sveltejs/kit';
import { eq, and, sql, ne, inArray, desc, gt } from 'drizzle-orm';
import { eq, and, sql, ne, inArray, desc, gt, lt, max } from 'drizzle-orm';
import type { Actions } from './$types';
import { getCurrentlyPrinting } from './utils.server';

Expand Down Expand Up @@ -31,15 +31,13 @@ export async function load({ locals }) {
.from(user)
.where(and(ne(user.trust, 'red'), ne(user.hackatimeTrust, 'red'))); // hide banned users

const legionAgg = db
.$with('legionAgg')
.as(
db
.select({ userId: legionReview.userId, legionCnt: sql<number>`COUNT(*)`.as('legionCnt') })
.from(legionReview)
.where(eq(legionReview.action, 'print'))
.groupBy(legionReview.userId)
);
const legionAgg = db.$with('legionAgg').as(
db
.select({ userId: legionReview.userId, legionCnt: sql<number>`COUNT(*)`.as('legionCnt') })
.from(legionReview)
.where(eq(legionReview.action, 'print'))
.groupBy(legionReview.userId)
);

const totalExpr = sql<number>`COALESCE(${legionAgg.legionCnt}, 0)`;

Expand All @@ -53,12 +51,55 @@ export async function load({ locals }) {

const currentlyPrinting = await getCurrentlyPrinting(locals.user);

// get the slow ahh printers (2 days)
const threeDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
const latestMarking = db
.select({
projectId: legionReview.projectId,
latestTimestamp: max(legionReview.timestamp).as('latest_timestamp')
})
.from(legionReview)
.where(eq(legionReview.action, 'mark_for_printing'))
.groupBy(legionReview.projectId)
.as('latest_marking');

const stuckPrinting = await db
.select({
id: project.id,
name: project.name,
printer: {
id: user.id,
name: user.name
},
updatedAt: latestMarking.latestTimestamp
})
.from(project)
.innerJoin(latestMarking, eq(latestMarking.projectId, project.id))
.innerJoin(
legionReview,
and(
eq(legionReview.projectId, project.id),
eq(legionReview.action, 'mark_for_printing'),
eq(legionReview.timestamp, latestMarking.latestTimestamp)
)
)
.innerJoin(user, eq(user.id, legionReview.userId))
.where(
and(
eq(project.status, 'printing'),
eq(project.deleted, false),
lt(latestMarking.latestTimestamp, threeDaysAgo)
)
)
.orderBy(desc(latestMarking.latestTimestamp));

return {
allProjects,
projects,
users,
currentlyPrinting,
leaderboard
leaderboard,
stuckPrinting
};
}

Expand Down
27 changes: 26 additions & 1 deletion src/routes/dashboard/admin/print/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { enhance } from '$app/forms';
import Head from '$lib/components/Head.svelte';
import { projectStatuses } from '$lib/utils.js';
import { ExternalLink, Info } from '@lucide/svelte';
import { ExternalLink, Info, TriangleAlert } from '@lucide/svelte';
import relativeDate from 'tiny-relative-date';

let { data, form } = $props();
Expand Down Expand Up @@ -43,6 +43,31 @@
</div>
{/if}

{#if data.stuckPrinting.length > 0}
<div class="themed-box mb-3 p-3">
<div class="mb-3 flex items-center gap-2">
<TriangleAlert class="text-orange-400" />
<h2 class="text-xl font-bold">Projects Stuck in Printing</h2>
</div>
<div class="space-y-2">
{#each data.stuckPrinting as stuck}
{@const daysStuck = Math.floor((Date.now() - (stuck.updatedAt?.getTime() ?? 0)) / (1000 * 60 * 60 * 24))}
<div class="flex items-center justify-between rounded border-l-3 border-primary-500 bg-primary-900/50 p-2.5 text-sm">
<div>
<a href={`/dashboard/admin/print/${stuck.id}`} class="underline hover:text-primary-300"
>{stuck.name}</a
>
<span class="text-primary-400">
- {stuck.printer?.name || 'Unknown printer'}
</span>
</div>
<span class="font-bold text-primary-500">{daysStuck}d</span>
</div>
{/each}
</div>
</div>
{/if}

<div class="flex flex-col-reverse gap-5 lg:flex-row">
<div class="themed-box grow p-3">
<h2 class="mb-2 text-xl font-bold">Filter & Sort</h2>
Expand Down
Loading