|
| 1 | +function Get-CippAuditLogPlannedWindows { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Compute the 35-minute audit-log search windows a tenant is missing (gaps + the newest settled). |
| 5 | + .DESCRIPTION |
| 6 | + Pure helper for the V2 audit-log pipeline. Windows are 35 minutes long on a 30-minute stride, |
| 7 | + so consecutive windows overlap by 5 minutes (covers boundary stragglers; alerting dedups by |
| 8 | + record id). Window ENDS sit on the 30-minute grid minus the settle (i.e. :25 / :55), which is |
| 9 | + exactly `floor_to_30min(now) - settle`. With the planner timer firing at :00/:15/:30/:45 and a |
| 10 | + 5-minute settle, a fresh window becomes creatable exactly at a :00/:30 tick - no tick delay - |
| 11 | + and the :15/:45 ticks naturally have no new window (they do retries + download/process). |
| 12 | +
|
| 13 | + Backfill of older gaps is bounded by -HorizonHours and capped at -MaxPerRun per call (oldest |
| 14 | + first). A brand-new tenant is seeded with only the newest settled window. |
| 15 | + .PARAMETER ExistingRows |
| 16 | + The tenant's current AuditLogCoverage rows. Reconciliation rows (RowKey 'RECON-*') are ignored |
| 17 | + here; only regular 14-digit window keys are considered. |
| 18 | + .PARAMETER Now |
| 19 | + Reference time (UTC). Defaults to now. |
| 20 | + .OUTPUTS |
| 21 | + Array of [pscustomobject]@{ RowKey; WindowStart; WindowEnd } sorted oldest-first. |
| 22 | + .FUNCTIONALITY |
| 23 | + Internal |
| 24 | + #> |
| 25 | + [CmdletBinding()] |
| 26 | + param( |
| 27 | + [object[]]$ExistingRows, |
| 28 | + [datetime]$Now = (Get-Date).ToUniversalTime(), |
| 29 | + [int]$SettleMinutes = 5, |
| 30 | + [int]$WindowMinutes = 35, |
| 31 | + [int]$StrideMinutes = 30, |
| 32 | + [int]$HorizonHours = 24, |
| 33 | + [int]$MaxPerRun = 6 |
| 34 | + ) |
| 35 | + |
| 36 | + $Now = $Now.ToUniversalTime() |
| 37 | + |
| 38 | + # Newest window end: floor to the 30-min grid, minus the settle (lands on :25 / :55). |
| 39 | + $FloorMinute = $Now.Minute - ($Now.Minute % $StrideMinutes) |
| 40 | + $Floor = [datetime]::new($Now.Year, $Now.Month, $Now.Day, $Now.Hour, $FloorMinute, 0, [System.DateTimeKind]::Utc) |
| 41 | + $NewestEnd = $Floor.AddMinutes(-$SettleMinutes) |
| 42 | + |
| 43 | + $HorizonStart = $Now.AddHours(-$HorizonHours) |
| 44 | + |
| 45 | + # Existing regular window keys (ignore reconciliation rows). |
| 46 | + $ExistingKeys = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) |
| 47 | + $ExistingStarts = [System.Collections.Generic.List[datetime]]::new() |
| 48 | + foreach ($Row in $ExistingRows) { |
| 49 | + if ($Row.RowKey -notmatch '^\d{14}$') { continue } |
| 50 | + [void]$ExistingKeys.Add([string]$Row.RowKey) |
| 51 | + if ($null -ne $Row.WindowStart) { |
| 52 | + try { $ExistingStarts.Add(([datetimeoffset]$Row.WindowStart).UtcDateTime) } catch {} |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + # Brand-new tenant: seed only the newest settled window. |
| 57 | + if ($ExistingStarts.Count -eq 0) { |
| 58 | + $Start = $NewestEnd.AddMinutes(-$WindowMinutes) |
| 59 | + if ($Start -lt $HorizonStart) { return @() } |
| 60 | + return , ([pscustomobject]@{ |
| 61 | + RowKey = $Start.ToString('yyyyMMddHHmmss') |
| 62 | + WindowStart = $Start |
| 63 | + WindowEnd = $NewestEnd |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + # Established tenant: backfill missing windows from the lower bound up to NewestEnd (oldest first). |
| 68 | + $EarliestExisting = ($ExistingStarts | Measure-Object -Minimum).Minimum |
| 69 | + $LowerEnd = if ($EarliestExisting -gt $HorizonStart) { $EarliestExisting.AddMinutes($WindowMinutes) } else { $HorizonStart.AddMinutes($WindowMinutes) } |
| 70 | + |
| 71 | + $Owed = [System.Collections.Generic.List[object]]::new() |
| 72 | + $End = $NewestEnd |
| 73 | + while ($End -ge $LowerEnd) { |
| 74 | + $Start = $End.AddMinutes(-$WindowMinutes) |
| 75 | + $Key = $Start.ToString('yyyyMMddHHmmss') |
| 76 | + if (-not $ExistingKeys.Contains($Key)) { |
| 77 | + $Owed.Add([pscustomobject]@{ RowKey = $Key; WindowStart = $Start; WindowEnd = $End }) |
| 78 | + } |
| 79 | + $End = $End.AddMinutes(-$StrideMinutes) |
| 80 | + } |
| 81 | + |
| 82 | + # $Owed is newest-first from the loop; reorder to oldest-first ([0]=oldest, [-1]=newest). |
| 83 | + $Owed.Reverse() |
| 84 | + if ($Owed.Count -le $MaxPerRun) { |
| 85 | + return @($Owed) |
| 86 | + } |
| 87 | + |
| 88 | + # Backlog exceeds the per-run cap: always include the NEWEST window so the live period can |
| 89 | + # be created promptly (current-first, see Push-AuditLogSearchCreationV2), plus the oldest |
| 90 | + # (MaxPerRun-1) so historical gaps still drain - oldest first - before they age out of the |
| 91 | + # horizon. Without seeding the newest here it would never be Planned during a backlog. |
| 92 | + $Newest = $Owed[$Owed.Count - 1] |
| 93 | + $Backfill = @($Owed[0..($MaxPerRun - 2)]) |
| 94 | + return @($Backfill + $Newest) |
| 95 | +} |
0 commit comments