1 | <?php |
||
15 | class StatsGet extends Command |
||
16 | { |
||
17 | protected function configure() |
||
72 | |||
73 | protected function execute(InputInterface $input, OutputInterface $output) |
||
74 | { |
||
75 | $config = $this->getApplication()->getConfig(); |
||
76 | $logger = $this->getApplication()->getLogger(); |
||
77 | $client = $this->getApplication()->getClient(); |
||
78 | |||
79 | try { |
||
80 | $influxDbClient = $this->getApplication()->getInfluxDbClient( |
||
81 | $config->get('influxdb-host'), |
||
82 | $config->get('influxdb-port'), |
||
83 | $config->get('influxdb-user'), |
||
84 | $config->get('influxdb-password'), |
||
85 | $config->get('influxdb-database') |
||
86 | ); |
||
87 | |||
88 | $lastDays = (int)$input->getOption('days') ? (int)$input->getOption('days') : 0; |
||
89 | $limit = (int)$input->getOption('limit') ? (int)$input->getOption('limit') : 0; |
||
90 | |||
91 | $torrentList = $client->getTorrentData(); |
||
92 | |||
93 | $torrentList = array_map(function ($torrent) { |
||
94 | $torrent['age'] = TorrentUtils::getTorrentAgeInDays($torrent); |
||
95 | return $torrent; |
||
96 | }, $torrentList); |
||
97 | |||
98 | $torrentList = TorrentListUtils::filterTorrents($torrentList, [ |
||
99 | 'age' => $input->getOption('age'), |
||
100 | 'name' => $input->getOption('name'), |
||
101 | ]); |
||
102 | |||
103 | $transmissionHost = $config->get('transmission-host'); |
||
104 | |||
105 | $torrentList = array_map(function ($torrent) use ($influxDbClient, $transmissionHost, $lastDays) { |
||
106 | $torrent['uploaded'] = $influxDbClient->getTorrentSum( |
||
107 | $torrent, |
||
108 | 'uploaded_last', |
||
109 | $transmissionHost, |
||
110 | $lastDays |
||
111 | ); |
||
112 | |||
113 | $days = max(1, min($torrent['age'], $lastDays)); |
||
114 | $torrent['per_day'] = $days ? |
||
115 | TorrentUtils::getSizeInGb($torrent['uploaded'] / $days) : 0; |
||
116 | |||
117 | $torrent['profit'] = $torrent[Torrent\Get::TOTAL_SIZE] ? |
||
118 | round($torrent['uploaded'] / $torrent[Torrent\Get::TOTAL_SIZE] / $days * 100, 2) : 0; |
||
119 | |||
120 | return $torrent; |
||
121 | }, $torrentList); |
||
122 | } catch (\Exception $e) { |
||
123 | $logger->critical($e->getMessage()); |
||
124 | return 1; |
||
125 | } |
||
126 | |||
127 | $torrentList = TorrentListUtils::filterTorrents($torrentList, [ |
||
128 | 'profit' => ['type' => 'numeric', 'value' => $input->getOption('profit')] |
||
129 | ]); |
||
130 | |||
131 | $tableData = $this->buildTableData($torrentList, $input->getOption('sort'), $limit); |
||
132 | |||
133 | TableUtils::printTable($tableData, $output); |
||
134 | |||
135 | $freeSpace = $client->getFreeSpace(); |
||
136 | $output->writeln('Free space: ' . TorrentUtils::getSizeInGb($freeSpace) . ' GB'); |
||
137 | |||
138 | if ($input->getOption('rm') && count($torrentList) > 0) { |
||
139 | return $this->removeTorrents($input, $output, $tableData['rows']); |
||
140 | } |
||
141 | |||
142 | return 0; |
||
143 | } |
||
144 | |||
145 | private function removeTorrents(InputInterface $input, OutputInterface $output, array $rows) |
||
160 | |||
161 | private function buildTableData(array $torrentList, $sort, $limit) |
||
194 | } |
||
195 |