-
Notifications
You must be signed in to change notification settings - Fork 3
/
stats.php
181 lines (145 loc) · 4.3 KB
/
stats.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
class StatItem
{
public int $id;
public string $name;
public int $lastUpdate;
public int $ttl;
public int $avgTTL;
public int $dateMax;
private int $maxTTL;
public function __construct(array $feed, int $maxTTL)
{
$this->id = (int) $feed['id'];
$this->name = $feed['name'];
$this->lastUpdate = (int) $feed['lastUpdate'];
$this->ttl = (int) $feed['ttl'];
$this->avgTTL = (int) $feed['avgTTL'];
$this->dateMax = (int) $feed['date_max'];
$this->maxTTL = $maxTTL;
}
public function isActive(int $now): bool
{
$timeSinceLastEntry = $now - $this->dateMax;
if ($timeSinceLastEntry > 2 * $this->maxTTL) {
return false;
}
return true;
}
}
class AutoTTLStats extends Minz_ModelPdo
{
/**
* @var int
*/
private $defaultTTL;
/**
* @var int
*/
private $maxTTL;
/**
* @var int
*/
private $statsCount;
public function __construct(int $defaultTTL, int $maxTTL, int $statsCount)
{
parent::__construct();
$this->defaultTTL = $defaultTTL;
$this->maxTTL = $maxTTL;
$this->statsCount = $statsCount;
}
public function calcAdjustedTTL(int $avgTTL, int $dateMax): int
{
$timeSinceLastEntry = time() - $dateMax;
if ($this->defaultTTL > $this->maxTTL) {
return $this->defaultTTL;
}
if ($avgTTL === 0 || $avgTTL > $this->maxTTL || $timeSinceLastEntry > 2 * $this->maxTTL) {
return $this->maxTTL;
} elseif ($avgTTL < $this->defaultTTL) {
return $this->defaultTTL;
}
return $avgTTL;
}
public function getAdjustedTTL(int $feedID): int
{
$sql = <<<SQL
SELECT
CASE WHEN COUNT(1) > 0 THEN ((MAX(stats.date) - MIN(stats.date)) / COUNT(1)) ELSE 0 END AS `avgTTL`,
MAX(stats.date) AS date_max
FROM `_entry` AS stats
WHERE id_feed = {$feedID}
SQL;
$stm = $this->pdo->query($sql);
$res = $stm->fetch(PDO::FETCH_NAMED);
return $this->calcAdjustedTTL((int) $res['avgTTL'], (int) $res['date_max']);
}
public function getFeedStats(bool $autoTTL): array
{
$where = '';
if ($autoTTL) {
$where = 'feed.ttl = 0';
} else {
$where = 'feed.ttl != 0';
}
$sql = <<<SQL
SELECT
feed.id,
feed.name,
feed.`lastUpdate`,
feed.ttl,
CASE WHEN COUNT(1) > 0 THEN ((MAX(stats.date) - MIN(stats.date)) / COUNT(1)) ELSE 0 END AS `avgTTL`,
MAX(stats.date) AS date_max
FROM `_feed` AS feed
LEFT JOIN `_entry` AS stats ON feed.id = stats.id_feed
WHERE {$where}
GROUP BY feed.id
ORDER BY `avgTTL` ASC
LIMIT {$this->statsCount}
SQL;
$stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_NAMED);
$list = [];
foreach ($res as $feed) {
$list[] = new StatItem($feed, $this->maxTTL);
}
return $list;
}
public function humanIntervalFromSeconds(int $seconds): string
{
$from = new \DateTime('@0');
$to = new \DateTime("@$seconds");
$interval = $from->diff($to);
$results = [];
if ($interval->y === 1) {
$results[] = "{$interval->y} year";
} elseif ($interval->y > 1) {
$results[] = "{$interval->y} years";
}
if ($interval->m === 1) {
$results[] = "{$interval->m} month";
} elseif ($interval->m > 1) {
$results[] = "{$interval->m} months";
}
if ($interval->d === 1) {
$results[] = "{$interval->d} day";
} elseif ($interval->d > 1) {
$results[] = "{$interval->d} days";
}
if ($interval->h === 1) {
$results[] = "{$interval->h} hour";
} elseif ($interval->h > 1) {
$results[] = "{$interval->h} hours";
}
if ($interval->i === 1) {
$results[] = "{$interval->i} minute";
} elseif ($interval->i > 1) {
$results[] = "{$interval->i} minutes";
} elseif ($interval->i === 0 && $interval->s === 1) {
$results[] = "{$interval->s} second";
} elseif ($interval->i === 0 && $interval->s > 1) {
$results[] = "{$interval->s} seconds";
}
return implode(' ', $results);
}
}