Archived
125 lines
4.0 KiB
PHP
125 lines
4.0 KiB
PHP
<?php
|
|
|
|
require "config.php";
|
|
|
|
if ($error_reporting) {
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
}
|
|
|
|
if (isset($_GET["recalculate"])) {
|
|
if (file_exists($stats_file)) {
|
|
$time = json_decode(file_get_contents($stats_file), true)["time"] ?? 0;
|
|
if ($time + $recalculate_cooldown_seconds < time()) {
|
|
unlink($stats_file);
|
|
}
|
|
}
|
|
|
|
header("Location: " . $absolute_root);
|
|
exit(0);
|
|
}
|
|
|
|
function human_readable_filesize(int $bytes, int $decimals = 1): string
|
|
{
|
|
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
|
|
$factor = $bytes > 0 ? floor(log($bytes, 1024)) : 0;
|
|
$factor = min($factor, count($units) - 1);
|
|
|
|
return sprintf("%.{$decimals}f %s", $bytes / (1024 ** $factor), $units[$factor]);
|
|
}
|
|
|
|
if (!file_exists($stats_file)) {
|
|
$stats = [
|
|
"storage" => $disks,
|
|
];
|
|
|
|
foreach ($stats["storage"] as $disk_name => $disk_stats) {
|
|
$stats["storage"][$disk_name]["total_bytes"] = disk_total_space($disk_stats["mount_point"]);
|
|
$stats["storage"][$disk_name]["used_bytes"] = (int) trim(shell_exec('df --block-size=1 --output=used ' . escapeshellarg($disk_stats["mount_point"]) . ' | tail -1'));
|
|
$other_used = $stats["storage"][$disk_name]["used_bytes"];
|
|
if (!isset($disk_stats["categories"])) {
|
|
$disk_stats["categories"] = [];
|
|
}
|
|
|
|
foreach ($disk_stats["categories"] as $category_name => $category_stats) {
|
|
$paths = $category_stats["path"];
|
|
if (!is_array($category_stats["path"])) {
|
|
$paths = [$category_stats["path"]];
|
|
}
|
|
|
|
$stats["storage"][$disk_name]["categories"][$category_name]["bytes"] = 0;
|
|
foreach ($paths as $path) {
|
|
$bytes = (int) shell_exec('du -sb ' . escapeshellarg($path) . ' | cut -f1');
|
|
$stats["storage"][$disk_name]["categories"][$category_name]["bytes"] += $bytes;
|
|
}
|
|
|
|
$other_used -= $stats["storage"][$disk_name]["categories"][$category_name]["bytes"];
|
|
}
|
|
|
|
$stats["storage"][$disk_name]["categories"]["Other"] = [
|
|
"color" => "white",
|
|
"bytes" => max(0, $other_used),
|
|
];
|
|
}
|
|
|
|
$stats["time"] = time();
|
|
|
|
$stats = file_put_contents($stats_file, json_encode($stats), LOCK_EX);
|
|
}
|
|
|
|
$stats = json_decode(file_get_contents($stats_file), true);
|
|
|
|
if ($stats["time"] + $auto_recalculate_seconds < time()) {
|
|
header("Location: ?recalculate");
|
|
exit(0);
|
|
}
|
|
|
|
?>
|
|
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<title>Stats</title>
|
|
<link rel="icon" href="/favicon.ico">
|
|
<meta charset="UTF-8">
|
|
<link rel="stylesheet" href="<?= $absolute_root ?>styles.css">
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<div>
|
|
<h2>Storage</h2>
|
|
<?php foreach ($stats["storage"] as $disk_name => $disk_stats): ?>
|
|
<h3>
|
|
<?= $disk_name ?>
|
|
<?= human_readable_filesize($disk_stats["total_bytes"]) ?>
|
|
<?= round(100 * $disk_stats["used_bytes"] / $disk_stats["total_bytes"]) ?>%
|
|
</h3>
|
|
<div class="storage-gauge">
|
|
<?php foreach ($disk_stats["categories"] as $category_name => $category_stats): ?>
|
|
<div style="--width: <?= round(100 * $category_stats["bytes"] / $disk_stats["total_bytes"], 2) ?>%; --color: <?= $category_stats["color"] ?? "red" ?>"></div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<div class="storage-breakdown">
|
|
<?php foreach ($disk_stats["categories"] as $category_name => $category_stats): ?>
|
|
<div>
|
|
<div class="storage-circle" style="--color: <?= $category_stats["color"] ?? "red" ?>"></div>
|
|
<p><?= human_readable_filesize($category_stats["bytes"]) ?></p>
|
|
<p><?= round(100 * $category_stats["bytes"] / $disk_stats["total_bytes"]) ?>%</p>
|
|
<p><?= $category_name ?></p>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php if ($show_actions): ?>
|
|
<div>
|
|
<h2>Actions</h2>
|
|
<form method="get">
|
|
<input type="hidden" name="recalculate"></input>
|
|
<button>Recalculate</button>
|
|
</form>
|
|
</div>
|
|
<?php endif; ?>
|
|
</main>
|
|
</body>
|
|
</html>
|