#!/usr/bin/env node import { openHistory } from '../server/db.js'; /** * Report-only view of the load history database. It opens the DB read-only * and PRINTS: recent loads, per-file counters of the last load, recent * quarantined rows and warnings. No cleanup, no repair, no writes - changing * anything is a separate deliberate step, never part of a report. * * Usage: node scripts/db-report.js [сколько загрузок показать] * DB path comes from DB_PATH, same as the panel itself. */ const limit = Number(process.argv[2] ?? 15); const history = openHistory(process.env.DB_PATH, { readOnly: true }); console.log(`База истории загрузок: ${history.path}`); if (!history.available) { console.log('База недоступна или ещё не создана. Это не поломка панели:'); console.log('она работает без базы. База появится после первого чтения данных'); console.log('сервером с настроенным DB_PATH (или по умолчанию).'); process.exit(0); } const loads = history.lastLoads(limit); if (loads.length === 0) { console.log('Загрузок в базе пока нет.'); process.exit(0); } const when = (iso) => (iso ? new Date(iso).toLocaleString('ru-RU') : '—'); console.log(''); console.log(`Последние загрузки (${loads.length}):`); console.log(' № когда причина файлов строк предупр. отказов'); for (const load of loads) { console.log(' ' + String(load.id).padEnd(6) + when(load.loaded_at).padEnd(22) + String(load.reason || '—').padEnd(20) + `${load.files_found}/${load.files_total}`.padEnd(8) + String(load.rows_total).padEnd(8) + String(load.warnings).padEnd(10) + String(load.rejects)); } const last = loads[0]; console.log(''); console.log(`Файлы загрузки №${last.id} (прочитано сырых строк / принято наблюдений / отброшено):`); for (const file of history.filesForLoad(last.id)) { const marks = []; if (!file.found) marks.push('файла нет'); if (file.stale) marks.push('показаны прежние данные'); if (file.facts_load_id !== file.load_id) marks.push(`данные без изменений с загрузки №${file.facts_load_id}`); console.log(' ' + String(file.file).padEnd(34) + `${file.rows_read} / ${file.rows_accepted} / ${file.rows_rejected}`.padEnd(20) + (marks.length > 0 ? `(${marks.join('; ')})` : '')); } const rejects = history.recentRejects(10); console.log(''); if (rejects.length === 0) { console.log('Карантин пуст: отброшенных строк нет.'); } else { console.log(`Последние отброшенные строки (${rejects.length}):`); for (const item of rejects) { const raw = item.raw.length > 160 ? `${item.raw.slice(0, 160)}…` : item.raw; console.log(` загрузка №${item.load_id}, ${when(item.loaded_at)}, ${item.file}: ${item.reason}`); console.log(` сырьё: ${raw}`); } } const warnings = history.recentWarnings(10); console.log(''); if (warnings.length === 0) { console.log('Предупреждений о качестве данных нет.'); } else { console.log(`Последние предупреждения (${warnings.length}):`); for (const item of warnings) { console.log(` загрузка №${item.load_id}, ${when(item.loaded_at)}, ${item.file}: ${item.message}`); } } const errors = history.recentErrors(5); if (errors.length > 0) { console.log(''); console.log(`Последние ошибки чтения (${errors.length}):`); for (const item of errors) { console.log(` загрузка №${item.load_id}, ${when(item.loaded_at)}, ${item.file}: ${item.message}`); } } history.close();