Skip to content

Commit

Permalink
Merge pull request #6 from sadmachine/update/stats-improvements
Browse files Browse the repository at this point in the history
Add `getLastStartTime` and option to prune stats records
  • Loading branch information
btam06 authored Nov 14, 2023
2 parents 8c43b35 + 0d08c3d commit 22ed453
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/Synchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ class Synchronizer
protected $truncate = array();


/**
*
*/
protected $pruneStatsWhere = array();


/**
*
*/
Expand Down Expand Up @@ -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']);
}


/**
*
Expand Down Expand Up @@ -263,6 +291,7 @@ public function run(array $mappings = array(), $force_update = FALSE): array
{
$this->stat();


if (!$this->statGet('new')) {
throw new RuntimeException(
sprintf(
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 22ed453

Please sign in to comment.