Skip to content

Commit 22ed453

Browse files
authored
Merge pull request #6 from sadmachine/update/stats-improvements
Add `getLastStartTime` and option to prune stats records
2 parents 8c43b35 + 0d08c3d commit 22ed453

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

src/Synchronizer.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ class Synchronizer
8080
protected $truncate = array();
8181

8282

83+
/**
84+
*
85+
*/
86+
protected $pruneStatsWhere = array();
87+
88+
8389
/**
8490
*
8591
*/
@@ -204,6 +210,28 @@ public function getCompletionTime(): ?int
204210
return strtotime($result->fetch(PDO::FETCH_ASSOC)['start_time']) + $this->getHighSyncInterval();
205211
}
206212

213+
/**
214+
*
215+
*/
216+
public function getStartTime(): ?int
217+
{
218+
$result = $this->destination->query("
219+
SELECT
220+
start_time
221+
FROM
222+
devour_stats
223+
WHERE
224+
end_time IS NULL
225+
LIMIT 1
226+
");
227+
228+
if (!$result->rowCount()) {
229+
return NULL;
230+
}
231+
232+
return strtotime($result->fetch(PDO::FETCH_ASSOC)['start_time']);
233+
}
234+
207235

208236
/**
209237
*
@@ -263,6 +291,7 @@ public function run(array $mappings = array(), $force_update = FALSE): array
263291
{
264292
$this->stat();
265293

294+
266295
if (!$this->statGet('new')) {
267296
throw new RuntimeException(
268297
sprintf(
@@ -272,6 +301,9 @@ public function run(array $mappings = array(), $force_update = FALSE): array
272301
);
273302

274303
} else {
304+
if (!empty($this->pruneStatsWhere)) {
305+
$this->pruneStats();
306+
}
275307
$this->statSet('start_time', date('Y-m-d H:i:s'));
276308
$this->statSet('force', $force_update ? 1 : 0);
277309

@@ -952,4 +984,27 @@ private function getPdoType($value)
952984
return PDO::PARAM_STR;
953985
}
954986
}
987+
988+
989+
/**
990+
*
991+
*/
992+
private function pruneStats()
993+
{
994+
$pruneCriteria = implode(' AND ', $this->pruneStatsWhere);
995+
996+
$this->destination->query("
997+
DELETE FROM devour_stats
998+
WHERE $pruneCriteria
999+
");
1000+
}
1001+
1002+
1003+
/**
1004+
*
1005+
*/
1006+
public function setPruneStatsWhere($pruneStatsWhere = [])
1007+
{
1008+
$this->pruneStatsWhere = $pruneStatsWhere;
1009+
}
9551010
}

0 commit comments

Comments
 (0)