diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index a407f7b..d7b2010 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -70,6 +70,65 @@ "sectionLibraries": "Libraries", "sectionUsers": "Users", "sectionProviders": "Providers", + "sectionJobs": "Jobs", + "jobs": { + "intro": "These jobs go through the whole library in the background, one item after the other. Each card says what it sends and to whom.", + "needs": "Needs {{names}} turned on: use the Providers section.", + "and": "and", + "refresh": "Do everything again", + "refreshHint": "Slower: asks again even for what already has an answer. A failure never replaces an answer that is stored.", + "start": "Start", + "startOne": "Start {{title}}", + "stop": "Stop", + "stopOne": "Stop {{title}}", + "confirmTitle": "Start {{title}}?", + "confirmBody": "This goes through the whole library. {{sends}}", + "confirmRefresh": "It asks again even for what already has an answer.", + "confirm": "Start job", + "progress": "{{done}} of {{total}}", + "last": "Last run", + "stopped": "It was stopped before the end.", + "finished": "Finished {{when}}", + "never": "This job has not run yet.", + "errors": { + "covers_job_running": "This job is already running.", + "artist_images_job_running": "This job is already running.", + "descriptions_job_running": "This job is already running." + }, + "covers": { + "title": "Album covers", + "what": "Builds the cover of every album that has none that is still good: first from the pictures in your own files, then, only for albums with none there, from the Cover Art Archive if it is on.", + "sendsNothing": "Nothing leaves this server: it only reads the pictures in your own files.", + "sendsArchive": "The release group id of every album with no picture in your files goes to the Cover Art Archive, one after the other." + }, + "artistImages": { + "title": "Artist pictures", + "what": "Looks for a free picture of every artist that has a MusicBrainz id and no answer that is still good.", + "sends": "The MusicBrainz id of every artist goes to MusicBrainz, Wikidata and Wikimedia Commons, one after the other." + }, + "descriptions": { + "title": "Artist descriptions", + "what": "Reads the Wikipedia description of every artist that has a MusicBrainz id and no answer that is still good.", + "sends": "The MusicBrainz id of every artist goes to MusicBrainz, Wikidata and Wikipedia, one after the other." + }, + "counts": { + "covers": { + "checked": "{{count}} looked at", + "from_files": "{{count}} from your files", + "from_archive": "{{count}} from the Cover Art Archive", + "none": "{{count}} with no cover", + "failed": "{{count}} could not be built", + "skipped": "{{count}} skipped" + }, + "artists": { + "checked": "{{count}} looked at", + "found": "{{count}} found", + "none": "{{count}} with nothing at the provider", + "failed": "{{count}} could not be looked up", + "skipped": "{{count}} skipped" + } + } + }, "cancel": "Cancel", "save": "Save", "errors": { diff --git a/web/src/i18n/locales/it.json b/web/src/i18n/locales/it.json index f08d7aa..d7c7fd7 100644 --- a/web/src/i18n/locales/it.json +++ b/web/src/i18n/locales/it.json @@ -70,6 +70,65 @@ "sectionLibraries": "Librerie", "sectionUsers": "Utenti", "sectionProviders": "Provider", + "sectionJobs": "Lavori", + "jobs": { + "intro": "Questi lavori passano in rassegna tutta la libreria in background, un elemento alla volta. Ogni scheda dice cosa manda e a chi.", + "needs": "Serve che {{names}} sia acceso: vai nella sezione Provider.", + "and": "e", + "refresh": "Rifai tutto", + "refreshHint": "Più lento: chiede di nuovo anche ciò che ha già una risposta. Un errore non sostituisce mai una risposta salvata.", + "start": "Avvia", + "startOne": "Avvia {{title}}", + "stop": "Ferma", + "stopOne": "Ferma {{title}}", + "confirmTitle": "Avviare {{title}}?", + "confirmBody": "Questo passa in rassegna tutta la libreria. {{sends}}", + "confirmRefresh": "Chiede di nuovo anche ciò che ha già una risposta.", + "confirm": "Avvia il lavoro", + "progress": "{{done}} di {{total}}", + "last": "Ultima esecuzione", + "stopped": "È stato fermato prima della fine.", + "finished": "Finito il {{when}}", + "never": "Questo lavoro non è ancora stato eseguito.", + "errors": { + "covers_job_running": "Questo lavoro è già in corso.", + "artist_images_job_running": "Questo lavoro è già in corso.", + "descriptions_job_running": "Questo lavoro è già in corso." + }, + "covers": { + "title": "Copertine degli album", + "what": "Costruisce la copertina di ogni album che non ne ha una ancora buona: prima dalle immagini nei tuoi file, poi, solo per gli album che lì non ne hanno, dal Cover Art Archive se è acceso.", + "sendsNothing": "Dal server non esce niente: legge solo le immagini nei tuoi file.", + "sendsArchive": "L'id del release group di ogni album senza immagine nei tuoi file va al Cover Art Archive, uno dopo l'altro." + }, + "artistImages": { + "title": "Immagini degli artisti", + "what": "Cerca un'immagine libera per ogni artista che ha un id MusicBrainz e nessuna risposta ancora buona.", + "sends": "L'id MusicBrainz di ogni artista va a MusicBrainz, Wikidata e Wikimedia Commons, uno dopo l'altro." + }, + "descriptions": { + "title": "Descrizioni degli artisti", + "what": "Legge la descrizione di Wikipedia di ogni artista che ha un id MusicBrainz e nessuna risposta ancora buona.", + "sends": "L'id MusicBrainz di ogni artista va a MusicBrainz, Wikidata e Wikipedia, uno dopo l'altro." + }, + "counts": { + "covers": { + "checked": "{{count}} esaminati", + "from_files": "{{count}} dai tuoi file", + "from_archive": "{{count}} dal Cover Art Archive", + "none": "{{count}} senza copertina", + "failed": "{{count}} non costruibili", + "skipped": "{{count}} saltati" + }, + "artists": { + "checked": "{{count}} esaminati", + "found": "{{count}} trovati", + "none": "{{count}} senza niente dal provider", + "failed": "{{count}} non cercabili", + "skipped": "{{count}} saltati" + } + } + }, "cancel": "Annulla", "save": "Salva", "errors": { diff --git a/web/src/pages/admin/JobCard.test.tsx b/web/src/pages/admin/JobCard.test.tsx new file mode 100644 index 0000000..f56d2f2 --- /dev/null +++ b/web/src/pages/admin/JobCard.test.tsx @@ -0,0 +1,209 @@ +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { render, screen, waitFor, within } from '@testing-library/react' +import userEvent from '@testing-library/user-event' +import { beforeEach, expect, test, vi } from 'vitest' +import type { JobKind } from '@/api/jobs' +import { sessionStore } from '@/auth/session' +import { initI18n } from '@/i18n' +import { apiError, mockFetch, type MockRoutes, type RecordedCall } from '@/test/mockFetch' +import JobCard from './JobCard' + +const PATH = { covers: '/api/v1/library/covers', 'artist-images': '/api/v1/library/artist-images', descriptions: '/api/v1/library/descriptions' } +const TITLE = { covers: 'Album covers', 'artist-images': 'Artist pictures', descriptions: 'Artist descriptions' } +const started = () => new Response(JSON.stringify({ status: 'started' }), { status: 202, headers: { 'Content-Type': 'application/json' } }) +const none = () => new Response(null, { status: 204 }) + +type Enabled = Partial> + +function renderCard(kind: JobKind, enabled: Enabled = { musicbrainz: true, commons: true, wikipedia: true, coverartarchive: false }) { + const client = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } }) + return render( + + + , + ) +} + +function routes(kind: JobKind, status: object, over: MockRoutes = {}): MockRoutes { + return { [`GET ${PATH[kind]}`]: () => status, ...over } +} + +const card = (kind: JobKind) => screen.findByRole('region', { name: TITLE[kind] }) +const calls = (all: RecordedCall[], key: string) => all.filter((c) => c.key === key) + +beforeEach(async () => { + vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US') + await initI18n() + sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } }) +}) + +// --- what it is and what it sends -------------------------------------------------------------- + +test.each([ + ['covers', 'Nothing leaves this server: it only reads the pictures in your own files.'], + ['artist-images', 'The MusicBrainz id of every artist goes to MusicBrainz, Wikidata and Wikimedia Commons, one after the other.'], + ['descriptions', 'The MusicBrainz id of every artist goes to MusicBrainz, Wikidata and Wikipedia, one after the other.'], +] as const)('the %s card says what it sends before anything is started', async (kind, sends) => { + mockFetch(routes(kind, { running: false })) + renderCard(kind) + expect(await card(kind)).toHaveTextContent(sends) +}) + +test('the covers card says what leaves the server once the Cover Art Archive is on', async () => { + mockFetch(routes('covers', { running: false })) + renderCard('covers', { coverartarchive: true }) + expect(await card('covers')).toHaveTextContent('The release group id of every album with no picture in your files goes to the Cover Art Archive, one after the other.') +}) + +test.each([ + ['artist-images', { musicbrainz: false, commons: true }, 'Needs MusicBrainz turned on: use the Providers section.'], + ['artist-images', { musicbrainz: false, commons: false }, 'Needs MusicBrainz and Wikimedia Commons turned on: use the Providers section.'], + ['descriptions', { musicbrainz: true, wikipedia: false }, 'Needs Wikipedia turned on: use the Providers section.'], +] as const)('a provider the %s job needs that is off blocks it, and the card says which: %j', async (kind, enabled, text) => { + mockFetch(routes(kind, { running: false })) + renderCard(kind, enabled) + const c = await card(kind) + expect(c).toHaveTextContent(text) + expect(within(c).getByRole('button', { name: `Start ${TITLE[kind]}` })).toBeDisabled() +}) + +test('while the providers are not known yet nothing blocks the job', async () => { + mockFetch(routes('artist-images', { running: false })) + renderCard('artist-images', {}) + expect(within(await card('artist-images')).getByRole('button', { name: 'Start Artist pictures' })).toBeEnabled() +}) + +// --- starting ---------------------------------------------------------------------------------- + +test('starting asks first, says it goes through the whole library, and sends nothing until the answer is yes', async () => { + const all = mockFetch(routes('artist-images', { running: false }, { [`POST ${PATH['artist-images']}`]: started })).calls + renderCard('artist-images') + const c = await card('artist-images') + await userEvent.click(within(c).getByRole('button', { name: 'Start Artist pictures' })) + const dialog = await screen.findByRole('dialog') + expect(dialog).toHaveTextContent('Start Artist pictures?') + expect(dialog).toHaveTextContent('This goes through the whole library.') + expect(dialog).toHaveTextContent('The MusicBrainz id of every artist goes to MusicBrainz, Wikidata and Wikimedia Commons') + expect(calls(all, `POST ${PATH['artist-images']}`)).toHaveLength(0) + + await userEvent.click(within(dialog).getByRole('button', { name: 'Cancel' })) + await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()) + expect(calls(all, `POST ${PATH['artist-images']}`)).toHaveLength(0) +}) + +test('yes starts the job, reading only what has no good answer', async () => { + const all = mockFetch(routes('descriptions', { running: false }, { [`POST ${PATH.descriptions}`]: started })).calls + renderCard('descriptions') + const c = await card('descriptions') + await userEvent.click(within(c).getByRole('button', { name: 'Start Artist descriptions' })) + await userEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: 'Start job' })) + await waitFor(() => expect(calls(all, `POST ${PATH.descriptions}`)).toHaveLength(1)) + expect(calls(all, `POST ${PATH.descriptions}`)[0].url.searchParams.has('refresh')).toBe(false) +}) + +test('"Do everything again" is a separate choice, said in the question and sent as refresh', async () => { + const all = mockFetch(routes('covers', { running: false }, { [`POST ${PATH.covers}`]: started })).calls + renderCard('covers') + const c = await card('covers') + await userEvent.click(within(c).getByRole('checkbox', { name: 'Do everything again' })) + await userEvent.click(within(c).getByRole('button', { name: 'Start Album covers' })) + const dialog = await screen.findByRole('dialog') + expect(dialog).toHaveTextContent('It asks again even for what already has an answer.') + await userEvent.click(within(dialog).getByRole('button', { name: 'Start job' })) + await waitFor(() => expect(calls(all, `POST ${PATH.covers}`)[0]?.url.searchParams.get('refresh')).toBe('true')) +}) + +test('a job that is already running is explained', async () => { + mockFetch(routes('covers', { running: false }, { [`POST ${PATH.covers}`]: () => apiError(409, 'covers_job_running') })) + renderCard('covers') + const c = await card('covers') + await userEvent.click(within(c).getByRole('button', { name: 'Start Album covers' })) + await userEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: 'Start job' })) + expect(await within(c).findByRole('alert')).toHaveTextContent('This job is already running.') +}) + +test('another failure to start is said in words', async () => { + mockFetch(routes('covers', { running: false }, { [`POST ${PATH.covers}`]: () => apiError(500, 'internal') })) + renderCard('covers') + const c = await card('covers') + await userEvent.click(within(c).getByRole('button', { name: 'Start Album covers' })) + await userEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: 'Start job' })) + expect(await within(c).findByRole('alert')).toHaveTextContent('Something went wrong. Try again.') +}) + +// --- running and stopping ---------------------------------------------------------------------- + +test('while it runs the card shows how far it is, cannot be started again, and can be stopped', async () => { + const all = mockFetch(routes('artist-images', { running: true, progress: { done: 30, total: 120 } }, { [`DELETE ${PATH['artist-images']}`]: none })).calls + renderCard('artist-images') + const c = await card('artist-images') + const bar = await within(c).findByRole('progressbar') + expect(bar).toHaveAttribute('aria-valuenow', '30') + expect(bar).toHaveAttribute('aria-valuemax', '120') + expect(c).toHaveTextContent('30 of 120') + expect(within(c).getByRole('button', { name: 'Start Artist pictures' })).toBeDisabled() + await userEvent.click(within(c).getByRole('button', { name: 'Stop Artist pictures' })) + await waitFor(() => expect(calls(all, `DELETE ${PATH['artist-images']}`)).toHaveLength(1)) +}) + +test('a job that is not running has no Stop button', async () => { + mockFetch(routes('covers', { running: false })) + renderCard('covers') + const c = await card('covers') + expect(within(c).queryByRole('button', { name: 'Stop Album covers' })).not.toBeInTheDocument() +}) + +// --- how it ended ------------------------------------------------------------------------------ + +test('after a covers job it says what was done, leaving out what is zero', async () => { + mockFetch( + routes('covers', { + running: false, + last_finished_at: '2026-11-12T10:00:00Z', + last: { checked: 40, from_files: 30, from_archive: 4, none: 5, skipped: 0, failed: 1, stopped: false }, + }), + ) + renderCard('covers') + const list = await within(await card('covers')).findByRole('list', { name: 'Last run' }) + expect(list).toHaveTextContent('40 looked at') + expect(list).toHaveTextContent('30 from your files') + expect(list).toHaveTextContent('4 from the Cover Art Archive') + expect(list).toHaveTextContent('5 with no cover') + expect(list).toHaveTextContent('1 could not be built') + expect(list).not.toHaveTextContent('skipped') +}) + +test('after an artist job it says what was found', async () => { + mockFetch( + routes('descriptions', { + running: false, + last_finished_at: '2026-11-12T10:00:00Z', + last: { checked: 50, found: 35, none: 10, skipped: 2, failed: 3, stopped: false }, + }), + ) + renderCard('descriptions') + const list = await within(await card('descriptions')).findByRole('list', { name: 'Last run' }) + expect(list).toHaveTextContent('50 looked at') + expect(list).toHaveTextContent('35 found') + expect(list).toHaveTextContent('10 with nothing at the provider') + expect(list).toHaveTextContent('3 could not be looked up') + expect(list).toHaveTextContent('2 skipped') +}) + +test('a job that was stopped says so', async () => { + mockFetch(routes('covers', { running: false, last: { checked: 3, from_files: 3, from_archive: 0, none: 0, skipped: 0, failed: 0, stopped: true } })) + renderCard('covers') + expect(await within(await card('covers')).findByText('It was stopped before the end.')).toBeInTheDocument() +}) + +test('the reason a job ended early is shown as it is', async () => { + mockFetch(routes('artist-images', { running: false, last_error: 'the providers failed for five artists in a row' })) + renderCard('artist-images') + expect(await within(await card('artist-images')).findByRole('alert')).toHaveTextContent('the providers failed for five artists in a row') +}) + +test('before the first run it says so', async () => { + mockFetch(routes('covers', { running: false })) + renderCard('covers') + expect(await within(await card('covers')).findByText('This job has not run yet.')).toBeInTheDocument() +}) diff --git a/web/src/pages/admin/JobCard.tsx b/web/src/pages/admin/JobCard.tsx new file mode 100644 index 0000000..a3b9675 --- /dev/null +++ b/web/src/pages/admin/JobCard.tsx @@ -0,0 +1,153 @@ +import { useState } from 'react' +import { useTranslation } from 'react-i18next' +import type { ProviderName } from '@/api/adminProviders' +import { useJobStatus, useStartJob, useStopJob, type JobKind } from '@/api/jobs' +import { ErrorMessage, Loading } from '@/components/Status' +import { Button } from '@/components/ui/button' +import Confirm from './Confirm' +import { errorText } from './errorText' + +/** The providers a job cannot do without. With any of them off the job ends at once, so the card does not start it. */ +const NEEDS: Record = { + covers: [], + 'artist-images': ['musicbrainz', 'commons'], + descriptions: ['musicbrainz', 'wikipedia'], +} + +/** The key of the job in the texts. */ +const TEXT: Record = { covers: 'covers', 'artist-images': 'artistImages', descriptions: 'descriptions' } + +const SCOPES = ['admin.jobs.errors', 'admin.errors'] + +type T = ReturnType['t'] + +/** What the last run did, as short sentences, leaving out what is zero (except what was looked at). */ +function Summary({ kind, last, finishedAt }: { kind: JobKind; last: Record; finishedAt?: string }) { + const { t, i18n } = useTranslation() + const keys = kind === 'covers' ? ['checked', 'from_files', 'from_archive', 'none', 'failed', 'skipped'] : ['checked', 'found', 'none', 'failed', 'skipped'] + const rows = keys.filter((key) => key === 'checked' || Number(last[key]) > 0) + return ( +
+
    + {rows.map((key) => ( +
  • + {t(`admin.jobs.counts.${kind === 'covers' ? 'covers' : 'artists'}.${key}`, { count: Number(last[key]) })} +
  • + ))} +
+ {last.stopped === true &&

{t('admin.jobs.stopped')}

} + {finishedAt &&

{t('admin.jobs.finished', { when: new Date(finishedAt).toLocaleString(i18n.language) })}

} +
+ ) +} + +/** What the job sends, in words: for covers it depends on whether the Cover Art Archive is on. */ +function sends(t: T, kind: JobKind, enabled: Partial>): string { + if (kind === 'covers') return t(enabled.coverartarchive ? 'admin.jobs.covers.sendsArchive' : 'admin.jobs.covers.sendsNothing') + return t(`admin.jobs.${TEXT[kind]}.sends`) +} + +/** + * One bulk job: what it does and what it sends, start (after a question that says it goes through the whole + * library), progress, stop, and how the last run ended. The status follows the job by itself while it runs. + */ +export default function JobCard({ kind, enabled }: { kind: JobKind; enabled: Partial> }) { + const { t } = useTranslation() + const status = useJobStatus(kind) + const start = useStartJob(kind) + const stop = useStopJob(kind) + const [asking, setAsking] = useState(false) + const [refresh, setRefresh] = useState(false) + const title = t(`admin.jobs.${TEXT[kind]}.title`) + const running = status.data?.running === true + const off = NEEDS[kind].filter((name) => enabled[name] === false) + const progress = status.data?.progress + const last = status.data?.last as Record | undefined + const failure = start.error ?? stop.error + + return ( +
+

{title}

+

{t(`admin.jobs.${TEXT[kind]}.what`)}

+

{sends(t, kind, enabled)}

+ + {off.length > 0 && ( +

+ {t('admin.jobs.needs', { names: off.map((name) => t(`provider.${name}`)).join(` ${t('admin.jobs.and')} `) })} +

+ )} + + +

{t('admin.jobs.refreshHint')}

+ +
+ + {running && ( + + )} +
+ + {failure ? ( +

+ {errorText(t, failure, SCOPES)} +

+ ) : null} + + {status.isError ? ( + void status.refetch()} /> + ) : status.isPending ? ( + + ) : running ? ( +
+ {progress && ( + <> +
+
0 ? Math.min(100, (progress.done / progress.total) * 100) : 0}%` }} /> +
+

+ {t('admin.jobs.progress', { done: progress.done, total: progress.total })} +

+ + )} +
+ ) : ( + <> + {last && } + {status.data.last_error && ( +

+ {status.data.last_error} +

+ )} + {!last && !status.data.last_error &&

{t('admin.jobs.never')}

} + + )} + + {asking && ( + setAsking(false)} + onConfirm={() => { + start.mutate({ refresh }) + setAsking(false) + }} + /> + )} +
+ ) +}