Completed
Push — master ( 31e203...1cb2c6 )
by Stanislav
02:20
created

TorrentUtils::getLastPoint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 2
eloc 12
nc 2
nop 3
1
<?php
2
3
namespace Popstas\Transmission\Console\Helpers;
4
5
use InfluxDB;
6
use Martial\Transmission\API\Argument\Torrent;
7
8
class TorrentUtils
9
{
10
    /**
11
     * @param array $torrent
12
     * @param string $transmissionHost
13
     * @param array $lastPoint
14
     * @return InfluxDB\Point
15
     */
16
    public static function buildPoint(array $torrent, $transmissionHost, array $lastPoint)
17
    {
18
        $age = TorrentUtils::getTorrentAge($torrent);
19
20
        $tagsData = [
21
            'host'             => $transmissionHost,
22
            'torrent_name'     => $torrent[Torrent\Get::NAME],
23
        ];
24
25
        $uploadedDerivative = count($lastPoint) && $torrent[Torrent\Get::UPLOAD_EVER] - $lastPoint['last'] >= 0 ?
26
            $torrent[Torrent\Get::UPLOAD_EVER] - $lastPoint['last'] : $torrent[Torrent\Get::UPLOAD_EVER];
27
28
        $fieldsData = [
29
            'uploaded_derivative' => $uploadedDerivative,
30
            'downloaded'          => $torrent[Torrent\Get::TOTAL_SIZE],
31
            'age'                 => $age,
32
            'uploaded_per_day'    => $age ? $torrent[Torrent\Get::UPLOAD_EVER] / $age * 86400 : 0,
33
        ];
34
35
        return new InfluxDB\Point(
36
            'uploaded',
37
            $torrent[Torrent\Get::UPLOAD_EVER],
38
            $tagsData,
39
            $fieldsData,
40
            time()
41
        );
42
    }
43
44
    public static function getLastPoint(array $torrent, $transmissionHost, InfluxDB\Database $database)
45
    {
46
        $torrentName = $torrent[Torrent\Get::NAME];
47
        $queryBuilder = $database->getQueryBuilder();
48
        $results = $queryBuilder
49
            ->last('value')
50
            ->from('uploaded')
51
            ->where([
52
                "host='$transmissionHost'",
53
                "torrent_name='$torrentName'"
54
            ])
55
            ->getResultSet()
56
            ->getPoints();
57
58
        return count($results) ? $results[0] : [];
59
    }
60
61
    /**
62
     * @param $torrent
63
     * @return int seconds from torrent finish download
64
     */
65
    public static function getTorrentAge($torrent)
66
    {
67
        $date = isset($torrent[Torrent\Get::DONE_DATE]) && $torrent[Torrent\Get::DONE_DATE] ?
68
            $torrent[Torrent\Get::DONE_DATE] :
69
            (isset($torrent[Torrent\Get::ADDED_DATE]) ? $torrent[Torrent\Get::ADDED_DATE] : 0);
70
        return $date ? time() - $date : 0;
71
    }
72
73
    public static function getTorrentAgeInDays($torrent)
74
    {
75
        return round(self::getTorrentAge($torrent) / 86400);
76
    }
77
78
    public static function getSizeInGb($sizeInBytes, $round = 2)
79
    {
80
        // 1024 not equals transmission value
81
        return round($sizeInBytes / 1000 / 1000 / 1000, $round);
82
    }
83
}
84