Lines 96.49% 55 / 57
Methods 83.33% 10 / 12
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 forApp 100.00% 1 / 1 100.00% 1 / 1 1
 forScope 100.00% 1 / 1 100.00% 1 / 1 1
 buildMetricsKey 100.00% 1 / 1 100.00% 1 / 1 2
 isPlacedOn 100.00% 1 / 1 100.00% 1 / 1 2
 isSharedScope 100.00% 1 / 1 100.00% 1 / 1 1
 recordActivityReport 100.00% 13 / 13 100.00% 1 / 1 1
 recordCall 100.00% 1 / 1 100.00% 1 / 1 1
 recordExposedCommand 0.00% 0 / 1 0.00% 0 / 1 2
 recordLastFailure 100.00% 1 / 1 100.00% 1 / 1 1
 buildAppStatus 100.00% 30 / 30 100.00% 1 / 1 6
 buildExposedCommandStats 80.00% 4 / 5 0.00% 0 / 1 3.07
24final class AppRunningTotals
25{
26    private ?AppState $state = null;
27
28    private ?Instant $reportedAt = null;
29
30    private int $subscriptions = 0;
31
32    private int $schedules = 0;
33
34    private int $delivered = 0;
35
36    private int $handled = 0;
37
38    private ?Instant $lastHandledAt = null;
39
40    private ?Instant $nextRunAt = null;
41
42    private int $subscriptionDropped = 0;
43
44    private int $scheduleRuns = 0;
45
46    private int $publishes = 0;
47
48    private int $failures = 0;
49
50    private int $suppressed = 0;
51
52    private ?FailureReport $lastFailure = null;
53
54    /** @var array<string, HaCallStatsRecorder> */
55    private array $calls = [];
56
57    /** @var array<string, int> */
58    private array $exposedCommandCounts = [];
59
60    private function __construct(
61        private readonly string $id,
62        private readonly ?AppId $appId,
63        private readonly string $class,
64        private readonly ?WorkerId $workerId,
65    ) {}
66
67    public static function forApp(AppId $appId, string $class, ?WorkerId $workerId): self
68    {
69        return new self($appId->value, $appId, $class, $workerId);
70    }
71
72    public static function forScope(ResourceScope $scope, WorkerId $workerId): self
73    {
74        return new self($scope->wireValue(), $scope->appId, '', $workerId);
75    }
76
77    // The shared services.php scope exists once per worker, so its totals are kept per worker.
78    public static function buildMetricsKey(ResourceScope $scope, WorkerId $workerId): string
79    {
80        return $scope->isShared() ? $scope->wireValue() . '#' . $workerId->value : $scope->wireValue();
81    }
82
83    public function isPlacedOn(WorkerId $workerId, string $class): bool
84    {
85        return $this->workerId?->value === $workerId->value && $this->class === $class;
86    }
87
88    public function isSharedScope(): bool
89    {
90        return $this->appId === null;
91    }
92
93    public function recordActivityReport(AppActivityReport $report, ?AppActivityReport $previous, Instant $at): void
94    {
95        $this->state = $report->state;
96        $this->reportedAt = $at;
97        $this->subscriptions = $report->subscriptions;
98        $this->schedules = $report->schedules;
99        $this->delivered += max(0, $report->delivered - ($previous->delivered ?? 0));
100        $this->handled += max(0, $report->handled - ($previous->handled ?? 0));
101        $this->lastHandledAt = $report->lastHandledAt ?? $this->lastHandledAt;
102        $this->nextRunAt = $report->nextRunAt;
103        $this->subscriptionDropped += max(0, $report->subscriptionDropped - ($previous->subscriptionDropped ?? 0));
104        $this->scheduleRuns += max(0, $report->scheduleRuns - ($previous->scheduleRuns ?? 0));
105        $this->publishes += max(0, $report->publishes - ($previous->publishes ?? 0));
106        $this->failures += max(0, $report->failures - ($previous->failures ?? 0));
107        $this->suppressed += max(0, $report->suppressed - ($previous->suppressed ?? 0));
108    }
109
110    public function recordCall(HaCallOutcome $outcome, ?Duration $latency): void
111    {
112        ($this->calls[$outcome->value] ??= new HaCallStatsRecorder($outcome))->recordCall($latency);
113    }
114
115    public function recordExposedCommand(ExposedCommandOutcome $outcome): void
116    {
117        $this->exposedCommandCounts[$outcome->value] = ($this->exposedCommandCounts[$outcome->value] ?? 0) + 1;
118    }
119
120    public function recordLastFailure(FailureReport $failure): void
121    {
122        $this->lastFailure = $failure;
123    }
124
125    public function buildAppStatus(AppPauseRegistry $pausedApps, ExposureLink $exposures): AppStatus
126    {
127        $calls = $this->calls;
128        ksort($calls);
129        $pause = $this->appId === null ? null : $pausedApps->findPause($this->appId);
130        $configPauseOverride = $this->appId === null ? null : $pausedApps->findConfigPauseOverride($this->appId);
131
132        return new AppStatus(
133            id: $this->id,
134            class: $this->class,
135            workerId: $this->workerId?->value,
136            state: $this->state,
137            pause: $pause === null ? null : new AppPauseStatus($pause->since, $pause->source),
138            configPauseOverride: $configPauseOverride === null ? null : new AppPauseStatus($configPauseOverride->since, $configPauseOverride->source),
139            reportedAt: $this->reportedAt,
140            subscriptions: $this->subscriptions,
141            schedules: $this->schedules,
142            exposedEntities: $this->appId === null ? 0 : $exposures->countExposuresOfApp($this->appId),
143            counters: new AppCounters(
144                delivered: $this->delivered,
145                handled: $this->handled,
146                subscriptionDropped: $this->subscriptionDropped,
147                scheduleRuns: $this->scheduleRuns,
148                publishes: $this->publishes,
149                failures: $this->failures,
150                suppressed: $this->suppressed,
151            ),
152            haCalls: array_values(array_map(static fn(HaCallStatsRecorder $recorder): HaCallStats => $recorder->buildHaCallStats(), $calls)),
153            lastFailure: $this->lastFailure,
154            exposedCommands: $this->buildExposedCommandStats(),
155            lastHandledAt: $this->lastHandledAt,
156            nextRunAt: $this->nextRunAt,
157        );
158    }
159
160    /** @return list<ExposedCommandStats> */
161    private function buildExposedCommandStats(): array
162    {
163        $stats = [];
164
165        foreach (ExposedCommandOutcome::cases() as $outcome) {
166            if (isset($this->exposedCommandCounts[$outcome->value])) {
167                $stats[] = new ExposedCommandStats($outcome, $this->exposedCommandCounts[$outcome->value]);
168            }
169        }
170
171        return $stats;
172    }
173}