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

This commit is contained in:
2026-05-04 22:38:51 +02:00
parent 748d2c2e59
commit 3795fc1de5
2 changed files with 182 additions and 49 deletions

View File

@@ -20,6 +20,7 @@ final class AnalyticsService
$baselineAt = (string) ($settings['baseline_measured_at'] ?? '');
$costPlans = is_array($settings['cost_plans'] ?? null) ? $settings['cost_plans'] : [];
$payouts = is_array($settings['payouts'] ?? null) ? $settings['payouts'] : [];
$purchasedMiners = is_array($settings['purchased_miners'] ?? null) ? $settings['purchased_miners'] : [];
$preferredPriceCurrencies = array_values(array_unique(array_filter([
strtoupper(trim((string) ($settings['report_currency'] ?? ''))),
'USD',
@@ -30,11 +31,11 @@ final class AnalyticsService
$baselineTs = $this->utcTimestamp($baselineAt);
$previous = null;
$previousMeasuredTs = null;
$previousEffectiveCoinsTotal = null;
$previousIntervalRate = null;
$result = [];
$payoutIndex = 0;
$cumulativePayouts = 0.0;
$lastPayoutTs = null;
$latestPriceByCurrency = [];
foreach ($measurements as $row) {
@@ -46,6 +47,7 @@ final class AnalyticsService
}
$cumulativePayouts += (float) ($payouts[$payoutIndex]['coins_amount'] ?? 0);
$lastPayoutTs = $payoutTs;
$payoutIndex++;
}
@@ -55,17 +57,32 @@ final class AnalyticsService
$hoursSinceBaseline = $baselineTs > 0 && $measuredTs > $baselineTs ? ($measuredTs - $baselineTs) / 3600 : 0.0;
$perHourSinceBaseline = $hoursSinceBaseline > 0 ? $growth / $hoursSinceBaseline : null;
$perDaySinceBaseline = $perHourSinceBaseline !== null ? $perHourSinceBaseline * 24 : null;
$activeHashrateMh = $this->measurementHashrateMh($costPlans, $purchasedMiners, $measuredTs > 0 ? $measuredTs : null);
$intervalHours = null;
$intervalGrowth = null;
$perHourInterval = null;
$perDayInterval = null;
$perHourPerMhInterval = null;
$perDayPerMhInterval = null;
if (is_array($previous) && $previousMeasuredTs !== null && $previousEffectiveCoinsTotal !== null) {
$intervalHours = max(0.0, ($measuredTs - $previousMeasuredTs) / 3600);
$intervalGrowth = $effectiveCoinsTotal - $previousEffectiveCoinsTotal;
if (is_array($previous) && $previousMeasuredTs !== null) {
$intervalStartTs = $previousMeasuredTs;
$intervalStartCoins = (float) ($previous['coins_total'] ?? 0.0);
if ($lastPayoutTs !== null && $lastPayoutTs > $previousMeasuredTs && $lastPayoutTs <= $measuredTs) {
$intervalStartTs = $lastPayoutTs;
$intervalStartCoins = 0.0;
}
$intervalHours = max(0.0, ($measuredTs - $intervalStartTs) / 3600);
$intervalGrowth = $visibleCoinsTotal - $intervalStartCoins;
$perHourInterval = $intervalHours > 0 ? $intervalGrowth / $intervalHours : null;
$perDayInterval = $perHourInterval !== null ? $perHourInterval * 24 : null;
if ($perHourInterval !== null && $activeHashrateMh > 0) {
$perHourPerMhInterval = $perHourInterval / $activeHashrateMh;
$perDayPerMhInterval = $perDayInterval !== null ? $perDayInterval / $activeHashrateMh : null;
}
}
$trendLabel = 'stabil';
@@ -147,10 +164,13 @@ final class AnalyticsService
'hours_since_baseline' => $this->roundOrNull($hoursSinceBaseline, 4),
'doge_per_hour_since_baseline' => $this->roundOrNull($perHourSinceBaseline, 6),
'doge_per_day_since_baseline' => $this->roundOrNull($perDaySinceBaseline, 6),
'active_hashrate_mh' => $this->roundOrNull($activeHashrateMh > 0 ? $activeHashrateMh : null, 4),
'interval_hours' => $this->roundOrNull($intervalHours, 4),
'interval_growth' => $this->roundOrNull($intervalGrowth, 6),
'doge_per_hour_interval' => $this->roundOrNull($perHourInterval, 6),
'doge_per_day_interval' => $this->roundOrNull($perDayInterval, 6),
'doge_per_hour_per_mh_interval' => $this->roundOrNull($perHourPerMhInterval, 8),
'doge_per_day_per_mh_interval' => $this->roundOrNull($perDayPerMhInterval, 8),
'trend_label' => $trendLabel,
'effective_price_per_coin' => $this->roundOrNull($price, 8),
'effective_price_currency' => $priceCurrency,
@@ -172,7 +192,6 @@ final class AnalyticsService
}
$previous = $row;
$previousMeasuredTs = $measuredTs > 0 ? $measuredTs : null;
$previousEffectiveCoinsTotal = $effectiveCoinsTotal;
}
return $result;
@@ -565,6 +584,37 @@ final class AnalyticsService
return $total;
}
private function measurementHashrateMh(array $costPlans, array $purchasedMiners, ?int $measurementTs = null): float
{
$total = 0.0;
foreach ($costPlans as $plan) {
if (array_key_exists('is_active', $plan) && empty($plan['is_active'])) {
continue;
}
if (!$this->entryIsCovered($plan, $measurementTs)) {
continue;
}
$total += $this->normalizeHashrateMh($plan['mining_speed_value'] ?? null, $plan['mining_speed_unit'] ?? null);
$total += $this->normalizeHashrateMh($plan['bonus_speed_value'] ?? null, $plan['bonus_speed_unit'] ?? null);
}
foreach ($purchasedMiners as $miner) {
if (array_key_exists('is_active', $miner) && empty($miner['is_active'])) {
continue;
}
if (!$this->entryIsCovered($miner, $measurementTs, 'purchased_at')) {
continue;
}
$total += $this->normalizeHashrateMh($miner['mining_speed_value'] ?? null, $miner['mining_speed_unit'] ?? null);
$total += $this->normalizeHashrateMh($miner['bonus_speed_value'] ?? null, $miner['bonus_speed_unit'] ?? null);
}
return $total;
}
private function totalPurchasedMinerCost(array $purchasedMiners, string $targetCurrency, ?int $measurementTs = null, ?array $fxContext = null): ?float
{
$target = strtoupper(trim($targetCurrency));