Completed
Push — master ( 70daad...2eb0da )
by Stanislav
02:30
created

StatsSend::createInfluxDb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
nop 4
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\TorrentUtils;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class StatsSend extends Command
13
{
14
    protected function configure()
15
    {
16
        parent::configure();
17
        $this
18
            ->setName('stats-send')
19
            ->setAliases(['ss'])
20
            ->setDescription('Send metrics to InfluxDB')
21
            ->addOption('influxdb-host', null, InputOption::VALUE_OPTIONAL, 'InfluxDb host')
22
            ->addOption('influxdb-port', null, InputOption::VALUE_OPTIONAL, 'InfluxDb port')
23
            ->addOption('influxdb-user', null, InputOption::VALUE_OPTIONAL, 'InfluxDb user')
24
            ->addOption('influxdb-password', null, InputOption::VALUE_OPTIONAL, 'InfluxDb password')
25
            ->addOption('influxdb-database', null, InputOption::VALUE_OPTIONAL, 'InfluxDb database')
26
            ->addOption('transmission-host', null, InputOption::VALUE_OPTIONAL, 'Transmission host')
27
            ->setHelp(<<<EOT
28
The <info>stats-send</info> sends upload ever for every torrent to InfluxDB.
29
EOT
30
            );
31
    }
32
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        $config = $this->getApplication()->getConfig();
36
        $logger = $this->getApplication()->getLogger();
37
        $client = $this->getApplication()->getClient();
38
39
        $torrentList = $client->getTorrentData();
40
        $obsoleteList = TorrentUtils::getObsoleteTorrents($torrentList);
41
        if (!empty($obsoleteList)) {
42
            $output->writeln('<comment>Found obsolete torrents,
43
                              remove it using transmission-cli torrent-remove-duplicates</comment>');
44
            return 1;
45
        }
46
47
        try {
48
            $database = $this->getApplication()->getDatabase($input);
49
        } catch (\Exception $e) {
50
            $logger->critical($e->getMessage());
51
            return 1;
52
        }
53
54
        $points = [];
55
56
        $torrentList = $client->getTorrentData();
57
58
        $transmissionHost = $config->overrideConfig($input, 'transmission-host');
59
60
        foreach ($torrentList as $torrent) {
61
            $age = TorrentUtils::getTorrentAge($torrent);
62
            $tagsData = [
63
                'host'             => $transmissionHost,
64
                'torrent_name'     => $torrent[Torrent\Get::NAME],
65
                'downloaded'       => $torrent[Torrent\Get::DOWNLOAD_EVER],
66
                'age'              => $age,
67
                'uploaded_per_day' => $age ? $torrent[Torrent\Get::UPLOAD_EVER] / $age * 86400 : 0
68
            ];
69
70
            $point = new InfluxDB\Point('uploaded', $torrent[Torrent\Get::UPLOAD_EVER], $tagsData, [], time());
71
72
            if ($age) {
73
                $points[] = $point;
74
                $logger->debug('Send point: {point}', ['point' => $point]);
75
            } else {
76
                $logger->debug('Skip point: {point}', ['point' => $point]);
77
            }
78
        }
79
80
        $this->dryRun($input, $output, function () use ($database, $points, $logger) {
81
            $isSuccess = $database->writePoints($points, InfluxDB\Database::PRECISION_SECONDS);
82
            $logger->info('InfluxDB write ' . ($isSuccess ? 'success' : 'failed'));
83
        }, 'dry-run, don\'t really send points');
84
85
        return 0;
86
    }
87
}
88