Lines 92.85% 39 / 42
Functions and Methods 70.00% 7 / 10
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AppMetrics 92.85% 39 / 42 70.00% 7 / 10 19.13 0.00% 0 / 1
 __construct 100.00% 4 / 4 100.00% 1 / 1 3
 replaceAppTotals 90.90% 10 / 11 0.00% 0 / 1 6.03
 recordActivityReports 100.00% 8 / 8 100.00% 1 / 1 2
 recordFinishedCall 100.00% 1 / 1 100.00% 1 / 1 1
 recordRefusedCall 0.00% 0 / 1 0.00% 0 / 1 2
 recordExposedCommand 0.00% 0 / 1 0.00% 0 / 1 2
 recordLastFailure 100.00% 10 / 10 100.00% 1 / 1 1
 listRunningTotals 100.00% 1 / 1 100.00% 1 / 1 1
 findAppRunningTotals 100.00% 1 / 1 100.00% 1 / 1 1
 findOrCreateRunningTotals 100.00% 4 / 4 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker;
6
7use Stewart\Contracts\App\AppId;
8use Stewart\Contracts\Time\Clock;
9use Stewart\Contracts\Time\Duration;
10use Stewart\Runtime\Broker\Collection\WorkerSlotCollection;
11use Stewart\Runtime\Control\Protocol\Status\FailureReport;
12use Stewart\Runtime\Ipc\Message\AppActivityReport;
13use Stewart\Runtime\Ipc\Message\AppFailed;
14use Stewart\Runtime\Ipc\Message\Pong;
15use Stewart\Runtime\Model\ExposedCommandOutcome;
16use Stewart\Runtime\Model\HaCallOutcome;
17use Stewart\Runtime\Model\ResourceScope;
18use Stewart\Runtime\Model\WorkerId;
19use WeakMap;
20
21final class AppMetrics
22{
23    /** @var array<string, AppRunningTotals> */
24    private array $runningTotals = [];
25
26    /** @var WeakMap<WorkerHandle, array<string, AppActivityReport>> */
27    private WeakMap $lastActivityReports;
28
29    public function __construct(
30        WorkerSlotCollection $workerSlots,
31        private readonly Clock $clock,
32    ) {
33        $this->lastActivityReports = new WeakMap();
34
35        foreach ($workerSlots as $slot) {
36            foreach ($slot->apps as $app) {
37                $this->runningTotals[$app->id->value] = AppRunningTotals::forApp($app->id, $app->class, $slot->workerId);
38            }
39        }
40    }
41
42    // Totals of an app that stays on its worker keep counting; a moved app starts again under its new worker.
43    public function replaceAppTotals(WorkerSlotCollection $workerSlots): void
44    {
45        $kept = [];
46
47        foreach ($workerSlots as $slot) {
48            foreach ($slot->apps as $app) {
49                $existing = $this->runningTotals[$app->id->value] ?? null;
50                $kept[$app->id->value] = $existing !== null && $existing->isPlacedOn($slot->workerId, $app->class)
51                    ? $existing
52                    : AppRunningTotals::forApp($app->id, $app->class, $slot->workerId);
53            }
54
55            $sharedKey = AppRunningTotals::buildMetricsKey(ResourceScope::shared(), $slot->workerId);
56
57            if (isset($this->runningTotals[$sharedKey])) {
58                $kept[$sharedKey] = $this->runningTotals[$sharedKey];
59            }
60        }
61
62        $this->runningTotals = $kept;
63    }
64
65    public function recordActivityReports(WorkerHandle $handle, Pong $pong): void
66    {
67        // A respawned worker counts from zero, so its first report is all new activity.
68        $previous = $this->lastActivityReports[$handle] ?? [];
69        $latest = [];
70        $at = $this->clock->getNow();
71
72        foreach ($pong->apps as $report) {
73            $scope = $report->scope->wireValue();
74            $this->findOrCreateRunningTotals($handle->id, $report->scope)?->recordActivityReport($report, $previous[$scope] ?? null, $at);
75            $latest[$scope] = $report;
76        }
77
78        $this->lastActivityReports[$handle] = $latest;
79    }
80
81    public function recordFinishedCall(WorkerId $workerId, ResourceScope $scope, HaCallOutcome $outcome, Duration $latency): void
82    {
83        $this->findOrCreateRunningTotals($workerId, $scope)?->recordCall($outcome, $latency);
84    }
85
86    public function recordRefusedCall(WorkerId $workerId, ResourceScope $scope): void
87    {
88        $this->findOrCreateRunningTotals($workerId, $scope)?->recordCall(HaCallOutcome::Refused, null);
89    }
90
91    public function recordExposedCommand(AppId $appId, ExposedCommandOutcome $outcome): void
92    {
93        $this->findAppRunningTotals($appId)?->recordExposedCommand($outcome);
94    }
95
96    public function recordLastFailure(WorkerId $workerId, AppFailed $failure): void
97    {
98        $this->findOrCreateRunningTotals($workerId, $failure->scope)?->recordLastFailure(
99            new FailureReport(
100                $failure->phase,
101                $failure->class,
102                $failure->message,
103                $failure->origin,
104                $failure->details?->reason,
105                $this->clock->getNow(),
106            ),
107        );
108    }
109
110    /** @return list<AppRunningTotals> */
111    public function listRunningTotals(): array
112    {
113        return array_values($this->runningTotals);
114    }
115
116    public function findAppRunningTotals(AppId $appId): ?AppRunningTotals
117    {
118        return $this->runningTotals[$appId->value] ?? null;
119    }
120
121    // An app a reload removed can still report from its retiring worker; its totals are gone for good.
122    private function findOrCreateRunningTotals(WorkerId $workerId, ResourceScope $scope): ?AppRunningTotals
123    {
124        $key = AppRunningTotals::buildMetricsKey($scope, $workerId);
125
126        if (!$scope->isShared()) {
127            return $this->runningTotals[$key] ?? null;
128        }
129
130        return $this->runningTotals[$key] ??= AppRunningTotals::forScope($scope, $workerId);
131    }
132}