Paginate Olist monitor history
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m17s

This commit is contained in:
Cauê Faleiros
2026-08-13 13:44:59 -03:00
parent bee9e6dc2e
commit 5fbdd354c7
6 changed files with 134 additions and 45 deletions

View File

@@ -15,7 +15,10 @@ const router = express.Router();
router.get('/olist/status', verifySuperAdmin, async (req, res, next) => {
try {
res.json(await getOlistStatus());
res.json(await getOlistStatus({
runsPage: req.query.runsPage,
runsPageSize: req.query.runsPageSize
}));
} catch (error) {
next(error);
}
@@ -51,7 +54,10 @@ router.post('/olist/sync/stop', verifySuperAdmin, async (req, res, next) => {
router.get('/olist/production-order-imports', verifySuperAdmin, async (req, res, next) => {
try {
res.json(await listProductionOrderImportData());
res.json(await listProductionOrderImportData({
runsPage: req.query.runsPage,
runsPageSize: req.query.runsPageSize
}));
} catch (error) {
next(error);
}

View File

@@ -102,13 +102,24 @@ const getConnection = async () => {
return result.rows[0] || null;
};
const listSyncRuns = async (limit = 5) => {
const result = await pool.query(`
const listSyncRuns = async ({ page = 1, pageSize = 5 } = {}) => {
const normalizedPageSize = Math.max(1, Math.min(Number(pageSize) || 5, 20));
const normalizedPage = Math.max(1, Number(page) || 1);
const offset = (normalizedPage - 1) * normalizedPageSize;
const [runsResult, countResult] = await Promise.all([
pool.query(`
SELECT * FROM olist_sync_runs
ORDER BY started_at DESC
LIMIT $1;
`, [Math.max(1, Math.min(Number(limit) || 5, 20))]);
return result.rows.map(mapRun);
LIMIT $1 OFFSET $2;
`, [normalizedPageSize, offset]),
pool.query('SELECT COUNT(*)::int AS count FROM olist_sync_runs;')
]);
return {
items: runsResult.rows.map(mapRun),
total: Number(countResult.rows[0]?.count || 0),
page: normalizedPage,
pageSize: normalizedPageSize
};
};
const hasScheduledSyncToday = async () => {
@@ -252,8 +263,14 @@ const markInterruptedOlistRuns = async () => {
return result.rowCount || 0;
};
const getOlistStatus = async () => {
const [connection, runs, productCache] = await Promise.all([getConnection(), listSyncRuns(), getProductCacheProgress()]);
const getOlistStatus = async ({ runsPage = 1, runsPageSize = 5 } = {}) => {
const [connection, syncRuns, productCache, latestRunResult, completedRunResult] = await Promise.all([
getConnection(),
listSyncRuns({ page: runsPage, pageSize: runsPageSize }),
getProductCacheProgress(),
pool.query('SELECT * FROM olist_sync_runs ORDER BY started_at DESC LIMIT 1;'),
pool.query("SELECT EXISTS (SELECT 1 FROM olist_sync_runs WHERE status = 'completed') AS exists;")
]);
return {
configured: isOlistConfigured(),
connected: Boolean(connection),
@@ -261,7 +278,12 @@ const getOlistStatus = async () => {
syncInProgress: Boolean(activeSync),
nextScheduledSyncAt,
productCache,
runs
latestRun: latestRunResult.rows[0] ? mapRun(latestRunResult.rows[0]) : null,
hasCompletedRun: Boolean(completedRunResult.rows[0]?.exists),
runs: syncRuns.items,
runsTotal: syncRuns.total,
runsPage: syncRuns.page,
runsPageSize: syncRuns.pageSize
};
};

View File

@@ -218,9 +218,13 @@ const refreshYieldRecommendations = async (client) => {
`);
};
const listProductionOrderImportData = async () => {
const [runs, recommendations, supplierRecommendations] = await Promise.all([
pool.query(`SELECT id, filename, status, total_rows, imported_count, updated_count, skipped_count, failed_count, error_message, started_at, completed_at FROM production_order_import_runs ORDER BY started_at DESC LIMIT 10;`),
const listProductionOrderImportData = async ({ runsPage = 1, runsPageSize = 5 } = {}) => {
const normalizedRunsPageSize = Math.max(1, Math.min(Number(runsPageSize) || 5, 20));
const normalizedRunsPage = Math.max(1, Number(runsPage) || 1);
const runsOffset = (normalizedRunsPage - 1) * normalizedRunsPageSize;
const [runs, runsCount, recommendations, supplierRecommendations] = await Promise.all([
pool.query(`SELECT id, filename, status, total_rows, imported_count, updated_count, skipped_count, failed_count, error_message, started_at, completed_at FROM production_order_import_runs ORDER BY started_at DESC LIMIT $1 OFFSET $2;`, [normalizedRunsPageSize, runsOffset]),
pool.query('SELECT COUNT(*)::int AS count FROM production_order_import_runs;'),
pool.query(`SELECT family_key, sample_count, yield_sample_count, roll_sample_count, avg_yield_pieces_per_kg, avg_kg_per_roll, recommended_units_per_roll, last_completed_date, updated_at FROM production_yield_recommendations ORDER BY family_key;`),
pool.query(`SELECT family_key, supplier, sample_count, yield_sample_count, roll_sample_count, avg_yield_pieces_per_kg, avg_kg_per_roll, recommended_units_per_roll, last_completed_date, updated_at FROM production_yield_supplier_recommendations ORDER BY family_key, supplier;`)
]);
@@ -231,6 +235,9 @@ const listProductionOrderImportData = async () => {
failed: row.failed_count, errorMessage: row.error_message || '', startedAt: row.started_at,
completedAt: row.completed_at
})),
runsTotal: Number(runsCount.rows[0]?.count || 0),
runsPage: normalizedRunsPage,
runsPageSize: normalizedRunsPageSize,
recommendations: recommendations.rows.map(row => ({
familyKey: row.family_key, sampleCount: Number(row.sample_count), yieldSampleCount: Number(row.yield_sample_count),
rollSampleCount: Number(row.roll_sample_count), avgYieldPiecesPerKg: row.avg_yield_pieces_per_kg === null ? null : Number(row.avg_yield_pieces_per_kg),