<?php
/**
 * dashboard-container-stats.php — returns the <tr> rows for the dashboard "Live
 * Container Memory Usage" table, scoped to the caller (root = all running site
 * containers + the User column; everyone else = only their own). Polled by the
 * dashboard auto-refresh control. Read-only GET, so no CSRF — just the standard
 * auth gate. Echoes an HTML row fragment that the client swaps into the tbody.
 */
require_once(__DIR__ . '/../auto-prepend.php');

header('Content-Type: text/html; charset=utf-8');

if (!defined('AUTH_USER')) {
    http_response_code(401);
    exit;
}

require_once(__DIR__ . '/../libs/mysqlmgmt.php');
require_once(__DIR__ . '/../libs/container_stats.php');

$is_root = (AUTH_USER === 'root');
$show_user_column = $is_root;

$MySQLMgmt = new mysqlmgmt();
$db = $MySQLMgmt->getMySQLConnection();
$live_container_stats = whp_live_container_stats($db, $is_root ? null : AUTH_USER);

if (empty($live_container_stats)) {
    $cols = $is_root ? 8 : 7;
    echo '<tr><td colspan="' . $cols . '" class="text-center text-muted py-3">No running containers.</td></tr>';
    exit;
}

include(__DIR__ . '/../includes/dashboard-container-stats-rows.php');
