mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
first commit
This commit is contained in:
90
app/Models/SyncLog.php
Normal file
90
app/Models/SyncLog.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
class SyncLog extends Model
|
||||
{
|
||||
protected string $table = 'sync_logs';
|
||||
protected array $fillable = [
|
||||
'source_id',
|
||||
'sync_type',
|
||||
'status',
|
||||
'total_items',
|
||||
'processed_items',
|
||||
'new_items',
|
||||
'updated_items',
|
||||
'deleted_items',
|
||||
'errors',
|
||||
'message',
|
||||
'started_at',
|
||||
'completed_at'
|
||||
];
|
||||
|
||||
protected array $casts = [
|
||||
'errors' => 'array',
|
||||
'started_at' => 'datetime',
|
||||
'completed_at' => 'datetime'
|
||||
];
|
||||
|
||||
public static function getRecent(\PDO $pdo, int $limit = 10): array
|
||||
{
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT sl.*, s.display_name as source_name
|
||||
FROM sync_logs sl
|
||||
JOIN sources s ON sl.source_id = s.id
|
||||
ORDER BY sl.created_at DESC
|
||||
LIMIT :limit
|
||||
");
|
||||
$stmt->execute(['limit' => $limit]);
|
||||
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
public function source()
|
||||
{
|
||||
return new Source($this->pdo);
|
||||
}
|
||||
|
||||
public function markAsStarted(): void
|
||||
{
|
||||
$this->update($this->id, [
|
||||
'status' => 'started',
|
||||
'started_at' => date('Y-m-d H:i:s'),
|
||||
'message' => null,
|
||||
'errors' => null
|
||||
]);
|
||||
}
|
||||
|
||||
public function markAsCompleted(array $stats = []): void
|
||||
{
|
||||
$data = [
|
||||
'status' => 'completed',
|
||||
'completed_at' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
if (!empty($stats)) {
|
||||
$data = array_merge($data, $stats);
|
||||
}
|
||||
|
||||
$this->update($this->id, $data);
|
||||
}
|
||||
|
||||
public function markAsFailed(string $errorMessage, array $errors = []): void
|
||||
{
|
||||
$this->update($this->id, [
|
||||
'status' => 'failed',
|
||||
'completed_at' => date('Y-m-d H:i:s'),
|
||||
'message' => $errorMessage,
|
||||
'errors' => !empty($errors) ? json_encode($errors) : null
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateProgress(int $processed, int $new = 0, int $updated = 0, int $deleted = 0): void
|
||||
{
|
||||
$this->update($this->id, [
|
||||
'processed_items' => $processed,
|
||||
'new_items' => $new,
|
||||
'updated_items' => $updated,
|
||||
'deleted_items' => $deleted
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user