Skip to content

Commit

Permalink
add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Sep 1, 2023
1 parent e385c66 commit 1906e60
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 22 deletions.
2 changes: 2 additions & 0 deletions app/Domains/Contact/DavClient/Jobs/DeleteMultipleVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ private function run(): void
$jobs = collect($this->hrefs)
->map(fn (string $href): DeleteVCard => $this->deleteVCard($href));

Log::channel('database')->info("Delete {$jobs->count()} cards on distant server...");

$this->batch()->add($jobs);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Domains/Contact/DavClient/Jobs/DeleteVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function handle(): void
*/
private function run(): void
{
Log::channel('database')->info("Delete card {$this->uri}");
Log::channel('database')->debug("Delete card {$this->uri}");

$this->subscription->getClient()
->request('DELETE', $this->uri);
Expand Down
2 changes: 2 additions & 0 deletions app/Domains/Contact/DavClient/Jobs/GetMultipleVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ private function run(): void
->map(fn (array $contact, string $href): ?UpdateVCard => $this->updateVCard($contact, $href))
->filter();

Log::channel('database')->info("Get {$jobs->count()} cards from distant server...");

$this->batch()->add($jobs);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Domains/Contact/DavClient/Jobs/GetVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function handle(): void
*/
private function run(): void
{
Log::channel('database')->info("Get card {$this->contact->uri}");
Log::channel('database')->debug("Get card {$this->contact->uri}");

$response = $this->subscription->getClient()
->request('GET', $this->contact->uri);
Expand Down
2 changes: 1 addition & 1 deletion app/Domains/Contact/DavClient/Jobs/PushVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private function run(): void
private function pushDistant(int $depth = 1): string
{
try {
Log::channel('database')->info("Push card {$this->uri}");
Log::channel('database')->debug("Push card {$this->uri}");

$response = $this->subscription->getClient()
->request('PUT', $this->uri, $this->card, $this->headers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Domains\Contact\DavClient\Services\Utils;

use App\Domains\Contact\DavClient\Jobs\DeleteVCard;
use App\Domains\Contact\DavClient\Jobs\PushVCard;
use App\Domains\Contact\DavClient\Services\UpdateSubscriptionLocalSyncToken;
use App\Domains\Contact\DavClient\Services\Utils\Dav\DavClient;
use App\Domains\Contact\DavClient\Services\Utils\Model\ContactDeleteDto;
Expand Down Expand Up @@ -77,11 +79,12 @@ private function sync(): Collection
// Get changes to sync
$localChanges = $this->getLocalChanges();

$jobs = $jobs->merge(
app(PrepareJobsContactPush::class)
->withSubscription($this->subscription)
->execute($localChanges, $changes)
);
$pushes = app(PrepareJobsContactPush::class)
->withSubscription($this->subscription)
->execute($localChanges, $changes);
$this->logs($pushes);

$jobs = $jobs->merge($pushes);
}

return $jobs;
Expand Down Expand Up @@ -113,11 +116,12 @@ private function forcesync(): Collection
// Get changes to sync
$localChanges = $this->getLocalChanges();

$jobs = $jobs->merge(
app(PrepareJobsContactPushMissed::class)
->withSubscription($this->subscription)
->execute($localChanges, $distContacts, $localContacts)
);
$pushes = app(PrepareJobsContactPushMissed::class)
->withSubscription($this->subscription)
->execute($localChanges, $distContacts, $localContacts);
$this->logs($pushes);

$jobs = $jobs->merge($pushes);
}

return $jobs;
Expand Down Expand Up @@ -149,7 +153,6 @@ private function getDistantChanges(): Collection

$updated = $data->filter(fn ($contact, $href): bool => $this->filterDistantContacts($contact, $href))
->map(fn (array $contact, string $href): ContactDto => new ContactDto($href, Arr::get($contact, 'properties.200.{DAV:}getetag')));

$deleted = $data->filter(fn ($contact): bool => is_array($contact) && $contact['status'] === '404')
->map(fn (array $contact, string $href): ContactDto => new ContactDeleteDto($href));

Expand Down Expand Up @@ -238,4 +241,16 @@ private function callSyncCollection(): array

return $collection;
}

private function logs(Collection $jobs): void
{
$counts = $jobs->countBy(fn ($job): string => $job::class);

if (($updated = $counts->get(PushVCard::class, 0)) > 0) {
Log::channel('database')->info("Update or create $updated cards to distant server...");
}
if (($deleted = $counts->get(DeleteVCard::class, 0)) > 0) {
Log::channel('database')->info("Delete $deleted cards to distant server...");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function execute(Collection $localChanges, Collection $distContacts, Coll
$missings = $this->preparePushMissedContacts($localChanges->get('added', collect()), $distContacts, $localContacts);

return $changes
->merge($missings);
->merge($missings)
->filter();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Domains\Contact\DavClient\Services\Utils\Traits\HasCapability;
use App\Domains\Contact\DavClient\Services\Utils\Traits\HasSubscription;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;

class PrepareJobsContactUpdater
{
Expand All @@ -35,14 +36,14 @@ public function execute(Collection $refresh): Collection
*/
private function refreshMultigetContacts(Collection $refresh): Collection
{
$refresh = $refresh->groupBy(fn ($item): string => $item instanceof ContactDeleteDto ? 'deleted' : 'updated')
$refresh = $refresh->groupBy(fn ($item): string => $item::class)
->map(fn (Collection $items): array => $items->pluck('uri')->toArray());

$jobs = collect();
if (($updated = $refresh->get('updated')) !== null) {
if (($updated = $refresh->get(ContactDto::class)) !== null) {
$jobs->add(new GetMultipleVCard($this->subscription, $updated));
}
if (($deleted = $refresh->get('deleted')) !== null) {
if (($deleted = $refresh->get(ContactDeleteDto::class)) !== null) {
$jobs->add(new DeleteMultipleVCard($this->subscription, $deleted));
}

Expand All @@ -56,10 +57,24 @@ private function refreshMultigetContacts(Collection $refresh): Collection
*/
private function refreshSimpleGetContacts(Collection $refresh): Collection
{
return $refresh
->map(fn (ContactDto $contact) => $contact instanceof ContactDeleteDto
? new DeleteVCard($this->subscription, $contact->uri)
: new GetVCard($this->subscription, $contact)
$refresh = $refresh->groupBy(fn ($item): string => $item::class);

$jobs = collect();
if (($updated = $refresh->get(ContactDto::class)) !== null) {
Log::channel('database')->info("Get {$updated->count()} cards from distant server...");

$jobs = $jobs->merge(
$updated->map(fn (ContactDto $contact) => new GetVCard($this->subscription, $contact))
);
}
if (($deleted = $refresh->get(ContactDeleteDto::class)) !== null) {
Log::channel('database')->info("Delete {$deleted->count()} cards from distant server...");

$jobs = $jobs->merge(
$deleted->map(fn (ContactDto $contact) => new DeleteVCard($this->subscription, $contact->uri))
);
}

return $jobs;
}
}

0 comments on commit 1906e60

Please sign in to comment.