TenantAtlas/app/Services/Alerts/AlertFingerprintService.php
ahmido 3ed275cef3 feat(alerts): Monitoring cluster + v1 resources (spec 099) (#121)
Implements spec `099-alerts-v1-teams-email`.

- Monitoring navigation: Alerts as a cluster under Monitoring; default landing is Alert deliveries.
- Tenant panel: Alerts points to `/admin/alerts` and the cluster navigation is hidden in tenant panel.
- Guard compliance: removes direct `Gate::` usage from Alert resources so `NoAdHocFilamentAuthPatternsTest` passes.

Verification:
- Full suite: `1348 passed, 7 skipped` (EXIT=0).

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #121
2026-02-18 15:20:43 +00:00

53 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Alerts;
use App\Models\AlertDestination;
use App\Models\AlertRule;
class AlertFingerprintService
{
/**
* @param array<string, mixed> $event
*/
public function hash(AlertRule $rule, AlertDestination $destination, int $tenantId, array $event): string
{
$fingerprintKey = trim((string) ($event['fingerprint_key'] ?? ''));
if ($fingerprintKey === '') {
$fingerprintKey = trim((string) ($event['idempotency_key'] ?? ''));
}
$payload = [
'workspace_id' => (int) $rule->workspace_id,
'rule_id' => (int) $rule->getKey(),
'destination_id' => (int) $destination->getKey(),
'tenant_id' => $tenantId,
'event_type' => trim((string) ($event['event_type'] ?? '')),
'severity' => strtolower(trim((string) ($event['severity'] ?? ''))),
'fingerprint_key' => $fingerprintKey,
];
return hash('sha256', json_encode($this->normalizeArray($payload), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR));
}
/**
* @param array<string, mixed> $payload
* @return array<string, mixed>
*/
private function normalizeArray(array $payload): array
{
ksort($payload);
foreach ($payload as $key => $value) {
if (is_array($value)) {
$payload[$key] = $this->normalizeArray($value);
}
}
return $payload;
}
}