1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popstas\Transmission\Console; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ConnectException; |
6
|
|
|
use InfluxDB; |
7
|
|
|
use Martial\Transmission\API\Argument\Torrent; |
8
|
|
|
use Popstas\Transmission\Console\Helpers\TorrentUtils; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
|
11
|
|
|
class InfluxDbClient |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var InfluxDB\Client $influxDb |
15
|
|
|
*/ |
16
|
|
|
private $influxDb; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var InfluxDB\Database |
20
|
|
|
*/ |
21
|
|
|
private $database; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var LoggerInterface |
25
|
|
|
*/ |
26
|
|
|
private $logger; |
27
|
|
|
|
28
|
|
|
private $databaseName; |
29
|
|
|
|
30
|
|
|
public function __construct(InfluxDB\Client $influxDb, $databaseName) |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
$this->influxDb = $influxDb; |
34
|
|
|
$this->databaseName = $databaseName; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param InfluxDB\Database $database |
39
|
|
|
*/ |
40
|
|
|
public function setDatabase($database) |
41
|
|
|
{ |
42
|
|
|
$this->database = $database; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Injects a logger. |
47
|
|
|
* |
48
|
|
|
* @param LoggerInterface $logger |
49
|
|
|
*/ |
50
|
|
|
public function setLogger(LoggerInterface $logger) |
51
|
|
|
{ |
52
|
|
|
$this->logger = $logger; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws InfluxDB\Database\Exception |
57
|
|
|
*/ |
58
|
|
|
public function connectDatabase() |
59
|
|
|
{ |
60
|
|
|
if (isset($this->database)) { |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$database = $this->influxDb->selectDB($this->databaseName); |
65
|
|
|
|
66
|
|
|
try { |
67
|
|
|
$databaseExists = $database->exists(); |
68
|
|
|
} catch (ConnectException $e) { |
69
|
|
|
throw new \RuntimeException('InfluxDb connection error: ' . $e->getMessage()); |
70
|
|
|
} |
71
|
|
|
if (!$databaseExists) { |
72
|
|
|
$this->logger->info('Database ' . $this->databaseName . ' not exists, creating'); |
73
|
|
|
$database->create(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->database = $database; |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $torrent |
82
|
|
|
* @param string $transmissionHost |
83
|
|
|
* @return InfluxDB\Point |
84
|
|
|
*/ |
85
|
|
|
public function buildPoint(array $torrent, $transmissionHost) |
86
|
|
|
{ |
87
|
|
|
$age = TorrentUtils::getTorrentAge($torrent); |
88
|
|
|
$lastPoint = $this->getLastPoint($torrent, $transmissionHost); |
89
|
|
|
|
90
|
|
|
$tagsData = [ |
91
|
|
|
'host' => $transmissionHost, |
92
|
|
|
'torrent_name' => $torrent[Torrent\Get::NAME], |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$uploadedDerivative = count($lastPoint) && $torrent[Torrent\Get::UPLOAD_EVER] - $lastPoint['last'] >= 0 ? |
96
|
|
|
$torrent[Torrent\Get::UPLOAD_EVER] - $lastPoint['last'] : $torrent[Torrent\Get::UPLOAD_EVER]; |
97
|
|
|
|
98
|
|
|
$fieldsData = [ |
99
|
|
|
'uploaded_last' => $uploadedDerivative, |
100
|
|
|
'downloaded' => $torrent[Torrent\Get::TOTAL_SIZE], |
101
|
|
|
'age' => $age, |
102
|
|
|
'uploaded_per_day' => $age ? intval($torrent[Torrent\Get::UPLOAD_EVER] / $age * 86400) : 0, |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
return new InfluxDB\Point( |
106
|
|
|
'uploaded', |
107
|
|
|
$torrent[Torrent\Get::UPLOAD_EVER], |
108
|
|
|
$tagsData, |
109
|
|
|
$fieldsData, |
110
|
|
|
time() |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getLastPoint(array $torrent, $transmissionHost) |
115
|
|
|
{ |
116
|
|
|
$this->connectDatabase(); |
117
|
|
|
|
118
|
|
|
$torrentName = $torrent[Torrent\Get::NAME]; |
119
|
|
|
$queryBuilder = $this->database->getQueryBuilder(); |
120
|
|
|
$results = $queryBuilder |
121
|
|
|
->last('value') |
122
|
|
|
->from('uploaded') |
123
|
|
|
->where([ |
124
|
|
|
"host='$transmissionHost'", |
125
|
|
|
"torrent_name='$torrentName'" |
126
|
|
|
]) |
127
|
|
|
->getResultSet() |
128
|
|
|
->getPoints(); |
129
|
|
|
|
130
|
|
|
return count($results) ? $results[0] : []; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function writePoints($points, $precision = InfluxDB\Database::PRECISION_SECONDS) |
134
|
|
|
{ |
135
|
|
|
$this->connectDatabase(); |
136
|
|
|
return $this->database->writePoints($points, $precision); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|