All files / src/helpers schedulerStats.js

0% Statements 0/7
0% Branches 0/8
0% Functions 0/3
0% Lines 0/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                                                                               
/**
 * Fonctions statistiques partagees entre digestScheduler et reportScheduler.
 *
 * @module helpers/schedulerStats
 */
 
const { pool: db } = require('../db');
 
/**
 * Statistiques uptime sur la periode.
 */
async function getUptimeStats(userId, interval) {
  const { rows } = await db.query(
    `SELECT um.url,
            COUNT(*) AS total_checks,
            COUNT(*) FILTER (WHERE uh.status BETWEEN 200 AND 399) AS up_checks
     FROM uptime_monitors um
     LEFT JOIN uptime_history uh ON uh.monitor_id = um.id
       AND uh.created_at > NOW() - INTERVAL '${interval}'
     WHERE um.user_id = $1
     GROUP BY um.id, um.url
     ORDER BY um.url`,
    [userId],
  );
  return rows.map((r) => ({
    url: r.url,
    total: Number(r.total_checks),
    up: Number(r.up_checks),
    percent: Number(r.total_checks) > 0
      ? Math.round((Number(r.up_checks) / Number(r.total_checks)) * 100)
      : null,
  }));
}
 
/**
 * Nombre de backups effectues sur la periode.
 */
async function getBackupStats(userId, interval) {
  const { rows } = await db.query(
    `SELECT COUNT(*) AS total,
            COUNT(*) FILTER (WHERE b.status = 'done') AS success,
            COUNT(*) FILTER (WHERE b.status = 'error') AS errors
     FROM backups b
     JOIN servers s ON s.id = b.server_id
     WHERE s.user_id = $1 AND b.created_at > NOW() - INTERVAL '${interval}'`,
    [userId],
  );
  return {
    total: Number(rows[0]?.total || 0),
    success: Number(rows[0]?.success || 0),
    errors: Number(rows[0]?.errors || 0),
  };
}
 
module.exports = { getUptimeStats, getBackupStats };