From 0d08c3da1cc876a0a04d438ef27b8d57afd6bc80 Mon Sep 17 00:00:00 2001 From: sadmachine Date: Fri, 10 Nov 2023 13:37:25 -0500 Subject: [PATCH] Add `getLastStartTime` and option to prune stats records --- src/Synchronizer.php | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/Synchronizer.php b/src/Synchronizer.php index bd1cfaf..1cce299 100644 --- a/src/Synchronizer.php +++ b/src/Synchronizer.php @@ -80,6 +80,12 @@ class Synchronizer protected $truncate = array(); + /** + * + */ + protected $pruneStatsWhere = array(); + + /** * */ @@ -204,6 +210,28 @@ public function getCompletionTime(): ?int return strtotime($result->fetch(PDO::FETCH_ASSOC)['start_time']) + $this->getHighSyncInterval(); } + /** + * + */ + public function getStartTime(): ?int + { + $result = $this->destination->query(" + SELECT + start_time + FROM + devour_stats + WHERE + end_time IS NULL + LIMIT 1 + "); + + if (!$result->rowCount()) { + return NULL; + } + + return strtotime($result->fetch(PDO::FETCH_ASSOC)['start_time']); + } + /** * @@ -263,6 +291,7 @@ public function run(array $mappings = array(), $force_update = FALSE): array { $this->stat(); + if (!$this->statGet('new')) { throw new RuntimeException( sprintf( @@ -272,6 +301,9 @@ public function run(array $mappings = array(), $force_update = FALSE): array ); } else { + if (!empty($this->pruneStatsWhere)) { + $this->pruneStats(); + } $this->statSet('start_time', date('Y-m-d H:i:s')); $this->statSet('force', $force_update ? 1 : 0); @@ -952,4 +984,27 @@ private function getPdoType($value) return PDO::PARAM_STR; } } + + + /** + * + */ + private function pruneStats() + { + $pruneCriteria = implode(' AND ', $this->pruneStatsWhere); + + $this->destination->query(" + DELETE FROM devour_stats + WHERE $pruneCriteria + "); + } + + + /** + * + */ + public function setPruneStatsWhere($pruneStatsWhere = []) + { + $this->pruneStatsWhere = $pruneStatsWhere; + } }