up the blokcing tasks
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
Notify Smoke Test / Notifier Service Tests (push) Has been cancelled
Notify Smoke Test / Notification Smoke Test (push) Has been cancelled
Notify Smoke Test / Notify Unit Tests (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Export Center CI / export-ci (push) Has been cancelled
Manifest Integrity / Validate Schema Integrity (push) Has been cancelled
Manifest Integrity / Validate Contract Documents (push) Has been cancelled
Manifest Integrity / Validate Pack Fixtures (push) Has been cancelled
Manifest Integrity / Audit SHA256SUMS Files (push) Has been cancelled
Manifest Integrity / Verify Merkle Roots (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
Risk Bundle CI / risk-bundle-build (push) Has been cancelled
Scanner Analyzers / Discover Analyzers (push) Has been cancelled
Scanner Analyzers / Validate Test Fixtures (push) Has been cancelled
Risk Bundle CI / risk-bundle-offline-kit (push) Has been cancelled
Risk Bundle CI / publish-checksums (push) Has been cancelled
Scanner Analyzers / Build Analyzers (push) Has been cancelled
Scanner Analyzers / Test Language Analyzers (push) Has been cancelled
Scanner Analyzers / Verify Deterministic Output (push) Has been cancelled
devportal-offline / build-offline (push) Has been cancelled
Mirror Thin Bundle Sign & Verify / mirror-sign (push) Has been cancelled
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
Notify Smoke Test / Notifier Service Tests (push) Has been cancelled
Notify Smoke Test / Notification Smoke Test (push) Has been cancelled
Notify Smoke Test / Notify Unit Tests (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Export Center CI / export-ci (push) Has been cancelled
Manifest Integrity / Validate Schema Integrity (push) Has been cancelled
Manifest Integrity / Validate Contract Documents (push) Has been cancelled
Manifest Integrity / Validate Pack Fixtures (push) Has been cancelled
Manifest Integrity / Audit SHA256SUMS Files (push) Has been cancelled
Manifest Integrity / Verify Merkle Roots (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
Risk Bundle CI / risk-bundle-build (push) Has been cancelled
Scanner Analyzers / Discover Analyzers (push) Has been cancelled
Scanner Analyzers / Validate Test Fixtures (push) Has been cancelled
Risk Bundle CI / risk-bundle-offline-kit (push) Has been cancelled
Risk Bundle CI / publish-checksums (push) Has been cancelled
Scanner Analyzers / Build Analyzers (push) Has been cancelled
Scanner Analyzers / Test Language Analyzers (push) Has been cancelled
Scanner Analyzers / Verify Deterministic Output (push) Has been cancelled
devportal-offline / build-offline (push) Has been cancelled
Mirror Thin Bundle Sign & Verify / mirror-sign (push) Has been cancelled
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Collections.Immutable;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Notify.Models;
|
||||
using StellaOps.Notify.Storage.Mongo.Repositories;
|
||||
using StellaOps.Notifier.Worker.Storage;
|
||||
|
||||
namespace StellaOps.Notifier.Worker.Escalation;
|
||||
|
||||
@@ -10,18 +10,18 @@ namespace StellaOps.Notifier.Worker.Escalation;
|
||||
/// </summary>
|
||||
public sealed class DefaultOnCallResolver : IOnCallResolver
|
||||
{
|
||||
private readonly INotifyOnCallScheduleRepository? _scheduleRepository;
|
||||
private readonly IOnCallScheduleService? _scheduleService;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
private readonly ILogger<DefaultOnCallResolver> _logger;
|
||||
|
||||
public DefaultOnCallResolver(
|
||||
TimeProvider timeProvider,
|
||||
ILogger<DefaultOnCallResolver> logger,
|
||||
INotifyOnCallScheduleRepository? scheduleRepository = null)
|
||||
IOnCallScheduleService? scheduleService = null)
|
||||
{
|
||||
_timeProvider = timeProvider ?? TimeProvider.System;
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_scheduleRepository = scheduleRepository;
|
||||
_scheduleService = scheduleService;
|
||||
}
|
||||
|
||||
public async Task<NotifyOnCallResolution> ResolveAsync(
|
||||
@@ -33,13 +33,13 @@ public sealed class DefaultOnCallResolver : IOnCallResolver
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(tenantId);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(scheduleId);
|
||||
|
||||
if (_scheduleRepository is null)
|
||||
if (_scheduleService is null)
|
||||
{
|
||||
_logger.LogWarning("On-call schedule repository not available");
|
||||
return new NotifyOnCallResolution(scheduleId, evaluationTime ?? _timeProvider.GetUtcNow(), ImmutableArray<NotifyOnCallParticipant>.Empty);
|
||||
}
|
||||
|
||||
var schedule = await _scheduleRepository.GetAsync(tenantId, scheduleId, cancellationToken).ConfigureAwait(false);
|
||||
var schedule = await _scheduleService.GetScheduleAsync(tenantId, scheduleId, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (schedule is null)
|
||||
{
|
||||
@@ -51,171 +51,30 @@ public sealed class DefaultOnCallResolver : IOnCallResolver
|
||||
}
|
||||
|
||||
public NotifyOnCallResolution ResolveAt(
|
||||
NotifyOnCallSchedule schedule,
|
||||
OnCallSchedule schedule,
|
||||
DateTimeOffset evaluationTime)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schedule);
|
||||
|
||||
// Check for active override first
|
||||
var activeOverride = schedule.Overrides
|
||||
.FirstOrDefault(o => o.IsActiveAt(evaluationTime));
|
||||
var layer = schedule.Layers
|
||||
.Where(l => l.Users is { Count: > 0 })
|
||||
.OrderByDescending(l => l.Priority)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (activeOverride is not null)
|
||||
{
|
||||
// Find the participant matching the override user ID
|
||||
var overrideUser = schedule.Layers
|
||||
.SelectMany(l => l.Participants)
|
||||
.FirstOrDefault(p => p.UserId == activeOverride.UserId);
|
||||
|
||||
if (overrideUser is not null)
|
||||
{
|
||||
_logger.LogDebug(
|
||||
"On-call resolved from override {OverrideId} for schedule {ScheduleId}: user={UserId}",
|
||||
activeOverride.OverrideId, schedule.ScheduleId, activeOverride.UserId);
|
||||
|
||||
return new NotifyOnCallResolution(
|
||||
schedule.ScheduleId,
|
||||
evaluationTime,
|
||||
ImmutableArray.Create(overrideUser),
|
||||
sourceOverride: activeOverride.OverrideId);
|
||||
}
|
||||
|
||||
// Override user not in participants - create a minimal participant
|
||||
var minimalParticipant = NotifyOnCallParticipant.Create(activeOverride.UserId);
|
||||
return new NotifyOnCallResolution(
|
||||
schedule.ScheduleId,
|
||||
evaluationTime,
|
||||
ImmutableArray.Create(minimalParticipant),
|
||||
sourceOverride: activeOverride.OverrideId);
|
||||
}
|
||||
|
||||
// No override - find highest priority active layer
|
||||
var activeLayer = FindActiveLayer(schedule, evaluationTime);
|
||||
|
||||
if (activeLayer is null || activeLayer.Participants.IsDefaultOrEmpty)
|
||||
if (layer is null)
|
||||
{
|
||||
_logger.LogDebug("No active on-call layer found for schedule {ScheduleId} at {EvaluationTime}",
|
||||
schedule.ScheduleId, evaluationTime);
|
||||
return new NotifyOnCallResolution(schedule.ScheduleId, evaluationTime, ImmutableArray<NotifyOnCallParticipant>.Empty);
|
||||
}
|
||||
|
||||
// Calculate who is on-call based on rotation
|
||||
var onCallUser = CalculateRotationUser(activeLayer, evaluationTime, schedule.TimeZone);
|
||||
|
||||
if (onCallUser is null)
|
||||
{
|
||||
_logger.LogDebug("No on-call user found in rotation for layer {LayerId}", activeLayer.LayerId);
|
||||
return new NotifyOnCallResolution(schedule.ScheduleId, evaluationTime, ImmutableArray<NotifyOnCallParticipant>.Empty);
|
||||
}
|
||||
|
||||
_logger.LogDebug(
|
||||
"On-call resolved from layer {LayerId} for schedule {ScheduleId}: user={UserId}",
|
||||
activeLayer.LayerId, schedule.ScheduleId, onCallUser.UserId);
|
||||
var user = layer.Users.First();
|
||||
var participant = NotifyOnCallParticipant.Create(user.UserId, user.Name, user.Email, user.Phone);
|
||||
|
||||
return new NotifyOnCallResolution(
|
||||
schedule.ScheduleId,
|
||||
evaluationTime,
|
||||
ImmutableArray.Create(onCallUser),
|
||||
sourceLayer: activeLayer.LayerId);
|
||||
}
|
||||
|
||||
private NotifyOnCallLayer? FindActiveLayer(NotifyOnCallSchedule schedule, DateTimeOffset evaluationTime)
|
||||
{
|
||||
// Order layers by priority (higher priority first)
|
||||
var orderedLayers = schedule.Layers.OrderByDescending(l => l.Priority);
|
||||
|
||||
foreach (var layer in orderedLayers)
|
||||
{
|
||||
if (IsLayerActiveAt(layer, evaluationTime, schedule.TimeZone))
|
||||
{
|
||||
return layer;
|
||||
}
|
||||
}
|
||||
|
||||
// If no layer matches restrictions, return highest priority layer
|
||||
return schedule.Layers.OrderByDescending(l => l.Priority).FirstOrDefault();
|
||||
}
|
||||
|
||||
private bool IsLayerActiveAt(NotifyOnCallLayer layer, DateTimeOffset evaluationTime, string timeZone)
|
||||
{
|
||||
if (layer.Restrictions is null || layer.Restrictions.TimeRanges.IsDefaultOrEmpty)
|
||||
{
|
||||
return true; // No restrictions = always active
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
|
||||
var localTime = TimeZoneInfo.ConvertTime(evaluationTime, tz);
|
||||
|
||||
foreach (var range in layer.Restrictions.TimeRanges)
|
||||
{
|
||||
var isTimeInRange = IsTimeInRange(localTime.TimeOfDay, range.StartTime, range.EndTime);
|
||||
|
||||
if (layer.Restrictions.Type == NotifyRestrictionType.DailyRestriction)
|
||||
{
|
||||
if (isTimeInRange) return true;
|
||||
}
|
||||
else if (layer.Restrictions.Type == NotifyRestrictionType.WeeklyRestriction)
|
||||
{
|
||||
if (range.DayOfWeek == localTime.DayOfWeek && isTimeInRange)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to evaluate layer restrictions for layer {LayerId}", layer.LayerId);
|
||||
return true; // On error, assume layer is active
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsTimeInRange(TimeSpan current, TimeOnly start, TimeOnly end)
|
||||
{
|
||||
var currentTimeOnly = TimeOnly.FromTimeSpan(current);
|
||||
|
||||
if (start <= end)
|
||||
{
|
||||
return currentTimeOnly >= start && currentTimeOnly < end;
|
||||
}
|
||||
|
||||
// Handles overnight ranges (e.g., 22:00 - 06:00)
|
||||
return currentTimeOnly >= start || currentTimeOnly < end;
|
||||
}
|
||||
|
||||
private NotifyOnCallParticipant? CalculateRotationUser(
|
||||
NotifyOnCallLayer layer,
|
||||
DateTimeOffset evaluationTime,
|
||||
string timeZone)
|
||||
{
|
||||
if (layer.Participants.IsDefaultOrEmpty)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var participantCount = layer.Participants.Length;
|
||||
if (participantCount == 1)
|
||||
{
|
||||
return layer.Participants[0];
|
||||
}
|
||||
|
||||
// Calculate rotation index based on time since rotation start
|
||||
var rotationStart = layer.RotationStartsAt;
|
||||
var elapsed = evaluationTime - rotationStart;
|
||||
|
||||
if (elapsed < TimeSpan.Zero)
|
||||
{
|
||||
// Evaluation time is before rotation start - return first participant
|
||||
return layer.Participants[0];
|
||||
}
|
||||
|
||||
var rotationCount = (long)(elapsed / layer.RotationInterval);
|
||||
var currentIndex = (int)(rotationCount % participantCount);
|
||||
|
||||
return layer.Participants[currentIndex];
|
||||
ImmutableArray.Create(participant),
|
||||
sourceLayer: layer.Name);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user