| 1 | namespace CascadeIDE.Cockpit.ComputingUnits.IdeHealth; |
| 2 | |
| 3 | using CascadeIDE.Cockpit.DataBus; |
| 4 | using CascadeIDE.Contracts; |
| 5 | using CascadeIDE.Services; |
| 6 | |
| 7 | /// <summary> |
| 8 | /// <strong>CCU</strong> «сбор снимка канала» (ADR 0097): <see cref="IdeHealthInputSnapshot"/> только из <see cref="IDataBus"/> (события домена, ADR 0099). |
| 9 | /// Не тянет <c>UiChromeViewModel</c> / DAP / git напрямую. Продуктовое имя канала — IDE Health (ADR 0089). |
| 10 | /// </summary> |
| 11 | [ComputingUnit] |
| 12 | [DataBusSubscriber("ide-health-ccu")] |
| 13 | public sealed class IdeHealthSnapshotUnit : Channels.WorkspaceHealth.IIdeHealthChannel, IDisposable |
| 14 | { |
| 15 | private readonly IdeHealthScopeDecisionUnit _scopeDecision = IdeHealthScopeDecisionUnit.Default; |
| 16 | private readonly IdeHealthBuildTestsUnit _buildTests = IdeHealthBuildTestsUnit.Default; |
| 17 | private readonly IdeHealthDebugSegmentUnit _debugSegment = IdeHealthDebugSegmentUnit.Default; |
| 18 | private readonly IdeHealthGitSegmentUnit _gitSegment = IdeHealthGitSegmentUnit.Default; |
| 19 | private readonly IdeHealthIdeHostUnit _ideHostUnit = IdeHealthIdeHostUnit.Default; |
| 20 | private readonly IDisposable? _buildStateSubscription; |
| 21 | private readonly IDisposable? _testsStateSubscription; |
| 22 | private readonly IDisposable? _debugStateSubscription; |
| 23 | private readonly IDisposable? _gitStateSubscription; |
| 24 | private readonly IDisposable? _ideHostStateSubscription; |
| 25 | private readonly IDisposable? _startupProjectSubscription; |
| 26 | private readonly object _buildSnapshotLock = new(); |
| 27 | private BuildStateSnapshot _buildSnapshot = BuildStateSnapshot.Empty; |
| 28 | private volatile bool _hasTestsStateFromBus; |
| 29 | private string _latestTestsSummaryFromBus = ""; |
| 30 | private int _latestImpactedTestsBadgeFromBus; |
| 31 | private DebugSessionSnapshot _latestDebugSnapshot = DebugSessionSnapshot.Empty; |
| 32 | private string _latestGitLine = ""; |
| 33 | private string _latestGitCockpitShort = ""; |
| 34 | private string? _latestStartupProjectPath; |
| 35 | private IdeHostStateChanged _latestIdeHost; |
| 36 | private bool _disposed; |
| 37 | |
| 38 | public IdeHealthSnapshotUnit(IDataBus dataBus) |
| 39 | { |
| 40 | _buildStateSubscription = dataBus.Subscribe<BuildStateChanged>(evt => |
| 41 | { |
| 42 | lock (_buildSnapshotLock) |
| 43 | _buildSnapshot = BuildStateSnapshotUnit.Apply(_buildSnapshot, evt); |
| 44 | }); |
| 45 | _testsStateSubscription = dataBus.Subscribe<TestsStateChanged>(evt => |
| 46 | { |
| 47 | _latestTestsSummaryFromBus = evt.Summary ?? ""; |
| 48 | _latestImpactedTestsBadgeFromBus = evt.ImpactedBadge; |
| 49 | _hasTestsStateFromBus = true; |
| 50 | }); |
| 51 | _debugStateSubscription = dataBus.Subscribe<DebugStateChanged>(evt => |
| 52 | { |
| 53 | _latestDebugSnapshot = evt.Snapshot; |
| 54 | }); |
| 55 | _gitStateSubscription = dataBus.Subscribe<GitStateChanged>(evt => |
| 56 | { |
| 57 | _latestGitLine = evt.Line; |
| 58 | _latestGitCockpitShort = evt.CockpitShort; |
| 59 | }); |
| 60 | _ideHostStateSubscription = dataBus.Subscribe<IdeHostStateChanged>(evt => _latestIdeHost = evt); |
| 61 | _startupProjectSubscription = dataBus.Subscribe<StartupProjectPathChanged>(evt => |
| 62 | { |
| 63 | _latestStartupProjectPath = evt.ProjectPath; |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | public IdeHealthInputSnapshot Build(in Channels.WorkspaceHealth.IdeHealthChannelContext context) |
| 68 | { |
| 69 | ObjectDisposedException.ThrowIf(_disposed, this); |
| 70 | |
| 71 | BuildStateSnapshot buildState; |
| 72 | lock (_buildSnapshotLock) |
| 73 | buildState = _buildSnapshot; |
| 74 | |
| 75 | var testSummary = _hasTestsStateFromBus ? _latestTestsSummaryFromBus : ""; |
| 76 | var impactedTestsBadge = _hasTestsStateFromBus ? _latestImpactedTestsBadgeFromBus : 0; |
| 77 | var dap = _latestDebugSnapshot; |
| 78 | var scopeDecision = _scopeDecision.Decide(_latestStartupProjectPath, buildState.IsBuilding, testSummary); |
| 79 | var buildTests = _buildTests.Compose(scopeDecision, buildState, testSummary, impactedTestsBadge); |
| 80 | |
| 81 | var build = buildTests.Build; |
| 82 | var tests = buildTests.Tests; |
| 83 | |
| 84 | var debug = _debugSegment.Compose(scopeDecision, dap); |
| 85 | var git = _gitSegment.Compose(_latestGitLine, _latestGitCockpitShort); |
| 86 | var ideHost = _ideHostUnit.Compose(_latestIdeHost); |
| 87 | |
| 88 | return IdeHealthStrataComposer.Compose( |
| 89 | new IdeHealthWorkspaceInput(git), |
| 90 | new IdeHealthSolutionInput(build, tests, debug), |
| 91 | ideHost); |
| 92 | } |
| 93 | |
| 94 | public void Dispose() |
| 95 | { |
| 96 | if (_disposed) |
| 97 | return; |
| 98 | _disposed = true; |
| 99 | _buildStateSubscription?.Dispose(); |
| 100 | _testsStateSubscription?.Dispose(); |
| 101 | _debugStateSubscription?.Dispose(); |
| 102 | _gitStateSubscription?.Dispose(); |
| 103 | _ideHostStateSubscription?.Dispose(); |
| 104 | _startupProjectSubscription?.Dispose(); |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | |