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'

View File

@@ -4,7 +4,7 @@ import { useState, useEffect, useTransition } from 'react';
import Link from 'next/link';
import WaveformPlayer from '../WaveformPlayer';
import { saveAnnotations } from '@/app/actions/annotations';
import { type AnnotationEntry, DIALECT_LABELS } from '@/lib/dialects';
import { type DatasetEntryForAnnotation, DIALECT_LABELS } from '@/lib/dialects';
const PAGE_SIZE = 1;
const AUTO_ADVANCE_DELAY_MS = 600;
@@ -18,11 +18,11 @@ const RATING_OPTIONS = (dialectLabel: string) => [
];
interface SingleChoiceViewProps {
entries: AnnotationEntry[];
datasetId: number;
entries: DatasetEntryForAnnotation[];
experimentId: number;
}
export default function SingleChoiceView({ entries, datasetId }: SingleChoiceViewProps) {
export default function SingleChoiceView({ entries, experimentId }: SingleChoiceViewProps) {
const [isPending, startTransition] = useTransition();
@@ -46,7 +46,7 @@ export default function SingleChoiceView({ entries, datasetId }: SingleChoiceVie
const timer = setTimeout(() => {
const batch = pageEntries.map((e) => ({ entryId: e.id, rating: answers[e.id], dialectLabel: e.dialect }));
startTransition(async () => {
await saveAnnotations(batch);
await saveAnnotations(batch, experimentId);
if (currentPage + 1 >= totalPages) {
setIsComplete(true);
} else {
@@ -108,7 +108,7 @@ export default function SingleChoiceView({ entries, datasetId }: SingleChoiceVie
{pageEntries.map((entry, idx) => {
const dialectLabel = DIALECT_LABELS[entry.dialect] ?? entry.dialect;
const fileExt = entry.fileName.substring(entry.fileName.lastIndexOf('.'));
const audioSrc = `/public/datasets/${datasetId}/${entry.externalId}${fileExt}`;
const audioSrc = `/public/datasets/${entry.datasetId}/${entry.externalId}${fileExt}`;
const isListened = fullyPlayed.has(entry.id);
const currentRating = answers[entry.id] ?? null;

View File

@@ -9,11 +9,13 @@ export const DIALECT_LABELS: Record<string, string> = {
de: 'Deutsch',
};
export type AnnotationEntry = {
export type DatasetEntryForAnnotation = {
id: number;
externalId: string;
fileName: string;
dialect: string;
durationMs: number | null;
datasetId: number;
experimentId: number;
annotation: number | null;
};

View File

@@ -1,24 +1,34 @@
import { sqliteTable, text, integer, uniqueIndex } from 'drizzle-orm/sqlite-core';
import { relations, sql } from 'drizzle-orm';
import { dataset_entry } from './dataset_entry';
import { experiment } from './experiment';
export const annotation = sqliteTable(
'annotation',
{
id: integer('id').primaryKey({ autoIncrement: true }),
datasetEntryId: integer('dataset_entry_id')
datasetEntryId: integer('dataset_entry_id').notNull().references(() => dataset_entry.id),
experimentId: integer('experiment_id')
.notNull()
.references(() => dataset_entry.id),
.references(() => experiment.id),
userId: text('user_id').notNull(),
rating: integer('rating').notNull(), // 15
dialectLabel: text('dialect_label').notNull(), // e.g. 'ch_be', 'ch_zh', …
createdAt: integer('created_at', { mode: 'timestamp' })
.notNull()
.default(sql`(unixepoch())`),
updatedAt: integer('updated_at', { mode: 'timestamp' })
.notNull()
.default(sql`(unixepoch())`)
.$onUpdate(() => sql`(unixepoch())`),
}
);
export const annotationRelations = relations(annotation, ({ one }) => ({
experiment: one(experiment, {
fields: [annotation.experimentId],
references: [experiment.id],
}),
datasetEntry: one(dataset_entry, {
fields: [annotation.datasetEntryId],
references: [dataset_entry.id],

View File

@@ -0,0 +1,34 @@
import { sqliteTable, text, integer, uniqueIndex } from 'drizzle-orm/sqlite-core';
import { relations, sql } from 'drizzle-orm';
import { annotation } from './annotation';
import { dataset } from './dataset';
export const experiment = sqliteTable(
'experiment',
{
id: integer('id').primaryKey({ autoIncrement: true }),
name: text('name').notNull(),
description: text('description'),
datasetId: integer('dataset_id')
.notNull()
.references(() => dataset.id),
createdAt: integer('created_at', { mode: 'timestamp' })
.notNull()
.default(sql`(unixepoch())`),
updatedAt: integer('updated_at', { mode: 'timestamp' })
.notNull()
.default(sql`(unixepoch())`)
.$onUpdate(() => sql`(unixepoch())`),
}
);
export const experimentRelations = relations(experiment, ({ one, many }) => ({
dataset: one(dataset, {
fields: [experiment.datasetId],
references: [dataset.id],
}),
annotations: many(annotation),
}));
export type Experiment = typeof experiment.$inferSelect;
export type NewExperiment = typeof experiment.$inferInsert;