Lines 100.00% 59 / 59
Methods 100.00% 7 / 7
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 takeSnapshot 100.00% 16 / 16 100.00% 1 / 1 1
 buildDaemonInfo 100.00% 9 / 9 100.00% 1 / 1 2
 buildBrokerStats 100.00% 7 / 7 100.00% 1 / 1 1
 buildDeployStatus 100.00% 10 / 10 100.00% 1 / 1 3
 buildReloadStatus 100.00% 8 / 8 100.00% 1 / 1 2
 buildComponentStatus 100.00% 8 / 8 100.00% 1 / 1 1
37final readonly class SnapshotAssembler implements SnapshotSource
38{
39    public function __construct(
40        private HaSession $session,
41        private StewartVersion $stewartVersion,
42        private DaemonStartTime $startTime,
43        private ConnectionTracker $connection,
44        private WorkerSlotRegistry $slots,
45        private HaCallSlots $callSlots,
46        private SubscriptionRegistry $registry,
47        private DeployState $deployState,
48        private ReloadState $reloadState,
49        private ComponentTracker $componentTracker,
50        private ExposeConfig $expose,
51        private WorkerStatusBuilder $workerStatuses,
52        private AppStatusBuilder $appStatuses,
53        private StoreHealthBuilder $storeHealth,
54        private SubscriptionInfoBuilder $subscriptionInfos,
55        private ScheduleStatusBuilder $scheduleStatuses,
56        private MqttStatusBuilder $mqttStatus,
57        private GeneratedCodeDrift $drift,
58        private ProcessMemory $processMemory,
59        private Clock $clock,
60    ) {}
61
62    public function takeSnapshot(): RuntimeSnapshot
63    {
64        return new RuntimeSnapshot(
65            takenAt: $this->clock->getNow(),
66            daemon: $this->buildDaemonInfo(),
67            connection: $this->connection->state,
68            broker: $this->buildBrokerStats(),
69            workers: $this->workerStatuses->buildWorkerStatuses(),
70            apps: $this->appStatuses->buildAppStatuses(),
71            subscriptions: $this->subscriptionInfos->buildSubscriptionInfos(),
72            store: $this->storeHealth->buildStoreHealth(),
73            deploy: $this->buildDeployStatus(),
74            component: $this->buildComponentStatus(),
75            reload: $this->buildReloadStatus(),
76            schedules: $this->scheduleStatuses->buildScheduleStatuses(),
77            mqtt: $this->mqttStatus->buildMqttStatus(),
78            generated: $this->drift->status,
79        );
80    }
81
82    private function buildDaemonInfo(): DaemonInfo
83    {
84        return new DaemonInfo(
85            pid: getmypid() ?: 0,
86            startedAt: $this->startTime->getStartedAt(),
87            memoryBytes: $this->processMemory->measureResidentBytes(),
88            version: $this->stewartVersion->value,
89            timeZone: $this->session->getTimeZone()->getName(),
90            entities: $this->session->countEntities(),
91            haVersion: $this->session->getHaVersion(),
92        );
93    }
94
95    private function buildBrokerStats(): BrokerStats
96    {
97        return new BrokerStats(
98            workers: $this->slots->countSpawnedSlots(),
99            liveWorkers: $this->slots->countLiveWorkers(),
100            inFlightHaCalls: $this->callSlots->inFlight,
101            refusedHaCalls: $this->callSlots->refusedCalls,
102            routing: $this->registry->getRoutingStats(),
103        );
104    }
105
106    private function buildDeployStatus(): ?DeployStatus
107    {
108        $commit = $this->deployState->findRunningCommit();
109
110        if ($commit === null) {
111            return null;
112        }
113
114        $failure = $this->deployState->findLastFailure();
115
116        return new DeployStatus(
117            commit: $commit->value,
118            lastPolledAt: $this->deployState->findLastPolledAt(),
119            failures: $this->deployState->countFailures(),
120            lastFailure: $failure === null ? null : new DeployFailure($failure->commit->value, $failure->reason, $failure->failedAt),
121        );
122    }
123
124    private function buildReloadStatus(): ReloadStatus
125    {
126        $last = $this->reloadState->findLastResult();
127
128        return new ReloadStatus(
129            underway: $this->reloadState->isUnderway(),
130            applied: $this->reloadState->countResults(ReloadOutcome::Applied),
131            partlyApplied: $this->reloadState->countResults(ReloadOutcome::PartlyApplied),
132            failed: $this->reloadState->countResults(ReloadOutcome::Failed),
133            last: $last === null ? null : new ReloadAttempt($last->outcome, $last->finishedAt, $last->error, $last->settingsNeedingRestart),
134        );
135    }
136
137    private function buildComponentStatus(): ComponentStatus
138    {
139        $detection = $this->componentTracker->detection;
140
141        return new ComponentStatus(
142            state: $detection->state,
143            since: $detection->since,
144            instance: $this->expose->instance->value,
145            version: $detection->version?->componentVersion,
146            protocol: $detection->version?->protocol,
147        );
148    }
149}