VBAF.Enterprise.Dashboard.ps1

#Requires -Version 5.1
<#
.SYNOPSIS
    Phase 15 - Enterprise Dashboard Intelligence
.DESCRIPTION
    Trains a DQN agent to manage and prioritise enterprise dashboard
    resources - deciding what to display, when to refresh, and how to
    allocate rendering resources across multiple data streams.
    The agent observes dashboard load signals and learns when to:
      - Cache : serve cached data, no refresh needed (action 0)
      - Refresh : update one panel with fresh data (action 1)
      - Prioritise: elevate critical KPIs to top of display (action 2)
      - Rebuild : full dashboard reload, all panels fresh (action 3)
.NOTES
    Part of VBAF - Phase 15 Enterprise Automation Engine
    Phase 15: Enterprise Dashboard Intelligence
    PS 5.1 compatible
    Real data: Get-Counter, WMI Win32_OperatingSystem, active session info
#>


# ============================================================
# PHASE 15 - ENTERPRISE DASHBOARD
# ============================================================

class DashboardEnvironment {

    # State: 4 normalised dashboard load dimensions (0.0 - 1.0)
    # state[0] = SeverityNorm — proven direct signal (same as SecurityMonitor)
    # state[1] = DataStaleness — how stale is the dashboard data
    # state[2] = RenderLoad — how busy is the rendering engine
    # state[3] = UrgencyScore — composite (Staleness + AlertDensity) / 2
    # REPLACES OffHours which is always 0 during daytime
    [double] $SeverityNorm   # CurrentSeverity / 3.0
    [double] $DataStaleness  # 0=fresh 1=critically stale
    [double] $RenderLoad     # 0=idle 1=rendering saturated
    [double] $UrgencyScore   # 0=calm 1=urgent composite signal
    [double] $AlertDensity   # kept for internal use / probe display

    [int]    $CorrectActions
    [int]    $MissedUpdates
    [int]    $Steps
    [double] $TotalReward
    [int]    $EpisodeCount

    # Confusion matrix
    [int]    $TruePositives
    [int]    $FalsePositives
    [int]    $TrueNegatives
    [int]    $FalseNegatives

    [int]    $CurrentSeverity  # raw 0-3 (maps directly to optimal action)

    # Required by VBAF framework
    [int]    $StateSize  = 4
    [int]    $ActionSize = 4

    # Step() stores result here - avoids PSCustomObject type corruption (PS 5.1)
    [double] $LastReward = 0.0
    [bool]   $LastDone   = $false

    DashboardEnvironment() {
        $this.Reset() | Out-Null
    }

    [double[]] GetState() {
        [double[]] $s = @(0.0, 0.0, 0.0, 0.0)
        $s[0] = $this.SeverityNorm
        $s[1] = $this.DataStaleness
        $s[2] = $this.RenderLoad
        $s[3] = $this.UrgencyScore
        return $s
    }

    [double[]] Reset() {
        $this.Steps           = 0
        $this.TotalReward     = 0.0
        $this.CorrectActions  = 0
        $this.MissedUpdates   = 0
        $this.TruePositives   = 0
        $this.FalsePositives  = 0
        $this.TrueNegatives   = 0
        $this.FalseNegatives  = 0
        $this.LastDone        = $false   # CRITICAL: must reset here
        $this.EpisodeCount++
        $this._SampleCondition()
        [double[]] $initState = $this.GetState()
        return $initState
    }

    [void] _SampleCondition() {
        # Balanced training distribution
        # 25% idle (0), 30% normal (1), 25% busy (2), 20% critical (3)
        $roll = Get-Random -Minimum 1 -Maximum 100
        if      ($roll -le 25) { $this.CurrentSeverity = 0 }
        elseif  ($roll -le 55) { $this.CurrentSeverity = 1 }
        elseif  ($roll -le 80) { $this.CurrentSeverity = 2 }
        else                   { $this.CurrentSeverity = 3 }

        [double[]] $snArr = @(0.0)
        $snArr[0]  = $this.CurrentSeverity
        $snArr[0] /= 3.0
        $this.SeverityNorm = $snArr[0]

        switch ($this.CurrentSeverity) {
            0 {
                # Idle: fresh data, low render, minimal alerts
                $this.DataStaleness = [double](Get-Random -Minimum 0  -Maximum 15) / 100.0
                $this.RenderLoad    = [double](Get-Random -Minimum 0  -Maximum 20) / 100.0
                $this.AlertDensity  = [double](Get-Random -Minimum 0  -Maximum 10) / 100.0
            }
            1 {
                # Normal: slightly stale, moderate render, few alerts
                $this.DataStaleness = [double](Get-Random -Minimum 20 -Maximum 45) / 100.0
                $this.RenderLoad    = [double](Get-Random -Minimum 20 -Maximum 55) / 100.0
                $this.AlertDensity  = [double](Get-Random -Minimum 10 -Maximum 30) / 100.0
            }
            2 {
                # Busy: stale data, high render load, active alerts
                $this.DataStaleness = [double](Get-Random -Minimum 50 -Maximum 75) / 100.0
                $this.RenderLoad    = [double](Get-Random -Minimum 55 -Maximum 80) / 100.0
                $this.AlertDensity  = [double](Get-Random -Minimum 30 -Maximum 65) / 100.0
            }
            3 {
                # Critical: very stale, saturated render, flooded alerts
                $this.DataStaleness = [double](Get-Random -Minimum 75  -Maximum 100) / 100.0
                $this.RenderLoad    = [double](Get-Random -Minimum 80  -Maximum 100) / 100.0
                $this.AlertDensity  = [double](Get-Random -Minimum 65  -Maximum 100) / 100.0
            }
        }

        # UrgencyScore: composite signal - always varies with severity
        # Replaces OffHours which is always 0 during business hours
        [double[]] $uArr = @(0.0)
        $uArr[0]  = $this.DataStaleness + $this.AlertDensity
        $uArr[0] /= 2.0
        $this.UrgencyScore = $uArr[0]
    }

    [int] _OptimalAction() {
        # 0=Cache 1=Refresh 2=Prioritise 3=Rebuild
        return $this.CurrentSeverity
    }

    [void] Step([int]$action) {
        $this.Steps++
        $optimal = $this._OptimalAction()

        # Symmetric distance-based reward - same proven pattern as SecurityMonitor
        [int] $dist = $action - $optimal
        if ($dist -lt 0) { $dist = -$dist }

        if    ($dist -eq 0) { $this.LastReward =  2.0; $this.CorrectActions++ }
        elseif($dist -eq 1) { $this.LastReward = -1.0 }
        elseif($dist -eq 2) { $this.LastReward = -2.0 }
        else                { $this.LastReward = -3.0 }

        if ($this.CurrentSeverity -ge 2 -and $action -lt 2) { $this.MissedUpdates++ }

        $isCritical  = ($this.CurrentSeverity -ge 2)
        $agentActs   = ($action -ge 2)
        if ($isCritical  -and $agentActs)  { $this.TruePositives++  }
        if (!$isCritical -and $agentActs)  { $this.FalsePositives++ }
        if (!$isCritical -and !$agentActs) { $this.TrueNegatives++  }
        if ($isCritical  -and !$agentActs) { $this.FalseNegatives++ }

        $this.TotalReward += $this.LastReward
        $this._SampleCondition()
        $this.LastDone = ($this.Steps -ge 200)
    }
}

# ------------------------------------
# Real Windows dashboard data probe
# ------------------------------------
function Get-VBAFDashboardSnapshot {
    [CmdletBinding()]
    param()

    Write-Host ""
    Write-Host " Probing dashboard data sources..." -ForegroundColor Gray

    try {
        # OS memory as render load proxy
        $os = Get-WmiObject -Class Win32_OperatingSystem -ErrorAction Stop
        [double[]] $memArr = @(0.0)
        $memArr[0]  = $os.TotalVisibleMemorySize - $os.FreePhysicalMemory
        $memArr[0] /= $os.TotalVisibleMemorySize
        $memArr[0] *= 100.0
        $usedPct = [Math]::Round($memArr[0], 1)
        Write-Host (" Memory used : {0}%" -f $usedPct) -ForegroundColor $(if ($usedPct -gt 85) { "Red" } elseif ($usedPct -gt 65) { "Yellow" } else { "Green" })

        # Active sessions
        $sessions = query session 2>$null
        $sessionCount = if ($sessions) { ($sessions | Measure-Object -Line).Lines - 1 } else { 1 }
        Write-Host (" Active sessions : {0}" -f $sessionCount) -ForegroundColor White

        # Event log - recent warnings as alert density proxy
        $recentEvents = Get-WinEvent -FilterHashtable @{
            LogName   = 'System'
            Level     = @(2,3)
            StartTime = (Get-Date).AddHours(-1)
        } -ErrorAction SilentlyContinue
        $evCount = if ($recentEvents) { @($recentEvents).Count } else { 0 }
        Write-Host (" System warnings (1h) : {0}" -f $evCount) -ForegroundColor $(if ($evCount -gt 10) { "Yellow" } else { "Green" })

        Write-Host " Dashboard probe : confirmed ✅" -ForegroundColor Green

    } catch {
        Write-Host " [WARNING] Dashboard probe incomplete: $($_.Exception.Message)" -ForegroundColor Yellow
        Write-Host " [INFO] Training will use simulated dashboard conditions."    -ForegroundColor Gray
    }
}

# ============================================================
# MAIN TRAINING FUNCTION
# ============================================================
function Invoke-VBAFDashboardTraining {
    param(
        [int]    $Episodes    = 100,
        [int]    $PrintEvery  = 10,
        [switch] $FastMode,
        [switch] $SimMode,
        [switch] $SkipRealData
    )

    Write-Host ""
    Write-Host "📊 VBAF Enterprise - Phase 15: Enterprise Dashboard"                  -ForegroundColor Cyan
    Write-Host " Training DQN agent on dashboard resource management..."             -ForegroundColor Cyan
    Write-Host " Actions: 0=Cache 1=Refresh 2=Prioritise 3=Rebuild"              -ForegroundColor Yellow
    Write-Host " State : SeverityNorm | Staleness | RenderLoad | UrgencyScore"     -ForegroundColor Yellow
    Write-Host " Reward : +2 correct -1 dist=1 -2 dist=2 -3 dist=3"             -ForegroundColor Yellow
    Write-Host ""

    if (-not $SkipRealData) {
        Get-VBAFDashboardSnapshot
    }

    $dbEnv = [DashboardEnvironment]::new()

    # Phase 1: Baseline - inline random loop
    Write-Host " Phase 1: Baseline (random agent - 10 episodes)..." -ForegroundColor Gray
    $baseRewards = @()
    for ($b = 1; $b -le 10; $b++) {
        $dbEnv.Reset() | Out-Null
        $bReward = 0.0
        while (-not $dbEnv.LastDone) {
            $rAction  = Get-Random -Minimum 0 -Maximum 4
            $dbEnv.Step($rAction)
            $bReward += $dbEnv.LastReward
        }
        $baseRewards += $bReward
    }
    [double[]] $bAvgArr = @(0.0)
    $bAvgArr[0] = ($baseRewards | Measure-Object -Average).Average
    Write-Host (" Baseline avg reward: {0:F2}" -f $bAvgArr[0]) -ForegroundColor Gray

    if ($FastMode) { $Episodes = [Math]::Min($Episodes, 30) }
    Write-Host ""
    Write-Host " Phase 2: Training DQN agent ($Episodes episodes)..." -ForegroundColor Gray

    $config              = [DQNConfig]::new()
    $config.StateSize    = 4
    $config.ActionSize   = 4
    $config.EpsilonDecay = 0.9995
    $config.EpsilonMin   = 0.05
    [int[]] $arch        = @(4, 24, 24, 4)
    $mainNetwork         = [NeuralNetwork]::new($arch, $config.LearningRate)
    $targetNetwork       = [NeuralNetwork]::new($arch, $config.LearningRate)
    $memory              = [ExperienceReplay]::new($config.MemorySize)
    $agent               = [DQNAgent]::new($config, $mainNetwork, $targetNetwork, $memory)

    $results = [System.Collections.Generic.List[object]]::new()

    for ($ep = 1; $ep -le $Episodes; $ep++) {

        [double[]] $state = @(0.0, 0.0, 0.0, 0.0)

        if ($SimMode) {
            $roll = Get-Random -Minimum 1 -Maximum 100
            if      ($roll -le 25) { $dbEnv.CurrentSeverity = 0 }
            elseif  ($roll -le 55) { $dbEnv.CurrentSeverity = 1 }
            elseif  ($roll -le 80) { $dbEnv.CurrentSeverity = 2 }
            else                   { $dbEnv.CurrentSeverity = 3 }

            [double[]] $snArr = @(0.0)
            $snArr[0]  = $dbEnv.CurrentSeverity
            $snArr[0] /= 3.0
            $dbEnv.SeverityNorm = $snArr[0]

            switch ($dbEnv.CurrentSeverity) {
                0 {
                    $dbEnv.DataStaleness = [double](Get-Random -Minimum 0  -Maximum 15) / 100.0
                    $dbEnv.RenderLoad    = [double](Get-Random -Minimum 0  -Maximum 20) / 100.0
                    $dbEnv.AlertDensity  = [double](Get-Random -Minimum 0  -Maximum 10) / 100.0
                }
                1 {
                    $dbEnv.DataStaleness = [double](Get-Random -Minimum 20 -Maximum 45) / 100.0
                    $dbEnv.RenderLoad    = [double](Get-Random -Minimum 20 -Maximum 55) / 100.0
                    $dbEnv.AlertDensity  = [double](Get-Random -Minimum 10 -Maximum 30) / 100.0
                }
                2 {
                    $dbEnv.DataStaleness = [double](Get-Random -Minimum 50 -Maximum 75) / 100.0
                    $dbEnv.RenderLoad    = [double](Get-Random -Minimum 55 -Maximum 80) / 100.0
                    $dbEnv.AlertDensity  = [double](Get-Random -Minimum 30 -Maximum 65) / 100.0
                }
                3 {
                    $dbEnv.DataStaleness = [double](Get-Random -Minimum 75  -Maximum 100) / 100.0
                    $dbEnv.RenderLoad    = [double](Get-Random -Minimum 80  -Maximum 100) / 100.0
                    $dbEnv.AlertDensity  = [double](Get-Random -Minimum 65  -Maximum 100) / 100.0
                }
            }

            # UrgencyScore: composite - always varies, no dead signal
            [double[]] $uArr = @(0.0)
            $uArr[0]  = $dbEnv.DataStaleness + $dbEnv.AlertDensity
            $uArr[0] /= 2.0
            $dbEnv.UrgencyScore = $uArr[0]

            $dbEnv.CorrectActions = 0
            $dbEnv.MissedUpdates  = 0
            $dbEnv.Steps          = 0
            $dbEnv.TotalReward    = 0.0
            $dbEnv.LastDone       = $false
            $dbEnv.EpisodeCount++
            $state = $dbEnv.GetState()
        } else {
            $state = $dbEnv.Reset()
        }

        $done              = $false
        $epReward          = 0.0
        $cacheCount        = 0
        $refreshCount      = 0
        $prioritiseCount   = 0
        $rebuildCount      = 0
        [int] $stepCount   = 0

        while (-not $done) {
            $action = $agent.Act($state)
            $dbEnv.Step($action)
            [double[]] $nextState = $dbEnv.GetState()
            [double]   $reward    = $dbEnv.LastReward
            [bool]     $isDone    = $dbEnv.LastDone
            $agent.Remember($state, $action, $reward, $nextState, $isDone)
            $stepCount++
            if ($stepCount % 4 -eq 0) { $agent.Replay() }
            $state     = $nextState
            $done      = $isDone
            $epReward += $reward
            switch ($action) {
                0 { $cacheCount++      }
                1 { $refreshCount++    }
                2 { $prioritiseCount++ }
                3 { $rebuildCount++    }
            }
        }

        $agent.EndEpisode($epReward)
        $results.Add(@{
            Episode    = $ep
            Reward     = $epReward
            Cache      = $cacheCount
            Refresh    = $refreshCount
            Prioritise = $prioritiseCount
            Rebuild    = $rebuildCount
            Epsilon    = $agent.Epsilon
        })

        if ($ep % $PrintEvery -eq 0) {
            $lastN  = $results | Select-Object -Last $PrintEvery
            $avgSum = 0.0
            foreach ($r2 in $lastN) { $avgSum += $r2.Reward }
            [double[]] $avgArr = @(0.0)
            $avgArr[0]  = $avgSum
            $avgArr[0] /= $lastN.Count
            $avg = [Math]::Round($avgArr[0], 2)
            Write-Host (" Ep {0,4}/{1} AvgReward: {2,7} Eps: {3:F3} Cac:{4} Ref:{5} Pri:{6} Rbd:{7}" -f `
                $ep, $Episodes, $avg, $agent.Epsilon, $cacheCount, $refreshCount, $prioritiseCount, $rebuildCount) -ForegroundColor White
        }
    }

    # Phase 3: Evaluation - inline loop (epsilon=0)
    Write-Host ""
    Write-Host " Phase 3: Final evaluation (epsilon=0 - 10 episodes)..." -ForegroundColor Gray
    $agent.Epsilon = 0.0
    $trainedRewards = @()
    for ($t = 1; $t -le 10; $t++) {
        [double[]] $evalState = $dbEnv.Reset()
        $tReward = 0.0
        while (-not $dbEnv.LastDone) {
            $tAction = $agent.Act($evalState)
            $dbEnv.Step($tAction)
            [double[]] $evalState = $dbEnv.GetState()
            $tReward += $dbEnv.LastReward
        }
        $trainedRewards += $tReward
    }
    [double[]] $tAvgArr = @(0.0)
    $tAvgArr[0] = ($trainedRewards | Measure-Object -Average).Average
    Write-Host (" Trained avg reward: {0:F2}" -f $tAvgArr[0]) -ForegroundColor Green

    [double[]] $impArr = @(0.0)
    if ($bAvgArr[0] -ne 0) {
        $impArr[0]  = $tAvgArr[0] - $bAvgArr[0]
        $impArr[0] /= [Math]::Abs($bAvgArr[0])
        $impArr[0] *= 100.0
    }
    $bAvg        = [Math]::Round($bAvgArr[0], 2)
    $tAvg        = [Math]::Round($tAvgArr[0], 2)
    $improvement = [Math]::Round($impArr[0], 1)

    [double[]] $precArr = @(0.0)
    [double[]] $recArr  = @(0.0)
    $denomP = $dbEnv.TruePositives + $dbEnv.FalsePositives
    $denomR = $dbEnv.TruePositives + $dbEnv.FalseNegatives
    if ($denomP -gt 0) { $precArr[0] = $dbEnv.TruePositives; $precArr[0] /= $denomP }
    if ($denomR -gt 0) { $recArr[0]  = $dbEnv.TruePositives; $recArr[0]  /= $denomR }
    $precPct = [Math]::Round($precArr[0] * 100, 1)
    $recPct  = [Math]::Round($recArr[0]  * 100, 1)

    Write-Host ""
    Write-Host "╔══════════════════════════════════════════════════╗" -ForegroundColor Cyan
    Write-Host "║ Phase 15: Enterprise Dashboard - Results ║" -ForegroundColor Cyan
    Write-Host "╠══════════════════════════════════════════════════╣" -ForegroundColor Cyan
    Write-Host ("║ Baseline (random) avg reward : {0,8} ║" -f $bAvg)        -ForegroundColor Gray
    Write-Host ("║ Trained (DQN) avg reward : {0,8} ║" -f $tAvg)        -ForegroundColor Green
    Write-Host ("║ Improvement : {0,7}% ║" -f $improvement) -ForegroundColor Yellow
    Write-Host "╠══════════════════════════════════════════════════╣" -ForegroundColor Cyan
    Write-Host ("║ Precision (Pri+Rebuild correct): {0,6}% ║" -f $precPct)    -ForegroundColor Cyan
    Write-Host ("║ Recall (critical updates) : {0,7}% ║" -f $recPct)      -ForegroundColor Cyan
    Write-Host "╠══════════════════════════════════════════════════╣" -ForegroundColor Cyan
    Write-Host "║ Agent learned to: ║" -ForegroundColor Cyan
    Write-Host "║ Cache serve fresh cached data ║" -ForegroundColor White
    Write-Host "║ Refresh update panels on staleness ║" -ForegroundColor White
    Write-Host "║ Prioritise elevate critical KPIs ║" -ForegroundColor White
    Write-Host "║ Rebuild full reload on critical state ║" -ForegroundColor White
    Write-Host "╚══════════════════════════════════════════════════╝" -ForegroundColor Cyan
    Write-Host ""

    return @{ Agent = $agent; Results = $results; Baseline = @{ Avg = $bAvg }; Trained = @{ Avg = $tAvg } }
}

# ============================================================
# TEST SUGGESTIONS
# ============================================================
# 1. Run VBAF.LoadAll.ps1 (loads core DQN + all pillars)
#
# 2. QUICK DEMO (simulated dashboard conditions)
# $r = Invoke-VBAFDashboardTraining -Episodes 100 -PrintEvery 10 -SimMode
#
# 3. FULL TRAINING (real WMI memory, sessions, event log)
# $r = Invoke-VBAFDashboardTraining -Episodes 100 -PrintEvery 10
#
# 4. SKIP REAL DATA PROBE
# $r = Invoke-VBAFDashboardTraining -Episodes 100 -PrintEvery 10 -SkipRealData
#
# 5. INSPECT AGENT DECISIONS
# $env = [DashboardEnvironment]::new()
# $state = $env.Reset()
# Write-Host "Staleness: $($env.DataStaleness) UrgencyScore: $($env.UrgencyScore)"
# $action = $r.Agent.Act($state)
# $labels = @("Cache","Refresh","Prioritise","Rebuild")
# Write-Host "Dashboard decision: $($labels[$action])"
#
# 6. VIEW CONFUSION MATRIX
# Write-Host "True Positives : $($env.TruePositives)"
# Write-Host "False Positives: $($env.FalsePositives)"
# Write-Host "True Negatives : $($env.TrueNegatives)"
# Write-Host "False Negatives: $($env.FalseNegatives)"
# ============================================================
Write-Host "📦 VBAF.Enterprise.Dashboard.ps1 loaded [v3.5.0 📊]"    -ForegroundColor Green
Write-Host " Phase 15 : Enterprise Dashboard Intelligence"           -ForegroundColor Cyan
Write-Host " Function : Invoke-VBAFDashboardTraining"                -ForegroundColor Cyan
Write-Host ""
Write-Host " Quick start:" -ForegroundColor Yellow
Write-Host ' $r = Invoke-VBAFDashboardTraining -Episodes 100 -PrintEvery 10 -SimMode' -ForegroundColor White
Write-Host ""