feat: админ-панель, лицензия, тестовый стек на распределённой инфраструктуре

Админ-панель (доступ: is_admin + секретный код сессии в /admin/<code>):
- разделы: дашборд (здоровье сервисов, статистика), клиенты, работы,
  база документов, хранилище MinIO, источники парсинга, отстойник работ
- backend: модели ParseSource/StagedWork/AdminSession, миграция 003,
  core/admin (авторизация), роутер api/admin
- frontend: AdminLayout + 7 вкладок, adminApi, вход из кабинета
- Celery index.run_parser (фоновый парсинг), автосбор проверенных работ
  в отстойник с ручным одобрением → индексация в базу

Исправления:
- openalex parser: тип article (не journal-article), убран is_oa-фильтр
- winnowing: приведение xxh64 к signed int64 (фикс bigint out of range)
- bcrypt пин 4.0.1 (совместимость с passlib 1.7.4)
- alembic: prepend_sys_path + asyncpg-драйвер
- frontend: контракт Task (public_id вместо id), react-dropzone

Инфраструктура:
- docker-compose.test.yml: api+frontend+воркеры локально, БД/кэш/хранилище/
  брокер/LLM — на удалённых серверах через .env
- .gitignore: исправлено ошибочное игнорирование кода services/*/app/models/
- LICENSE: проприетарная

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
jze9
2026-05-30 20:45:53 +05:00
parent 9ee5ccdc3f
commit 9894fa9320
50 changed files with 5680 additions and 58 deletions

View File

@@ -0,0 +1,102 @@
import React from 'react';
import { NavLink, useParams, useNavigate, Navigate } from 'react-router-dom';
import { useQuery } from '@tanstack/react-query';
import {
LayoutDashboard, Users, FileText, Database, HardDrive, Download, Inbox, LogOut, ShieldAlert,
} from 'lucide-react';
import { clsx } from 'clsx';
import { adminApi } from '../../api/client';
import { useAuthStore } from '../../store/auth';
const NAV = [
{ to: 'dashboard', label: 'Дашборд', icon: LayoutDashboard },
{ to: 'users', label: 'Клиенты', icon: Users },
{ to: 'works', label: 'Работы', icon: FileText },
{ to: 'documents', label: 'База', icon: Database },
{ to: 'storage', label: 'Хранилище', icon: HardDrive },
{ to: 'sources', label: 'Источники', icon: Download },
{ to: 'staging', label: 'Отстойник', icon: Inbox },
];
interface AdminLayoutProps {
children: React.ReactNode;
}
export function AdminLayout({ children }: AdminLayoutProps) {
const { code } = useParams<{ code: string }>();
const { isAuthenticated, user, logout } = useAuthStore();
const navigate = useNavigate();
// Доступ: только аутентифицированный админ
const { data: verify, isLoading } = useQuery({
queryKey: ['admin-verify', code],
queryFn: () => adminApi.verifySession(code!).then((r) => r.data),
enabled: !!code && isAuthenticated && !!user?.is_admin,
retry: false,
});
if (!isAuthenticated || !user?.is_admin) {
return <Navigate to="/login" replace />;
}
if (isLoading) {
return <div className="min-h-screen flex items-center justify-center text-gray-400">Проверка доступа</div>;
}
if (!verify?.valid) {
return (
<div className="min-h-screen flex flex-col items-center justify-center gap-4 bg-gray-50">
<ShieldAlert className="w-12 h-12 text-red-400" />
<p className="text-gray-600">Код доступа недействителен или истёк.</p>
<button
onClick={() => navigate('/cabinet')}
className="px-4 py-2 bg-brand-600 text-white rounded-lg text-sm"
>
В кабинет
</button>
</div>
);
}
return (
<div className="min-h-screen flex bg-gray-50">
{/* Сайдбар */}
<aside className="w-56 bg-gray-900 text-gray-300 flex flex-col fixed inset-y-0 left-0">
<div className="px-5 py-5 border-b border-gray-800 flex items-center gap-2">
<ShieldAlert className="w-5 h-5 text-amber-400" />
<span className="font-semibold text-white">Админ-панель</span>
</div>
<nav className="flex-1 px-3 py-4 space-y-1">
{NAV.map(({ to, label, icon: Icon }) => (
<NavLink
key={to}
to={`/admin/${code}/${to}`}
className={({ isActive }) =>
clsx(
'flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium transition-colors',
isActive ? 'bg-brand-600 text-white' : 'hover:bg-gray-800 hover:text-white'
)
}
>
<Icon className="w-4 h-4" />
{label}
</NavLink>
))}
</nav>
<div className="px-3 py-4 border-t border-gray-800">
<div className="px-3 mb-2 text-xs text-gray-500">{user?.email}</div>
<button
onClick={() => { logout(); navigate('/'); }}
className="w-full flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm hover:bg-gray-800 hover:text-white transition-colors"
>
<LogOut className="w-4 h-4" />
Выйти
</button>
</div>
</aside>
{/* Контент */}
<main className="flex-1 ml-56 p-8">{children}</main>
</div>
);
}

View File

@@ -0,0 +1,119 @@
import React from 'react';
import { useQuery } from '@tanstack/react-query';
import { CheckCircle2, XCircle, Users, FileText, Database, Inbox } from 'lucide-react';
import { adminApi } from '../../api/client';
interface Health { name: string; ok: boolean; detail?: string }
interface Stats {
users_total: number;
tasks_by_status: Record<string, number>;
documents_total: number;
documents_by_source: Record<string, number>;
staging_pending: number;
storage: Record<string, { objects: number; bytes: number } | unknown>;
queues: Record<string, number | string>;
}
function fmtBytes(b: number): string {
if (b < 1024) return `${b} Б`;
if (b < 1024 ** 2) return `${(b / 1024).toFixed(1)} КБ`;
if (b < 1024 ** 3) return `${(b / 1024 ** 2).toFixed(1)} МБ`;
return `${(b / 1024 ** 3).toFixed(2)} ГБ`;
}
export function Dashboard() {
const { data: health } = useQuery({
queryKey: ['admin-health'],
queryFn: () => adminApi.health().then((r) => r.data as Health[]),
refetchInterval: 10000,
});
const { data: stats } = useQuery({
queryKey: ['admin-stats'],
queryFn: () => adminApi.stats().then((r) => r.data as Stats),
refetchInterval: 10000,
});
const totalStorage = Object.values(stats?.storage || {}).reduce(
(acc: number, v) => acc + (typeof v === 'object' && v && 'bytes' in v ? (v as { bytes: number }).bytes : 0),
0
);
return (
<div className="space-y-6">
<h1 className="text-2xl font-bold text-gray-900">Дашборд</h1>
{/* Счётчики */}
<div className="grid grid-cols-2 lg:grid-cols-4 gap-4">
<StatCard icon={Users} label="Клиентов" value={stats?.users_total ?? '—'} color="text-blue-600" />
<StatCard icon={FileText} label="Работ всего" value={Object.values(stats?.tasks_by_status || {}).reduce((a, b) => a + b, 0)} color="text-purple-600" />
<StatCard icon={Database} label="Документов в базе" value={stats?.documents_total ?? '—'} color="text-emerald-600" />
<StatCard icon={Inbox} label="В отстойнике" value={stats?.staging_pending ?? '—'} color="text-amber-600" />
</div>
<div className="grid lg:grid-cols-2 gap-6">
{/* Здоровье сервисов */}
<div className="bg-white rounded-xl border border-gray-100 p-5">
<h2 className="font-semibold text-gray-800 mb-4">Сервисы</h2>
<div className="space-y-2">
{health?.map((h) => (
<div key={h.name} className="flex items-center justify-between py-1.5">
<div className="flex items-center gap-2">
{h.ok ? <CheckCircle2 className="w-4 h-4 text-emerald-500" /> : <XCircle className="w-4 h-4 text-red-500" />}
<span className="text-sm text-gray-700">{h.name}</span>
</div>
<span className={`text-xs ${h.ok ? 'text-gray-400' : 'text-red-500'}`}>{h.detail || (h.ok ? 'OK' : 'недоступен')}</span>
</div>
))}
{!health && <div className="text-sm text-gray-400">Загрузка</div>}
</div>
</div>
{/* Работы по статусам + очереди */}
<div className="bg-white rounded-xl border border-gray-100 p-5 space-y-4">
<div>
<h2 className="font-semibold text-gray-800 mb-3">Работы по статусам</h2>
<div className="flex flex-wrap gap-2">
{Object.entries(stats?.tasks_by_status || {}).map(([s, c]) => (
<span key={s} className="px-3 py-1 bg-gray-100 rounded-lg text-sm text-gray-700">{s}: <b>{c}</b></span>
))}
{!Object.keys(stats?.tasks_by_status || {}).length && <span className="text-sm text-gray-400">нет данных</span>}
</div>
</div>
<div>
<h2 className="font-semibold text-gray-800 mb-3">Очереди</h2>
<div className="flex flex-wrap gap-2">
{Object.entries(stats?.queues || {}).map(([q, n]) => (
<span key={q} className="px-3 py-1 bg-gray-100 rounded-lg text-sm text-gray-700">{q}: <b>{String(n)}</b></span>
))}
</div>
</div>
<div>
<h2 className="font-semibold text-gray-800 mb-2">Хранилище</h2>
<p className="text-sm text-gray-600">Всего: <b>{fmtBytes(totalStorage)}</b></p>
</div>
</div>
</div>
{/* База по источникам */}
<div className="bg-white rounded-xl border border-gray-100 p-5">
<h2 className="font-semibold text-gray-800 mb-3">База документов по источникам</h2>
<div className="flex flex-wrap gap-2">
{Object.entries(stats?.documents_by_source || {}).map(([s, c]) => (
<span key={s} className="px-3 py-1 bg-emerald-50 text-emerald-700 rounded-lg text-sm">{s}: <b>{c}</b></span>
))}
{!Object.keys(stats?.documents_by_source || {}).length && <span className="text-sm text-gray-400">база пуста</span>}
</div>
</div>
</div>
);
}
function StatCard({ icon: Icon, label, value, color }: { icon: React.ElementType; label: string; value: React.ReactNode; color: string }) {
return (
<div className="bg-white rounded-xl border border-gray-100 p-5">
<Icon className={`w-6 h-6 ${color} mb-2`} />
<div className="text-2xl font-bold text-gray-900">{value}</div>
<div className="text-sm text-gray-500">{label}</div>
</div>
);
}

View File

@@ -0,0 +1,71 @@
import React, { useState } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { Trash2, Search } from 'lucide-react';
import toast from 'react-hot-toast';
import { adminApi } from '../../api/client';
interface Doc {
id: number; source: string; title: string; authors: Array<{ last_name?: string }>;
year: number | null; lang: string | null; url: string | null; indexed_at: string;
}
export function Documents() {
const qc = useQueryClient();
const [q, setQ] = useState('');
const [source, setSource] = useState('');
const { data: docs } = useQuery({
queryKey: ['admin-docs', q, source],
queryFn: () => adminApi.documents({ q: q || undefined, source: source || undefined }).then((r) => r.data as Doc[]),
});
const del = useMutation({
mutationFn: (id: number) => adminApi.deleteDocument(id),
onSuccess: () => { qc.invalidateQueries({ queryKey: ['admin-docs'] }); toast.success('Удалён из базы'); },
onError: () => toast.error('Ошибка'),
});
return (
<div className="space-y-5">
<h1 className="text-2xl font-bold text-gray-900">База документов</h1>
<div className="flex gap-3">
<div className="relative flex-1 max-w-sm">
<Search className="w-4 h-4 text-gray-400 absolute left-3 top-1/2 -translate-y-1/2" />
<input value={q} onChange={(e) => setQ(e.target.value)} placeholder="Поиск по названию"
className="w-full pl-9 pr-3 py-2 border border-gray-200 rounded-lg text-sm" />
</div>
<select value={source} onChange={(e) => setSource(e.target.value)}
className="border border-gray-200 rounded-lg px-3 py-2 text-sm">
<option value="">все источники</option>
<option value="openalex">openalex</option>
<option value="cyberleninka">cyberleninka</option>
<option value="arxiv">arxiv</option>
<option value="user_submission">работы пользователей</option>
</select>
</div>
<div className="bg-white rounded-xl border border-gray-100 overflow-hidden">
<table className="w-full text-sm">
<thead className="bg-gray-50 text-gray-500 text-left">
<tr>
<th className="px-4 py-3">Название</th><th className="px-4 py-3">Источник</th>
<th className="px-4 py-3">Год</th><th className="px-4 py-3">Язык</th><th className="px-4 py-3"></th>
</tr>
</thead>
<tbody className="divide-y divide-gray-50">
{docs?.map((d) => (
<tr key={d.id} className="hover:bg-gray-50">
<td className="px-4 py-3 text-gray-800 max-w-md truncate">{d.title}</td>
<td className="px-4 py-3"><span className="px-2 py-0.5 bg-emerald-50 text-emerald-700 rounded text-xs">{d.source}</span></td>
<td className="px-4 py-3 text-gray-600">{d.year || '—'}</td>
<td className="px-4 py-3 text-gray-600">{d.lang || '—'}</td>
<td className="px-4 py-3">
<button onClick={() => { if (confirm('Удалить документ из базы?')) del.mutate(d.id); }}
className="p-1.5 text-gray-400 hover:text-red-500 rounded"><Trash2 className="w-4 h-4" /></button>
</td>
</tr>
))}
</tbody>
</table>
{!docs?.length && <div className="p-6 text-center text-gray-400 text-sm">База пуста</div>}
</div>
</div>
);
}

View File

@@ -0,0 +1,114 @@
import React, { useState } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { Play, Trash2, Plus } from 'lucide-react';
import toast from 'react-hot-toast';
import { adminApi } from '../../api/client';
interface Source {
id: number; source_type: string; name: string; query: string | null;
lang: string | null; year_from: number | null; year_to: number | null;
limit: number; enabled: boolean; last_status: string; last_error: string | null;
last_run_at: string | null; docs_added: number;
}
const STATUS_COLORS: Record<string, string> = {
idle: 'bg-gray-100 text-gray-600',
running: 'bg-blue-100 text-blue-700',
done: 'bg-emerald-100 text-emerald-700',
error: 'bg-red-100 text-red-700',
};
export function Sources() {
const qc = useQueryClient();
const [form, setForm] = useState({ source_type: 'openalex', name: '', query: '', lang: '', limit: 100 });
const { data: sources } = useQuery({
queryKey: ['admin-sources'],
queryFn: () => adminApi.sources().then((r) => r.data as Source[]),
refetchInterval: 5000,
});
const create = useMutation({
mutationFn: (data: Record<string, unknown>) => adminApi.createSource(data),
onSuccess: () => { qc.invalidateQueries({ queryKey: ['admin-sources'] }); toast.success('Источник добавлен'); setForm({ source_type: 'openalex', name: '', query: '', lang: '', limit: 100 }); },
onError: (e: any) => toast.error(e.response?.data?.detail || 'Ошибка'),
});
const run = useMutation({
mutationFn: (id: number) => adminApi.runSource(id),
onSuccess: () => { qc.invalidateQueries({ queryKey: ['admin-sources'] }); toast.success('Парсинг запущен'); },
onError: (e: any) => toast.error(e.response?.data?.detail || 'Ошибка'),
});
const del = useMutation({
mutationFn: (id: number) => adminApi.deleteSource(id),
onSuccess: () => { qc.invalidateQueries({ queryKey: ['admin-sources'] }); toast.success('Удалён'); },
});
return (
<div className="space-y-5">
<h1 className="text-2xl font-bold text-gray-900">Источники парсинга</h1>
{/* Форма добавления */}
<div className="bg-white rounded-xl border border-gray-100 p-5">
<h2 className="font-semibold text-gray-800 mb-3">Добавить источник</h2>
<div className="grid grid-cols-2 lg:grid-cols-5 gap-3">
<select value={form.source_type} onChange={(e) => setForm({ ...form, source_type: e.target.value })}
className="border border-gray-200 rounded-lg px-3 py-2 text-sm">
<option value="openalex">OpenAlex</option>
<option value="cyberleninka">КиберЛенинка</option>
<option value="arxiv">arXiv</option>
</select>
<input value={form.name} onChange={(e) => setForm({ ...form, name: e.target.value })} placeholder="Название"
className="border border-gray-200 rounded-lg px-3 py-2 text-sm" />
<input value={form.query} onChange={(e) => setForm({ ...form, query: e.target.value })} placeholder="Запрос"
className="border border-gray-200 rounded-lg px-3 py-2 text-sm" />
<input value={form.lang} onChange={(e) => setForm({ ...form, lang: e.target.value })} placeholder="Язык (ru/en)"
className="border border-gray-200 rounded-lg px-3 py-2 text-sm" />
<input type="number" value={form.limit} onChange={(e) => setForm({ ...form, limit: Number(e.target.value) })} placeholder="Лимит"
className="border border-gray-200 rounded-lg px-3 py-2 text-sm" />
</div>
<button
onClick={() => {
if (!form.name) { toast.error('Укажите название'); return; }
create.mutate({ ...form, lang: form.lang || null, query: form.query || null });
}}
className="mt-3 flex items-center gap-2 px-4 py-2 bg-brand-600 text-white rounded-lg text-sm font-medium hover:bg-brand-700"
>
<Plus className="w-4 h-4" /> Добавить
</button>
</div>
{/* Список */}
<div className="bg-white rounded-xl border border-gray-100 overflow-hidden">
<table className="w-full text-sm">
<thead className="bg-gray-50 text-gray-500 text-left">
<tr>
<th className="px-4 py-3">Название</th><th className="px-4 py-3">Тип</th>
<th className="px-4 py-3">Запрос</th><th className="px-4 py-3">Лимит</th>
<th className="px-4 py-3">Статус</th><th className="px-4 py-3">Добавлено</th><th className="px-4 py-3"></th>
</tr>
</thead>
<tbody className="divide-y divide-gray-50">
{sources?.map((s) => (
<tr key={s.id} className="hover:bg-gray-50">
<td className="px-4 py-3 text-gray-800">{s.name}</td>
<td className="px-4 py-3 text-gray-600">{s.source_type}</td>
<td className="px-4 py-3 text-gray-500 max-w-[160px] truncate">{s.query || '—'}</td>
<td className="px-4 py-3 text-gray-600">{s.limit}</td>
<td className="px-4 py-3">
<span className={`px-2 py-0.5 rounded text-xs ${STATUS_COLORS[s.last_status] || 'bg-gray-100'}`} title={s.last_error || ''}>{s.last_status}</span>
</td>
<td className="px-4 py-3 text-gray-600">{s.docs_added}</td>
<td className="px-4 py-3 flex gap-1">
<button onClick={() => run.mutate(s.id)} disabled={s.last_status === 'running'} title="Запустить"
className="p-1.5 text-gray-400 hover:text-emerald-600 rounded disabled:opacity-40"><Play className="w-4 h-4" /></button>
<button onClick={() => { if (confirm('Удалить источник?')) del.mutate(s.id); }} title="Удалить"
className="p-1.5 text-gray-400 hover:text-red-500 rounded"><Trash2 className="w-4 h-4" /></button>
</td>
</tr>
))}
</tbody>
</table>
{!sources?.length && <div className="p-6 text-center text-gray-400 text-sm">Нет источников</div>}
</div>
</div>
);
}

View File

@@ -0,0 +1,119 @@
import React, { useState } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { Check, X, Eye } from 'lucide-react';
import toast from 'react-hot-toast';
import { adminApi } from '../../api/client';
interface Staged {
id: number; user_id: number | null; filename: string | null; title: string | null;
word_count: number | null; status: string; document_id: number | null; created_at: string;
}
interface StagedDetail extends Staged { text_preview: string | null }
const STATUS_COLORS: Record<string, string> = {
pending: 'bg-amber-100 text-amber-700',
approved: 'bg-emerald-100 text-emerald-700',
rejected: 'bg-red-100 text-red-700',
};
export function Staging() {
const qc = useQueryClient();
const [status, setStatus] = useState('pending');
const [preview, setPreview] = useState<StagedDetail | null>(null);
const { data: items } = useQuery({
queryKey: ['admin-staging', status],
queryFn: () => adminApi.staging({ status }).then((r) => r.data as Staged[]),
refetchInterval: 8000,
});
const approve = useMutation({
mutationFn: (id: number) => adminApi.approveStaging(id),
onSuccess: () => { qc.invalidateQueries({ queryKey: ['admin-staging'] }); setPreview(null); toast.success('Одобрено — добавляется в базу'); },
onError: (e: any) => toast.error(e.response?.data?.detail || 'Ошибка'),
});
const reject = useMutation({
mutationFn: (id: number) => adminApi.rejectStaging(id),
onSuccess: () => { qc.invalidateQueries({ queryKey: ['admin-staging'] }); setPreview(null); toast.success('Отклонено'); },
});
const openPreview = async (id: number) => {
const { data } = await adminApi.stagingDetail(id);
setPreview(data as StagedDetail);
};
return (
<div className="space-y-5">
<h1 className="text-2xl font-bold text-gray-900">Отстойник проверенных работ</h1>
<p className="text-sm text-gray-500">Работы, прошедшие проверку. Одобрение добавляет их в базу источников для будущих сравнений.</p>
<div className="flex gap-2">
{['pending', 'approved', 'rejected', 'all'].map((s) => (
<button key={s} onClick={() => setStatus(s)}
className={`px-3 py-1.5 rounded-lg text-sm ${status === s ? 'bg-brand-600 text-white' : 'bg-white border border-gray-200 text-gray-600'}`}>
{s === 'all' ? 'все' : s}
</button>
))}
</div>
<div className="bg-white rounded-xl border border-gray-100 overflow-hidden">
<table className="w-full text-sm">
<thead className="bg-gray-50 text-gray-500 text-left">
<tr>
<th className="px-4 py-3">Файл</th><th className="px-4 py-3">Клиент</th>
<th className="px-4 py-3">Слов</th><th className="px-4 py-3">Статус</th>
<th className="px-4 py-3">Дата</th><th className="px-4 py-3"></th>
</tr>
</thead>
<tbody className="divide-y divide-gray-50">
{items?.map((s) => (
<tr key={s.id} className="hover:bg-gray-50">
<td className="px-4 py-3 text-gray-800 max-w-xs truncate">{s.filename || s.title || `#${s.id}`}</td>
<td className="px-4 py-3 text-gray-600">{s.user_id ? `#${s.user_id}` : '—'}</td>
<td className="px-4 py-3 text-gray-600">{s.word_count ?? '—'}</td>
<td className="px-4 py-3"><span className={`px-2 py-0.5 rounded text-xs ${STATUS_COLORS[s.status]}`}>{s.status}</span></td>
<td className="px-4 py-3 text-gray-400 text-xs">{new Date(s.created_at).toLocaleString('ru')}</td>
<td className="px-4 py-3 flex gap-1">
<button onClick={() => openPreview(s.id)} title="Просмотр"
className="p-1.5 text-gray-400 hover:text-brand-600 rounded"><Eye className="w-4 h-4" /></button>
{s.status === 'pending' && (
<>
<button onClick={() => approve.mutate(s.id)} title="Одобрить"
className="p-1.5 text-gray-400 hover:text-emerald-600 rounded"><Check className="w-4 h-4" /></button>
<button onClick={() => reject.mutate(s.id)} title="Отклонить"
className="p-1.5 text-gray-400 hover:text-red-500 rounded"><X className="w-4 h-4" /></button>
</>
)}
</td>
</tr>
))}
</tbody>
</table>
{!items?.length && <div className="p-6 text-center text-gray-400 text-sm">Пусто</div>}
</div>
{/* Превью */}
{preview && (
<div className="fixed inset-0 bg-black/40 flex items-center justify-center z-50 p-6" onClick={() => setPreview(null)}>
<div className="bg-white rounded-xl max-w-2xl w-full max-h-[80vh] flex flex-col" onClick={(e) => e.stopPropagation()}>
<div className="px-5 py-4 border-b border-gray-100 flex items-center justify-between">
<h3 className="font-semibold text-gray-800 truncate">{preview.filename || preview.title}</h3>
<button onClick={() => setPreview(null)} className="text-gray-400 hover:text-gray-600"><X className="w-5 h-5" /></button>
</div>
<div className="p-5 overflow-auto text-sm text-gray-700 whitespace-pre-wrap flex-1">
{preview.text_preview || '(нет текста)'}
</div>
{preview.status === 'pending' && (
<div className="px-5 py-4 border-t border-gray-100 flex gap-2 justify-end">
<button onClick={() => reject.mutate(preview.id)}
className="px-4 py-2 border border-gray-200 text-gray-600 rounded-lg text-sm">Отклонить</button>
<button onClick={() => approve.mutate(preview.id)}
className="px-4 py-2 bg-emerald-600 text-white rounded-lg text-sm">Одобрить в базу</button>
</div>
)}
</div>
</div>
)}
</div>
);
}

View File

@@ -0,0 +1,57 @@
import React from 'react';
import { useQuery } from '@tanstack/react-query';
import { HardDrive } from 'lucide-react';
import { adminApi } from '../../api/client';
interface Bucket {
name: string; objects_total?: number; bytes?: number;
sample?: Array<{ name: string; size: number }>; error?: string;
}
function fmtBytes(b: number): string {
if (b < 1024) return `${b} Б`;
if (b < 1024 ** 2) return `${(b / 1024).toFixed(1)} КБ`;
if (b < 1024 ** 3) return `${(b / 1024 ** 2).toFixed(1)} МБ`;
return `${(b / 1024 ** 3).toFixed(2)} ГБ`;
}
export function Storage() {
const { data } = useQuery({
queryKey: ['admin-storage'],
queryFn: () => adminApi.storage().then((r) => r.data as { buckets: Bucket[] }),
refetchInterval: 15000,
});
return (
<div className="space-y-5">
<h1 className="text-2xl font-bold text-gray-900">Хранилище (MinIO)</h1>
<div className="grid lg:grid-cols-3 gap-4">
{data?.buckets.map((b) => (
<div key={b.name} className="bg-white rounded-xl border border-gray-100 p-5">
<div className="flex items-center gap-2 mb-3">
<HardDrive className="w-5 h-5 text-brand-600" />
<span className="font-semibold text-gray-800">{b.name}</span>
</div>
{b.error ? (
<p className="text-sm text-red-500">{b.error}</p>
) : (
<>
<p className="text-sm text-gray-600">Объектов: <b>{b.objects_total}</b></p>
<p className="text-sm text-gray-600">Объём: <b>{fmtBytes(b.bytes || 0)}</b></p>
<div className="mt-3 max-h-40 overflow-auto space-y-1">
{b.sample?.map((o) => (
<div key={o.name} className="flex justify-between text-xs text-gray-400">
<span className="truncate max-w-[160px]">{o.name}</span>
<span>{fmtBytes(o.size)}</span>
</div>
))}
</div>
</>
)}
</div>
))}
{!data?.buckets.length && <div className="text-gray-400 text-sm">Нет бакетов</div>}
</div>
</div>
);
}

View File

@@ -0,0 +1,92 @@
import React, { useState } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { Trash2, Search } from 'lucide-react';
import toast from 'react-hot-toast';
import { adminApi } from '../../api/client';
interface AdminUser {
id: number; email: string; name: string; plan: string;
is_verified: boolean; is_admin: boolean; created_at: string; tasks_count: number;
}
const PLANS = ['free', 'student', 'premium', 'science'];
export function Users() {
const qc = useQueryClient();
const [q, setQ] = useState('');
const { data: users } = useQuery({
queryKey: ['admin-users', q],
queryFn: () => adminApi.users({ q: q || undefined }).then((r) => r.data as AdminUser[]),
});
const update = useMutation({
mutationFn: ({ id, data }: { id: number; data: Record<string, unknown> }) => adminApi.updateUser(id, data),
onSuccess: () => { qc.invalidateQueries({ queryKey: ['admin-users'] }); toast.success('Сохранено'); },
onError: () => toast.error('Ошибка'),
});
const del = useMutation({
mutationFn: (id: number) => adminApi.deleteUser(id),
onSuccess: () => { qc.invalidateQueries({ queryKey: ['admin-users'] }); toast.success('Удалён'); },
onError: () => toast.error('Ошибка удаления'),
});
return (
<div className="space-y-5">
<h1 className="text-2xl font-bold text-gray-900">Клиенты</h1>
<div className="relative max-w-sm">
<Search className="w-4 h-4 text-gray-400 absolute left-3 top-1/2 -translate-y-1/2" />
<input
value={q} onChange={(e) => setQ(e.target.value)} placeholder="Поиск по email/имени"
className="w-full pl-9 pr-3 py-2 border border-gray-200 rounded-lg text-sm"
/>
</div>
<div className="bg-white rounded-xl border border-gray-100 overflow-hidden">
<table className="w-full text-sm">
<thead className="bg-gray-50 text-gray-500 text-left">
<tr>
<th className="px-4 py-3">Email</th><th className="px-4 py-3">Имя</th>
<th className="px-4 py-3">Тариф</th><th className="px-4 py-3">Работ</th>
<th className="px-4 py-3">Verified</th><th className="px-4 py-3">Admin</th>
<th className="px-4 py-3"></th>
</tr>
</thead>
<tbody className="divide-y divide-gray-50">
{users?.map((u) => (
<tr key={u.id} className="hover:bg-gray-50">
<td className="px-4 py-3 text-gray-800">{u.email}</td>
<td className="px-4 py-3 text-gray-600">{u.name}</td>
<td className="px-4 py-3">
<select
value={u.plan}
onChange={(e) => update.mutate({ id: u.id, data: { plan: e.target.value } })}
className="border border-gray-200 rounded px-2 py-1 text-xs"
>
{PLANS.map((p) => <option key={p} value={p}>{p}</option>)}
</select>
</td>
<td className="px-4 py-3 text-gray-600">{u.tasks_count}</td>
<td className="px-4 py-3">
<input type="checkbox" checked={u.is_verified}
onChange={(e) => update.mutate({ id: u.id, data: { is_verified: e.target.checked } })} />
</td>
<td className="px-4 py-3">
<input type="checkbox" checked={u.is_admin}
onChange={(e) => update.mutate({ id: u.id, data: { is_admin: e.target.checked } })} />
</td>
<td className="px-4 py-3">
<button
onClick={() => { if (confirm(`Удалить ${u.email}?`)) del.mutate(u.id); }}
className="p-1.5 text-gray-400 hover:text-red-500 rounded"
>
<Trash2 className="w-4 h-4" />
</button>
</td>
</tr>
))}
</tbody>
</table>
{!users?.length && <div className="p-6 text-center text-gray-400 text-sm">Нет клиентов</div>}
</div>
</div>
);
}

View File

@@ -0,0 +1,81 @@
import React, { useState } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { RefreshCw, Trash2 } from 'lucide-react';
import toast from 'react-hot-toast';
import { adminApi } from '../../api/client';
interface AdminTask {
public_id: string; user_id: number; type: string; status: string;
error: string | null; created_at: string;
}
const STATUS_COLORS: Record<string, string> = {
queued: 'bg-gray-100 text-gray-600',
processing: 'bg-blue-100 text-blue-700',
done: 'bg-emerald-100 text-emerald-700',
failed: 'bg-red-100 text-red-700',
};
export function Works() {
const qc = useQueryClient();
const [status, setStatus] = useState('');
const { data: tasks } = useQuery({
queryKey: ['admin-tasks', status],
queryFn: () => adminApi.tasks({ status: status || undefined }).then((r) => r.data as AdminTask[]),
refetchInterval: 8000,
});
const requeue = useMutation({
mutationFn: (id: string) => adminApi.requeueTask(id),
onSuccess: () => { qc.invalidateQueries({ queryKey: ['admin-tasks'] }); toast.success('Перезапущено'); },
onError: (e: any) => toast.error(e.response?.data?.detail || 'Ошибка'),
});
const del = useMutation({
mutationFn: (id: string) => adminApi.deleteTask(id),
onSuccess: () => { qc.invalidateQueries({ queryKey: ['admin-tasks'] }); toast.success('Удалено'); },
onError: () => toast.error('Ошибка'),
});
return (
<div className="space-y-5">
<h1 className="text-2xl font-bold text-gray-900">Работы</h1>
<div className="flex gap-2">
{['', 'queued', 'processing', 'done', 'failed'].map((s) => (
<button key={s} onClick={() => setStatus(s)}
className={`px-3 py-1.5 rounded-lg text-sm ${status === s ? 'bg-brand-600 text-white' : 'bg-white border border-gray-200 text-gray-600'}`}>
{s || 'все'}
</button>
))}
</div>
<div className="bg-white rounded-xl border border-gray-100 overflow-hidden">
<table className="w-full text-sm">
<thead className="bg-gray-50 text-gray-500 text-left">
<tr>
<th className="px-4 py-3">ID</th><th className="px-4 py-3">Клиент</th>
<th className="px-4 py-3">Тип</th><th className="px-4 py-3">Статус</th>
<th className="px-4 py-3">Создано</th><th className="px-4 py-3"></th>
</tr>
</thead>
<tbody className="divide-y divide-gray-50">
{tasks?.map((t) => (
<tr key={t.public_id} className="hover:bg-gray-50">
<td className="px-4 py-3 font-mono text-xs text-gray-500">{t.public_id.slice(0, 10)}</td>
<td className="px-4 py-3 text-gray-600">#{t.user_id}</td>
<td className="px-4 py-3 text-gray-600">{t.type}</td>
<td className="px-4 py-3"><span className={`px-2 py-0.5 rounded text-xs ${STATUS_COLORS[t.status] || 'bg-gray-100'}`}>{t.status}</span></td>
<td className="px-4 py-3 text-gray-400 text-xs">{new Date(t.created_at).toLocaleString('ru')}</td>
<td className="px-4 py-3 flex gap-1">
<button onClick={() => requeue.mutate(t.public_id)} title="Перезапустить"
className="p-1.5 text-gray-400 hover:text-brand-600 rounded"><RefreshCw className="w-4 h-4" /></button>
<button onClick={() => { if (confirm('Удалить работу?')) del.mutate(t.public_id); }} title="Удалить"
className="p-1.5 text-gray-400 hover:text-red-500 rounded"><Trash2 className="w-4 h-4" /></button>
</td>
</tr>
))}
</tbody>
</table>
{!tasks?.length && <div className="p-6 text-center text-gray-400 text-sm">Нет работ</div>}
</div>
</div>
);
}