// Generated, do not edit. sha256:4da5cbe36b669344f62663079e6a6414b09f2f6d56a746c2f3daa2989f1e621a
import { t } from '../i18n/index.js';
import { listIntegrationCalls, lastIntegrationFailures } from '../data/integration-log.js';
import { listProviders } from '../data/providers.js';
import { esc } from '../ui/escape.js';
import * as dialog from '../ui/dialog.js';
import { selectLabel } from '../ui/select-label.js';
import { localDateTimeNl } from '../../shared/time.js';
import { CONFIG } from '../../shared/config.js';
const DAYS = CONFIG.INTEGRATION_LOG.WINDOW_DAYS;
const sourceOf = row => (row.target === 'service' ? 'service' : row.providerKey);
const reasonOf = row => row.detail || row.errorKey || t(`integrationLog.outcome_${row.outcome}`);
export async function lastFailureHtml(source) {
const row = (await lastIntegrationFailures()).find(r => sourceOf(r) === source);
if (!row) return '';
const status = row.httpStatus ? ` (${row.httpStatus})` : '';
return `
${esc(t('integrationLog.lastFailure'))}
${esc(localDateTimeNl(row.occurredAt))} ยท ${esc(reasonOf(row))}${esc(status)}
`;
}
export function wireFailureLog(host) {
host.querySelectorAll('[data-ilog-open]').forEach(b => {
b.onclick = (e) => { e.stopPropagation(); openFailureLog(b.dataset.ilogOpen); };
});
}
function tableHtml(rows, names) {
if (!rows.length) return `${esc(t('integrationLog.noFailures', { n: DAYS }))}
`;
const head = ['colTime', 'colSource', 'colOperation', 'colSubject', 'colOutcome', 'colStatus', 'colKey', 'colDetail', 'colRequestId']
.map(k => `${esc(t(`integrationLog.${k}`))} | `).join('');
const body = rows.map(r => `
| ${esc(localDateTimeNl(r.occurredAt))} |
${esc(names.get(sourceOf(r)) || sourceOf(r))} |
${esc(r.operation)} |
${esc(r.subject ?? '')} |
${esc(t(`integrationLog.outcome_${r.outcome}`))} |
${esc(r.httpStatus ?? '')} |
${esc(r.errorKey ?? '')} |
${esc(r.detail ?? '')} |
${esc(r.requestId ?? '')} |
`).join('');
return ``;
}
export async function openFailureLog(source = '') {
const [rows, providers] = await Promise.all([listIntegrationCalls({ days: DAYS }), listProviders()]);
const names = new Map([['service', t('integrationLog.service')], ...providers.map(p => [p.key, p.name])]);
const sources = [...new Set([...rows.map(sourceOf), ...(source ? [source] : [])])]
.sort((a, b) => (a === 'service' ? -1 : b === 'service' ? 1 : String(names.get(a) || a).localeCompare(String(names.get(b) || b))));
const options = [``,
...sources.map(s => ``)].join('');
dialog.detail({
title: t('integrationLog.title'), size: 'xl',
asideHtml: selectLabel(``),
html: '',
});
const overlays = document.querySelectorAll('.dialog-overlay');
const d = overlays[overlays.length - 1];
if (!d) return;
const bodyEl = d.querySelector('[data-ilog-body]');
const filter = d.querySelector('[data-ilog-filter]');
const paint = () => { bodyEl.innerHTML = tableHtml(rows.filter(r => !filter.value || sourceOf(r) === filter.value), names); };
filter.onchange = paint;
paint();
}