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; }
/** * @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()); }