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

@@ -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
};
};