<?php

$algo = VEXON_DEFAULT_ALGO;
$coin = getdbosql('db_coins', "symbol='VEX'");
$port = getAlgoPort($algo);
if (!$port) $port = 4499;

$now = time();
$t15 = $now - 15*60;
$t60 = $now - 60*60;

$active_miners = vexon_live_active_miners($algo, 15*60);
$workers_total = intval(dboscalar("SELECT COUNT(*) FROM workers WHERE algo=:algo", array(':algo'=>$algo)));
$accepted_15m = intval(dboscalar("SELECT COUNT(*) FROM shares WHERE valid=1 AND algo=:algo AND time>$t15", array(':algo'=>$algo)));
$rejected_15m = intval(dboscalar("SELECT COUNT(*) FROM shares WHERE valid=0 AND algo=:algo AND time>$t15", array(':algo'=>$algo)));
$accepted_1h = intval(dboscalar("SELECT COUNT(*) FROM shares WHERE valid=1 AND algo=:algo AND time>$t60", array(':algo'=>$algo)));
$pool_hashrate = vexon_live_pool_rate($algo, 15*60);
$pool_hashrate_text = $pool_hashrate > 0 ? Itoa2($pool_hashrate).'h/s' : 'Waiting';
$fee = floatval(vexon_fee($algo));
$difficulty = $coin && $coin->difficulty ? Itoa2($coin->difficulty, 3) : 'Waiting';
$reward = $coin && $coin->reward ? floatval($coin->reward) : floatval(VEXON_BLOCK_REWARD_ESTIMATE);
$last_share_time = intval(dboscalar("SELECT MAX(time) FROM shares WHERE algo=:algo", array(':algo'=>$algo)));
$last_share = $last_share_time ? datetoa2($last_share_time).' ago' : 'No shares yet';

$reject_rate = ($accepted_15m + $rejected_15m) > 0 ? round($rejected_15m * 100 / ($accepted_15m + $rejected_15m), 2).'%' : '0%';

echo <<<END
<section class="vexon-telemetry-card">
	<div class="vexon-card-heading">
		<div>
			<span class="vexon-card-kicker">Live mining telemetry</span>
			<h2>VEX Pool Status</h2>
		</div>
		<img src="/vexon/ui/images/vexon-logo.png" alt="Vexon">
	</div>

	<div class="vexon-metric-grid">
		<div><span>Algorithm</span><strong>Blake256r14</strong></div>
		<div><span>Port</span><strong>{$port}</strong></div>
		<div><span>Pool hashrate</span><strong>{$pool_hashrate_text}</strong></div>
		<div><span>Active miners</span><strong>{$active_miners}</strong></div>
	</div>

	<table class="vexon-stat-table">
		<thead>
			<tr>
				<th>Metric</th>
				<th>Current</th>
				<th>Window</th>
				<th>Note</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>Accepted shares</td>
				<td>{$accepted_15m}</td>
				<td>15 minutes</td>
				<td>Used for miner accounting</td>
			</tr>
			<tr>
				<td>Rejected shares</td>
				<td>{$rejected_15m}</td>
				<td>15 minutes</td>
				<td>Reject rate {$reject_rate}</td>
			</tr>
			<tr>
				<td>Share flow</td>
				<td>{$accepted_1h}</td>
				<td>1 hour</td>
				<td>Last share: {$last_share}</td>
			</tr>
			<tr>
				<td>Network difficulty</td>
				<td>{$difficulty}</td>
				<td>Current VEX</td>
				<td>Reward target {$reward} VEX</td>
			</tr>
			<tr>
				<td>Registered workers</td>
				<td>{$workers_total}</td>
				<td>All time</td>
				<td>Only active workers affect live hashrate</td>
			</tr>
		</tbody>
	</table>
</section>
END;
