import { h } from '/static/admin2/vendor/preact.module.js';
import { useEffect, useState } from '/static/admin2/vendor/hooks.module.js';
import htm from '/static/admin2/vendor/htm.module.js';
import { api } from '/static/admin2/api.js';
import { Table } from '/static/admin2/ui/table.js';
import { Loading } from '/static/admin2/ui/loading.js';
const html = htm.bind(h);
const base = '/api/admin/v2/stove-api';
export function StoveApiPage() {
const [data, setData] = useState(null);
const [error, setError] = useState('');
const [name, setName] = useState('');
const [note, setNote] = useState('');
const [allowedIp, setAllowedIp] = useState('');
const [secret, setSecret] = useState('');
const [selected, setSelected] = useState(null);
const load = () => api(base).then((d) => { setData(d); setError(''); })
.catch((e) => setError(e.message || 'API не отвечает'));
useEffect(() => { load(); const t = setInterval(load, 60000); return () => clearInterval(t); }, []);
const act = async (path, opts) => {
try { await api(path, opts); await load(); }
catch (e) { setError(e.message || 'Действие не выполнено'); }
};
const create = async (e) => {
e.preventDefault();
try {
const made = await api(base + '/keys', {method: 'POST', body: JSON.stringify({name, note, allowed_ip: allowedIp.trim()})});
setSecret(made.secret);
setName(''); setNote(''); setAllowedIp(''); await load();
} catch (err) { setError(err.message || 'Ключ не создан'); }
};
const show = async (row) => {
try { setSelected(await api(base + '/requests/' + encodeURIComponent(row.request_id))); }
catch (e) { setError(e.message || 'Запрос не найден'); }
};
if (!data) return html`${error && html`
${error}
`}<${Loading} />`;
return html`
${error && html`${error}
`}
Состояние API
${data.enabled ? 'Включено' : 'Выключено'} · неверных ключей за сутки: ${data.invalid_keys.count_24h}
Выдать ключ
${secret && html`Секрет виден только сейчас. Скопируйте и передайте владельцу защищённым способом:
${secret}
`}
Ключи
<${Table} columns=${[
{key: 'name', label: 'Название'}, {key: 'key_prefix', label: 'Префикс'},
{key: 'allowed_ip', label: 'IP', render: (r) => r.allowed_ip || 'не задан — доступ закрыт'},
{key: 'calls_24h', label: 'Вызовов/сут'}, {key: 'limit_day', label: 'Лимит/сут'},
{key: 'ai_budget_rub_day', label: 'ИИ ₽/сут'},
{key: 'status', label: 'Статус', render: (r) => r.revoked_at ? 'отозван' : r.disabled ? 'отключён' : !r.allowed_ip ? 'адрес не задан' : 'работает'},
{key: 'actions', label: 'Действия', render: (r) => r.revoked_at ? '' : html`
`},
]} rows=${data.keys.map((r) => ({...r, key: r.id}))} empty="Ключей пока нет" />
Последние вызовы
<${Table} columns=${[
{key: 'ts', label: 'Когда'}, {key: 'key_id', label: 'Ключ'},
{key: 'request_id', label: 'Запрос'}, {key: 'http_status', label: 'HTTP'},
{key: 'error_code', label: 'Ошибка'}, {key: 'duration_ms', label: 'мс'},
]} rows=${data.history.map((r) => ({...r, key: r.id}))} onRowClick=${show} empty="Вызовов нет" />
${selected && html`Запрос ${selected.request_id}
${JSON.stringify({request: selected.request, response: selected.response}, null, 2)}
`}
`;
}