qewqwe
This commit is contained in:
@@ -153,6 +153,15 @@ final class Router
|
||||
$this->respond(['data' => $this->fxHistory()]);
|
||||
}
|
||||
|
||||
if ($resource === 'offer-basis-history' && $method === 'GET') {
|
||||
$this->respond(['data' => $this->offerBasisHistory($projectKey)]);
|
||||
}
|
||||
|
||||
if ($resource === 'offer-basis-history' && $method === 'POST') {
|
||||
$this->repository()->ensureProject($projectKey);
|
||||
$this->respond(['data' => $this->saveOfferBasisHistory($projectKey, Http::input())], 201);
|
||||
}
|
||||
|
||||
if ($resource === 'legacy-fx-migrate' && $method === 'POST') {
|
||||
$this->respond(['data' => $this->migrateLegacyFxRates($projectKey)], 201);
|
||||
}
|
||||
@@ -818,6 +827,11 @@ final class Router
|
||||
return $rows;
|
||||
}
|
||||
|
||||
private function offerBasisHistory(string $projectKey): array
|
||||
{
|
||||
return $this->repository()->listOfferBasisHistory($projectKey, 50);
|
||||
}
|
||||
|
||||
private function migrateLegacyFxRates(string $projectKey): array
|
||||
{
|
||||
$fxRepository = $this->sharedFxRatesRepository();
|
||||
@@ -1694,6 +1708,21 @@ final class Router
|
||||
}
|
||||
|
||||
$measurements = $this->measurements($projectKey, $settings);
|
||||
$freshFetchId = null;
|
||||
if ($settings['crypto_base_price_amount'] !== null) {
|
||||
try {
|
||||
$fresh = $this->fx()->refreshLatestRates(null, 'USD');
|
||||
$freshFetchId = is_numeric($fresh['fetch_id'] ?? null) ? (int) $fresh['fetch_id'] : null;
|
||||
} catch (\Throwable) {
|
||||
$freshFetchId = null;
|
||||
}
|
||||
}
|
||||
if ($freshFetchId !== null && $freshFetchId > 0 && $measurements !== []) {
|
||||
$latestIndex = array_key_last($measurements);
|
||||
if ($latestIndex !== null && is_array($measurements[$latestIndex])) {
|
||||
$measurements[$latestIndex]['fx_fetch_id'] = $freshFetchId;
|
||||
}
|
||||
}
|
||||
$summary = $this->analytics()->buildSummary($measurements, $settings, [], [
|
||||
'include_offer_scenarios' => true,
|
||||
'include_long_term_projection' => true,
|
||||
@@ -1701,9 +1730,38 @@ final class Router
|
||||
|
||||
return [
|
||||
'miner_offers' => is_array($summary['miner_offers'] ?? null) ? $summary['miner_offers'] : [],
|
||||
'fx_fetch_id' => $freshFetchId,
|
||||
];
|
||||
}
|
||||
|
||||
private function saveOfferBasisHistory(string $projectKey, array $input): array
|
||||
{
|
||||
$offerType = $this->enumValue($input['offer_type'] ?? null, ['crypto', 'fiat'], 'offer_type');
|
||||
$basePriceAmount = $this->requiredDecimal($input['base_price_amount'] ?? null, 'base_price_amount');
|
||||
if ($basePriceAmount <= 0) {
|
||||
throw new ApiException('base_price_amount muss groesser als 0 sein.', 422, ['field' => 'base_price_amount']);
|
||||
}
|
||||
|
||||
$basePriceCurrency = $offerType === 'crypto'
|
||||
? $this->requiredCurrency($input['base_price_currency'] ?? 'USD', 'base_price_currency')
|
||||
: 'EUR';
|
||||
|
||||
if ($offerType === 'crypto') {
|
||||
$cryptoCurrency = $this->requiredCurrency($this->settings($projectKey)['crypto_currency'] ?? 'DOGE', 'crypto_currency');
|
||||
if (!in_array($basePriceCurrency, ['USD', $cryptoCurrency], true)) {
|
||||
throw new ApiException('Bei Crypto-Basispreisen sind nur USD oder die Projekt-Kryptowaehrung erlaubt.', 422, [
|
||||
'field' => 'base_price_currency',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->repository()->saveOfferBasisEntry($projectKey, [
|
||||
'offer_type' => $offerType,
|
||||
'base_price_amount' => $basePriceAmount,
|
||||
'base_price_currency' => $basePriceCurrency,
|
||||
]);
|
||||
}
|
||||
|
||||
private function purchasedMiners(string $projectKey): array
|
||||
{
|
||||
return $this->repository()->listPurchasedMiners($projectKey);
|
||||
|
||||
@@ -1188,6 +1188,23 @@ final class AnalyticsService
|
||||
}
|
||||
$referencePriceAmount = $basePriceAmount;
|
||||
$referencePriceCurrency = $basePriceCurrency !== '' ? $basePriceCurrency : null;
|
||||
$cryptoDisplayAmount = null;
|
||||
$cryptoDisplayCurrency = null;
|
||||
$usdDisplayAmount = null;
|
||||
$usdDisplayCurrency = null;
|
||||
|
||||
if ($paymentType === 'crypto') {
|
||||
$cryptoDisplayCurrency = strtoupper(trim((string) ($settings['crypto_currency'] ?? 'DOGE')));
|
||||
$cryptoDisplayAmount = $this->convertAmount($basePriceAmount, $basePriceCurrency, $cryptoDisplayCurrency, $latest);
|
||||
if ($cryptoDisplayAmount === null && strtoupper($basePriceCurrency) === $cryptoDisplayCurrency) {
|
||||
$cryptoDisplayAmount = $basePriceAmount;
|
||||
}
|
||||
$usdDisplayCurrency = 'USD';
|
||||
$usdDisplayAmount = $this->convertAmount($basePriceAmount, $basePriceCurrency, 'USD', $latest);
|
||||
if ($usdDisplayAmount === null && strtoupper($basePriceCurrency) === 'USD') {
|
||||
$usdDisplayAmount = $basePriceAmount;
|
||||
}
|
||||
}
|
||||
|
||||
$expectedDogePerDay = null;
|
||||
if ($currentHashrateMh > 0 && $offerHashrateMh > 0 && is_numeric($latest['doge_per_day_interval'] ?? null)) {
|
||||
@@ -1202,16 +1219,11 @@ final class AnalyticsService
|
||||
? $effectivePriceAmount / $expectedDailyRevenue
|
||||
: null;
|
||||
|
||||
$recommendation = 'keine Basis';
|
||||
if ($breakEvenDays !== null) {
|
||||
if ($breakEvenDays <= 180) {
|
||||
$recommendation = 'lohnt eher';
|
||||
} elseif ($breakEvenDays <= 365) {
|
||||
$recommendation = 'abwaegen';
|
||||
} else {
|
||||
$recommendation = 'eher warten';
|
||||
}
|
||||
}
|
||||
$recommendation = $this->offerRecommendation(
|
||||
$breakEvenDays,
|
||||
(int) ($offer['runtime_months'] ?? 0),
|
||||
!empty($offer['auto_renew'])
|
||||
);
|
||||
|
||||
return array_merge($offer, [
|
||||
'payment_type' => $paymentType,
|
||||
@@ -1222,6 +1234,10 @@ final class AnalyticsService
|
||||
'effective_price_currency' => $effectivePriceCurrency,
|
||||
'reference_price_amount' => $this->roundOrNull($referencePriceAmount, 8),
|
||||
'reference_price_currency' => $referencePriceCurrency !== '' ? $referencePriceCurrency : null,
|
||||
'crypto_display_price_amount' => $this->roundOrNull($cryptoDisplayAmount, 8),
|
||||
'crypto_display_price_currency' => $cryptoDisplayCurrency,
|
||||
'usd_display_price_amount' => $this->roundOrNull($usdDisplayAmount, 8),
|
||||
'usd_display_price_currency' => $usdDisplayCurrency,
|
||||
'expected_doge_per_day' => $this->roundOrNull($expectedDogePerDay, 6),
|
||||
'expected_daily_revenue' => $this->roundOrNull($expectedDailyRevenue, 8),
|
||||
'break_even_days' => $this->roundOrNull($breakEvenDays, 2),
|
||||
@@ -1229,6 +1245,34 @@ final class AnalyticsService
|
||||
]);
|
||||
}
|
||||
|
||||
private function offerRecommendation(?float $breakEvenDays, int $runtimeMonths, bool $autoRenew): string
|
||||
{
|
||||
if ($breakEvenDays === null) {
|
||||
return 'keine Basis';
|
||||
}
|
||||
|
||||
if (!$autoRenew && $runtimeMonths > 0) {
|
||||
$runtimeDays = $runtimeMonths * 30.0;
|
||||
if ($breakEvenDays <= ($runtimeDays * 0.85)) {
|
||||
return 'lohnt eher';
|
||||
}
|
||||
if ($breakEvenDays <= $runtimeDays) {
|
||||
return 'abwaegen';
|
||||
}
|
||||
|
||||
return 'eher warten';
|
||||
}
|
||||
|
||||
if ($breakEvenDays <= 180) {
|
||||
return 'lohnt eher';
|
||||
}
|
||||
if ($breakEvenDays <= 365) {
|
||||
return 'abwaegen';
|
||||
}
|
||||
|
||||
return 'eher warten';
|
||||
}
|
||||
|
||||
private function enrichOfferScenario(array $offer, array $latest, float $currentHashrateMh, array $costPlans, array $purchasedMiners): array
|
||||
{
|
||||
$latestCurrency = (string) ($latest['effective_price_currency'] ?? $latest['price_currency'] ?? '');
|
||||
|
||||
@@ -111,6 +111,92 @@ final class MiningRepository
|
||||
]);
|
||||
}
|
||||
|
||||
public function listOfferBasisHistory(string $projectKey, int $limit = 50): array
|
||||
{
|
||||
$stmt = $this->pdo->prepare(
|
||||
'SELECT * FROM ' . $this->table('offer_basis_history') . '
|
||||
WHERE project_key = :project_key AND owner_sub = :owner_sub
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT :limit'
|
||||
);
|
||||
$stmt->bindValue(':project_key', $projectKey, PDO::PARAM_STR);
|
||||
$stmt->bindValue(':owner_sub', $this->ownerSub, PDO::PARAM_STR);
|
||||
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
return $this->normalizeRows($stmt->fetchAll() ?: []);
|
||||
}
|
||||
|
||||
public function latestOfferBasisEntry(string $projectKey, string $offerType): ?array
|
||||
{
|
||||
$stmt = $this->pdo->prepare(
|
||||
'SELECT * FROM ' . $this->table('offer_basis_history') . '
|
||||
WHERE project_key = :project_key AND owner_sub = :owner_sub AND offer_type = :offer_type
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT 1'
|
||||
);
|
||||
$stmt->execute([
|
||||
'project_key' => $projectKey,
|
||||
'owner_sub' => $this->ownerSub,
|
||||
'offer_type' => strtolower(trim($offerType)),
|
||||
]);
|
||||
$row = $stmt->fetch();
|
||||
return is_array($row) ? $this->normalizeRow($row) : null;
|
||||
}
|
||||
|
||||
public function saveOfferBasisEntry(string $projectKey, array $payload): array
|
||||
{
|
||||
$normalizedType = strtolower(trim((string) ($payload['offer_type'] ?? '')));
|
||||
$latest = $this->latestOfferBasisEntry($projectKey, $normalizedType);
|
||||
if (
|
||||
is_array($latest)
|
||||
&& (string) ($latest['base_price_currency'] ?? '') === (string) ($payload['base_price_currency'] ?? '')
|
||||
&& is_numeric($latest['base_price_amount'] ?? null)
|
||||
&& is_numeric($payload['base_price_amount'] ?? null)
|
||||
&& abs((float) $latest['base_price_amount'] - (float) $payload['base_price_amount']) < 0.00000001
|
||||
) {
|
||||
return $latest;
|
||||
}
|
||||
|
||||
if ($this->driver === 'pgsql') {
|
||||
$stmt = $this->pdo->prepare(
|
||||
'INSERT INTO ' . $this->table('offer_basis_history') . ' (
|
||||
project_key, owner_sub, offer_type, base_price_amount, base_price_currency
|
||||
) VALUES (
|
||||
:project_key, :owner_sub, :offer_type, :base_price_amount, :base_price_currency
|
||||
)
|
||||
RETURNING *'
|
||||
);
|
||||
$stmt->execute([
|
||||
'project_key' => $projectKey,
|
||||
'owner_sub' => $this->ownerSub,
|
||||
'offer_type' => $normalizedType,
|
||||
'base_price_amount' => $payload['base_price_amount'],
|
||||
'base_price_currency' => $payload['base_price_currency'],
|
||||
]);
|
||||
return $this->normalizeRow($stmt->fetch() ?: []);
|
||||
}
|
||||
|
||||
$stmt = $this->pdo->prepare(
|
||||
'INSERT INTO ' . $this->table('offer_basis_history') . ' (
|
||||
project_key, owner_sub, offer_type, base_price_amount, base_price_currency
|
||||
) VALUES (
|
||||
:project_key, :owner_sub, :offer_type, :base_price_amount, :base_price_currency
|
||||
)'
|
||||
);
|
||||
$stmt->execute([
|
||||
'project_key' => $projectKey,
|
||||
'owner_sub' => $this->ownerSub,
|
||||
'offer_type' => $normalizedType,
|
||||
'base_price_amount' => $payload['base_price_amount'],
|
||||
'base_price_currency' => $payload['base_price_currency'],
|
||||
]);
|
||||
|
||||
$id = (int) $this->pdo->lastInsertId();
|
||||
$fetch = $this->pdo->prepare('SELECT * FROM ' . $this->table('offer_basis_history') . ' WHERE id = :id LIMIT 1');
|
||||
$fetch->execute(['id' => $id]);
|
||||
return $this->normalizeRow($fetch->fetch() ?: []);
|
||||
}
|
||||
|
||||
public function tableExists(string $logicalName): bool
|
||||
{
|
||||
$tableName = $this->table($logicalName);
|
||||
@@ -1384,6 +1470,7 @@ final class MiningRepository
|
||||
'wallet_snapshots' => $this->prefix . 'wallet_snapshots',
|
||||
'wallet_withdrawals' => $this->prefix . 'wallet_withdrawals',
|
||||
'miner_offers' => $this->prefix . 'miner_offers',
|
||||
'offer_basis_history' => $this->prefix . 'offer_basis_history',
|
||||
'purchased_miners' => $this->prefix . 'purchased_miners',
|
||||
default => throw new \RuntimeException('Unknown mining table: ' . $logicalName),
|
||||
};
|
||||
|
||||
@@ -217,6 +217,10 @@ final class SchemaManager
|
||||
$this->upgradeMinerOffersTable();
|
||||
$applied[] = 'miner_offers_table';
|
||||
}
|
||||
if (!$this->tableExists($this->prefix . 'offer_basis_history')) {
|
||||
$this->ensureOfferBasisHistoryTable();
|
||||
$applied[] = 'offer_basis_history_table';
|
||||
}
|
||||
if (!$this->tableExists($this->prefix . 'purchased_miners')) {
|
||||
$this->upgradePurchasedMinersTable();
|
||||
$applied[] = 'purchased_miners_table';
|
||||
@@ -337,6 +341,10 @@ final class SchemaManager
|
||||
$this->upgradeMinerOffersTable();
|
||||
$applied[] = 'miner_offers_table';
|
||||
}
|
||||
if (!$this->tableExists($this->prefix . 'offer_basis_history')) {
|
||||
$this->ensureOfferBasisHistoryTable();
|
||||
$applied[] = 'offer_basis_history_table';
|
||||
}
|
||||
|
||||
if (!$this->tableExists($this->prefix . 'purchased_miners')) {
|
||||
$this->upgradePurchasedMinersTable();
|
||||
@@ -693,6 +701,9 @@ final class SchemaManager
|
||||
if (!$this->tableExists($this->prefix . 'miner_offers')) {
|
||||
$upgrades[] = 'miner_offers_table';
|
||||
}
|
||||
if (!$this->tableExists($this->prefix . 'offer_basis_history')) {
|
||||
$upgrades[] = 'offer_basis_history_table';
|
||||
}
|
||||
if (!$this->tableExists($this->prefix . 'purchased_miners')) {
|
||||
$upgrades[] = 'purchased_miners_table';
|
||||
}
|
||||
@@ -1179,6 +1190,45 @@ final class SchemaManager
|
||||
$this->executeUpgradeStatements($statements, 'Schema-Upgrade fuer Miner-Angebote fehlgeschlagen.');
|
||||
}
|
||||
|
||||
private function ensureOfferBasisHistoryTable(): void
|
||||
{
|
||||
if ($this->tableExists($this->prefix . 'offer_basis_history')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$table = $this->prefix . 'offer_basis_history';
|
||||
$projectTable = $this->prefix . 'projects';
|
||||
$statements = $this->driver === 'pgsql'
|
||||
? [
|
||||
'CREATE TABLE IF NOT EXISTS ' . $table . ' (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
project_key VARCHAR(64) NOT NULL,
|
||||
owner_sub VARCHAR(191) NOT NULL DEFAULT \'local\',
|
||||
offer_type VARCHAR(16) NOT NULL,
|
||||
base_price_amount NUMERIC(20,8) NOT NULL,
|
||||
base_price_currency VARCHAR(10) NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_mining_offer_basis_history_project FOREIGN KEY (project_key) REFERENCES ' . $projectTable . '(project_key) ON DELETE CASCADE
|
||||
)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_mining_offer_basis_history_project_owner_type_created ON ' . $table . ' (project_key, owner_sub, offer_type, created_at)',
|
||||
]
|
||||
: [
|
||||
'CREATE TABLE IF NOT EXISTS `' . $table . '` (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
project_key VARCHAR(64) NOT NULL,
|
||||
owner_sub VARCHAR(191) NOT NULL DEFAULT \'local\',
|
||||
offer_type VARCHAR(16) NOT NULL,
|
||||
base_price_amount DECIMAL(20,8) NOT NULL,
|
||||
base_price_currency VARCHAR(10) NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_mining_offer_basis_history_project FOREIGN KEY (project_key) REFERENCES `' . $projectTable . '`(project_key) ON DELETE CASCADE,
|
||||
KEY idx_mining_offer_basis_history_project_owner_type_created (project_key, owner_sub, offer_type, created_at)
|
||||
)',
|
||||
];
|
||||
|
||||
$this->executeUpgradeStatements($statements, 'Schema-Upgrade fuer Angebots-Basis-Historie fehlgeschlagen.');
|
||||
}
|
||||
|
||||
private function upgradePurchasedMinersTable(): void
|
||||
{
|
||||
$table = $this->prefix . 'purchased_miners';
|
||||
@@ -1528,6 +1578,7 @@ final class SchemaManager
|
||||
$this->prefix . 'wallet_snapshots',
|
||||
$this->prefix . 'wallet_withdrawals',
|
||||
$this->prefix . 'miner_offers',
|
||||
$this->prefix . 'offer_basis_history',
|
||||
$this->prefix . 'targets',
|
||||
$this->prefix . 'dashboard_definitions',
|
||||
$this->prefix . 'purchased_miners',
|
||||
|
||||
Reference in New Issue
Block a user