Align sysstat buckets to interval

This commit is contained in:
root
2026-01-30 01:53:35 +02:00
parent 6147777c02
commit 98b6da4f52
2 changed files with 26 additions and 7 deletions

View File

@@ -35,7 +35,20 @@ class ServerChartsWidget extends Widget
$data = $this->getData();
$config = $this->rangeConfig($this->range);
$sysstat = new SysstatMetrics;
$label = now($sysstat->timezoneName())->format($config['label_format']);
$labelTime = now($sysstat->timezoneName());
if ($config['interval_seconds'] < 60) {
$second = intdiv($labelTime->second, $config['interval_seconds']) * $config['interval_seconds'];
$labelTime = $labelTime->setTime($labelTime->hour, $labelTime->minute, $second);
} else {
$labelTime = $labelTime->second(0);
if ($config['interval_seconds'] >= 3600) {
$labelTime = $labelTime->minute(0);
}
if ($config['interval_seconds'] >= 86400) {
$labelTime = $labelTime->hour(0)->minute(0);
}
}
$label = $labelTime->format($config['label_format']);
$this->dispatch('server-charts-updated', [
'cpu' => $data['cpu']['usage'] ?? 0,

View File

@@ -20,12 +20,18 @@ class SysstatMetrics
}
$timezone = $this->systemTimezone();
$end = CarbonImmutable::now($timezone)->second(0);
if ($intervalSeconds >= 3600) {
$end = $end->minute(0);
}
if ($intervalSeconds >= 86400) {
$end = $end->hour(0)->minute(0);
$end = CarbonImmutable::now($timezone);
if ($intervalSeconds < 60) {
$second = intdiv($end->second, $intervalSeconds) * $intervalSeconds;
$end = $end->setTime($end->hour, $end->minute, $second);
} else {
$end = $end->second(0);
if ($intervalSeconds >= 3600) {
$end = $end->minute(0);
}
if ($intervalSeconds >= 86400) {
$end = $end->hour(0)->minute(0);
}
}
$start = $end->subSeconds(($points - 1) * $intervalSeconds);
$samples = $this->readSamples($start, $end);