An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
feat(analytics): log Compose resources on Caddy and add service analytics
Andras Bacsai510a2d838be7
diff --git a/app/Actions/Server/StartSentinel.php b/app/Actions/Server/StartSentinel.phpindex 57db2d3779..914ec7e099 100644--- a/app/Actions/Server/StartSentinel.php+++ b/app/Actions/Server/StartSentinel.php@@ -17,9 +17,18 @@ class StartSentinel: rtrim($server->proxyPath(), '/');}+ /**+ * Sentinel reads the proxy access log only when analytics is on and a Traefik or Caddy proxy writes it.+ * After a switch to a proxy without analytics support, Sentinel must not keep the old log mount.+ */+ public static function collectsTraffic(Server $server): bool+ {+ return $server->isTrafficAnalyticsEnabled() && $server->hasTrafficAnalyticsProxy();+ }+public static function sentinelTrafficEnvironment(Server $server): array{- if (! $server->isTrafficAnalyticsEnabled()) {+ if (! self::collectsTraffic($server)) {return [];}@@ -104,7 +113,7 @@ class StartSentinel$dockerEnvironments = implode(' ', array_map(fn ($key, $value) => '-e '.escapeshellarg("$key=$value"), array_keys($environments), $environments));$dockerLabels = implode(' ', array_map(fn ($key, $value) => "$key=$value", array_keys($labels), $labels));$trafficLogDirectory = self::trafficLogDirectory($server);- $trafficMount = $server->isTrafficAnalyticsEnabled()+ $trafficMount = self::collectsTraffic($server)? '-v '.escapeshellarg("{$trafficLogDirectory}:{$trafficLogDirectory}:ro").' ': '';$network = $server->isLocalhost() ? ' --network coolify' : '';diff --git a/app/Livewire/Analytics.php b/app/Livewire/Analytics.phpindex be5d603eeb..2ebc6dc78f 100644--- a/app/Livewire/Analytics.php+++ b/app/Livewire/Analytics.php@@ -5,8 +5,10 @@ namespace App\Livewire;use App\Livewire\Concerns\BuildsTrafficChartPayload;use App\Models\Application;use App\Models\Server;+use App\Models\Service;use App\Services\SentinelTrafficClient;use App\Services\TrafficAnalyticsAggregator;+use App\Services\TrafficResource;use Illuminate\Contracts\View\View;use Illuminate\Support\Collection;use Illuminate\Support\Facades\Log;@@ -30,12 +32,12 @@ class Analytics extends Component/** @var array<string, string> uuid => name, for the server filter */public array $serverOptions = [];- /** @var array<string, string> uuid => name, for the application filter (scoped to the selected server) */+ /** @var array<string, string> uuid => name, for the application/service filter (scoped to the selected server) */public array $appOptions = [];/**- * Listbox options for the application filter, grouped under project headers so- * it's clear which application belongs to which project.+ * Listbox options for the application/service filter, grouped under project headers so+ * it's clear which resource belongs to which project. Services carry a "(Service)" suffix.** @var array<int, array{value: string, label: string, header?: bool}>*/@@ -105,13 +107,21 @@ class Analytics extends Componentprotected array $breakdownDimensions = ['country', 'referer', 'browser', 'os', 'device', 'protocol', 'cache', 'status', 'agent', 'ip', 'useragent'];/**- * Per-request cache of app uuid => display metadata, so resolving a name/domain/link- * for the leaderboard and path domains hits the DB at most once per app.+ * Per-request cache of Sentinel key => display metadata, so resolving a name/domain/link+ * for the leaderboard and path domains runs at most once per key.*- * @var array<string, array{name: string, domain: ?string, link: ?string}>+ * @var array<string, array{uuid: string, name: string, domain: ?string, rowDomain: ?string, link: ?string, isService: bool}>*/protected array $appMetaCache = [];+ /**+ * Per-request map of every team Application and Service by uuid; the only source used+ * to name Sentinel keys, so a key never resolves to another team's resource.+ *+ * @var array<string, TrafficResource>|null+ */+ protected ?array $teamResources = null;+public function mount(?string $scopedServerUuid = null): void{$allServers = Server::ownedByCurrentTeamCached();@@ -223,9 +233,9 @@ class Analytics extends Component{$enabledUuids = $this->servers->pluck('uuid');- $apps = Application::ownedByCurrentTeam()->with(['environment.project', 'destination.server'])->get()- ->filter(function (Application $app) use ($enabledUuids): bool {- $serverUuid = $app->destination?->server?->uuid;+ $resources = collect($this->teamResources())+ ->filter(function (TrafficResource $resource) use ($enabledUuids): bool {+ $serverUuid = $resource->server()?->uuid;if (! $serverUuid || ! $enabledUuids->contains($serverUuid)) {return false;@@ -235,26 +245,61 @@ class Analytics extends Component});// Flat uuid => name map, used to validate a bookmarked ?app= filter.- $options = $apps->mapWithKeys(fn (Application $app) => [$app->uuid => $app->name])->all();+ $options = $resources->mapWithKeys(fn (TrafficResource $resource) => [$resource->uuid() => $resource->name()])->all();asort($options);$this->appOptions = $options;- // Grouped listbox options: a header row per project, then its apps (both alpha-sorted).+ // Grouped listbox options: a header row per project, then its applications and+ // services (both alpha-sorted by name).$grouped = [];- $byProject = $apps- ->groupBy(fn (Application $app) => (string) (data_get($app, 'environment.project.name') ?: 'Ungrouped'))+ $byProject = $resources+ ->groupBy(fn (TrafficResource $resource) => $resource->projectName())->sortKeys();- foreach ($byProject as $projectName => $projectApps) {+ foreach ($byProject as $projectName => $projectResources) {$grouped[] = ['value' => '__group_'.md5($projectName), 'label' => $projectName, 'header' => true];- foreach ($projectApps->sortBy('name') as $app) {- $grouped[] = ['value' => $app->uuid, 'label' => $app->name];+ foreach ($projectResources->sortBy(fn (TrafficResource $resource) => $resource->name()) as $resource) {+ $grouped[] = [+ 'value' => $resource->uuid(),+ 'label' => $resource->isService() ? $resource->name().' (Service)' : $resource->name(),+ ];}}$this->appGroupedOptions = $grouped;}+ /**+ * Every Application and Service of the current team, keyed by uuid. Loaded once per+ * request with the relations the filter, links, and domain lookups need.+ *+ * @return array<string, TrafficResource>+ */+ protected function teamResources(): array+ {+ if ($this->teamResources !== null) {+ return $this->teamResources;+ }++ $resources = [];+ foreach (Application::ownedByCurrentTeam()->with(['environment.project', 'destination.server'])->get() as $application) {+ $resources[$application->uuid] = TrafficResource::for($application);+ }+ foreach (Service::ownedByCurrentTeam()->with(['environment.project', 'server', 'applications'])->get() as $service) {+ $resources[$service->uuid] = TrafficResource::for($service);+ }++ return $this->teamResources = $resources;+ }++ /**+ * The team resource selected in the application/service filter, or null.+ */+ protected function selectedResource(): ?TrafficResource+ {+ return $this->appUuid !== '' ? ($this->teamResources()[$this->appUuid] ?? null) : null;+ }+/*** Servers this view should query, honoring the active server/app filters.*/@@ -265,8 +310,7 @@ class Analytics extends Component}if ($this->appUuid !== '') {- $server = Application::ownedByCurrentTeam()->whereUuid($this->appUuid)->first()- ?->destination?->server;+ $server = $this->selectedResource()?->server();return $server && $this->servers->contains(fn (Server $s) => $s->uuid === $server->uuid)? collect([$server])@@ -287,111 +331,73 @@ class Analytics extends Component}[$from, $to] = $this->window();- $appKey = $this->appUuid !== '' ? $this->appUuid : null;+ // A selected application or service is queried under all of its Sentinel keys.+ $resource = $this->selectedResource();$servers = $this->targetServers();+ $domainForKey = fn (string $key): ?string => $this->appMeta($key)['domain'];- $overviews = [];+ $aggregator = new TrafficAnalyticsAggregator($this->breakdownDimensions);$appRows = [];- $pathTotals = [];- $breakdownTotals = array_fill_keys($this->breakdownDimensions, []);- $seriesByBucket = [];- $attribution = null;+ $hostTotals = [];foreach ($servers as $server) {try {$client = $this->trafficClient($server);+ if ($resource !== null) {+ $keys = $client->prefetchResource($resource->uuid(), $from, $to, $this->breakdownDimensions, $this->range);+ foreach ($keys as $key) {+ $aggregator->collect($client, $key, $from, $to, $this->range, $domainForKey);+ }++ continue;+ }+// Warm every server-wide endpoint in one docker exec instead of ~15 serial// SSH round-trips; the per-call methods below then read from cache.- $leaderboardUuids = $client->prefetchServerWide($appKey, $from, $to, $this->breakdownDimensions, $this->range, appsLimit: self::MAX_LEADERBOARD_APPS);+ $leaderboardKeys = $client->prefetchServerWide(null, $from, $to, $this->breakdownDimensions, $this->range, appsLimit: self::MAX_LEADERBOARD_APPS);- // Per-application leaderboard only makes sense when not already filtered to one app.- if ($appKey === null && $leaderboardUuids !== []) {- if (count($leaderboardUuids) > self::MAX_LEADERBOARD_APPS) {+ if ($leaderboardKeys !== []) {+ if (count($leaderboardKeys) > self::MAX_LEADERBOARD_APPS) {Log::warning('Traffic analytics leaderboard truncated', ['server' => $server->uuid,- 'total' => count($leaderboardUuids),+ 'total' => count($leaderboardKeys),'shown' => self::MAX_LEADERBOARD_APPS,]);- $leaderboardUuids = array_slice($leaderboardUuids, 0, self::MAX_LEADERBOARD_APPS);- }- // Warm the leaderboard's per-app overviews in a second batched exec.- $client->prefetchAppOverviews($leaderboardUuids, $from, $to);- }-- $overviews[] = $client->overview($appKey, $from, $to);-- if ($appKey === null) {- foreach ($leaderboardUuids as $uuid) {- $appOverview = $client->overview($uuid, $from, $to)->toArray();- $meta = $this->appMeta($uuid);-- $appRows[] = [- 'uuid' => $uuid,- 'name' => $meta['name'],- 'domain' => $meta['domain'],- 'link' => $meta['link'],- 'requests' => (int) ($appOverview['requests'] ?? 0),- 'bandwidth' => (int) ($appOverview['bytesIn'] ?? 0) + (int) ($appOverview['bytesOut'] ?? 0),- ];- }- }-- foreach ($client->paths($appKey, $from, $to, 50) as $path) {- $data = $path->toArray();- $pathStr = (string) ($data['path'] ?? '');- // Prefer the per-path app from Sentinel; fall back to the active app filter- // (older Sentinel omits `app`, but a filtered view still knows the app).- $appId = (string) ($data['app'] ?? '');- $resolveId = $appId !== '' ? $appId : ($appKey ?? '');- // Key by (app, path) so the same path under two apps stays two rows, each- // carrying its own domain.- $key = $resolveId."\n".$pathStr;- $domain = $resolveId !== '' ? ($this->appMeta($resolveId)['domain'] ?? null) : null;-- $pathTotals[$key] ??= ['path' => $pathStr, 'domain' => $domain, 'requests' => 0, 'bytesOut' => 0, 's4xx' => 0, 's5xx' => 0, 'p95' => 0.0];- $pathTotals[$key]['requests'] += (int) ($data['requests'] ?? 0);- $pathTotals[$key]['bytesOut'] += (int) ($data['bytesOut'] ?? 0);- $pathTotals[$key]['s4xx'] += (int) ($data['s4xx'] ?? 0);- $pathTotals[$key]['s5xx'] += (int) ($data['s5xx'] ?? 0);- $pathTotals[$key]['p95'] = max($pathTotals[$key]['p95'], (float) ($data['p95'] ?? 0));- }-- foreach ($this->breakdownDimensions as $dimension) {- foreach ($client->breakdown($appKey, $dimension, $from, $to, 50) as $row) {- $data = $row->toArray();- $value = (string) ($data['value'] ?? '');-- $breakdownTotals[$dimension][$value] ??= ['value' => $value, 'requests' => 0, 'bytesOut' => 0];- $breakdownTotals[$dimension][$value]['requests'] += (int) ($data['requests'] ?? 0);- $breakdownTotals[$dimension][$value]['bytesOut'] += (int) ($data['bytesOut'] ?? 0);+ $leaderboardKeys = array_slice($leaderboardKeys, 0, self::MAX_LEADERBOARD_APPS);}+ // Warm the leaderboard's per-key overviews in a second batched exec.+ $client->prefetchAppOverviews($leaderboardKeys, $from, $to);}- $attribution ??= $client->attribution();-- // Per-bucket status series; summed by bucket across servers. Isolated so a- // series hiccup (or an older Sentinel lacking the endpoint) never discards a- // server's other data — an empty result simply flips the chart to the donut.- try {- foreach ($client->series($appKey, $this->range) as $bucket) {- $data = $bucket->toArray();- $ts = (int) ($data['bucket'] ?? 0);-- $seriesByBucket[$ts] ??= ['bucket' => $ts, 's2xx' => 0, 's3xx' => 0, 's4xx' => 0, 's5xx' => 0, 'requests' => 0, 'bytesIn' => 0, 'bytesOut' => 0, 'uniqueVisitors' => 0, 'p95' => 0.0];- $seriesByBucket[$ts]['s2xx'] += (int) ($data['s2xx'] ?? 0);- $seriesByBucket[$ts]['s3xx'] += (int) ($data['s3xx'] ?? 0);- $seriesByBucket[$ts]['s4xx'] += (int) ($data['s4xx'] ?? 0);- $seriesByBucket[$ts]['s5xx'] += (int) ($data['s5xx'] ?? 0);- $seriesByBucket[$ts]['requests'] += (int) ($data['requests'] ?? 0);- $seriesByBucket[$ts]['bytesIn'] += (int) ($data['bytesIn'] ?? 0);- $seriesByBucket[$ts]['bytesOut'] += (int) ($data['bytesOut'] ?? 0);- // Uniques summed across servers (approximate); p95 takes the worst bucket.- $seriesByBucket[$ts]['uniqueVisitors'] += (int) ($data['uniqueVisitors'] ?? 0);- $seriesByBucket[$ts]['p95'] = max($seriesByBucket[$ts]['p95'], (float) ($data['p95'] ?? 0));- }- } catch (\Throwable $e) {- // Leave this server out of the series; donut fallback covers it.+ $aggregator->collect($client, null, $from, $to, $this->range, $domainForKey);++ // Leaderboard: fold every key into its owning team resource (compose services+ // and previews into their application or service); unknown keys stay raw.+ foreach ($leaderboardKeys as $key) {+ $keyOverview = $client->overview($key, $from, $to)->toArray();+ $requests = (int) ($keyOverview['requests'] ?? 0);+ $bandwidth = (int) ($keyOverview['bytesIn'] ?? 0) + (int) ($keyOverview['bytesOut'] ?? 0);+ $meta = $this->appMeta($key);++ $appRows[$meta['uuid']] ??= [+ 'uuid' => $meta['uuid'],+ 'name' => $meta['name'],+ 'domain' => $meta['rowDomain'],+ 'link' => $meta['link'],+ 'isService' => $meta['isService'],+ 'requests' => 0,+ 'bandwidth' => 0,+ ];+ $appRows[$meta['uuid']]['requests'] += $requests;+ $appRows[$meta['uuid']]['bandwidth'] += $bandwidth;++ // Top hosts: fold per-key volume up to the served hostname. Keys without+ // a configured domain collapse into one "Unknown host" row.+ $host = $meta['domain'] ?? '';+ $hostTotals[$host] ??= ['host' => $host, 'requests' => 0, 'bandwidth' => 0];+ $hostTotals[$host]['requests'] += $requests;+ $hostTotals[$host]['bandwidth'] += $bandwidth;}} catch (\Throwable $e) {// Skip unreachable/failed servers so one bad server doesn't break the whole view.@@ -399,7 +405,7 @@ class Analytics extends Component}}- if (empty($overviews)) {+ if (! $aggregator->hasOverview()) {$this->resetData();// The chart lives under wire:ignore, so it only updates via this event — dispatch// even when cleared so a previously-populated chart flips to its no-data state@@ -409,46 +415,17 @@ class Analytics extends Componentreturn;}- $result = TrafficAnalyticsAggregator::sumOverviews($overviews);+ $result = $aggregator->overview();$this->overview = $result['overview']->toArray();$this->latencyApproximate = $result['latencyApproximate'];$this->uniquesApproximate = $result['uniquesApproximate'];- usort($appRows, fn ($a, $b) => $b['requests'] <=> $a['requests']);- $this->topApps = array_slice($appRows, 0, 50);-- // Top hosts: fold per-app volume up to the served hostname (an app's primary- // domain). Apps without a configured FQDN collapse into one "Unknown host" row.- $hostTotals = [];- foreach ($appRows as $row) {- $host = $row['domain'] ?? '';- $hostTotals[$host] ??= ['host' => $host, 'requests' => 0, 'bandwidth' => 0];- $hostTotals[$host]['requests'] += (int) $row['requests'];- $hostTotals[$host]['bandwidth'] += (int) $row['bandwidth'];- }- $hosts = array_values($hostTotals);- usort($hosts, fn ($a, $b) => $b['requests'] <=> $a['requests']);- $this->topHosts = array_slice($hosts, 0, 50);-- $paths = array_values($pathTotals);- usort($paths, fn ($a, $b) => $b['requests'] <=> $a['requests']);- $this->topPaths = array_slice($paths, 0, 50);-- $breakdowns = [];- foreach ($this->breakdownDimensions as $dimension) {- $rows = array_values($breakdownTotals[$dimension]);- usort($rows, fn ($a, $b) => $b['requests'] <=> $a['requests']);- if ($dimension === 'referer') {- $rows = groupRefererBreakdownRows($rows);- }- $breakdowns[$dimension] = array_slice($rows, 0, 50);- }- $this->breakdowns = $breakdowns;-- $this->attribution = $attribution;-- ksort($seriesByBucket);- $this->series = array_values($seriesByBucket);+ $this->topApps = TrafficAnalyticsAggregator::topByRequests(array_values($appRows));+ $this->topHosts = TrafficAnalyticsAggregator::topByRequests(array_values($hostTotals));+ $this->topPaths = $aggregator->topPaths();+ $this->breakdowns = $aggregator->breakdowns();+ $this->attribution = $aggregator->attribution();+ $this->series = $aggregator->series();$this->hasSeries = $this->series !== [];$this->dispatch("refreshChartData-{$this->chartId}-status", $this->chartPayload());@@ -534,39 +511,47 @@ class Analytics extends Component}/**- * Resolve an app uuid to its display name, primary domain, and analytics-page link,- * memoized per request. Returns the uuid as the name for apps not owned by the team- * so a Sentinel-reported uuid never discloses another team's application name.+ * Resolve a Sentinel key to its owning team resource: row id (resource uuid), name,+ * key-specific domain (compose service / preview), primary domain for the leaderboard+ * row, and analytics link. Memoized per request. A key that no team resource owns+ * keeps the raw key as its name and no domain or link, so a Sentinel-reported key+ * never discloses another team's resource.*- * @return array{name: string, domain: ?string, link: ?string}+ * @return array{uuid: string, name: string, domain: ?string, rowDomain: ?string, link: ?string, isService: bool}*/- protected function appMeta(string $uuid): array+ protected function appMeta(string $key): array{- if (isset($this->appMetaCache[$uuid])) {- return $this->appMetaCache[$uuid];+ if (isset($this->appMetaCache[$key])) {+ return $this->appMetaCache[$key];}- $app = Application::ownedByCurrentTeam()->with('environment.project')->whereUuid($uuid)->first();-- $domain = null;- if ($app) {- $first = collect($app->fqdns)->first();- $domain = $first ? (parse_url($first, PHP_URL_HOST) ?: null) : null;+ $resources = $this->teamResources();+ $owner = null;+ foreach (SentinelTrafficClient::candidateOwnerUuids($key) as $candidate) {+ if (isset($resources[$candidate])) {+ $owner = $resources[$candidate];+ break;+ }}- $link = null;- if ($app && data_get($app, 'environment.project.uuid')) {- $link = route('project.application.analytics', [- 'project_uuid' => $app->environment->project->uuid,- 'environment_uuid' => $app->environment->uuid,- 'application_uuid' => $app->uuid,- ]);+ if ($owner === null) {+ return $this->appMetaCache[$key] = [+ 'uuid' => $key,+ 'name' => $key,+ 'domain' => null,+ 'rowDomain' => null,+ 'link' => null,+ 'isService' => false,+ ];}- return $this->appMetaCache[$uuid] = [- 'name' => $app?->name ?? $uuid,- 'domain' => $domain,- 'link' => $link,+ return $this->appMetaCache[$key] = [+ 'uuid' => $owner->uuid(),+ 'name' => $owner->name(),+ 'domain' => $owner->domainForKey($key),+ 'rowDomain' => $owner->primaryDomain(),+ 'link' => $owner->analyticsLink(),+ 'isService' => $owner->isService(),];}diff --git a/app/Livewire/Dashboard/TrafficAnalytics.php b/app/Livewire/Dashboard/TrafficAnalytics.phpindex 532460d454..c24d7dac79 100644--- a/app/Livewire/Dashboard/TrafficAnalytics.php+++ b/app/Livewire/Dashboard/TrafficAnalytics.php@@ -60,33 +60,18 @@ class TrafficAnalytics extends Component[$from, $to] = $this->window();- $overviews = [];- $seriesByBucket = [];+ $aggregator = new TrafficAnalyticsAggregator;foreach ($this->servers as $server) {try {$client = $this->trafficClient($server);- $overviews[] = $client->overview(null, $from, $to);+ $aggregator->addOverview($client->overview(null, $from, $to));// Per-bucket status series, summed across servers, for the sparklines.// Isolated so a series hiccup (older Sentinel) never drops a server's overview.try {- foreach ($client->series(null, $this->range) as $bucket) {- $data = $bucket->toArray();- $ts = (int) ($data['bucket'] ?? 0);-- $seriesByBucket[$ts] ??= ['bucket' => $ts, 's2xx' => 0, 's3xx' => 0, 's4xx' => 0, 's5xx' => 0, 'requests' => 0, 'bytesIn' => 0, 'bytesOut' => 0, 'uniqueVisitors' => 0, 'p95' => 0.0];- $seriesByBucket[$ts]['s2xx'] += (int) ($data['s2xx'] ?? 0);- $seriesByBucket[$ts]['s3xx'] += (int) ($data['s3xx'] ?? 0);- $seriesByBucket[$ts]['s4xx'] += (int) ($data['s4xx'] ?? 0);- $seriesByBucket[$ts]['s5xx'] += (int) ($data['s5xx'] ?? 0);- $seriesByBucket[$ts]['requests'] += (int) ($data['requests'] ?? 0);- $seriesByBucket[$ts]['bytesIn'] += (int) ($data['bytesIn'] ?? 0);- $seriesByBucket[$ts]['bytesOut'] += (int) ($data['bytesOut'] ?? 0);- $seriesByBucket[$ts]['uniqueVisitors'] += (int) ($data['uniqueVisitors'] ?? 0);- $seriesByBucket[$ts]['p95'] = max($seriesByBucket[$ts]['p95'], (float) ($data['p95'] ?? 0));- }+ $aggregator->addSeries($client->series(null, $this->range));} catch (\Throwable $e) {// Leave this server out of the sparkline series.\Log::debug('Traffic series fetch failed', ['server' => $server->uuid, 'error' => $e->getMessage()]);@@ -99,7 +84,7 @@ class TrafficAnalytics extends Component}}- if (empty($overviews)) {+ if (! $aggregator->hasOverview()) {// Every server's fetch failed; don't present an all-zero KPI panel as if it were real data.$this->overview = null;$this->latencyApproximate = false;@@ -109,14 +94,13 @@ class TrafficAnalytics extends Componentreturn;}- $result = TrafficAnalyticsAggregator::sumOverviews($overviews);+ $result = $aggregator->overview();$this->overview = $result['overview']->toArray();$this->latencyApproximate = $result['latencyApproximate'];$this->uniquesApproximate = $result['uniquesApproximate'];- ksort($seriesByBucket);- $this->series = array_values($seriesByBucket);+ $this->series = $aggregator->series();$this->dispatch("refreshChartData-{$this->chartId}-status", ['requestsSpark' => $this->requestsSpark(),diff --git a/app/Livewire/Project/Application/Analytics.php b/app/Livewire/Project/Application/Analytics.phpindex 52d15d9e10..b6f4578b5f 100644--- a/app/Livewire/Project/Application/Analytics.php+++ b/app/Livewire/Project/Application/Analytics.php@@ -2,245 +2,19 @@namespace App\Livewire\Project\Application;-use App\Livewire\Concerns\BuildsTrafficChartPayload;+use App\Livewire\Project\Shared\ResourceTrafficAnalytics;use App\Models\Application;-use App\Services\SentinelTrafficClient;-use Illuminate\Contracts\View\View;use Livewire\Attributes\Lazy;-use Livewire\Component;#[Lazy]-class Analytics extends Component+class Analytics extends ResourceTrafficAnalytics{- use BuildsTrafficChartPayload;-public Application $application;public string $chartId = 'application-analytics';- public string $range = '24h';-- public bool $enabled = false;-- // Realtime refresh. Off by default (click "Live" to arm it). Only meaningful on the- // 24h range; the 60s cadence matches the SentinelTrafficClient cache TTL and- // Sentinel's per-minute rollups. The control is disabled for 7d/30d.- public bool $live = false;-- public ?array $overview = null;-- public array $topPaths = [];-- /** @var array<string, array<int, array<string, mixed>>> */- public array $breakdowns = [];-- public ?string $attribution = null;-- /**- * Per-bucket status-class time series for the stacked area chart. Empty when this- * app's Sentinel lacks the series endpoint, which flips the chart to the donut.- *- * @var array<int, array{bucket: int, s2xx: int, s3xx: int, s4xx: int, s5xx: int}>- */- public array $series = [];-- public bool $hasSeries = false;-- /** @var array<int, string> */- protected array $breakdownDimensions = ['country', 'referer', 'browser', 'os', 'device', 'protocol', 'cache', 'status', 'agent', 'ip', 'useragent'];-- public function mount(): void- {- $this->enabled = (bool) $this->application->destination?->server?->isTrafficAnalyticsEnabled();-- if ($this->enabled) {- $this->loadData();- }- }-- public function setRange(string $range): void- {- $this->range = in_array($range, ['24h', '7d', '30d'], true) ? $range : '24h';- $this->loadData();- }-- public function toggleLive(): void- {- if ($this->range !== '24h') {- return;- }- $this->live = ! $this->live;- }-- /**- * Realtime polling is only armed when the user has it on and the range is 24h.- */- public function isLivePollable(): bool- {- return $this->live && $this->range === '24h';- }-- public function loadData(): void- {- if (! $this->enabled) {- return;- }-- try {- [$from, $to] = $this->window();- $client = $this->trafficClient();- $key = $this->application->uuid;-- // Warm every endpoint for this app in one docker exec instead of ~14 serial- // SSH round-trips; the per-call methods below then read from cache.- $client->prefetchServerWide($key, $from, $to, $this->breakdownDimensions, $this->range);-- $this->overview = $client->overview($key, $from, $to)->toArray();-- // Every path belongs to this one app, so decorate each row with its domain- // for a consistent "domain + path" presentation and an openable live link.- $domain = $this->applicationDomain();- $this->topPaths = $client->paths($key, $from, $to, 50)- ->map(fn ($path) => ['domain' => $domain] + $path->toArray())- ->all();-- $breakdowns = [];- foreach ($this->breakdownDimensions as $dimension) {- $rows = $client->breakdown($key, $dimension, $from, $to, 50)- ->map(fn ($row) => $row->toArray())- ->all();- $breakdowns[$dimension] = $dimension === 'referer'- ? groupRefererBreakdownRows($rows)- : $rows;- }- $this->breakdowns = $breakdowns;-- $this->attribution = $client->attribution();-- // Per-bucket status series; absent on older Sentinel builds (empty → donut fallback).- // Isolated so a series hiccup never errors the rest of the widget.- try {- $this->series = $client->series($key, $this->range)- ->map(fn ($bucket) => $bucket->toArray())- ->all();- } catch (\Throwable $e) {- $this->series = [];- }- $this->hasSeries = $this->series !== [];-- $this->dispatch("refreshChartData-{$this->chartId}-status", $this->chartPayload());- } catch (\Throwable $e) {- handleError($e, $this);- }- }-- /**- * Payload for the status chart: the stacked-area time series when available,- * plus the donut totals as a fallback for older Sentinel builds.- *- * @return array<string, mixed>- */- protected function chartPayload(): array- {- $device = $this->deviceChartData();-- return [- 'hasSeries' => $this->hasSeries,- 'range' => $this->range,- 'seriesData' => [- $this->overview['s2xx'] ?? 0,- $this->overview['s3xx'] ?? 0,- $this->overview['s4xx'] ?? 0,- $this->overview['s5xx'] ?? 0,- ],- 'timeSeries' => [- 'categories' => array_column($this->series, 'bucket'),- 'requests' => $this->requestsSpark(),- 's2xx' => array_column($this->series, 's2xx'),- 's3xx' => array_column($this->series, 's3xx'),- 's4xx' => array_column($this->series, 's4xx'),- 's5xx' => array_column($this->series, 's5xx'),- ],- 'requestsSpark' => $this->requestsSpark(),- 'sparkCategories' => array_column($this->series, 'bucket'),- 'errorsSpark' => $this->errorsSpark(),- 'bandwidthSpark' => $this->bandwidthSpark(),- 'uniquesSpark' => $this->uniquesSpark(),- 'latencySpark' => $this->latencySpark(),- 'geo' => $this->geoMarkers(),- 'deviceLabels' => $device['labels'],- 'deviceSeries' => $device['series'],- ];- }-- public function errorRate(): float- {- if (! $this->overview || (int) ($this->overview['requests'] ?? 0) === 0) {- return 0.0;- }-- $errors = (int) ($this->overview['s4xx'] ?? 0) + (int) ($this->overview['s5xx'] ?? 0);-- return round(($errors / $this->overview['requests']) * 100, 2);- }-- public function bandwidthBytes(): int- {- if (! $this->overview) {- return 0;- }-- return (int) ($this->overview['bytesIn'] ?? 0) + (int) ($this->overview['bytesOut'] ?? 0);- }-- protected function trafficClient(): SentinelTrafficClient- {- return app(SentinelTrafficClient::class, ['server' => $this->application->destination->server]);- }-- /**- * Primary domain host for this application (first configured FQDN), or null when- * none is set — used to present paths as "domain + path" with an openable link.- */- protected function applicationDomain(): ?string- {- $first = collect($this->application->fqdns)->first();-- return $first ? (parse_url($first, PHP_URL_HOST) ?: null) : null;- }-- /**- * @return array{0: string, 1: string}- */- private function window(): array- {- $to = now();- $from = match ($this->range) {- '7d' => now()->subDays(7),- '30d' => now()->subDays(30),- default => now()->subDay(),- };-- return [$from->toIso8601ZuluString(), $to->toIso8601ZuluString()];- }-- public function placeholder(array $params = []): View- {- $application = $params['application'] ?? null;-- if ($application instanceof Application && ! $application->destination?->server?->isTrafficAnalyticsEnabled()) {- $this->application = $application;- $this->enabled = false;-- return view('livewire.project.application.analytics');- }-- // Rendered instantly; the Sentinel round-trip runs in the deferred lazy-load request.- return view('livewire.project.application.analytics-placeholder');- }-- public function render()+ protected function resourceProperty(): string{- return view('livewire.project.application.analytics');+ return 'application';}}diff --git a/app/Livewire/Project/Application/TrafficOverview.php b/app/Livewire/Project/Application/TrafficOverview.phpindex 223c6047ae..86a6ff64f4 100644--- a/app/Livewire/Project/Application/TrafficOverview.php+++ b/app/Livewire/Project/Application/TrafficOverview.php@@ -4,6 +4,7 @@ namespace App\Livewire\Project\Application;use App\Models\Application;use App\Services\SentinelTrafficClient;+use App\Services\TrafficAnalyticsAggregator;use Livewire\Attributes\Lazy;use Livewire\Component;@@ -36,7 +37,14 @@ class TrafficOverview extends Componentif ($this->enabled && $server) {try {$client = app(SentinelTrafficClient::class, ['server' => $server]);- $this->overview = $client->appOverview($this->application->uuid, '24h')->toArray();+ // Compose services and previews record under their own keys; sum all of them.+ $keys = $client->resourceKeys($this->application->uuid);+ [$from, $to] = SentinelTrafficClient::rangeWindow('24h');+ if (count($keys) > 1) {+ $client->prefetchAppOverviews($keys, $from, $to);+ }+ $overviews = array_map(fn (string $key) => $client->overview($key, $from, $to), $keys);+ $this->overview = TrafficAnalyticsAggregator::sumOverviews($overviews)['overview']->toArray();} catch (\Throwable $e) {$this->overview = null;}diff --git a/app/Livewire/Project/Service/Analytics.php b/app/Livewire/Project/Service/Analytics.phpnew file mode 100644index 0000000000..9bdb130077--- /dev/null+++ b/app/Livewire/Project/Service/Analytics.php@@ -0,0 +1,20 @@+<?php++namespace App\Livewire\Project\Service;++use App\Livewire\Project\Shared\ResourceTrafficAnalytics;+use App\Models\Service;+use Livewire\Attributes\Lazy;++#[Lazy]+class Analytics extends ResourceTrafficAnalytics+{+ public Service $service;++ public string $chartId = 'service-analytics';++ protected function resourceProperty(): string+ {+ return 'service';+ }+}diff --git a/app/Livewire/Project/Shared/ResourceTrafficAnalytics.php b/app/Livewire/Project/Shared/ResourceTrafficAnalytics.phpnew file mode 100644index 0000000000..7ba0829855--- /dev/null+++ b/app/Livewire/Project/Shared/ResourceTrafficAnalytics.php@@ -0,0 +1,236 @@+<?php++namespace App\Livewire\Project\Shared;++use App\Livewire\Concerns\BuildsTrafficChartPayload;+use App\Models\Application;+use App\Models\Service;+use App\Services\SentinelTrafficClient;+use App\Services\TrafficAnalyticsAggregator;+use App\Services\TrafficResource;+use Illuminate\Contracts\View\View;+use Illuminate\Foundation\Auth\Access\AuthorizesRequests;+use Livewire\Component;++/**+ * Traffic analytics tab of one resource (Application or Service). A resource can record+ * under several Sentinel keys (compose services, previews); every key is fetched and+ * merged into one view.+ */+abstract class ResourceTrafficAnalytics extends Component+{+ use AuthorizesRequests;+ use BuildsTrafficChartPayload;++ public string $chartId = 'resource-analytics';++ public string $range = '24h';++ public bool $enabled = false;++ public ?string $analyticsServerUuid = null;++ // Realtime refresh. Off by default (click "Live" to arm it). Only meaningful on the+ // 24h range; the 60s cadence matches the SentinelTrafficClient cache TTL and+ // Sentinel's per-minute rollups. The control is disabled for 7d/30d.+ public bool $live = false;++ public ?array $overview = null;++ public bool $latencyApproximate = false;++ public bool $uniquesApproximate = false;++ public array $topPaths = [];++ /** @var array<string, array<int, array<string, mixed>>> */+ public array $breakdowns = [];++ public ?string $attribution = null;++ /**+ * Per-bucket status-class time series for the stacked area chart. Empty when this+ * resource's Sentinel lacks the series endpoint, which flips the chart to the donut.+ *+ * @var array<int, array{bucket: int, s2xx: int, s3xx: int, s4xx: int, s5xx: int}>+ */+ public array $series = [];++ public bool $hasSeries = false;++ /** @var array<int, string> */+ protected array $breakdownDimensions = ['country', 'referer', 'browser', 'os', 'device', 'protocol', 'cache', 'status', 'agent', 'ip', 'useragent'];++ /**+ * Name of the public model property and lazy-load parameter ('application' / 'service').+ */+ abstract protected function resourceProperty(): string;++ protected function resourceModel(): Application|Service+ {+ return $this->{$this->resourceProperty()};+ }++ protected function trafficResource(): TrafficResource+ {+ return TrafficResource::for($this->resourceModel());+ }++ public function mount(): void+ {+ $this->authorize('view', $this->resourceModel());++ $server = $this->trafficResource()->server();+ $this->analyticsServerUuid = $server?->uuid;+ $this->enabled = (bool) $server?->isTrafficAnalyticsEnabled();++ if ($this->enabled) {+ $this->loadData();+ }+ }++ public function setRange(string $range): void+ {+ $this->range = in_array($range, ['24h', '7d', '30d'], true) ? $range : '24h';+ $this->loadData();+ }++ public function toggleLive(): void+ {+ if ($this->range !== '24h') {+ return;+ }+ $this->live = ! $this->live;+ }++ /**+ * Realtime polling is only armed when the user has it on and the range is 24h.+ */+ public function isLivePollable(): bool+ {+ return $this->live && $this->range === '24h';+ }++ public function loadData(): void+ {+ if (! $this->enabled) {+ return;+ }++ try {+ $this->authorize('view', $this->resourceModel());++ $resource = $this->trafficResource();+ [$from, $to] = SentinelTrafficClient::rangeWindow($this->range);+ $client = app(SentinelTrafficClient::class, ['server' => $resource->server()]);++ // Resolve every key of this resource and warm all of them in one or two+ // docker execs; the per-call methods below then read from cache.+ $keys = $client->prefetchResource($resource->uuid(), $from, $to, $this->breakdownDimensions, $this->range);++ $aggregator = new TrafficAnalyticsAggregator($this->breakdownDimensions);+ foreach ($keys as $key) {+ $aggregator->collect($client, $key, $from, $to, $this->range, fn (string $appKey) => $resource->domainForKey($appKey));+ }++ $result = $aggregator->overview();+ $this->overview = $result['overview']->toArray();+ $this->latencyApproximate = $result['latencyApproximate'];+ $this->uniquesApproximate = $result['uniquesApproximate'];+ $this->topPaths = $aggregator->topPaths();+ $this->breakdowns = $aggregator->breakdowns();+ $this->attribution = $aggregator->attribution();+ $this->series = $aggregator->series();+ $this->hasSeries = $this->series !== [];++ $this->dispatch("refreshChartData-{$this->chartId}-status", $this->chartPayload());+ } catch (\Throwable $e) {+ handleError($e, $this);+ }+ }++ /**+ * Payload for the status chart: the stacked-area time series when available,+ * plus the donut totals as a fallback for older Sentinel builds.+ *+ * @return array<string, mixed>+ */+ protected function chartPayload(): array+ {+ $device = $this->deviceChartData();++ return [+ 'hasSeries' => $this->hasSeries,+ 'range' => $this->range,+ 'seriesData' => [+ $this->overview['s2xx'] ?? 0,+ $this->overview['s3xx'] ?? 0,+ $this->overview['s4xx'] ?? 0,+ $this->overview['s5xx'] ?? 0,+ ],+ 'timeSeries' => [+ 'categories' => array_column($this->series, 'bucket'),+ 'requests' => $this->requestsSpark(),+ 's2xx' => array_column($this->series, 's2xx'),+ 's3xx' => array_column($this->series, 's3xx'),+ 's4xx' => array_column($this->series, 's4xx'),+ 's5xx' => array_column($this->series, 's5xx'),+ ],+ 'requestsSpark' => $this->requestsSpark(),+ 'sparkCategories' => array_column($this->series, 'bucket'),+ 'errorsSpark' => $this->errorsSpark(),+ 'bandwidthSpark' => $this->bandwidthSpark(),+ 'uniquesSpark' => $this->uniquesSpark(),+ 'latencySpark' => $this->latencySpark(),+ 'geo' => $this->geoMarkers(),+ 'deviceLabels' => $device['labels'],+ 'deviceSeries' => $device['series'],+ ];+ }++ public function errorRate(): float+ {+ if (! $this->overview || (int) ($this->overview['requests'] ?? 0) === 0) {+ return 0.0;+ }++ $errors = (int) ($this->overview['s4xx'] ?? 0) + (int) ($this->overview['s5xx'] ?? 0);++ return round(($errors / $this->overview['requests']) * 100, 2);+ }++ public function bandwidthBytes(): int+ {+ if (! $this->overview) {+ return 0;+ }++ return (int) ($this->overview['bytesIn'] ?? 0) + (int) ($this->overview['bytesOut'] ?? 0);+ }++ public function placeholder(array $params = []): View+ {+ $model = $params[$this->resourceProperty()] ?? null;++ if ($model instanceof Application || $model instanceof Service) {+ $this->authorize('view', $model);+ $server = TrafficResource::for($model)->server();++ if (! $server?->isTrafficAnalyticsEnabled()) {+ $this->{$this->resourceProperty()} = $model;+ $this->enabled = false;+ $this->analyticsServerUuid = $server?->uuid;++ return view('livewire.project.application.analytics');+ }+ }++ // Rendered instantly; the Sentinel round-trip runs in the deferred lazy-load request.+ return view('livewire.project.application.analytics-placeholder');+ }++ public function render()+ {+ return view('livewire.project.application.analytics');+ }+}diff --git a/app/Livewire/Server/TrafficAnalyticsSettings.php b/app/Livewire/Server/TrafficAnalyticsSettings.phpindex ce7837dcc9..221e724a2a 100644--- a/app/Livewire/Server/TrafficAnalyticsSettings.php+++ b/app/Livewire/Server/TrafficAnalyticsSettings.php@@ -3,6 +3,7 @@namespace App\Livewire\Server;use App\Actions\Server\ConfigureTrafficAnalytics;+use App\Enums\ProxyTypes;use App\Livewire\Analytics;use App\Models\Server;use Illuminate\Foundation\Auth\Access\AuthorizesRequests;@@ -110,10 +111,29 @@ class TrafficAnalyticsSettings extends Component{return view('livewire.server.traffic-analytics-settings', ['unsupportedReason' => $this->server->trafficAnalyticsUnsupportedReason(),+ 'caddyRedeployNote' => $this->caddyRedeployNote(),]);}+ /**+ * Caddy gets its log labels at deploy time, so resources that already run are not logged until a redeploy.+ */+ private function caddyRedeployNote(): ?string+ {+ return $this->server->proxyType() === ProxyTypes::CADDY->value+ ? 'Caddy logs a resource only after you redeploy it.'+ : null;+ }+private function toggleMessage(bool $enabled, bool $proxyRestarted): string+ {+ $message = $this->proxyToggleMessage($enabled, $proxyRestarted);+ $caddyRedeployNote = $this->caddyRedeployNote();++ return $enabled && $caddyRedeployNote !== null ? "{$message} {$caddyRedeployNote}" : $message;+ }++ private function proxyToggleMessage(bool $enabled, bool $proxyRestarted): string{$state = $enabled ? 'enabled' : 'disabled';diff --git a/app/Models/Server.php b/app/Models/Server.phpindex 9b5bc54145..f4a40c4787 100644--- a/app/Models/Server.php+++ b/app/Models/Server.php@@ -1943,6 +1943,7 @@ $siteAddress {return str($proxyType->value)->lower();});if ($validProxyTypes->contains(str($proxyType)->lower())) {+ $previousProxyType = $this->proxyType();$this->proxy->set('type', str($proxyType)->upper());$this->proxy->set('status', 'exited');$this->proxy->set('last_saved_proxy_configuration', null);@@ -1958,11 +1959,26 @@ $siteAddress {StartProxy::run($this);}}+ if ($previousProxyType !== $this->proxyType() && $this->shouldRestartSentinelForTrafficAnalytics()) {+ // Sentinel keeps the traffic log mount, path and log format of the old proxy until it is recreated.+ $this->restartSentinel();+ }} else {throw new \Exception('Invalid proxy type.');}}+ /**+ * Sentinel reads the proxy access log only when it runs and traffic analytics is on.+ * The raw setting is used, because a switch to a proxy without analytics support must also drop the old log mount.+ */+ private function shouldRestartSentinelForTrafficAnalytics(): bool+ {+ return (bool) $this->settings->is_sentinel_enabled+ && $this->isSentinelEnabled()+ && $this->isTrafficAnalyticsEnabled();+ }+public function isEmpty(){return $this->applications()->count() == 0 &&diff --git a/app/Services/SentinelTrafficClient.php b/app/Services/SentinelTrafficClient.phpindex 3cd7eabe6c..74e70367dd 100644--- a/app/Services/SentinelTrafficClient.php+++ b/app/Services/SentinelTrafficClient.php@@ -50,17 +50,6 @@ class SentinelTrafficClientreturn [$from->toIso8601ZuluString(), $to->toIso8601ZuluString()];}- /**- * Slim shared fetch for a single application's overview over a UI range, so the- * General-page widget and the full analytics tab don't duplicate window + client calls.- */- public function appOverview(string $appKey, string $range = '24h'): TrafficOverviewData- {- [$from, $to] = self::rangeWindow($range);-- return $this->overview($appKey, $from, $to);- }-public function paths(?string $appKey, string $from, string $to, int $limit = 50): Collection{$rows = json_decode($this->raw($this->pathsUrl($appKey, $from, $to, $limit)), true) ?? [];@@ -102,6 +91,137 @@ class SentinelTrafficClientreturn json_decode($this->raw($this->appsUrl()), true) ?? [];}+ /**+ * True when Sentinel key $key belongs to the resource with $resourceUuid. A resource+ * (Application or Service) owns the bare `{uuid}` key and every `{uuid}-…` key: compose+ * services (`{uuid}-{service}`), previews (`{uuid}-{pr}`), and preview compose services.+ */+ public static function keyBelongsTo(string $key, string $resourceUuid): bool+ {+ return $resourceUuid !== '' && ($key === $resourceUuid || str_starts_with($key, $resourceUuid.'-'));+ }++ /**+ * Uuids that could own $key, most specific first: the key itself, then every prefix+ * that ends before a `-`. Lets a caller match a key against a uuid map in O(key length).+ *+ * @return array<int, string>+ */+ public static function candidateOwnerUuids(string $key): array+ {+ $candidates = [$key];+ $position = strlen($key);+ while (($position = strrpos(substr($key, 0, $position), '-')) !== false) {+ if ($position > 0) {+ $candidates[] = substr($key, 0, $position);+ }+ }++ return $candidates;+ }++ /**+ * Every Sentinel key recorded on this server that belongs to the resource. Falls back to+ * the bare uuid when none is recorded (or the key list is unavailable), so per-key queries+ * still run and return an empty result instead of failing.+ *+ * @return array<int, string>+ */+ public function resourceKeys(string $resourceUuid): array+ {+ $this->assertSafeKey($resourceUuid);++ try {+ $recorded = $this->apps();+ } catch (\Throwable) {+ $recorded = [];+ }++ $keys = array_values(array_unique(array_filter(+ $recorded,+ fn ($key) => is_string($key)+ && $this->isSafeKey($key)+ && self::keyBelongsTo($key, $resourceUuid)+ )));+ sort($keys);++ return $keys === [] ? [$resourceUuid] : $keys;+ }++ /**+ * Resolve the resource's keys and warm every endpoint for each key, in as few SSH+ * round-trips as possible: the key list and the bare-uuid dashboard bundle (the common+ * single-key case) share one exec, and extra keys share one more.+ *+ * @param array<int, string> $dimensions+ * @return array<int, string>+ */+ public function prefetchResource(string $resourceUuid, string $from, string $to, array $dimensions, string $range, int $pathLimit = 50, int $breakdownLimit = 50): array+ {+ $this->assertSafeKey($resourceUuid);++ $urls = [$this->appsUrl()];+ if (Cache::get($this->dashboardAbsenceKey()) !== true) {+ $urls[] = $this->dashboardUrl($resourceUuid, $from, $to, $range, $pathLimit, $breakdownLimit, 0);+ }+ $this->warm($urls);++ $keys = $this->resourceKeys($resourceUuid);+ $this->prefetchKeys($keys, $from, $to, $dimensions, $range, $pathLimit, $breakdownLimit);++ return $keys;+ }++ /**+ * Warm every endpoint for several app keys. One key uses prefetchServerWide(); several+ * keys batch their dashboard bundles (or, on an older Sentinel, their individual+ * endpoints) into one exec instead of one SSH round-trip per key.+ *+ * @param array<int, string> $appKeys+ * @param array<int, string> $dimensions+ */+ public function prefetchKeys(array $appKeys, string $from, string $to, array $dimensions, string $range, int $pathLimit = 50, int $breakdownLimit = 50): void+ {+ $appKeys = array_values(array_unique($appKeys));+ if ($appKeys === []) {+ return;+ }++ if (count($appKeys) === 1 || ! $this->usesBatchableTransport()) {+ foreach ($appKeys as $appKey) {+ $this->prefetchServerWide($appKey, $from, $to, $dimensions, $range, $pathLimit, $breakdownLimit);+ }++ return;+ }++ if (Cache::get($this->dashboardAbsenceKey()) !== true) {+ $dashboardUrls = array_map(+ fn (string $appKey) => $this->dashboardUrl($appKey, $from, $to, $range, $pathLimit, $breakdownLimit, 0),+ $appKeys+ );+ $this->warm($dashboardUrls);++ if (Cache::has($this->cacheKey($dashboardUrls[0]))) {+ // Each call now reads its bundle from cache and seeds the per-endpoint cache.+ foreach ($appKeys as $appKey) {+ $this->prefetchServerWide($appKey, $from, $to, $dimensions, $range, $pathLimit, $breakdownLimit);+ }++ return;+ }++ // Older Sentinel without the dashboard route: remember it like fetchDashboard() does.+ Cache::put($this->dashboardAbsenceKey(), true, 60);+ }++ $urls = [$this->attributionUrl()];+ foreach ($appKeys as $appKey) {+ array_push($urls, ...$this->endpointUrls($appKey, $from, $to, $dimensions, $range, $pathLimit, $breakdownLimit));+ }+ $this->warm($urls);+ }+public function attribution(): ?string{$json = json_decode($this->raw($this->attributionUrl()), true) ?? [];@@ -139,14 +259,9 @@ class SentinelTrafficClient// Fallback for older Sentinel without /traffic/dashboard: batch the individual endpoints.$urls = [- $this->overviewUrl($appKey, $from, $to),- $this->pathsUrl($appKey, $from, $to, $pathLimit),- $this->seriesUrl($appKey, $range),+ ...$this->endpointUrls($appKey, $from, $to, $dimensions, $range, $pathLimit, $breakdownLimit),$this->attributionUrl(),];- foreach ($dimensions as $dimension) {- $urls[] = $this->breakdownUrl($appKey, $dimension, $from, $to, $breakdownLimit);- }// The per-application leaderboard only exists on the unfiltered view.if ($appKey === null) {$urls[] = $this->appsUrl();@@ -178,7 +293,7 @@ class SentinelTrafficClient// so without a marker every refresh would re-probe over SSH before falling back to the// batch. Remember the absence for the same 60s window as the data cache: at most one// wasted probe per minute, and a Sentinel upgrade is picked up on the next window.- $absenceKey = 'traffic:dashboard-absent:'.$this->server->uuid;+ $absenceKey = $this->dashboardAbsenceKey();if (Cache::get($absenceKey) === true) {return null;}@@ -242,6 +357,32 @@ class SentinelTrafficClient$this->warm($urls);}+ private function dashboardAbsenceKey(): string+ {+ return 'traffic:dashboard-absent:'.$this->server->uuid;+ }++ /**+ * The individual endpoint urls (overview, paths, series, breakdowns) for one key or the+ * whole server, used by the batched fallback on Sentinel builds without the dashboard.+ *+ * @param array<int, string> $dimensions+ * @return array<int, string>+ */+ private function endpointUrls(?string $appKey, string $from, string $to, array $dimensions, string $range, int $pathLimit, int $breakdownLimit): array+ {+ $urls = [+ $this->overviewUrl($appKey, $from, $to),+ $this->pathsUrl($appKey, $from, $to, $pathLimit),+ $this->seriesUrl($appKey, $range),+ ];+ foreach ($dimensions as $dimension) {+ $urls[] = $this->breakdownUrl($appKey, $dimension, $from, $to, $breakdownLimit);+ }++ return $urls;+ }+private function overviewUrl(?string $appKey, string $from, string $to): string{$path = $this->appScopedPath($appKey, 'overview');@@ -324,11 +465,16 @@ class SentinelTrafficClient*/private function assertSafeKey(string $value): void{- if ($value === '' || ! preg_match('/\A[A-Za-z0-9._:-]+\z/', $value)) {+ if (! $this->isSafeKey($value)) {throw new \InvalidArgumentException('Invalid traffic analytics app key.');}}+ private function isSafeKey(string $value): bool+ {+ return $value !== '' && preg_match('/\A[A-Za-z0-9._:-]+\z/', $value) === 1;+ }+private function assertSafeDimension(string $dimension): void{if (! in_array($dimension, self::ALLOWED_DIMENSIONS, true)) {diff --git a/app/Services/TrafficAnalyticsAggregator.php b/app/Services/TrafficAnalyticsAggregator.phpindex 6e057a3816..8ff50dc0fa 100644--- a/app/Services/TrafficAnalyticsAggregator.php+++ b/app/Services/TrafficAnalyticsAggregator.php@@ -4,8 +4,35 @@ namespace App\Services;use App\Data\Traffic\TrafficOverviewData;+/**+ * Merges Sentinel traffic data from several sources (servers, or the several app keys of+ * one resource) into one view. Counters are summed; latency takes the worst value and+ * uniques are summed, so both are flagged approximate once more than one source is merged.+ */class TrafficAnalyticsAggregator{+ /** @var array<int, TrafficOverviewData> */+ private array $overviews = [];++ /** @var array<string, array{path: string, domain: ?string, requests: int, bytesOut: int, s4xx: int, s5xx: int, p95: float}> */+ private array $paths = [];++ /** @var array<string, array<string, array{value: string, requests: int, bytesOut: int}>> */+ private array $breakdowns;++ /** @var array<int, array<string, int|float>> */+ private array $series = [];++ private ?string $attribution = null;++ /**+ * @param array<int, string> $dimensions+ */+ public function __construct(private array $dimensions = [])+ {+ $this->breakdowns = array_fill_keys($dimensions, []);+ }+/*** @param array<int, TrafficOverviewData> $overviews* @return array{overview: TrafficOverviewData, latencyApproximate: bool, uniquesApproximate: bool}@@ -36,4 +63,165 @@ class TrafficAnalyticsAggregator'uniquesApproximate' => $multi,];}++ /**+ * Fetch every shape for one app key (or the whole server when null) and merge it in.+ * The series fetch is isolated: a failure (or an older Sentinel without the endpoint)+ * never discards the other data; an empty series flips the chart to the donut.+ *+ * @param callable(string): ?string $domainForKey resolves a path row's app key to its domain+ */+ public function collect(SentinelTrafficClient $client, ?string $appKey, string $from, string $to, string $range, callable $domainForKey, int $limit = 50): void+ {+ $this->addOverview($client->overview($appKey, $from, $to));+ $this->addPaths($client->paths($appKey, $from, $to, $limit), $appKey, $domainForKey);++ foreach ($this->dimensions as $dimension) {+ $this->addBreakdown($dimension, $client->breakdown($appKey, $dimension, $from, $to, $limit));+ }++ $this->attribution ??= $client->attribution();++ try {+ $this->addSeries($client->series($appKey, $range));+ } catch (\Throwable) {+ // Leave this source out of the series; the donut fallback covers it.+ }+ }++ public function addOverview(TrafficOverviewData $overview): void+ {+ $this->overviews[] = $overview;+ }++ /**+ * Merge path rows keyed by (app, path), so the same path under two apps stays two+ * rows, each with its own domain. Sentinel's per-row `app` wins; older Sentinel omits+ * it, so a key-scoped fetch falls back to the key it queried.+ *+ * @param iterable<int, mixed> $paths+ * @param callable(string): ?string $domainForKey+ */+ public function addPaths(iterable $paths, ?string $fallbackAppKey, callable $domainForKey): void+ {+ foreach ($paths as $path) {+ $data = $path->toArray();+ $pathStr = (string) ($data['path'] ?? '');+ $appId = (string) ($data['app'] ?? '');+ $resolveId = $appId !== '' ? $appId : ($fallbackAppKey ?? '');+ $key = $resolveId."\n".$pathStr;++ $this->paths[$key] ??= [+ 'path' => $pathStr,+ 'domain' => $resolveId !== '' ? $domainForKey($resolveId) : null,+ 'requests' => 0, 'bytesOut' => 0, 's4xx' => 0, 's5xx' => 0, 'p95' => 0.0,+ ];+ $this->paths[$key]['requests'] += (int) ($data['requests'] ?? 0);+ $this->paths[$key]['bytesOut'] += (int) ($data['bytesOut'] ?? 0);+ $this->paths[$key]['s4xx'] += (int) ($data['s4xx'] ?? 0);+ $this->paths[$key]['s5xx'] += (int) ($data['s5xx'] ?? 0);+ $this->paths[$key]['p95'] = max($this->paths[$key]['p95'], (float) ($data['p95'] ?? 0));+ }+ }++ /**+ * @param iterable<int, mixed> $rows+ */+ public function addBreakdown(string $dimension, iterable $rows): void+ {+ foreach ($rows as $row) {+ $data = $row->toArray();+ $value = (string) ($data['value'] ?? '');++ $this->breakdowns[$dimension][$value] ??= ['value' => $value, 'requests' => 0, 'bytesOut' => 0];+ $this->breakdowns[$dimension][$value]['requests'] += (int) ($data['requests'] ?? 0);+ $this->breakdowns[$dimension][$value]['bytesOut'] += (int) ($data['bytesOut'] ?? 0);+ }+ }++ /**+ * Sum status series by bucket. Uniques are summed (approximate); p95 takes the worst bucket.+ *+ * @param iterable<int, mixed> $buckets+ */+ public function addSeries(iterable $buckets): void+ {+ foreach ($buckets as $bucket) {+ $data = $bucket->toArray();+ $ts = (int) ($data['bucket'] ?? 0);++ $this->series[$ts] ??= ['bucket' => $ts, 's2xx' => 0, 's3xx' => 0, 's4xx' => 0, 's5xx' => 0, 'requests' => 0, 'bytesIn' => 0, 'bytesOut' => 0, 'uniqueVisitors' => 0, 'p95' => 0.0];+ foreach (['s2xx', 's3xx', 's4xx', 's5xx', 'requests', 'bytesIn', 'bytesOut', 'uniqueVisitors'] as $field) {+ $this->series[$ts][$field] += (int) ($data[$field] ?? 0);+ }+ $this->series[$ts]['p95'] = max($this->series[$ts]['p95'], (float) ($data['p95'] ?? 0));+ }+ }++ public function hasOverview(): bool+ {+ return $this->overviews !== [];+ }++ /**+ * @return array{overview: TrafficOverviewData, latencyApproximate: bool, uniquesApproximate: bool}+ */+ public function overview(): array+ {+ return self::sumOverviews($this->overviews);+ }++ /**+ * @return array<int, array{path: string, domain: ?string, requests: int, bytesOut: int, s4xx: int, s5xx: int, p95: float}>+ */+ public function topPaths(int $limit = 50): array+ {+ return self::topByRequests(array_values($this->paths), $limit);+ }++ /**+ * @return array<string, array<int, array{value: string, requests: int, bytesOut: int}>>+ */+ public function breakdowns(int $limit = 50): array+ {+ $result = [];+ foreach ($this->dimensions as $dimension) {+ $rows = self::topByRequests(array_values($this->breakdowns[$dimension] ?? []), PHP_INT_MAX);+ if ($dimension === 'referer') {+ $rows = groupRefererBreakdownRows($rows);+ }+ $result[$dimension] = array_slice($rows, 0, $limit);+ }++ return $result;+ }++ /**+ * @return array<int, array<string, int|float>>+ */+ public function series(): array+ {+ $series = $this->series;+ ksort($series);++ return array_values($series);+ }++ public function attribution(): ?string+ {+ return $this->attribution;+ }++ /**+ * @template T of array{requests: int}+ *+ * @param array<int, T> $rows+ * @return array<int, T>+ */+ public static function topByRequests(array $rows, int $limit = 50): array+ {+ usort($rows, fn ($a, $b) => $b['requests'] <=> $a['requests']);++ return array_slice($rows, 0, $limit);+ }}diff --git a/app/Services/TrafficResource.php b/app/Services/TrafficResource.phpnew file mode 100644index 0000000000..e89189c95a--- /dev/null+++ b/app/Services/TrafficResource.php@@ -0,0 +1,189 @@+<?php++namespace App\Services;++use App\Models\Application;+use App\Models\ApplicationPreview;+use App\Models\Server;+use App\Models\Service;+use App\Models\ServiceApplication;++/**+ * A team resource that Sentinel records traffic for: an Application or a Service stack.+ * Gives the traffic views one way to read its name, server, analytics link, and the+ * domain behind each of its Sentinel keys (see SentinelTrafficClient::keyBelongsTo()).+ */+final class TrafficResource+{+ public function __construct(public readonly Application|Service $model) {}++ public static function for(Application|Service $model): self+ {+ return new self($model);+ }++ public function uuid(): string+ {+ return (string) $this->model->uuid;+ }++ public function name(): string+ {+ return (string) $this->model->name;+ }++ public function isService(): bool+ {+ return $this->model instanceof Service;+ }++ public function server(): ?Server+ {+ if ($this->model instanceof Service) {+ return $this->model->server ?? $this->model->destination?->server;+ }++ return $this->model->destination?->server;+ }++ public function projectName(): string+ {+ return (string) (data_get($this->model, 'environment.project.name') ?: 'Ungrouped');+ }++ public function analyticsLink(): ?string+ {+ $projectUuid = data_get($this->model, 'environment.project.uuid');+ $environmentUuid = data_get($this->model, 'environment.uuid');+ if (! $projectUuid || ! $environmentUuid) {+ return null;+ }++ if ($this->model instanceof Service) {+ return route('project.service.analytics', [+ 'project_uuid' => $projectUuid,+ 'environment_uuid' => $environmentUuid,+ 'service_uuid' => $this->model->uuid,+ ]);+ }++ return route('project.application.analytics', [+ 'project_uuid' => $projectUuid,+ 'environment_uuid' => $environmentUuid,+ 'application_uuid' => $this->model->uuid,+ ]);+ }++ /**+ * First configured domain host of the resource, or null when it has none.+ */+ public function primaryDomain(): ?string+ {+ if ($this->model instanceof Service) {+ foreach ($this->model->applications as $serviceApplication) {+ if ($host = self::firstHost($serviceApplication->fqdn)) {+ return $host;+ }+ }++ return null;+ }++ if ($host = self::firstHost($this->model->fqdn)) {+ return $host;+ }++ foreach (self::composeDomains($this->model->docker_compose_domains) as $domain) {+ if ($host = self::firstHost($domain)) {+ return $host;+ }+ }++ return null;+ }++ /**+ * Domain host served under one of this resource's Sentinel keys: the compose service+ * (`{uuid}-{service}`), the preview (`{uuid}-pr-{pr}` or `{uuid}-{pr}`), or the preview compose service+ * (`{uuid}-{pr}-{service}`). Falls back to the primary domain.+ */+ public function domainForKey(string $key): ?string+ {+ if (! SentinelTrafficClient::keyBelongsTo($key, $this->uuid()) || $key === $this->uuid()) {+ return $this->primaryDomain();+ }++ $suffix = substr($key, strlen($this->uuid()) + 1);++ if ($this->model instanceof Service) {+ $serviceApplication = $this->model->applications+ ->first(fn (ServiceApplication $app) => self::matchesServiceName($suffix, (string) $app->name));++ return self::firstHost($serviceApplication?->fqdn) ?? $this->primaryDomain();+ }++ // Normal previews use `{uuid}-pr-{id}`, compose previews `{uuid}-{id}` and `{uuid}-{id}-{service}`.+ if (preg_match('/\A(?:pr-)?(\d+)(?:-(.+))?\z/', $suffix, $matches)) {+ /** @var ApplicationPreview|null $preview */+ $preview = $this->model->previews()->where('pull_request_id', (int) $matches[1])->first();+ if ($preview) {+ $host = isset($matches[2])+ ? self::composeDomainFor($preview->docker_compose_domains, $matches[2])+ : self::firstHost($preview->fqdn);+ if ($host) {+ return $host;+ }+ }+ }++ return self::composeDomainFor($this->model->docker_compose_domains, $suffix) ?? $this->primaryDomain();+ }++ /**+ * Key suffixes use the label-safe compose service name; accept the raw name too.+ */+ private static function matchesServiceName(string $suffix, string $serviceName): bool+ {+ return $serviceName !== ''+ && ($suffix === $serviceName || $suffix === traefikSafeServiceNameSegment($serviceName));+ }++ private static function composeDomainFor(?string $composeDomains, string $suffix): ?string+ {+ foreach (self::composeDomains($composeDomains) as $serviceName => $domain) {+ if (self::matchesServiceName($suffix, (string) $serviceName)) {+ return self::firstHost($domain);+ }+ }++ return null;+ }++ /**+ * @return array<string, string> compose service name => comma-separated domains+ */+ private static function composeDomains(?string $json): array+ {+ $decoded = json_decode($json ?: '[]', true);+ if (! is_array($decoded)) {+ return [];+ }++ $domains = [];+ foreach ($decoded as $serviceName => $config) {+ $domain = data_get($config, 'domain');+ if (is_string($domain) && trim($domain) !== '') {+ $domains[(string) $serviceName] = $domain;+ }+ }++ return $domains;+ }++ private static function firstHost(?string $domains): ?string+ {+ $first = trim((string) str($domains ?? '')->explode(',')->first());++ return $first !== '' ? (parse_url($first, PHP_URL_HOST) ?: null) : null;+ }+}diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.phpindex 898e6d7412..746d2c2691 100644--- a/bootstrap/helpers/docker.php+++ b/bootstrap/helpers/docker.php@@ -43,6 +43,19 @@ function traefikSafeServiceNameSegment(string $serviceName): stringreturn $normalized.'-'.traefikServiceNameHash($serviceName);}+/**+ * Key that Sentinel uses to group Caddy access-log lines. It is the same key that Sentinel gets+ * from the Traefik router name (`https-{i}-{KEY}@docker`), so both proxies report a resource the same way.+ * Only [A-Za-z0-9-] can occur, so the value is safe in a Docker label and in a Caddyfile.+ */+function caddyTrafficAppKey(string $uuid, ?string $serviceName = null): string+{+ // Same truthiness check as fqdnLabelsForTraefik(), so the keys stay equal.+ $key = $serviceName ? $uuid.'-'.traefikSafeServiceNameSegment($serviceName) : $uuid;++ return (string) preg_replace('/[^A-Za-z0-9-]+/', '-', $key);+}+function getCurrentApplicationContainerStatus(Server $server, int $id, ?int $pullRequestId = null, ?bool $includePullrequests = false): Collection{$containers = collect([]);@@ -559,6 +572,8 @@ function fqdnLabelsForCaddy(string $network, string $uuid, Collection $domains,$hashedPassword = password_hash($http_basic_auth_password, PASSWORD_BCRYPT, ['cost' => 10]);}+ $trafficAppKey = caddyTrafficAppKey($uuid, $service_name);+foreach ($domains as $loop => $domain) {$url = Url::fromString($domain);$host = $url->getHost();@@ -623,7 +638,7 @@ function fqdnLabelsForCaddy(string $network, string $uuid, Collection $domains,$labels->push("caddy_{$loop}.log.format=json");// Only Caddy 2.8+ knows log_append; see Server::caddySupportsLogAppend().if ($supports_log_append) {- $labels->push("caddy_{$loop}.log_append=coolify_app_id {$uuid}");+ $labels->push("caddy_{$loop}.log_append=coolify_app_id {$trafficAppKey}");}}}diff --git a/bootstrap/helpers/parsers.php b/bootstrap/helpers/parsers.phpindex 4c5dff37cc..c0fc2d731b 100644--- a/bootstrap/helpers/parsers.php+++ b/bootstrap/helpers/parsers.php@@ -1510,6 +1510,8 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int? ($previewForPorts?->domain_port_overrides ?? []): ($originalResource->domain_port_overrides ?? []);$onlyPort = firstDockerComposeServicePort($service);+ $isTrafficAnalyticsEnabled = (bool) $server?->isTrafficAnalyticsEnabled();+ $supportsLogAppend = (bool) $server?->caddySupportsLogAppend();if (! $use_network_mode && (! $shouldGenerateLabelsExactly || $server->proxyType() === ProxyTypes::TRAEFIK->value)) {$serviceLabels = addTraefikDockerNetworkLabel($serviceLabels, $baseNetwork->first());}@@ -1547,6 +1549,8 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?intnoindex_domains: $noindexDomains,redirect_direction: $redirectDirection,domainPortOverrides: $domainPortOverrides,+ is_traffic_analytics_enabled: $isTrafficAnalyticsEnabled,+ supports_log_append: $supportsLogAppend,));break;}@@ -1580,6 +1584,8 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?intnoindex_domains: $noindexDomains,redirect_direction: $redirectDirection,domainPortOverrides: $domainPortOverrides,+ is_traffic_analytics_enabled: $isTrafficAnalyticsEnabled,+ supports_log_append: $supportsLogAppend,));}}@@ -2766,6 +2772,8 @@ function serviceParser(Service $resource): Collection$onlyPort = $originalResource instanceof ServiceApplication? $originalResource->getRequiredPort(): $predefinedPort;+ $isTrafficAnalyticsEnabled = (bool) $server?->isTrafficAnalyticsEnabled();+ $supportsLogAppend = (bool) $server?->caddySupportsLogAppend();if (! $use_network_mode && (! $shouldGenerateLabelsExactly || $server->proxyType() === ProxyTypes::TRAEFIK->value)) {$serviceLabels = addTraefikDockerNetworkLabel($serviceLabels, $baseNetwork->first());}@@ -2802,7 +2810,9 @@ function serviceParser(Service $resource): CollectionpredefinedPort: $onlyPort,domainPortOverrides: $originalResource->domain_port_overrides ?? [],noindex_domains: $noindexDomains,- redirect_direction: $redirectDirection+ redirect_direction: $redirectDirection,+ is_traffic_analytics_enabled: $isTrafficAnalyticsEnabled,+ supports_log_append: $supportsLogAppend,));break;}@@ -2835,7 +2845,9 @@ function serviceParser(Service $resource): CollectionpredefinedPort: $onlyPort,domainPortOverrides: $originalResource->domain_port_overrides ?? [],noindex_domains: $noindexDomains,- redirect_direction: $redirectDirection+ redirect_direction: $redirectDirection,+ is_traffic_analytics_enabled: $isTrafficAnalyticsEnabled,+ supports_log_append: $supportsLogAppend,));}}diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.phpindex 9eee10d6ec..228427a48f 100644--- a/bootstrap/helpers/shared.php+++ b/bootstrap/helpers/shared.php@@ -3190,6 +3190,8 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = falnoindex_domains: $noindexDomains,redirect_direction: $redirectDirection,domainPortOverrides: $domainPortOverrides,+ is_traffic_analytics_enabled: $resource->server?->isTrafficAnalyticsEnabled() ?? false,+ supports_log_append: $resource->server?->caddySupportsLogAppend() ?? false,));break;}@@ -3223,6 +3225,8 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = falnoindex_domains: $noindexDomains,redirect_direction: $redirectDirection,domainPortOverrides: $domainPortOverrides,+ is_traffic_analytics_enabled: $resource->server?->isTrafficAnalyticsEnabled() ?? false,+ supports_log_append: $resource->server?->caddySupportsLogAppend() ?? false,));}}@@ -3994,6 +3998,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = faldomains: $fqdns,serviceLabels: $serviceLabels,image: data_get($service, 'image'),+ service_name: $serviceName,is_force_https_enabled: $resource->isForceHttpsEnabled(),is_gzip_enabled: $resource->isGzipEnabled(),is_stripprefix_enabled: $resource->isStripprefixEnabled(),@@ -4001,6 +4006,8 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = falnoindex_domains: $noindexDomains,redirect_direction: $redirectDirection,domainPortOverrides: $domainPortOverrides,+ is_traffic_analytics_enabled: $server?->isTrafficAnalyticsEnabled() ?? false,+ supports_log_append: $server?->caddySupportsLogAppend() ?? false,));break;@@ -4029,6 +4036,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = faldomains: $fqdns,serviceLabels: $serviceLabels,image: data_get($service, 'image'),+ service_name: $serviceName,is_force_https_enabled: $resource->isForceHttpsEnabled(),is_gzip_enabled: $resource->isGzipEnabled(),is_stripprefix_enabled: $resource->isStripprefixEnabled(),@@ -4036,6 +4044,8 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = falnoindex_domains: $noindexDomains,redirect_direction: $redirectDirection,domainPortOverrides: $domainPortOverrides,+ is_traffic_analytics_enabled: $server?->isTrafficAnalyticsEnabled() ?? false,+ supports_log_append: $server?->caddySupportsLogAppend() ?? false,));}diff --git a/resources/views/components/service/configuration-sidebar.blade.php b/resources/views/components/service/configuration-sidebar.blade.phpindex 318b04b8d2..208414e240 100644--- a/resources/views/components/service/configuration-sidebar.blade.php+++ b/resources/views/components/service/configuration-sidebar.blade.php@@ -16,6 +16,7 @@['label' => 'Import Backup', 'route' => 'project.service.import-backup', 'icon' => 'upload', 'navigate' => false],['label' => 'Runtime Logs', 'route' => 'project.service.logs', 'icon' => 'unordered-list', 'navigate' => false],['label' => 'Terminal', 'route' => 'project.service.command', 'icon' => 'browser-terminal', 'navigate' => false, 'visible' => auth()->user()?->can('canAccessTerminal')],+ ['label' => 'Analytics', 'route' => 'project.service.analytics', 'icon' => 'analytics'],['label' => 'Scheduled Tasks', 'route' => 'project.service.scheduled-tasks.show', 'icon' => 'calendar'],['label' => 'Webhooks', 'route' => 'project.service.webhooks', 'icon' => 'notifications'],['label' => 'Resource Operations', 'route' => 'project.service.resource-operations', 'icon' => 'server-update'],@@ -35,7 +36,7 @@$menuGroups = ['Settings' => ['General', 'Domains', 'Environment Variables', 'Persistent Storage'],- 'Observe & troubleshoot' => ['Runtime Logs', 'Terminal'],+ 'Observe & troubleshoot' => ['Runtime Logs', 'Terminal', 'Analytics'],'Automation' => ['Scheduled Tasks', 'Webhooks', 'Backups', 'Import Backup'],'Operations' => ['Resource Operations', 'Tags', 'Danger Zone'],];diff --git a/resources/views/livewire/analytics.blade.php b/resources/views/livewire/analytics.blade.phpindex 3ae6f2f137..1e984ac775 100644--- a/resources/views/livewire/analytics.blade.php+++ b/resources/views/livewire/analytics.blade.php@@ -16,7 +16,7 @@ $serverListboxOptions = array_merge(collect($serverOptions)->map(fn ($name, $uuid) => ['value' => $uuid, 'label' => $name])->values()->all(),);$appListboxOptions = array_merge(- [['value' => '', 'label' => 'All applications']],+ [['value' => '', 'label' => 'All resources']],$appGroupedOptions,);?>@@ -54,7 +54,7 @@ $appListboxOptions = array_merge(newly-scoped options (and reset value) instead of showing stale Alpine state. --}}<div class="relative w-full transition-opacity sm:w-52" wire:key="app-filter-{{ $serverUuid }}"wire:loading.class="pointer-events-none opacity-60" wire:target="appUuid">- <x-forms.listbox id="appUuid" live :options="$appListboxOptions" placeholder="All applications" />+ <x-forms.listbox id="appUuid" live :options="$appListboxOptions" placeholder="All resources" /><div class="absolute inset-0 hidden items-center justify-center rounded-lg bg-white/70 dark:bg-base/70"wire:loading.flex wire:target="appUuid"><x-loading compact aria-label="Loading analytics" />@@ -164,7 +164,7 @@ $appListboxOptions = array_merge(<span class="flex items-center text-[11px] font-medium tracking-wide text-neutral-500 uppercase dark:text-fg-dim">Unique visitors@if ($uniquesApproximate)- {!! $approxBadge('Summed across servers; visitors seen on multiple servers may be double-counted.') !!}+ {!! $approxBadge('Summed across servers or services; visitors seen on more than one may be double-counted.') !!}@endif</span><span class="mt-1 text-xl font-semibold text-black tabular-nums dark:text-fg">{{ number_format($overview['uniqueVisitors'] ?? 0) }}</span>@@ -208,7 +208,7 @@ $appListboxOptions = array_merge(<span class="flex items-center text-[11px] font-medium tracking-wide text-neutral-500 uppercase dark:text-fg-dim">p95 latency@if ($latencyApproximate)- {!! $approxBadge('Highest p95 latency across servers; not a true cross-server percentile.') !!}+ {!! $approxBadge('Highest p95 latency across servers or services; not a true merged percentile.') !!}@endif</span><span class="mt-1 text-xl font-semibold text-black tabular-nums dark:text-fg">{{ number_format($overview['latencyP95'] ?? 0, 1) }} ms</span>@@ -247,7 +247,7 @@ $appListboxOptions = array_merge(</x-application.settings-section><x-application.settings-section id="analytics-apps-section" title="Top applications"- helper="Applications ranked by request volume. Open one for its analytics." flush>+ helper="Applications and services ranked by request volume. Open one for its analytics." flush>@if (empty($topApps))<x-empty size="sm" title="No application data"description="No per-application requests were recorded for the selected range."@@ -269,6 +269,9 @@ $appListboxOptions = array_merge(])><span class="flex min-w-0 flex-1 items-baseline gap-1.5"><span class="truncate text-[12px] text-black dark:text-fg">{{ $row['name'] }}</span>+ @if (! empty($row['isService']))+ <span class="shrink-0 text-[10px] font-medium tracking-wide text-neutral-400 uppercase dark:text-fg-faint">Service</span>+ @endif@if (! empty($row['domain']))<span class="hidden truncate text-[11px] text-neutral-400 sm:inline dark:text-fg-faint">{{ $row['domain'] }}</span>@endifdiff --git a/resources/views/livewire/project/application/analytics.blade.php b/resources/views/livewire/project/application/analytics.blade.phpindex 5a0fa32a87..692d3fda82 100644--- a/resources/views/livewire/project/application/analytics.blade.php+++ b/resources/views/livewire/project/application/analytics.blade.php@@ -8,7 +8,9 @@ $dimensionLabels = ['browser' => 'Browsers','os' => 'Operating systems',];-$analyticsServerUuid = $application->destination?->server?->uuid;+// Shared by the application and service analytics tabs. A resource with several Sentinel+// keys (compose services, previews) is merged, so latency and uniques become approximate.+$approxBadge = fn (string $tooltip) => '<span title="'.e($tooltip).'" class="ml-1.5 inline-flex items-center rounded-full bg-amber-100 px-1.5 py-0.5 text-[9px] font-medium tracking-wide text-amber-700 uppercase dark:bg-amber-500/10 dark:text-amber-400">~ approximate</span>';?><div class="flex flex-col gap-6">@if (! $enabled)@@ -76,7 +78,12 @@ $analyticsServerUuid = $application->destination?->server?->uuid;</div></div><div class="flex flex-col bg-[var(--coollabs-base)] px-4 py-3">- <span class="text-[11px] font-medium tracking-wide text-neutral-500 uppercase dark:text-fg-dim">Unique visitors</span>+ <span class="flex items-center text-[11px] font-medium tracking-wide text-neutral-500 uppercase dark:text-fg-dim">+ Unique visitors+ @if ($uniquesApproximate)+ {!! $approxBadge('Summed across services; visitors of more than one service may be double-counted.') !!}+ @endif+ </span><span class="mt-1 text-xl font-semibold text-black tabular-nums dark:text-fg">{{ number_format($overview['uniqueVisitors'] ?? 0) }}</span><div class="mt-auto pt-3">@include('livewire.traffic._sparkline', [@@ -115,7 +122,12 @@ $analyticsServerUuid = $application->destination?->server?->uuid;</div></div><div class="col-span-2 flex flex-col bg-[var(--coollabs-base)] px-4 py-3 sm:col-span-1">- <span class="text-[11px] font-medium tracking-wide text-neutral-500 uppercase dark:text-fg-dim">p95 latency</span>+ <span class="flex items-center text-[11px] font-medium tracking-wide text-neutral-500 uppercase dark:text-fg-dim">+ p95 latency+ @if ($latencyApproximate)+ {!! $approxBadge('Highest p95 latency across services; not a true merged percentile.') !!}+ @endif+ </span><span class="mt-1 text-xl font-semibold text-black tabular-nums dark:text-fg">{{ number_format($overview['latencyP95'] ?? 0, 1) }} ms</span><div class="mt-auto pt-3">@include('livewire.traffic._sparkline', [diff --git a/resources/views/livewire/project/service/configuration.blade.php b/resources/views/livewire/project/service/configuration.blade.phpindex fe2799e3bb..51dd41a9f7 100644--- a/resources/views/livewire/project/service/configuration.blade.php+++ b/resources/views/livewire/project/service/configuration.blade.php@@ -21,6 +21,7 @@['label' => 'Import Backup', 'route' => 'project.service.import-backup', 'icon' => 'upload', 'navigate' => false],['label' => 'Runtime Logs', 'route' => 'project.service.logs', 'icon' => 'unordered-list', 'navigate' => false],['label' => 'Terminal', 'route' => 'project.service.command', 'icon' => 'browser-terminal', 'navigate' => false, 'visible' => auth()->user()?->can('canAccessTerminal')],+ ['label' => 'Analytics', 'route' => 'project.service.analytics', 'icon' => 'analytics'],['label' => 'Scheduled Tasks', 'route' => 'project.service.scheduled-tasks.show', 'icon' => 'calendar'],['label' => 'Webhooks', 'route' => 'project.service.webhooks', 'icon' => 'notifications'],['label' => 'Resource Operations', 'route' => 'project.service.resource-operations', 'icon' => 'server-update'],@@ -35,7 +36,7 @@$menuGroups = ['Settings' => ['General', 'Domains', 'Environment Variables', 'Persistent Storage'],- 'Observe & troubleshoot' => ['Runtime Logs', 'Terminal'],+ 'Observe & troubleshoot' => ['Runtime Logs', 'Terminal', 'Analytics'],'Automation' => ['Scheduled Tasks', 'Webhooks', 'Backups', 'Import Backup'],'Operations' => ['Resource Operations', 'Tags', 'Danger Zone'],];@@ -190,6 +191,9 @@@endforeach</div></div>+ @elseif ($currentRoute === 'project.service.analytics')+ <livewire:project.service.analytics :service="$service"+ :lazy="(bool) ($service->server ?? $service->destination?->server)?->isTrafficAnalyticsEnabled()" />@elseif ($currentRoute === 'project.service.domains')<livewire:project.service.domains :service="$service" />@elseif ($currentRoute === 'project.service.environment-variables')diff --git a/resources/views/livewire/project/service/heading.blade.php b/resources/views/livewire/project/service/heading.blade.phpindex 480ad3b539..dbaac3d51a 100644--- a/resources/views/livewire/project/service/heading.blade.php+++ b/resources/views/livewire/project/service/heading.blade.php@@ -11,6 +11,7 @@|| request()->routeIs('project.service.scheduled-tasks*')|| request()->routeIs('project.service.webhooks')|| request()->routeIs('project.service.resource-operations')+ || request()->routeIs('project.service.analytics')|| request()->routeIs('project.service.tags')|| request()->routeIs('project.service.danger')|| request()->routeIs('project.service.index*')diff --git a/resources/views/livewire/server/traffic-analytics-settings.blade.php b/resources/views/livewire/server/traffic-analytics-settings.blade.phpindex 83ab167ec5..0cc046ba60 100644--- a/resources/views/livewire/server/traffic-analytics-settings.blade.php+++ b/resources/views/livewire/server/traffic-analytics-settings.blade.php@@ -27,6 +27,9 @@</x-slot:actions>@if ($isTrafficAnalyticsEnabled)+ @if ($caddyRedeployNote)+ <x-callout type="info" title="Redeploy to start logging">{{ $caddyRedeployNote }}</x-callout>+ @endif<div class="grid gap-4 lg:grid-cols-2"><x-forms.input canGate="update" :canResource="$server" type="number" min="1"id="trafficTopn" label="Top-N cap" requireddiff --git a/routes/web.php b/routes/web.phpindex 712c415b79..a3ca3630f1 100644--- a/routes/web.php+++ b/routes/web.php@@ -339,6 +339,7 @@ Route::middleware(['auth', 'verified'])->group(function () {Route::get('/', ServiceConfiguration::class)->name('project.service.configuration');Route::get('/domains', ServiceConfiguration::class)->name('project.service.domains');Route::get('/logs', Logs::class)->name('project.service.logs');+ Route::get('/analytics', ServiceConfiguration::class)->name('project.service.analytics');Route::get('/environment-variables', ServiceConfiguration::class)->name('project.service.environment-variables');Route::get('/storages', ServiceConfiguration::class)->name('project.service.storages');Route::get('/import-backup', ServiceImportBackup::class)->name('project.service.import-backup')->middleware('can.update.resource');diff --git a/tests/Feature/ServiceDatabaseVerticalNavigationTest.php b/tests/Feature/ServiceDatabaseVerticalNavigationTest.phpindex 0afd70dffc..21c0801e56 100644--- a/tests/Feature/ServiceDatabaseVerticalNavigationTest.php+++ b/tests/Feature/ServiceDatabaseVerticalNavigationTest.php@@ -59,7 +59,7 @@ it('groups database and service navigation by user workflow', function () {foreach ($serviceSidebars as $serviceSidebar) {expect($serviceSidebar)->toContain("'Settings' => ['General', 'Domains', 'Environment Variables', 'Persistent Storage']")- ->toContain("'Observe & troubleshoot' => ['Runtime Logs', 'Terminal']")+ ->toContain("'Observe & troubleshoot' => ['Runtime Logs', 'Terminal', 'Analytics']")->toContain("'Automation' => ['Scheduled Tasks', 'Webhooks', 'Backups', 'Import Backup']")->toContain("'Operations' => ['Resource Operations', 'Tags', 'Danger Zone']");}@@ -347,6 +347,6 @@ it('shows the service sidebar on runtime logs and terminal pages', function () {->toContain("in_array(\$type, ['application', 'database', 'service', 'server'], true)")->toContain('<x-service.configuration-sidebar :service="$resource" current-route="project.service.command"')->and($sidebar)- ->toContain("'Observe & troubleshoot' => ['Runtime Logs', 'Terminal']")+ ->toContain("'Observe & troubleshoot' => ['Runtime Logs', 'Terminal', 'Analytics']")->toContain("'Operations' => ['Resource Operations', 'Tags', 'Danger Zone']");});diff --git a/tests/Feature/TrafficAnalytics/CaddyComposeTrafficLabelsTest.php b/tests/Feature/TrafficAnalytics/CaddyComposeTrafficLabelsTest.phpnew file mode 100644index 0000000000..4ed808a97a--- /dev/null+++ b/tests/Feature/TrafficAnalytics/CaddyComposeTrafficLabelsTest.php@@ -0,0 +1,209 @@+<?php++use App\Enums\ProxyTypes;+use App\Models\Application;+use App\Models\ApplicationPreview;+use App\Models\Environment;+use App\Models\PrivateKey;+use App\Models\Project;+use App\Models\Server;+use App\Models\Service;+use App\Models\ServiceApplication;+use App\Models\StandaloneDocker;+use App\Models\Team;+use Illuminate\Foundation\Testing\RefreshDatabase;+use Illuminate\Support\Collection;+use Illuminate\Support\Facades\Bus;+use Illuminate\Support\Facades\Process;++uses(RefreshDatabase::class);++beforeEach(function () {+ Bus::fake();+ Process::fake();+ config(['constants.ssh.mux_enabled' => false]);+ Server::flushIdentityMap();++ $this->team = Team::factory()->create();+ $this->environment = Environment::factory()->create([+ 'project_id' => Project::factory()->create(['team_id' => $this->team->id])->id,+ ]);+});++afterEach(fn () => Server::flushIdentityMap());++/**+ * A server that runs a Caddy image with `log_append` support (caddy-docker-proxy 2.9+).+ */+function caddyComposeTrafficServer(object $test, bool $analyticsEnabled, bool $exactLabels = false): Server+{+ $server = Server::factory()->create([+ 'team_id' => $test->team->id,+ 'private_key_id' => PrivateKey::factory()->create(['team_id' => $test->team->id])->id,+ 'proxy' => [+ 'type' => ProxyTypes::CADDY->value,+ 'status' => 'running',+ 'last_saved_settings' => 'applied',+ 'last_applied_settings' => 'applied',+ 'last_saved_proxy_configuration' => "services:\n caddy:\n image: 'lucaslorentz/caddy-docker-proxy:2.11-alpine'\n",+ ],+ ]);+ $server->settings->update([+ 'is_traffic_analytics_enabled' => $analyticsEnabled,+ 'generate_exact_labels' => $exactLabels,+ ]);+ Server::flushIdentityMap();++ return $server->fresh();+}++function caddyComposeTrafficApplication(object $test, Server $server): Application+{+ $destination = StandaloneDocker::query()->where('server_id', $server->id)->firstOrFail();++ return Application::factory()->create([+ 'environment_id' => $test->environment->id,+ 'destination_id' => $destination->id,+ 'destination_type' => $destination->getMorphClass(),+ 'build_pack' => 'dockercompose',+ 'docker_compose_raw' => "services:\n web.app:\n image: nginx:alpine\n",+ 'docker_compose_domains' => json_encode([+ 'web.app' => ['domain' => 'https://app.example.com'],+ ]),+ ]);+}++function caddyComposeTrafficService(object $test, Server $server): Service+{+ $destination = StandaloneDocker::query()->where('server_id', $server->id)->firstOrFail();+ $service = Service::factory()->create([+ 'environment_id' => $test->environment->id,+ 'server_id' => $server->id,+ 'destination_id' => $destination->id,+ 'destination_type' => $destination->getMorphClass(),+ 'docker_compose_raw' => "services:\n web_app:\n image: nginx:alpine\n",+ ]);+ ServiceApplication::create([+ 'name' => 'web_app',+ 'service_id' => $service->id,+ 'fqdn' => 'https://service.example.com',+ ]);++ return $service->fresh();+}++function composeServiceLabels(Collection $compose, string $serviceName): Collection+{+ // Service names can contain dots, so data_get() cannot be used for the service key.+ return collect(data_get($compose, 'services')[$serviceName]['labels'] ?? [])->values();+}++it('logs compose application traffic on Caddy with the same key as the Traefik router', function (bool $exactLabels) {+ $server = caddyComposeTrafficServer($this, analyticsEnabled: true, exactLabels: $exactLabels);+ $application = caddyComposeTrafficApplication($this, $server);++ $labels = composeServiceLabels(applicationParser($application), 'web.app');+ $key = $application->uuid.'-'.traefikSafeServiceNameSegment('web.app');++ expect($labels->all())+ ->toContain('caddy_0.log.output=file /traffic/access.log')+ ->toContain("caddy_0.log_append=coolify_app_id {$key}");+ if (! $exactLabels) {+ expect($labels->all())->toContain("traefik.http.routers.https-0-{$key}.entryPoints=https");+ }+})->with([+ 'exact Caddy labels' => true,+ 'Traefik and Caddy labels' => false,+]);++it('uses the preview uuid in the Caddy traffic key of a compose preview', function () {+ $server = caddyComposeTrafficServer($this, analyticsEnabled: true);+ $application = caddyComposeTrafficApplication($this, $server);+ $preview = ApplicationPreview::create([+ 'application_id' => $application->id,+ 'pull_request_id' => 42,+ 'pull_request_html_url' => 'https://github.com/example/repo/pull/42',+ 'docker_compose_domains' => $application->docker_compose_domains,+ ]);++ $labels = composeServiceLabels(applicationParser($application, 42, $preview->id), 'web.app-pr-42');+ $key = "{$application->uuid}-42-".traefikSafeServiceNameSegment('web.app');++ expect($labels->all())+ ->toContain("caddy_0.log_append=coolify_app_id {$key}")+ ->toContain("traefik.http.routers.https-0-{$key}.entryPoints=https");+});++it('logs service traffic on Caddy with the same key as the Traefik router', function (bool $exactLabels) {+ $server = caddyComposeTrafficServer($this, analyticsEnabled: true, exactLabels: $exactLabels);+ $service = caddyComposeTrafficService($this, $server);++ $labels = composeServiceLabels(serviceParser($service), 'web_app');+ $key = $service->uuid.'-'.traefikSafeServiceNameSegment('web_app');++ expect($labels->all())+ ->toContain('caddy_0.log.output=file /traffic/access.log')+ ->toContain("caddy_0.log_append=coolify_app_id {$key}");+ if (! $exactLabels) {+ expect($labels->all())->toContain("traefik.http.routers.https-0-{$key}.entryPoints=https");+ }+})->with([+ 'exact Caddy labels' => true,+ 'Traefik and Caddy labels' => false,+]);++it('logs legacy parser service traffic on Caddy with the same key as the Traefik router', function () {+ $server = caddyComposeTrafficServer($this, analyticsEnabled: true);+ $service = caddyComposeTrafficService($this, $server);+ $service->update(['compose_parsing_version' => '2']);++ $compose = parseDockerComposeFile($service->fresh());+ $labels = composeServiceLabels($compose, 'web_app');+ $key = $service->uuid.'-'.traefikSafeServiceNameSegment('web_app');++ expect($labels->all())+ ->toContain('caddy_0.log.output=file /traffic/access.log')+ ->toContain("caddy_0.log_append=coolify_app_id {$key}")+ ->toContain("traefik.http.routers.https-0-{$key}.entryPoints=https");+});++it('logs legacy parser compose application traffic on Caddy with the service key', function () {+ $server = caddyComposeTrafficServer($this, analyticsEnabled: true);+ $application = caddyComposeTrafficApplication($this, $server);+ $application->update(['compose_parsing_version' => '2']);++ $compose = parseDockerComposeFile($application->fresh());+ $labels = composeServiceLabels($compose, 'web.app');+ $key = $application->uuid.'-'.traefikSafeServiceNameSegment('web.app');++ expect($labels->all())+ ->toContain('caddy_0.log.output=file /traffic/access.log')+ ->toContain("caddy_0.log_append=coolify_app_id {$key}");+});++it('adds no Caddy traffic labels to compose resources when analytics is off', function () {+ $server = caddyComposeTrafficServer($this, analyticsEnabled: false);+ $application = caddyComposeTrafficApplication($this, $server);+ $service = caddyComposeTrafficService($this, $server);++ $labels = composeServiceLabels(applicationParser($application), 'web.app')+ ->merge(composeServiceLabels(serviceParser($service), 'web_app'));++ expect($labels->filter(fn (string $label) => str_contains($label, '.log')))->toBeEmpty();+});++it('keeps the application uuid as the Caddy traffic key of a normal application', function () {+ $server = caddyComposeTrafficServer($this, analyticsEnabled: true);+ $destination = StandaloneDocker::query()->where('server_id', $server->id)->firstOrFail();+ $application = Application::factory()->create([+ 'environment_id' => $this->environment->id,+ 'destination_id' => $destination->id,+ 'destination_type' => $destination->getMorphClass(),+ 'fqdn' => 'https://normal.example.com',+ ]);++ $labels = collect(generateLabelsApplication($application->fresh()));++ expect($labels->filter(fn (string $label) => str_contains($label, 'log_append'))->values()->all())+ ->toBe(["caddy_0.log_append=coolify_app_id {$application->uuid}"]);+});diff --git a/tests/Feature/TrafficAnalytics/CaddyTrafficLabelsTest.php b/tests/Feature/TrafficAnalytics/CaddyTrafficLabelsTest.phpindex 30c350e46f..0f18877183 100644--- a/tests/Feature/TrafficAnalytics/CaddyTrafficLabelsTest.php+++ b/tests/Feature/TrafficAnalytics/CaddyTrafficLabelsTest.php@@ -31,3 +31,19 @@ it('omits lumberjack roll directives when disabled', function () {$labels = fqdnLabelsForCaddy('coolify', 'app-uuid', collect(['https://example.com']), is_traffic_analytics_enabled: false);expect($labels->filter(fn ($l) => str_contains($l, 'roll_'))->isEmpty())->toBeTrue();});++it('keys a compose service like its Traefik router', function () {+ $labels = fqdnLabelsForCaddy('coolify', 'app-uuid', collect(['https://example.com']), service_name: 'web.app', is_traffic_analytics_enabled: true, supports_log_append: true);+ $key = 'app-uuid-'.traefikSafeServiceNameSegment('web.app');++ expect($key)->toMatch('/^[A-Za-z0-9-]+$/')+ ->and($labels->contains("caddy_0.log_append=coolify_app_id {$key}"))->toBeTrue()+ ->and($labels->contains('caddy_0.log.output=file /traffic/access.log'))->toBeTrue();+});++it('keeps only safe characters in the caddy traffic key', function () {+ $labels = fqdnLabelsForCaddy('coolify', "app uuid\n{x}", collect(['https://example.com']), is_traffic_analytics_enabled: true, supports_log_append: true);++ expect($labels->first(fn ($label) => str_contains($label, 'log_append')))+ ->toBe('caddy_0.log_append=coolify_app_id app-uuid-x-');+});diff --git a/tests/Feature/TrafficAnalytics/ChangeProxySentinelRestartTest.php b/tests/Feature/TrafficAnalytics/ChangeProxySentinelRestartTest.phpnew file mode 100644index 0000000000..592e92da51--- /dev/null+++ b/tests/Feature/TrafficAnalytics/ChangeProxySentinelRestartTest.php@@ -0,0 +1,95 @@+<?php++use App\Actions\Proxy\StartProxy;+use App\Actions\Server\StartSentinel;+use App\Enums\ProxyTypes;+use App\Models\Server;+use App\Models\User;+use Illuminate\Foundation\Testing\RefreshDatabase;+use Illuminate\Support\Facades\Queue;++uses(RefreshDatabase::class);++beforeEach(function () {+ Queue::fake();+ Server::flushIdentityMap();+ $this->team = User::factory()->create()->teams()->first();+});++afterEach(fn () => Server::flushIdentityMap());++function proxySwitchServer(object $test, string $proxyType, bool $sentinelEnabled, bool $analyticsEnabled): Server+{+ $server = Server::factory()->create(['team_id' => $test->team->id]);+ $server->proxy->set('type', $proxyType);+ $server->save();+ $server->settings->is_sentinel_enabled = $sentinelEnabled;+ $server->settings->is_traffic_analytics_enabled = $analyticsEnabled;+ $server->settings->save();+ Server::flushIdentityMap();++ return $server->fresh();+}++it('restarts Sentinel after a proxy switch when Sentinel and traffic analytics are on', function (string $from, string $to) {+ $server = proxySwitchServer($this, $from, sentinelEnabled: true, analyticsEnabled: true);++ $server->changeProxy($to, async: true);++ StartSentinel::assertPushed(1);+})->with([+ 'Traefik to Caddy' => [ProxyTypes::TRAEFIK->value, ProxyTypes::CADDY->value],+ 'Caddy to Traefik' => [ProxyTypes::CADDY->value, ProxyTypes::TRAEFIK->value],+ 'Traefik to none' => [ProxyTypes::TRAEFIK->value, ProxyTypes::NONE->value],+ 'Caddy to Nginx' => [ProxyTypes::CADDY->value, ProxyTypes::NGINX->value],+]);++it('restarts Sentinel when the Livewire switch selects the proxy synchronously', function () {+ StartProxy::shouldRun();+ $server = proxySwitchServer($this, ProxyTypes::TRAEFIK->value, sentinelEnabled: true, analyticsEnabled: true);++ $server->changeProxy(ProxyTypes::CADDY->value, async: false);++ StartSentinel::assertPushed(1);+});++it('does not restart Sentinel after a proxy switch when nothing relevant is on', function (bool $sentinelEnabled, bool $analyticsEnabled) {+ $server = proxySwitchServer($this, ProxyTypes::TRAEFIK->value, $sentinelEnabled, $analyticsEnabled);++ $server->changeProxy(ProxyTypes::CADDY->value, async: true);++ StartSentinel::assertNotPushed();+ expect((bool) $server->fresh()->settings->is_sentinel_enabled)->toBe($sentinelEnabled);+})->with([+ 'analytics off' => [true, false],+ 'Sentinel off' => [false, true],+ 'both off' => [false, false],+]);++it('does not restart Sentinel when the proxy type does not change', function () {+ $server = proxySwitchServer($this, ProxyTypes::CADDY->value, sentinelEnabled: true, analyticsEnabled: true);++ $server->changeProxy(ProxyTypes::CADDY->value, async: true);++ StartSentinel::assertNotPushed();+});++it('points Sentinel at the Caddy access log after a switch from Traefik', function () {+ $server = proxySwitchServer($this, ProxyTypes::TRAEFIK->value, sentinelEnabled: true, analyticsEnabled: true);+ expect(StartSentinel::trafficLogDirectory($server))->toBe('/data/coolify/proxy');++ $server->changeProxy(ProxyTypes::CADDY->value, async: true);+ $server = $server->fresh();++ expect(StartSentinel::trafficLogDirectory($server))->toBe('/data/coolify/proxy/caddy')+ ->and(StartSentinel::sentinelTrafficEnvironment($server)['TRAFFIC_ACCESS_LOG_PATH'])+ ->toBe('/data/coolify/proxy/caddy/access.log');+});++it('gives Sentinel no traffic environment after a switch to a proxy without analytics support', function () {+ $server = proxySwitchServer($this, ProxyTypes::TRAEFIK->value, sentinelEnabled: true, analyticsEnabled: true);++ $server->changeProxy(ProxyTypes::NONE->value, async: true);++ expect(StartSentinel::sentinelTrafficEnvironment($server->fresh()))->toBe([]);+});diff --git a/tests/Feature/TrafficAnalytics/ChartTokensTest.php b/tests/Feature/TrafficAnalytics/ChartTokensTest.phpindex 8406b34e24..fa6ad02aa2 100644--- a/tests/Feature/TrafficAnalytics/ChartTokensTest.php+++ b/tests/Feature/TrafficAnalytics/ChartTokensTest.php@@ -168,7 +168,8 @@ it('shows values with local and UTC timestamps in analytics chart tooltips', funforeach ([app_path('Livewire/Analytics.php'),- app_path('Livewire/Project/Application/Analytics.php'),+ // Shared by the application and service analytics tabs.+ app_path('Livewire/Project/Shared/ResourceTrafficAnalytics.php'),app_path('Livewire/Dashboard/TrafficAnalytics.php'),] as $component) {expect(file_get_contents($component))->toContain("'sparkCategories' => array_column(\$this->series, 'bucket')");diff --git a/tests/Feature/TrafficAnalytics/ResourceTrafficKeysTest.php b/tests/Feature/TrafficAnalytics/ResourceTrafficKeysTest.phpnew file mode 100644index 0000000000..7299f5c923--- /dev/null+++ b/tests/Feature/TrafficAnalytics/ResourceTrafficKeysTest.php@@ -0,0 +1,426 @@+<?php++use App\Data\Traffic\TrafficBreakdownData;+use App\Data\Traffic\TrafficOverviewData;+use App\Data\Traffic\TrafficPathData;+use App\Data\Traffic\TrafficSeriesBucketData;+use App\Livewire\Analytics as GlobalAnalytics;+use App\Livewire\Project\Application\Analytics as ApplicationAnalytics;+use App\Livewire\Project\Application\TrafficOverview;+use App\Models\Application;+use App\Models\Environment;+use App\Models\PrivateKey;+use App\Models\Project;+use App\Models\Server;+use App\Models\Service;+use App\Models\ServiceApplication;+use App\Models\StandaloneDocker;+use App\Models\Team;+use App\Models\User;+use App\Services\SentinelTrafficClient;+use App\Services\TrafficAnalyticsAggregator;+use App\Services\TrafficResource;+use Illuminate\Foundation\Testing\RefreshDatabase;+use Illuminate\Support\Facades\Cache;+use Livewire\Livewire;++uses(RefreshDatabase::class);++/**+ * Serves canned bodies by the first needle contained in the url, in insertion order, so+ * per-key needles (e.g. "/app/{key}/traffic/overview") must come before generic ones.+ */+class FakeResourceKeysTrafficClient extends SentinelTrafficClient+{+ public array $responses = [];++ protected function raw(string $url): string+ {+ foreach ($this->responses as $needle => $response) {+ if (str_contains($url, $needle)) {+ if ($response instanceof Throwable) {+ throw $response;+ }++ return $response;+ }+ }++ return '{}';+ }+}++function resourceKeysOverview(int $requests, int $uniques = 10, float $p95 = 20.0): string+{+ return json_encode([+ 'requests' => $requests,+ 'bytes_in' => $requests * 10,+ 'bytes_out' => $requests * 100,+ 'status' => ['s2xx' => $requests, 's3xx' => 0, 's4xx' => 0, 's5xx' => 0],+ 'latency' => ['p50' => 5.0, 'p95' => $p95, 'p99' => 50.0],+ 'unique_visitors' => $uniques,+ ]);+}++function bindResourceKeysFake(array $responses): void+{+ app()->bind(SentinelTrafficClient::class, function ($app, $params) use ($responses) {+ $client = new FakeResourceKeysTrafficClient($params['server']);+ $client->responses = $responses;++ return $client;+ });+}++beforeEach(function () {+ Cache::flush();+ Server::flushIdentityMap();++ $this->team = Team::factory()->create();+ $this->user = User::factory()->create();+ $this->team->members()->attach($this->user->id, ['role' => 'owner']);+ $this->actingAs($this->user);+ session(['currentTeam' => $this->team]);++ $this->privateKey = PrivateKey::factory()->create(['team_id' => $this->team->id]);+ $this->server = Server::factory()->create([+ 'team_id' => $this->team->id,+ 'private_key_id' => $this->privateKey->id,+ ]);+ $this->server->settings->is_traffic_analytics_enabled = true;+ $this->server->settings->save();+ $this->destination = StandaloneDocker::where('server_id', $this->server->id)->first()+ ?? StandaloneDocker::factory()->create(['server_id' => $this->server->id, 'network' => 'coolify-test']);++ $this->project = Project::factory()->create(['team_id' => $this->team->id, 'name' => 'Storefront']);+ $this->environment = Environment::factory()->create(['project_id' => $this->project->id]);+});++function makeComposeApplication(): Application+{+ return Application::factory()->create([+ 'name' => 'Compose Shop',+ 'build_pack' => 'dockercompose',+ 'fqdn' => null,+ 'docker_compose_domains' => json_encode([+ 'api' => ['domain' => 'https://api.shop.test'],+ 'web' => ['domain' => 'https://www.shop.test'],+ ]),+ 'environment_id' => test()->environment->id,+ 'destination_id' => test()->destination->id,+ 'destination_type' => StandaloneDocker::class,+ ]);+}++function makeTrafficService(Server $server, StandaloneDocker $destination, Environment $environment, string $name, string $fqdn): Service+{+ $service = Service::factory()->create([+ 'name' => $name,+ 'server_id' => $server->id,+ 'destination_id' => $destination->id,+ 'destination_type' => $destination->getMorphClass(),+ 'environment_id' => $environment->id,+ ]);+ ServiceApplication::create([+ 'service_id' => $service->id,+ 'name' => 'web',+ 'image' => 'nginx:alpine',+ 'fqdn' => $fqdn,+ ]);++ return $service;+}++it('resolves the bare uuid and every uuid-prefixed key, without false prefix matches', function () {+ $client = new FakeResourceKeysTrafficClient($this->server);+ $client->responses = ['/traffic/apps' => json_encode([+ 'abc', 'abc-api-1a2b', 'abc-12', 'abcx', 'abcd-web', 'xabc', 'other', 'abc-bad key', 42,+ ])];++ expect($client->resourceKeys('abc'))->toBe(['abc', 'abc-12', 'abc-api-1a2b'])+ ->and($client->resourceKeys('zzz'))->toBe(['zzz'])+ ->and(SentinelTrafficClient::keyBelongsTo('abcx', 'abc'))->toBeFalse()+ ->and(SentinelTrafficClient::keyBelongsTo('abc-web', 'abc'))->toBeTrue()+ ->and(SentinelTrafficClient::candidateOwnerUuids('abc-12-web'))->toBe(['abc-12-web', 'abc-12', 'abc']);++ expect(fn () => $client->resourceKeys("x'; touch /tmp/pwned; '"))->toThrow(InvalidArgumentException::class);+});++it('falls back to the bare uuid when the recorded key list is unavailable', function () {+ $client = new FakeResourceKeysTrafficClient($this->server);+ $client->responses = ['/traffic/apps' => new RuntimeException('unreachable')];++ expect($client->resourceKeys('abc'))->toBe(['abc']);+});++it('warms the key list and every key bundle in two batched execs', function () {+ $client = new class($this->server) extends SentinelTrafficClient+ {+ public array $batches = [];++ protected function batchRemoteFetch(array $urls): string+ {+ $this->batches[] = $urls;++ return implode("\x1e", array_map(fn ($url) => match (true) {+ str_contains($url, '/traffic/apps') => json_encode(['res', 'res-api', 'res-web', 'other']),+ str_contains($url, '/app/res-api/traffic/dashboard') => json_encode(['overview' => ['requests' => 7], 'paths' => [], 'breakdowns' => [], 'series' => [], 'attribution' => null]),+ str_contains($url, '/traffic/dashboard') => json_encode(['overview' => ['requests' => 1], 'paths' => [], 'breakdowns' => [], 'series' => [], 'attribution' => null]),+ default => '[]',+ }, $urls))."\x1e";+ }++ protected function remoteFetch(string $url): string+ {+ throw new RuntimeException("individual fetch should not run for: {$url}");+ }+ };++ $keys = $client->prefetchResource('res', 'F', 'T', ['country'], '24h');++ expect($keys)->toBe(['res', 'res-api', 'res-web'])+ ->and($client->batches)->toHaveCount(2)+ // The bare-uuid bundle came with the key list; only the other two are fetched.+ ->and($client->batches[1])->toHaveCount(2);+ // Served from the seeded cache; remoteFetch() would throw otherwise.+ expect($client->overview('res-api', 'F', 'T')->requests)->toBe(7)+ ->and($client->overview('res-web', 'F', 'T')->requests)->toBe(1);+ $client->breakdown('res-web', 'country', 'F', 'T');+});++it('batches the individual endpoints of every key when Sentinel lacks the dashboard route', function () {+ $client = new class($this->server) extends SentinelTrafficClient+ {+ public int $batchCalls = 0;++ protected function batchRemoteFetch(array $urls): string+ {+ $this->batchCalls++;++ return implode("\x1e", array_map(fn ($url) => match (true) {+ str_contains($url, '/traffic/apps') => json_encode(['res-api', 'res-web']),+ str_contains($url, '/traffic/dashboard') => 'Not Found',+ str_contains($url, '/attribution') => '{"attribution":"demo"}',+ str_contains($url, '/overview') => '{"requests":3}',+ default => '[]',+ }, $urls))."\x1e";+ }++ protected function remoteFetch(string $url): string+ {+ throw new RuntimeException("individual fetch should not run for: {$url}");+ }+ };++ $keys = $client->prefetchResource('res', 'F', 'T', ['country'], '24h');++ expect($keys)->toBe(['res-api', 'res-web'])+ ->and($client->batchCalls)->toBe(3)+ ->and($client->overview('res-api', 'F', 'T')->requests)->toBe(3)+ ->and($client->overview('res-web', 'F', 'T')->requests)->toBe(3)+ ->and($client->attribution())->toBe('demo');+});++it('merges overviews, paths, breakdowns, and series of several keys', function () {+ $aggregator = new TrafficAnalyticsAggregator(['country']);++ $aggregator->addOverview(TrafficOverviewData::fromSentinel(json_decode(resourceKeysOverview(100, 10, 20.0), true)));+ $aggregator->addOverview(TrafficOverviewData::fromSentinel(json_decode(resourceKeysOverview(50, 5, 40.0), true)));++ $domains = ['k-api' => 'api.test', 'k-web' => 'web.test'];+ $domainForKey = fn (string $key) => $domains[$key] ?? null;+ $aggregator->addPaths(collect([+ TrafficPathData::fromSentinel(['path' => '/', 'app' => 'k-api', 'requests' => 10, 'p95' => 5]),+ TrafficPathData::fromSentinel(['path' => '/', 'app' => 'k-api', 'requests' => 5, 'p95' => 9]),+ ]), 'k-api', $domainForKey);+ // Older Sentinel omits `app`: the queried key is the fallback.+ $aggregator->addPaths(collect([TrafficPathData::fromSentinel(['path' => '/', 'requests' => 30])]), 'k-web', $domainForKey);++ $aggregator->addBreakdown('country', collect([TrafficBreakdownData::fromSentinel(['value' => 'US', 'requests' => 4, 'bytes_out' => 1])]));+ $aggregator->addBreakdown('country', collect([TrafficBreakdownData::fromSentinel(['value' => 'US', 'requests' => 6, 'bytes_out' => 2])]));++ $aggregator->addSeries(collect([TrafficSeriesBucketData::fromSentinel(['bucket' => 2000, 's2xx' => 1]), TrafficSeriesBucketData::fromSentinel(['bucket' => 1000, 's2xx' => 2])]));+ $aggregator->addSeries(collect([TrafficSeriesBucketData::fromSentinel(['bucket' => 1000, 's2xx' => 3])]));++ $overview = $aggregator->overview();+ expect($overview['overview']->requests)->toBe(150)+ ->and($overview['overview']->uniqueVisitors)->toBe(15)+ ->and($overview['overview']->latencyP95)->toBe(40.0)+ ->and($overview['latencyApproximate'])->toBeTrue()+ ->and($overview['uniquesApproximate'])->toBeTrue();++ $paths = $aggregator->topPaths();+ expect($paths)->toHaveCount(2)+ ->and($paths[0])->toMatchArray(['path' => '/', 'domain' => 'web.test', 'requests' => 30])+ ->and($paths[1])->toMatchArray(['path' => '/', 'domain' => 'api.test', 'requests' => 15, 'p95' => 9.0]);++ expect($aggregator->breakdowns()['country'])->toBe([['value' => 'US', 'requests' => 10, 'bytesOut' => 3]]);+ expect(array_column($aggregator->series(), 'bucket'))->toBe([1000, 2000])+ ->and($aggregator->series()[0]['s2xx'])->toBe(5);+});++it('shows a compose application with the data of all its compose service keys', function () {+ $application = makeComposeApplication();+ $apiKey = $application->uuid.'-'.traefikSafeServiceNameSegment('api');+ $webKey = $application->uuid.'-'.traefikSafeServiceNameSegment('web');++ bindResourceKeysFake([+ '/traffic/apps' => json_encode([$apiKey, $webKey, 'someone-else']),+ "/app/{$apiKey}/traffic/overview" => resourceKeysOverview(100),+ "/app/{$webKey}/traffic/overview" => resourceKeysOverview(50),+ "/app/{$apiKey}/traffic/paths" => json_encode([['path' => '/v1/orders', 'app' => $apiKey, 'requests' => 100]]),+ "/app/{$webKey}/traffic/paths" => json_encode([['path' => '/checkout', 'app' => $webKey, 'requests' => 50]]),+ "/app/{$application->uuid}/traffic" => new RuntimeException('bare uuid must not be queried when compose keys exist'),+ ]);++ $component = loadLazy(Livewire::test(ApplicationAnalytics::class, ['application' => $application]))+ ->assertOk()+ ->assertSee('150')+ ->assertSee('api.shop.test')+ ->assertSee('www.shop.test')+ ->assertSet('uniquesApproximate', true);++ $paths = collect($component->instance()->topPaths)->keyBy('path');+ expect($component->instance()->overview['requests'])->toBe(150)+ ->and($paths['/v1/orders']['domain'])->toBe('api.shop.test')+ ->and($paths['/checkout']['domain'])->toBe('www.shop.test');+});++it('sums every compose key in the application traffic card', function () {+ $application = makeComposeApplication();+ $apiKey = $application->uuid.'-'.traefikSafeServiceNameSegment('api');+ $webKey = $application->uuid.'-'.traefikSafeServiceNameSegment('web');++ bindResourceKeysFake([+ '/traffic/apps' => json_encode([$apiKey, $webKey]),+ "/app/{$apiKey}/traffic/overview" => resourceKeysOverview(4000),+ "/app/{$webKey}/traffic/overview" => resourceKeysOverview(200),+ ]);++ loadLazy(Livewire::test(TrafficOverview::class, ['application' => $application]))+ ->assertOk()+ ->assertSee('4,200');+});++it('groups compose keys of one application into one leaderboard row', function () {+ $application = makeComposeApplication();+ $apiKey = $application->uuid.'-'.traefikSafeServiceNameSegment('api');+ $webKey = $application->uuid.'-'.traefikSafeServiceNameSegment('web');++ bindResourceKeysFake([+ '/traffic/apps' => json_encode([$apiKey, $webKey, 'orphan-key']),+ "/app/{$apiKey}/traffic/overview" => resourceKeysOverview(100),+ "/app/{$webKey}/traffic/overview" => resourceKeysOverview(50),+ '/app/orphan-key/traffic/overview' => resourceKeysOverview(5),+ '/traffic/overview' => resourceKeysOverview(155),+ '/traffic/paths' => json_encode([['path' => '/v1', 'app' => $apiKey, 'requests' => 9]]),+ ]);++ $instance = loadLazy(Livewire::test(GlobalAnalytics::class))+ ->assertOk()+ ->assertSee('Compose Shop')+ ->assertDontSee($apiKey)+ ->instance();++ $rows = collect($instance->topApps)->keyBy('uuid');+ expect($rows)->toHaveCount(2)+ ->and($rows[$application->uuid]['name'])->toBe('Compose Shop')+ ->and($rows[$application->uuid]['requests'])->toBe(150)+ ->and($rows[$application->uuid]['link'])->toBe(route('project.application.analytics', [+ 'project_uuid' => $this->project->uuid,+ 'environment_uuid' => $this->environment->uuid,+ 'application_uuid' => $application->uuid,+ ]))+ ->and($rows['orphan-key']['name'])->toBe('orphan-key')+ ->and($rows['orphan-key']['link'])->toBeNull();++ // Hosts stay per served domain; path rows carry the compose service domain.+ $hosts = collect($instance->topHosts)->keyBy('host');+ expect($hosts['api.shop.test']['requests'])->toBe(100)+ ->and($hosts['www.shop.test']['requests'])->toBe(50)+ ->and($instance->topPaths[0]['domain'])->toBe('api.shop.test');+});++it('lists services in the filter, links service rows to service analytics, and queries all service keys', function () {+ $service = makeTrafficService($this->server, $this->destination, $this->environment, 'Umami', 'https://stats.shop.test');+ $serviceKey = $service->uuid.'-'.traefikSafeServiceNameSegment('web');++ bindResourceKeysFake([+ '/traffic/apps' => json_encode([$serviceKey]),+ "/app/{$serviceKey}/traffic/overview" => resourceKeysOverview(70),+ '/traffic/overview' => resourceKeysOverview(70),+ ]);++ $component = loadLazy(Livewire::test(GlobalAnalytics::class))+ ->assertOk()+ ->assertSee('Umami');++ $serviceLink = route('project.service.analytics', [+ 'project_uuid' => $this->project->uuid,+ 'environment_uuid' => $this->environment->uuid,+ 'service_uuid' => $service->uuid,+ ]);+ $row = $component->instance()->topApps[0];+ expect($row['uuid'])->toBe($service->uuid)+ ->and($row['name'])->toBe('Umami')+ ->and($row['isService'])->toBeTrue()+ ->and($row['domain'])->toBe('stats.shop.test')+ ->and($row['link'])->toBe($serviceLink);+ $component->assertSee($serviceLink, false);++ expect(collect($component->instance()->appGroupedOptions)->firstWhere('value', $service->uuid)['label'])->toBe('Umami (Service)');++ $component->set('appUuid', $service->uuid)+ ->assertSet('appUuid', $service->uuid)+ ->assertDontSee('Top applications');+ expect($component->instance()->overview['requests'])->toBe(70);+});++it('never names or links a key owned by another team resource', function () {+ $otherTeam = Team::factory()->create();+ $otherProject = Project::factory()->create(['team_id' => $otherTeam->id]);+ $otherEnvironment = Environment::factory()->create(['project_id' => $otherProject->id]);+ $otherServer = Server::factory()->create(['team_id' => $otherTeam->id]);+ $otherDestination = StandaloneDocker::factory()->create(['server_id' => $otherServer->id, 'network' => 'other-team-test']);+ $otherService = makeTrafficService($otherServer, $otherDestination, $otherEnvironment, 'Secret Other Service', 'https://secret.other.test');+ $otherKey = $otherService->uuid.'-'.traefikSafeServiceNameSegment('web');++ bindResourceKeysFake([+ '/traffic/apps' => json_encode([$otherKey]),+ '/traffic/overview' => resourceKeysOverview(10),+ ]);++ $component = loadLazy(Livewire::test(GlobalAnalytics::class))+ ->assertOk()+ ->assertDontSee('Secret Other Service')+ ->assertDontSee('secret.other.test')+ ->assertSee($otherKey);++ expect($component->instance()->topApps[0]['link'])->toBeNull()+ ->and($component->instance()->appOptions)->not->toHaveKey($otherService->uuid);++ // A bookmarked filter for another team's service is dropped, not queried.+ loadLazy(Livewire::withQueryParams(['app' => $otherService->uuid])->test(GlobalAnalytics::class))+ ->assertSet('appUuid', '');+});++it('finds the preview domain for normal and compose preview keys', function () {+ $application = Application::factory()->create([+ 'fqdn' => 'https://shop.test',+ 'environment_id' => $this->environment->id,+ 'destination_id' => $this->destination->id,+ 'destination_type' => StandaloneDocker::class,+ ]);+ $application->previews()->create([+ 'pull_request_id' => 12,+ 'pull_request_html_url' => 'https://github.com/coollabsio/shop/pull/12',+ 'fqdn' => 'https://12.shop.test',+ 'docker_compose_domains' => json_encode(['api' => ['domain' => 'https://api-12.shop.test']]),+ ]);+ $resource = TrafficResource::for($application->fresh());++ expect($resource->domainForKey("{$application->uuid}-pr-12"))->toBe('12.shop.test')+ ->and($resource->domainForKey("{$application->uuid}-12"))->toBe('12.shop.test')+ ->and($resource->domainForKey("{$application->uuid}-12-api"))->toBe('api-12.shop.test')+ ->and($resource->domainForKey($application->uuid))->toBe('shop.test');+});diff --git a/tests/Feature/TrafficAnalytics/ServiceAnalyticsTest.php b/tests/Feature/TrafficAnalytics/ServiceAnalyticsTest.phpnew file mode 100644index 0000000000..09de3c8e98--- /dev/null+++ b/tests/Feature/TrafficAnalytics/ServiceAnalyticsTest.php@@ -0,0 +1,175 @@+<?php++use App\Livewire\Project\Service\Analytics as ServiceAnalytics;+use App\Models\Environment;+use App\Models\InstanceSettings;+use App\Models\PrivateKey;+use App\Models\Project;+use App\Models\Server;+use App\Models\Service;+use App\Models\ServiceApplication;+use App\Models\StandaloneDocker;+use App\Models\Team;+use App\Models\User;+use App\Services\SentinelTrafficClient;+use Illuminate\Foundation\Testing\RefreshDatabase;+use Illuminate\Support\Facades\Cache;+use Illuminate\Support\Once;+use Livewire\Livewire;++uses(RefreshDatabase::class);++class FakeServiceAnalyticsTrafficClient extends SentinelTrafficClient+{+ public array $responses = [];++ protected function raw(string $url): string+ {+ foreach ($this->responses as $needle => $response) {+ if (str_contains($url, $needle)) {+ return $response;+ }+ }++ return '{}';+ }+}++function serviceAnalyticsOverview(int $requests): string+{+ return json_encode([+ 'requests' => $requests,+ 'bytes_in' => 100,+ 'bytes_out' => 900,+ 'status' => ['s2xx' => $requests, 's3xx' => 0, 's4xx' => 0, 's5xx' => 0],+ 'latency' => ['p50' => 5.0, 'p95' => 12.0, 'p99' => 30.0],+ 'unique_visitors' => 3,+ ]);+}++/**+ * @return array{server: Server, project: Project, environment: Environment, service: Service}+ */+function makeServiceAnalyticsStack(Team $team, bool $enabled, string $name): array+{+ $privateKey = PrivateKey::factory()->create(['team_id' => $team->id]);+ $server = Server::factory()->create(['team_id' => $team->id, 'private_key_id' => $privateKey->id]);+ $server->settings->is_traffic_analytics_enabled = $enabled;+ $server->settings->save();+ $destination = StandaloneDocker::where('server_id', $server->id)->first()+ ?? StandaloneDocker::factory()->create(['server_id' => $server->id, 'network' => 'coolify-test-'.$team->id]);++ $project = Project::factory()->create(['team_id' => $team->id]);+ $environment = Environment::factory()->create(['project_id' => $project->id]);+ $service = Service::factory()->create([+ 'name' => $name,+ 'server_id' => $server->id,+ 'destination_id' => $destination->id,+ 'destination_type' => $destination->getMorphClass(),+ 'environment_id' => $environment->id,+ ]);+ foreach (['web' => 'https://app.svc.test', 'admin' => 'https://admin.svc.test'] as $serviceName => $fqdn) {+ ServiceApplication::create([+ 'service_id' => $service->id,+ 'name' => $serviceName,+ 'image' => 'nginx:alpine',+ 'fqdn' => $fqdn,+ ]);+ }++ return compact('server', 'project', 'environment', 'service');+}++function serviceAnalyticsRoute(array $stack): string+{+ return route('project.service.analytics', [+ 'project_uuid' => $stack['project']->uuid,+ 'environment_uuid' => $stack['environment']->uuid,+ 'service_uuid' => $stack['service']->uuid,+ ]);+}++beforeEach(function () {+ config()->set('app.maintenance.store', 'array');+ Cache::flush();+ Once::flush();+ Server::flushIdentityMap();+ InstanceSettings::forceCreate(['id' => 0]);++ $this->team = Team::factory()->create();+ $this->user = User::factory()->create();+ $this->team->members()->attach($this->user->id, ['role' => 'owner']);+ $this->actingAs($this->user);+ session(['currentTeam' => $this->team]);+});++it('renders the service analytics page with the disabled state and a navigation item', function () {+ $stack = makeServiceAnalyticsStack($this->team, false, 'Umami');++ $this->get(serviceAnalyticsRoute($stack))+ ->assertOk()+ ->assertSee('Traffic analytics is not enabled')+ ->assertSee(serviceAnalyticsRoute($stack), false)+ ->assertSee(route('server.analytics', ['server_uuid' => $stack['server']->uuid]), false)+ ->assertDontSee('__lazyLoad', escape: false);+});++it('lazy loads the service analytics page when traffic analytics is enabled', function () {+ $stack = makeServiceAnalyticsStack($this->team, true, 'Umami');++ $this->get(serviceAnalyticsRoute($stack))+ ->assertOk()+ ->assertSee('__lazyLoad', escape: false);+});++it('merges the traffic of every service application key', function () {+ $stack = makeServiceAnalyticsStack($this->team, true, 'Umami');+ $service = $stack['service'];+ $webKey = $service->uuid.'-'.traefikSafeServiceNameSegment('web');+ $adminKey = $service->uuid.'-'.traefikSafeServiceNameSegment('admin');++ app()->bind(SentinelTrafficClient::class, function ($app, $params) use ($webKey, $adminKey) {+ $client = new FakeServiceAnalyticsTrafficClient($params['server']);+ $client->responses = [+ '/traffic/apps' => json_encode([$webKey, $adminKey, 'unrelated']),+ "/app/{$webKey}/traffic/overview" => serviceAnalyticsOverview(300),+ "/app/{$adminKey}/traffic/overview" => serviceAnalyticsOverview(20),+ "/app/{$adminKey}/traffic/paths" => json_encode([['path' => '/login', 'app' => $adminKey, 'requests' => 20]]),+ ];++ return $client;+ });++ $component = loadLazy(Livewire::test(ServiceAnalytics::class, ['service' => $service]))+ ->assertOk()+ ->assertSee('320')+ ->assertSee('admin.svc.test')+ ->assertSet('latencyApproximate', true)+ ->assertDispatched('refreshChartData-service-analytics-status');++ expect($component->instance()->overview['requests'])->toBe(320)+ ->and($component->instance()->topPaths[0]['domain'])->toBe('admin.svc.test');+});++it('returns not found for another team service analytics page', function () {+ $otherTeam = Team::factory()->create();+ $other = makeServiceAnalyticsStack($otherTeam, true, 'Secret Other Service');+ $own = makeServiceAnalyticsStack($this->team, true, 'Umami');++ $this->get(serviceAnalyticsRoute($other))->assertNotFound();++ // Own project path with the other team's service uuid.+ $this->get(route('project.service.analytics', [+ 'project_uuid' => $own['project']->uuid,+ 'environment_uuid' => $own['environment']->uuid,+ 'service_uuid' => $other['service']->uuid,+ ]))->assertNotFound()->assertDontSee('Secret Other Service');+});++it('forbids the service analytics component for another team service', function () {+ $otherTeam = Team::factory()->create();+ $other = makeServiceAnalyticsStack($otherTeam, false, 'Secret Other Service');++ Livewire::test(ServiceAnalytics::class, ['service' => $other['service'], 'lazy' => false])+ ->assertForbidden();+});diff --git a/tests/Feature/TrafficAnalytics/StartSentinelTrafficTest.php b/tests/Feature/TrafficAnalytics/StartSentinelTrafficTest.phpindex 1588a7c430..b85fbd86de 100644--- a/tests/Feature/TrafficAnalytics/StartSentinelTrafficTest.php+++ b/tests/Feature/TrafficAnalytics/StartSentinelTrafficTest.php@@ -29,7 +29,7 @@ it('produces no traffic env when disabled', function () {});it('produces traffic + geoip env when enabled', function () {- $server = Server::factory()->create(['team_id' => $this->team->id]);+ $server = trafficProxyServer($this);$server->settings->is_traffic_analytics_enabled = true;$server->settings->geoip_maxmind_license_key = 'lic';$server->settings->save();@@ -49,7 +49,7 @@ it('produces traffic + geoip env when enabled', function () {it('uses the dev proxy volume for traffic logs locally', function () {config()->set('app.env', 'local');- $server = Server::factory()->create(['team_id' => $this->team->id]);+ $server = trafficProxyServer($this);$server->settings->is_traffic_analytics_enabled = true;$server->settings->save();@@ -60,7 +60,7 @@ it('uses the dev proxy volume for traffic logs locally', function () {});it('passes custom traffic settings as sentinel env', function () {- $server = Server::factory()->create(['team_id' => $this->team->id]);+ $server = trafficProxyServer($this);$server->settings->is_traffic_analytics_enabled = true;$server->settings->traffic_topn = 100;$server->settings->traffic_sample_threshold = 500;@@ -83,7 +83,7 @@ it('passes custom traffic settings as sentinel env', function () {});it('injects the maxmind license key only when geoip is enabled', function () {- $server = Server::factory()->create(['team_id' => $this->team->id]);+ $server = trafficProxyServer($this);$server->settings->is_traffic_analytics_enabled = true;$server->settings->is_geoip_enabled = true;$server->settings->geoip_maxmind_license_key = 'secret-maxmind-key';@@ -93,6 +93,15 @@ it('injects the maxmind license key only when geoip is enabled', function () {expect($env['GEOIP_MAXMIND_LICENSE_KEY'])->toBe('secret-maxmind-key');});+function trafficProxyServer(object $test): Server+{+ $server = Server::factory()->create(['team_id' => $test->team->id]);+ $server->proxy->set('type', 'TRAEFIK');+ $server->save();++ return $server;+}+function sentinelTrafficServer(object $test, ?string $proxyType, bool $analyticsEnabled): Server{$server = Server::factory()->create([@@ -159,7 +168,9 @@ it('does not touch the access log when traffic analytics cannot run', function ($script = runStartSentinelAndCaptureScript($server);expect($script)->toContain('docker run -d')- ->not->toContain('touch ');+ ->not->toContain('touch ')+ ->not->toContain('/data/coolify/proxy:/data/coolify/proxy')+ ->not->toContain('TRAFFIC_ENABLED');})->with(['analytics disabled' => ['TRAEFIK', false],'proxy none' => ['NONE', true],diff --git a/tests/Feature/TrafficAnalytics/ToggleTrafficAnalyticsTest.php b/tests/Feature/TrafficAnalytics/ToggleTrafficAnalyticsTest.phpindex fca6e5a981..24e8eb9825 100644--- a/tests/Feature/TrafficAnalytics/ToggleTrafficAnalyticsTest.php+++ b/tests/Feature/TrafficAnalytics/ToggleTrafficAnalyticsTest.php@@ -237,3 +237,44 @@ it('does not let a team member toggle traffic analytics', function () {expect($server->fresh()->isTrafficAnalyticsEnabled())->toBeFalse();});++it('tells the user that Caddy logs a resource only after a redeploy', function () {+ ConfigureTrafficAnalytics::partialMock()->shouldReceive('handle')->once()->andReturnUsing(function ($server, $enable) {+ $server->settings->is_traffic_analytics_enabled = $enable;+ $server->settings->save();++ return true;+ });++ $server = Server::factory()->create(['team_id' => $this->team->id]);+ $server->proxy->set('type', 'CADDY');+ $server->save();+ $server->settings->is_traffic_analytics_enabled = false;+ $server->settings->save();++ Livewire::test(TrafficAnalyticsSettings::class, ['server' => $server])+ ->assertDontSee('Caddy logs a resource only after you redeploy it.')+ ->call('toggleTrafficAnalytics')+ ->assertDispatched('success', 'Traffic analytics enabled. Restarting proxy and Sentinel. Caddy logs a resource only after you redeploy it.')+ ->assertSee('Caddy logs a resource only after you redeploy it.');+});++it('does not show the Caddy redeploy note on a Traefik server', function () {+ ConfigureTrafficAnalytics::partialMock()->shouldReceive('handle')->once()->andReturnUsing(function ($server, $enable) {+ $server->settings->is_traffic_analytics_enabled = $enable;+ $server->settings->save();++ return true;+ });++ $server = Server::factory()->create(['team_id' => $this->team->id]);+ $server->proxy->set('type', 'TRAEFIK');+ $server->save();+ $server->settings->is_traffic_analytics_enabled = false;+ $server->settings->save();++ Livewire::test(TrafficAnalyticsSettings::class, ['server' => $server])+ ->call('toggleTrafficAnalytics')+ ->assertDispatched('success', 'Traffic analytics enabled. Restarting proxy and Sentinel.')+ ->assertDontSee('Caddy logs a resource only after you redeploy it.');+});