added experiment-table: refactored all frontend pages and services

This commit is contained in:
averel10
2026-03-20 06:51:49 +01:00
parent 5464eae0e9
commit 4b35e2392e
13 changed files with 1598 additions and 51 deletions

View File

@@ -7,44 +7,58 @@ import { annotation } from '@/lib/model/annotation';
import { eq, and } from 'drizzle-orm';
import { auth } from '@/lib/auth';
import { headers } from 'next/headers';
import type { AnnotationEntry } from '@/lib/dialects';
import type { DatasetEntryForAnnotation } from '@/lib/dialects';
import { experiment } from '@/lib/model/experiment';
/**
* Returns unannotated entries for the given dataset and authenticated user.
*/
export async function getAnnotationEntries(datasetId: number): Promise<AnnotationEntry[]> {
export async function getAnnotationEntries(experimentId: number): Promise<DatasetEntryForAnnotation[]> {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) throw new Error('Nicht angemeldet');
const allEntries = await db
const currentExperiments = await db
.select()
.from(experiment)
.where(eq(experiment.id, experimentId))
.leftJoin(dataset, eq(experiment.datasetId, dataset.id))
.limit(1);
if (currentExperiments.length === 0) {
throw new Error('Experiment nicht gefunden');
}
if(!currentExperiments[0].dataset) {
throw new Error('Experiment hat kein zugeordnetes Dataset');
}
const allDatasetEntries = await db
.select()
.from(dataset_entry)
.where(eq(dataset_entry.datasetId, datasetId));
.where(eq(dataset_entry.datasetId, currentExperiments[0].dataset.id));
if (allEntries.length === 0) return [];
if (allDatasetEntries.length === 0) return [];
// Find entries already annotated by this user in this dataset (via join)
const annotated = await db
.select({ entryId: annotation.datasetEntryId })
const annotationEntries = await db
.select()
.from(annotation)
.innerJoin(dataset_entry, eq(annotation.datasetEntryId, dataset_entry.id))
.where(
and(
eq(annotation.userId, session.user.id),
eq(dataset_entry.datasetId, datasetId)
eq(annotation.experimentId, experimentId)
)
);
const annotatedIds = new Set(annotated.map((a) => a.entryId));
const remaining = allEntries.filter((e) => !annotatedIds.has(e.id));
const mapped: AnnotationEntry[] = remaining.map((e) => ({
const mapped: DatasetEntryForAnnotation[] = allDatasetEntries.map((e) => ({
id: e.id,
externalId: e.externalId,
fileName: e.fileName,
dialect: e.dialect,
durationMs: e.durationMs,
experimentId: experimentId,
datasetId: e.datasetId,
annotation: annotationEntries.find((a) => a.datasetEntryId === e.id)?.rating || null,
}));
return mapped;
@@ -55,7 +69,8 @@ export async function getAnnotationEntries(datasetId: number): Promise<Annotatio
* Silently ignores duplicates (onConflictDoNothing).
*/
export async function saveAnnotations(
ratings: { entryId: number; rating: number; dialectLabel: string }[]
ratings: { entryId: number; rating: number; dialectLabel: string }[],
experimentId: number
): Promise<void> {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) throw new Error('Nicht angemeldet');
@@ -65,22 +80,30 @@ export async function saveAnnotations(
for (const { entryId, rating, dialectLabel } of ratings) {
await db
.insert(annotation)
.values({ datasetEntryId: entryId, userId: session.user.id, rating, dialectLabel })
.values({experimentId, datasetEntryId: entryId, userId: session.user.id, rating, dialectLabel })
.onConflictDoNothing();
}
}
/** Returns all datasets (for the home page). */
export async function getAllDatasets() {
return db.select().from(dataset).orderBy(dataset.name);
export async function getAllExperiments() {
return db.select().from(experiment).orderBy(experiment.id);
}
/** Returns annotation progress for the current user in a dataset. */
export async function getAnnotationProgress(
datasetId: number
experimentId: number
): Promise<{ total: number; done: number }> {
const session = await auth.api.getSession({ headers: await headers() });
const datasetIdResult = await db
.select({ datasetId: experiment.datasetId })
.from(experiment)
.where(eq(experiment.id, experimentId))
.limit(1);
const datasetId = datasetIdResult[0]?.datasetId;
const allEntries = await db
.select({ id: dataset_entry.id })
.from(dataset_entry)
@@ -97,7 +120,7 @@ export async function getAnnotationProgress(
.where(
and(
eq(annotation.userId, session.user.id),
eq(dataset_entry.datasetId, datasetId)
eq(annotation.experimentId, experimentId)
)
);

View File

View File

@@ -6,20 +6,20 @@ import { getAnnotationEntries } from '@/app/actions/annotations';
import SingleChoiceView from '@/components/AnnotationViews/SingleChoiceView';
interface Props {
params: Promise<{ datasetId: string; prototype: string }>;
params: Promise<{ experimentId: string; prototype: string }>;
}
export default async function AnnotatePage({ params }: Props) {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) redirect('/user/sign-in');
const { datasetId: datasetIdStr, prototype } = await params;
const datasetId = parseInt(datasetIdStr, 10);
const { experimentId: experimentIdStr, prototype } = await params;
const experimentId = parseInt(experimentIdStr, 10);
if (isNaN(datasetId)) {
if (isNaN(experimentId)) {
return (
<div className="max-w-xl mx-auto py-16 text-center">
<p className="text-gray-600">Ungültige Dataset-ID.</p>
<p className="text-gray-600">Ungültige Experiment-ID.</p>
<Link href="/" className="text-blue-600 hover:underline mt-4 inline-block">
Startseite
</Link>
@@ -31,7 +31,7 @@ export default async function AnnotatePage({ params }: Props) {
notFound();
}
const entries = await getAnnotationEntries(datasetId);
const entries = await getAnnotationEntries(experimentId);
if (entries.length === 0) {
return (
@@ -41,7 +41,7 @@ export default async function AnnotatePage({ params }: Props) {
Alle Samples bewertet!
</h1>
<p className="text-gray-600 mb-8">
Sie haben alle Samples in diesem Dataset bereits bewertet. Danke für Ihre Mitarbeit!
Sie haben alle Samples in diesem Experiment bereits bewertet. Danke für Ihre Mitarbeit!
</p>
<Link
href="/"
@@ -53,5 +53,5 @@ export default async function AnnotatePage({ params }: Props) {
);
}
return <SingleChoiceView entries={entries} datasetId={datasetId} />;
return <SingleChoiceView entries={entries} experimentId={experimentId} />;
}

View File

@@ -1,7 +1,7 @@
import Link from 'next/link';
import { headers } from 'next/headers';
import { auth } from '@/lib/auth';
import { getAllDatasets, getAnnotationProgress } from '@/app/actions/annotations';
import { getAllExperiments, getAnnotationProgress } from '@/app/actions/annotations';
export default async function HomePage() {
const session = await auth.api.getSession({ headers: await headers() });
@@ -25,49 +25,49 @@ export default async function HomePage() {
);
}
const datasets = await getAllDatasets();
const experiments = await getAllExperiments();
if (datasets.length === 0) {
if (experiments.length === 0) {
return (
<div className="max-w-xl mx-auto text-center py-20">
<h1 className="text-2xl font-bold text-gray-800 mb-3">
Keine Datasets verfügbar
Keine Experimente verfügbar
</h1>
<p className="text-gray-500">
Es wurden noch keine Datasets angelegt. Wenden Sie sich an einen Administrator.
Es wurden noch keine Experimente freigeschalten. Wenden Sie sich an einen Administrator.
</p>
</div>
);
}
// Fetch progress for all datasets in parallel
// Fetch progress for all experiments in parallel
const progressData = await Promise.all(
datasets.map((ds) => getAnnotationProgress(ds.id))
experiments.map((exp) => getAnnotationProgress(exp.id))
);
return (
<div className="max-w-2xl mx-auto py-8">
<h1 className="text-2xl font-bold text-gray-800 mb-2">Datasets</h1>
<h1 className="text-2xl font-bold text-gray-800 mb-2">Experimente</h1>
<p className="text-gray-500 mb-8">
Wählen Sie ein Dataset aus, um mit der Annotation zu beginnen oder fortzufahren.
Wählen Sie ein Experiment aus, um mit der Annotation zu beginnen oder fortzufahren.
</p>
<div className="flex flex-col gap-4">
{datasets.map((ds, i) => {
{experiments.map((exp, i) => {
const { total, done } = progressData[i];
const pct = total > 0 ? Math.round((done / total) * 100) : 0;
const isFinished = total > 0 && done >= total;
return (
<div
key={ds.id}
key={exp.id}
className="bg-white border border-gray-200 rounded-xl p-5 shadow-sm"
>
<div className="flex items-start justify-between gap-4">
<div className="flex-1 min-w-0">
<h2 className="font-semibold text-gray-800 truncate">{ds.name}</h2>
{ds.description && (
<p className="text-sm text-gray-500 mt-0.5">{ds.description}</p>
<h2 className="font-semibold text-gray-800 truncate">{exp.name}</h2>
{exp.description && (
<p className="text-sm text-gray-500 mt-0.5">{exp.description}</p>
)}
{/* Progress */}
@@ -88,7 +88,7 @@ export default async function HomePage() {
</div>
<Link
href={`/annotate/${ds.id}`}
href={`/annotate/${exp.id}`}
className={`flex-shrink-0 px-4 py-2 rounded-lg font-medium text-sm transition-colors ${
isFinished
? 'bg-green-100 text-green-700 hover:bg-green-200'