Skip to content

Commit 51a3a59

Browse files
wofferlGrotax
authored andcommitted
Use the date of the latest item to determine if a feed is sleeping and to calculate the expected next update
Signed-off-by: Wolfgang <[email protected]>
1 parent 3149bed commit 51a3a59

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/FeedIo/Reader/Result/UpdateStats.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@ class UpdateStats
3131

3232
protected array $intervals = [];
3333

34+
protected int $newestItemDate = 0;
35+
3436
/**
3537
* UpdateStats constructor.
3638
* @param FeedInterface $feed
3739
*/
3840
public function __construct(
3941
protected FeedInterface $feed
4042
) {
41-
$this->intervals = $this->computeIntervals($this->extractDates($feed));
43+
$dates = $this->extractDates($feed);
44+
if (count($dates) > 0) {
45+
$this->newestItemDate = max($dates);
46+
} else {
47+
$this->newestItemDate = $this->getFeedTimestamp();
48+
}
49+
$this->intervals = $this->computeIntervals($dates);
4250
}
4351

4452
/**
@@ -57,7 +65,6 @@ public function computeNextUpdate(
5765
if ($this->isSleepy($sleepyDuration, $marginRatio)) {
5866
return (new \DateTime())->setTimestamp(time() + $sleepyDelay);
5967
}
60-
$feedTimeStamp = $this->getFeedTimestamp();
6168
$now = time();
6269
$intervals = [
6370
$this->getAverageInterval(),
@@ -66,7 +73,7 @@ public function computeNextUpdate(
6673
sort($intervals);
6774
$newTimestamp = $now + $minDelay;
6875
foreach ($intervals as $interval) {
69-
$computedTimestamp = $this->addInterval($feedTimeStamp, $interval, $marginRatio);
76+
$computedTimestamp = $this->addInterval($this->newestItemDate, $interval, $marginRatio);
7077
if ($computedTimestamp > $now) {
7178
$newTimestamp = $computedTimestamp;
7279
break;
@@ -82,7 +89,7 @@ public function computeNextUpdate(
8289
*/
8390
public function isSleepy(int $sleepyDuration, float $marginRatio): bool
8491
{
85-
return time() > $this->addInterval($this->getFeedTimestamp(), $sleepyDuration, $marginRatio);
92+
return time() > $this->addInterval($this->newestItemDate, $sleepyDuration, $marginRatio);
8693
}
8794

8895
/**
@@ -171,6 +178,14 @@ public function getMedianInterval(): int
171178
}
172179
}
173180

181+
/**
182+
* @return int
183+
*/
184+
public function getNewestItemDate(): int
185+
{
186+
return $this->newestItemDate;
187+
}
188+
174189
private function computeIntervals(array $dates): array
175190
{
176191
rsort($dates);

tests/FeedIo/Reader/Result/UpdateStatsTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public function testIntervals()
3535
$this->assertEquals(86400, $stats->getMinInterval());
3636
$nextUpdate = $stats->computeNextUpdate();
3737
$averageInterval = $stats->getAverageInterval();
38-
$this->assertEquals($feed->getLastModified()->getTimestamp() + intval($averageInterval + 0.1 * $averageInterval), $nextUpdate->getTimestamp());
38+
$medianInterval = $stats->getMedianInterval();
39+
$computedInterval = ($medianInterval < $averageInterval ? $medianInterval : $averageInterval);
40+
$this->assertEquals($stats->getNewestItemDate() + intval($computedInterval + 0.1 * $computedInterval), $nextUpdate->getTimestamp());
3941
}
4042

4143
public function testSleepyFeed()

0 commit comments

Comments
 (0)