public function testGetTorrentAgeInDays() { // without doneDate $this->assertEquals(1, TorrentUtils::getTorrentAgeInDays(['doneDate' => 0, 'addedDate' => time() - 86400])); // with doneDate $this->assertEquals(2, TorrentUtils::getTorrentAgeInDays(['doneDate' => time() - 86400 * 2, 'addedDate' => time() - 86400])); }
protected function execute(InputInterface $input, OutputInterface $output) { $config = $this->getApplication()->getConfig(); $logger = $this->getApplication()->getLogger(); $client = $this->getApplication()->getClient(); $torrentList = $client->getTorrentData(); $obsoleteList = TorrentListUtils::getObsoleteTorrents($torrentList); if (!empty($obsoleteList)) { $output->writeln('<comment>Found obsolete torrents, remove it using transmission-cli torrent-remove-duplicates</comment>'); return 1; } try { $influxDbClient = $this->getApplication()->getInfluxDbClient($config->get('influxdb-host'), $config->get('influxdb-port'), $config->get('influxdb-user'), $config->get('influxdb-password'), $config->get('influxdb-database')); $points = []; $transmissionHost = $config->get('transmission-host'); foreach ($torrentList as $torrent) { $age = TorrentUtils::getTorrentAge($torrent); $torrentPoint = $influxDbClient->buildPoint($torrent, $transmissionHost); if ($age) { $points[] = $torrentPoint; } else { $logger->debug('Skip point: {point}', ['point' => $torrentPoint]); } } $this->dryRun($input, $output, function () use($influxDbClient, $points) { $influxDbClient->writePoints($points); }, 'dry-run, don\'t really send points'); } catch (\Exception $e) { $logger->critical($e->getMessage()); return 1; } return 0; }
public static function buildTableData(array $torrentList) { $headers = ['Name', 'Id', 'Age', 'Size', 'Uploaded', 'Per day']; $rows = []; foreach ($torrentList as $torrent) { $age = TorrentUtils::getTorrentAgeInDays($torrent); $perDay = $age ? TorrentUtils::getSizeInGb($torrent[Torrent\Get::UPLOAD_EVER] / $age) : 0; $rows[] = [$torrent[Torrent\Get::NAME], $torrent[Torrent\Get::ID], $age, TorrentUtils::getSizeInGb($torrent[Torrent\Get::TOTAL_SIZE]), TorrentUtils::getSizeInGb($torrent[Torrent\Get::UPLOAD_EVER]), $perDay]; } return ['headers' => $headers, 'rows' => $rows]; }
private function buildTableData(array $torrentList, $sort, $limit) { $rows = []; foreach ($torrentList as $torrent) { $rows[] = [$torrent[Torrent\Get::NAME], $torrent[Torrent\Get::ID], $torrent['age'], TorrentUtils::getSizeInGb($torrent[Torrent\Get::TOTAL_SIZE]), TorrentUtils::getSizeInGb($torrent['uploaded']), $torrent['per_day'], $torrent['profit']]; } $rows = TableUtils::sortRowsByColumnNumber($rows, $sort); $rows = TableUtils::limitRows($rows, $limit); return ['headers' => ['Name', 'Id', 'Age, days', 'Size, GB', 'Uploaded, GB', 'Per day, GB', 'Profit, %'], 'rows' => $rows, 'totals' => ['Total: ' . count($rows), '', '', TorrentListUtils::sumArrayField($rows, 3), TorrentListUtils::sumArrayField($rows, 4), TorrentListUtils::sumArrayField($rows, 5), TorrentListUtils::sumArrayField($rows, 6)]]; }
protected function execute(InputInterface $input, OutputInterface $output) { $client = $this->getApplication()->getClient(); $torrentList = $client->getTorrentData(); $torrentList = array_map(function ($torrent) { $torrent['age'] = TorrentUtils::getTorrentAgeInDays($torrent); return $torrent; }, $torrentList); $torrentList = TorrentListUtils::filterTorrents($torrentList, ['age' => $input->getOption('age'), 'name' => $input->getOption('name')]); TorrentListUtils::printTorrentsTable($torrentList, $output, $input->getOption('sort'), $input->getOption('limit')); $freeSpace = $client->getFreeSpace(); $output->writeln('Free space: ' . TorrentUtils::getSizeInGb($freeSpace) . ' GB'); }
/** * @param array $torrent * @param string $transmissionHost * @return InfluxDB\Point */ public function buildPoint(array $torrent, $transmissionHost) { $age = TorrentUtils::getTorrentAge($torrent); $lastPoint = $this->getLastPoint($torrent, $transmissionHost); $tagsData = ['host' => $transmissionHost, 'torrent_name' => $torrent[Torrent\Get::NAME]]; $uploadedDerivative = count($lastPoint) && $torrent[Torrent\Get::UPLOAD_EVER] - $lastPoint['last'] >= 0 ? $torrent[Torrent\Get::UPLOAD_EVER] - $lastPoint['last'] : $torrent[Torrent\Get::UPLOAD_EVER]; $fieldsData = ['uploaded_last' => $uploadedDerivative, 'downloaded' => $torrent[Torrent\Get::TOTAL_SIZE], 'age' => $age, 'uploaded_per_day' => $age ? intval($torrent[Torrent\Get::UPLOAD_EVER] / $age * 86400) : 0]; return new InfluxDB\Point('uploaded', $torrent[Torrent\Get::UPLOAD_EVER], $tagsData, $fieldsData, time()); }