diff --git a/database/yggstate.mwb b/database/yggstate.mwb index 4acbca8..330f18f 100644 Binary files a/database/yggstate.mwb and b/database/yggstate.mwb differ diff --git a/src/crontab/crawler.php b/src/crontab/crawler.php index f74f515..b61256b 100644 --- a/src/crontab/crawler.php +++ b/src/crontab/crawler.php @@ -121,6 +121,9 @@ if ($connectedPeers = Yggverse\Yggdrasilctl\Yggdrasil::getPeers()) { $db->updatePeerRemoteUptime($dbPeerRemote->peerRemoteId, $connectedPeerInfo->uptime, time()); } + $debug['yggdrasil']['peer']['remote']['total']['update'] += + $db->updatePeerRemoteTimeOnline($dbPeerRemote->peerRemoteId, time()); + } else { if ($peerRemoteId = $db->addPeerRemote($dbPeerId, @@ -133,6 +136,9 @@ if ($connectedPeers = Yggverse\Yggdrasilctl\Yggdrasil::getPeers()) { time())) { $debug['yggdrasil']['peer']['remote']['total']['insert']++; + + $debug['yggdrasil']['peer']['remote']['total']['update'] += + $db->updatePeerRemoteTimeOnline($peerRemoteId, time()); } } diff --git a/src/library/mysql.php b/src/library/mysql.php index 9979939..4049ba8 100644 --- a/src/library/mysql.php +++ b/src/library/mysql.php @@ -97,16 +97,17 @@ class MySQL { $this->_debug->query->insert->total++; - $query = $this->_db->prepare('INSERT INTO `peerRemote` SET `peerId` = ?, - `scheme` = ?, - `host` = ?, - `port` = ?, - `received` = ?, - `sent` = ?, - `uptime` = ?, - `timeAdded` = ?'); - - $query->execute([$peerId, $scheme, $host, $port, $received, $sent, $uptime, $timeAdded]); + $query = $this->_db->prepare('INSERT INTO `peerRemote` SET `peerId` = ?, + `scheme` = ?, + `host` = ?, + `port` = ?, + `received` = ?, + `sent` = ?, + `uptime` = ?, + `timeAdded` = ?, + `timeUpdated` = ?'); + + $query->execute([$peerId, $scheme, $host, $port, $received, $sent, $uptime, $timeAdded, $timeAdded]); return $this->_db->lastInsertId(); } @@ -144,6 +145,17 @@ class MySQL { return $query->rowCount(); } + public function updatePeerRemoteTimeOnline(int $peerRemoteId, $time) { + + $this->_debug->query->update->total++; + + $query = $this->_db->prepare('UPDATE `peerRemote` SET `timeOnline` = ?, `timeUpdated` = ? WHERE `peerRemoteId` = ? LIMIT 1'); + + $query->execute([$time, $time, $peerRemoteId]); + + return $query->rowCount(); + } + public function findPeerRemote(int $peerId, string $scheme, string $host, int $port) { $this->_debug->query->select->total++; @@ -162,9 +174,9 @@ class MySQL { $this->_debug->query->insert->total++; - $query = $this->_db->prepare('INSERT INTO `peerCoordinate` SET `peerId` = ?, - `port` = ?, - `timeAdded` = ?'); + $query = $this->_db->prepare('INSERT INTO `peerCoordinate` SET `peerId` = ?, + `port` = ?, + `timeAdded` = ?'); $query->execute([$peerId, $port, $timeAdded]); @@ -217,6 +229,62 @@ class MySQL { return $query->fetchAll(); } + // Analytics + public function getPeerFirst() { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT * FROM `peer` ORDER BY `peerId` ASC LIMIT 1'); + + $query->execute(); + + return $query->fetch(); + } + + public function getPeerTotal() { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT COUNT(*) AS `total` FROM `peer`'); + + $query->execute(); + + return $query->fetch()->total; + } + + public function findPeerTotalByTime(int $timeUpdatedFrom, int $timeUpdatedTo) : int { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `peerId`) AS `total` FROM `peerRemote` WHERE (`timeAdded` > ? AND `timeAdded` < ?) OR (`timeUpdated` > ? AND `timeUpdated` < ?)'); + + $query->execute([$timeUpdatedFrom, $timeUpdatedTo, $timeUpdatedFrom, $timeUpdatedTo]); + + return (int) $query->fetch()->total; + } + + public function findPeerRemoteReceivedSumByTime(int $timeUpdatedFrom, int $timeUpdatedTo) : int { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT SUM(`received`) AS `result` FROM `peerRemote` WHERE (`timeAdded` > ? AND `timeAdded` < ?) OR (`timeUpdated` > ? AND `timeUpdated` < ?)'); + + $query->execute([$timeUpdatedFrom, $timeUpdatedTo, $timeUpdatedFrom, $timeUpdatedTo]); + + return (int) $query->fetch()->result; + } + + public function findPeerRemoteSentSumByTime(int $timeUpdatedFrom, int $timeUpdatedTo) : int { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT SUM(`sent`) AS `result` FROM `peerRemote` WHERE (`timeAdded` > ? AND `timeAdded` < ?) OR (`timeUpdated` > ? AND `timeUpdated` < ?)'); + + $query->execute([$timeUpdatedFrom, $timeUpdatedTo, $timeUpdatedFrom, $timeUpdatedTo]); + + return (int) $query->fetch()->result; + } + // Other public function optimize() {