Completed
Push — master ( 07e153...ca5d76 )
by Stanislav
02:20
created

StatsGet::execute()   B

Complexity

Conditions 5
Paths 39

Size

Total Lines 55
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 55
rs 8.7752
cc 5
eloc 35
nc 39
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Popstas\Transmission\Console\Command;
4
5
use InfluxDB;
6
use Martial\Transmission\API\Argument\Torrent;
7
use Popstas\Transmission\Console\Helpers\TorrentListUtils;
8
use Popstas\Transmission\Console\Helpers\TorrentUtils;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class StatsGet extends Command
14
{
15
    protected function configure()
16
    {
17
        parent::configure();
18
        $this
19
            ->setName('stats-get')
20
            ->setAliases(['sg'])
21
            ->setDescription('Get metrics from InfluxDB')
22
            ->addOption('sort', null, InputOption::VALUE_OPTIONAL, 'Sort by column number', 4)
23
            ->addOption('name', null, InputOption::VALUE_OPTIONAL, 'Sort by torrent name (regexp)')
24
            ->addOption('days', null, InputOption::VALUE_OPTIONAL, 'Show stats for last days', 7)
25
            ->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'Limit torrent list')
26
            ->addOption('profit', null, InputOption::VALUE_OPTIONAL, 'Filter by profit')
27
            ->addOption('transmission-host', null, InputOption::VALUE_OPTIONAL, 'Transmission host')
28
            ->setHelp(<<<EOT
29
The <info>stats-get</info> sends upload ever for every torrent to InfluxDB.
30
EOT
31
            );
32
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $config = $this->getApplication()->getConfig();
37
        $logger = $this->getApplication()->getLogger();
38
        $client = $this->getApplication()->getClient();
39
40
        try {
41
            $influxDbClient = $this->getApplication()->getInfluxDbClient(
42
                $config->get('influxdb-host'),
43
                $config->get('influxdb-port'),
44
                $config->get('influxdb-user'),
45
                $config->get('influxdb-password'),
46
                $config->get('influxdb-database')
47
            );
48
49
            $lastDays = (int)$input->getOption('days') ? (int)$input->getOption('days') : 0;
50
            $limit = (int)$input->getOption('limit') ? (int)$input->getOption('limit') : 0;
51
52
            $torrentList = $client->getTorrentData();
53
54
            $torrentList = TorrentListUtils::filterTorrents($torrentList, [
55
                'name' => $input->getOption('name'),
56
            ]);
57
58
            $transmissionHost = $config->overrideConfig($input, 'transmission-host');
59
60
            $rows = [];
61
62
            foreach ($torrentList as $torrent) {
63
                $uploaded = $influxDbClient->getTorrentSum($torrent, 'uploaded_last', $transmissionHost, $lastDays);
64
65
                $profit = round($uploaded / $torrent[Torrent\Get::TOTAL_SIZE], 2);
66
67
                $rows[] = [
68
                    $torrent[Torrent\Get::NAME],
69
                    TorrentUtils::getSizeInGb($uploaded) . ' GB',
70
                    $profit
71
                ];
72
            }
73
        } catch (\Exception $e) {
74
            $logger->critical($e->getMessage());
75
            return 1;
76
        }
77
78
        $rows = TorrentListUtils::filterRows($rows, [
79
            '2' => ['type' => 'numeric', 'value' => $input->getOption('profit')]
80
        ]);
81
82
        TorrentListUtils::printTable([
83
            'headers' => ['Name', 'Uploaded', 'Profit'],
84
            'rows' => $rows
85
        ], $output, $input->getOption('sort'), $limit);
86
87
        return 0;
88
    }
89
}
90