sdfsf
All checks were successful
Deploy / deploy-staging (push) Successful in 6s
Deploy / deploy-production (push) Has been skipped

This commit is contained in:
2026-05-02 02:03:42 +02:00
parent 58d0c18e25
commit 6cbf76918c
4 changed files with 195 additions and 50 deletions

View File

@@ -22,6 +22,7 @@ final class Router
private const BOOTSTRAP_MEASUREMENT_LIMIT = 150;
private const BOOTSTRAP_SNAPSHOT_LIMIT = 40;
private const LONG_REQUEST_BUDGET_SECONDS = 8.0;
private const OVERVIEW_WINDOW_DAYS = 15;
private string $moduleBasePath;
private ModuleConfig $config;
@@ -164,7 +165,8 @@ final class Router
}
if ($resource === 'bootstrap' && $method === 'GET') {
$this->respond(['data' => $this->bootstrap($projectKey)]);
$view = trim((string) ($_GET['view'] ?? 'overview'));
$this->respond(['data' => $this->bootstrap($projectKey, $view)]);
}
if ($resource === 'measurements' && $method === 'GET') {
@@ -310,11 +312,13 @@ final class Router
}
}
private function bootstrap(string $projectKey): array
private function bootstrap(string $projectKey, string $view = 'overview'): array
{
$startedAt = microtime(true);
$view = $this->normalizeBootstrapView($view);
$this->debug->add('bootstrap.start', [
'project_key' => $projectKey,
'view' => $view,
'measurement_limit' => self::BOOTSTRAP_MEASUREMENT_LIMIT,
'snapshot_limit' => self::BOOTSTRAP_SNAPSHOT_LIMIT,
]);
@@ -339,25 +343,30 @@ final class Router
'purchased_miners' => [],
'measurement_rates' => [],
], ['project_key' => $projectKey]);
$measurements = $this->safeTimed('bootstrap.measurements', fn () => $this->bootstrapMeasurements($projectKey, $settings), [], [
$measurements = $this->safeTimed('bootstrap.measurements', fn () => $this->bootstrapMeasurements($projectKey, $settings, $view), [], [
'project_key' => $projectKey,
'view' => $view,
]);
$targets = $this->safeTimed('bootstrap.targets', fn () => $this->targets($projectKey), [], [
$targets = $this->safeTimed('bootstrap.targets', fn () => $this->bootstrapTargets($projectKey, $view), [], [
'project_key' => $projectKey,
'view' => $view,
]);
$dashboards = $this->safeTimed('bootstrap.dashboards', fn () => $this->dashboards($projectKey), [], [
$dashboards = $this->safeTimed('bootstrap.dashboards', fn () => $this->bootstrapDashboards($projectKey, $view), [], [
'project_key' => $projectKey,
'view' => $view,
]);
$fxSnapshots = $this->safeTimed('bootstrap.fx_snapshots', fn () => $this->measurementFxSnapshots($measurements, self::BOOTSTRAP_SNAPSHOT_LIMIT), [], [
$fxSnapshots = $this->safeTimed('bootstrap.fx_snapshots', fn () => $this->bootstrapFxSnapshots($measurements, $view), [], [
'view' => $view,
'measurement_count' => is_array($measurements) ? count($measurements) : 0,
]);
$summary = $this->safeTimed('bootstrap.summary', fn () => $this->analytics()->buildSummary($measurements, $settings, $targets), [
$summary = $this->safeTimed('bootstrap.summary', fn () => $this->bootstrapSummary($measurements, $settings, $targets, $view), [
'latest_measurement' => $measurements !== [] ? $measurements[array_key_last($measurements)] : null,
'baseline' => $settings,
'targets' => [],
'payouts' => [],
'miner_offers' => [],
], [
'view' => $view,
'measurement_count' => is_array($measurements) ? count($measurements) : 0,
'target_count' => is_array($targets) ? count($targets) : 0,
]);
@@ -381,6 +390,8 @@ final class Router
'summary' => $summary,
'bootstrap_meta' => [
'degraded' => $measurementCount >= self::BOOTSTRAP_MEASUREMENT_LIMIT,
'view' => $view,
'overview_window_days' => self::OVERVIEW_WINDOW_DAYS,
'measurement_limit' => self::BOOTSTRAP_MEASUREMENT_LIMIT,
'snapshot_limit' => self::BOOTSTRAP_SNAPSHOT_LIMIT,
'measurement_count' => $measurementCount,
@@ -1254,17 +1265,93 @@ final class Router
return $this->analytics()->enrichMeasurements($rows, $settings);
}
private function bootstrapMeasurements(string $projectKey, array $settings): array
private function bootstrapMeasurements(string $projectKey, array $settings, string $view): array
{
$rows = $this->repository()->listMeasurements($projectKey, self::BOOTSTRAP_MEASUREMENT_LIMIT);
if (in_array($view, ['settings', 'currencies', 'dashboards'], true)) {
return [];
}
$rows = in_array($view, ['overview', 'mining'], true)
? $this->repository()->listRecentMeasurements($projectKey, self::BOOTSTRAP_MEASUREMENT_LIMIT)
: $this->repository()->listMeasurements($projectKey, self::BOOTSTRAP_MEASUREMENT_LIMIT);
if (in_array($view, ['overview', 'mining'], true)) {
$rows = $this->filterMeasurementsToRecentWindow($rows, self::OVERVIEW_WINDOW_DAYS);
}
$this->debug->add('bootstrap.measurements.loaded', [
'project_key' => $projectKey,
'view' => $view,
'row_count' => count($rows),
'limit' => self::BOOTSTRAP_MEASUREMENT_LIMIT,
]);
return $this->analytics()->enrichMeasurements($rows, $settings);
}
private function bootstrapTargets(string $projectKey, string $view): array
{
return in_array($view, ['overview', 'mining'], true) ? $this->targets($projectKey) : [];
}
private function bootstrapDashboards(string $projectKey, string $view): array
{
return $view === 'dashboards' ? $this->dashboards($projectKey) : [];
}
private function bootstrapFxSnapshots(array $measurements, string $view): array
{
if (!in_array($view, ['overview', 'mining', 'measurements'], true)) {
return [];
}
return $this->measurementFxSnapshots($measurements, self::BOOTSTRAP_SNAPSHOT_LIMIT);
}
private function bootstrapSummary(array $measurements, array $settings, array $targets, string $view): array
{
if (!in_array($view, ['overview', 'mining'], true)) {
return [
'latest_measurement' => $measurements !== [] ? $measurements[array_key_last($measurements)] : null,
'baseline' => $settings,
'targets' => [],
'payouts' => [],
'miner_offers' => [],
];
}
return $this->analytics()->buildSummary($measurements, $settings, $targets);
}
private function filterMeasurementsToRecentWindow(array $rows, int $windowDays): array
{
if ($rows === [] || $windowDays <= 0) {
return $rows;
}
$latest = $rows[array_key_last($rows)] ?? null;
$latestTs = is_array($latest) ? strtotime((string) ($latest['measured_at'] ?? '')) : false;
if ($latestTs === false) {
return $rows;
}
$minTs = $latestTs - ($windowDays * 86400);
$filtered = array_values(array_filter($rows, static function (array $row) use ($minTs): bool {
$measuredTs = strtotime((string) ($row['measured_at'] ?? ''));
return $measuredTs !== false && $measuredTs >= $minTs;
}));
$latestRow = $rows[array_key_last($rows)] ?? null;
return $filtered !== [] || !is_array($latestRow) ? $filtered : [$latestRow];
}
private function normalizeBootstrapView(string $view): string
{
$normalized = trim(strtolower($view));
return in_array($normalized, ['overview', 'measurements', 'dashboards', 'currencies', 'mining', 'settings'], true)
? $normalized
: 'overview';
}
private function createMeasurement(string $projectKey, array $input): array
{
$projectTimezone = $this->projectTimezone($projectKey);