Merge branch 'main' of https://git.stella-ops.org/stella-ops.org/git.stella-ops.org
Some checks failed
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Docs CI / lint-and-preview (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
api-governance / spectral-lint (push) Has been cancelled
oas-ci / oas-validate (push) Has been cancelled
Policy Simulation / policy-simulate (push) Has been cancelled
sdk-generator-smoke / sdk-smoke (push) Has been cancelled
SDK Publish & Sign / sdk-publish (push) Has been cancelled
Some checks failed
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Docs CI / lint-and-preview (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
api-governance / spectral-lint (push) Has been cancelled
oas-ci / oas-validate (push) Has been cancelled
Policy Simulation / policy-simulate (push) Has been cancelled
sdk-generator-smoke / sdk-smoke (push) Has been cancelled
SDK Publish & Sign / sdk-publish (push) Has been cancelled
This commit is contained in:
@@ -1,221 +1,221 @@
|
||||
using System.Collections.Immutable;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Notify.Models;
|
||||
using StellaOps.Notify.Storage.Mongo.Repositories;
|
||||
|
||||
namespace StellaOps.Notifier.Worker.Escalation;
|
||||
|
||||
/// <summary>
|
||||
/// Default implementation of on-call schedule resolution.
|
||||
/// </summary>
|
||||
public sealed class DefaultOnCallResolver : IOnCallResolver
|
||||
{
|
||||
private readonly INotifyOnCallScheduleRepository? _scheduleRepository;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
private readonly ILogger<DefaultOnCallResolver> _logger;
|
||||
|
||||
public DefaultOnCallResolver(
|
||||
TimeProvider timeProvider,
|
||||
ILogger<DefaultOnCallResolver> logger,
|
||||
INotifyOnCallScheduleRepository? scheduleRepository = null)
|
||||
{
|
||||
_timeProvider = timeProvider ?? TimeProvider.System;
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_scheduleRepository = scheduleRepository;
|
||||
}
|
||||
|
||||
public async Task<NotifyOnCallResolution> ResolveAsync(
|
||||
string tenantId,
|
||||
string scheduleId,
|
||||
DateTimeOffset? evaluationTime = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(tenantId);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(scheduleId);
|
||||
|
||||
if (_scheduleRepository 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);
|
||||
|
||||
if (schedule is null)
|
||||
{
|
||||
_logger.LogWarning("On-call schedule {ScheduleId} not found for tenant {TenantId}", scheduleId, tenantId);
|
||||
return new NotifyOnCallResolution(scheduleId, evaluationTime ?? _timeProvider.GetUtcNow(), ImmutableArray<NotifyOnCallParticipant>.Empty);
|
||||
}
|
||||
|
||||
return ResolveAt(schedule, evaluationTime ?? _timeProvider.GetUtcNow());
|
||||
}
|
||||
|
||||
public NotifyOnCallResolution ResolveAt(
|
||||
NotifyOnCallSchedule schedule,
|
||||
DateTimeOffset evaluationTime)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schedule);
|
||||
|
||||
// Check for active override first
|
||||
var activeOverride = schedule.Overrides
|
||||
.FirstOrDefault(o => o.IsActiveAt(evaluationTime));
|
||||
|
||||
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)
|
||||
{
|
||||
_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);
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
using System.Collections.Immutable;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Notify.Models;
|
||||
using StellaOps.Notify.Storage.Mongo.Repositories;
|
||||
|
||||
namespace StellaOps.Notifier.Worker.Escalation;
|
||||
|
||||
/// <summary>
|
||||
/// Default implementation of on-call schedule resolution.
|
||||
/// </summary>
|
||||
public sealed class DefaultOnCallResolver : IOnCallResolver
|
||||
{
|
||||
private readonly INotifyOnCallScheduleRepository? _scheduleRepository;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
private readonly ILogger<DefaultOnCallResolver> _logger;
|
||||
|
||||
public DefaultOnCallResolver(
|
||||
TimeProvider timeProvider,
|
||||
ILogger<DefaultOnCallResolver> logger,
|
||||
INotifyOnCallScheduleRepository? scheduleRepository = null)
|
||||
{
|
||||
_timeProvider = timeProvider ?? TimeProvider.System;
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_scheduleRepository = scheduleRepository;
|
||||
}
|
||||
|
||||
public async Task<NotifyOnCallResolution> ResolveAsync(
|
||||
string tenantId,
|
||||
string scheduleId,
|
||||
DateTimeOffset? evaluationTime = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(tenantId);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(scheduleId);
|
||||
|
||||
if (_scheduleRepository 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);
|
||||
|
||||
if (schedule is null)
|
||||
{
|
||||
_logger.LogWarning("On-call schedule {ScheduleId} not found for tenant {TenantId}", scheduleId, tenantId);
|
||||
return new NotifyOnCallResolution(scheduleId, evaluationTime ?? _timeProvider.GetUtcNow(), ImmutableArray<NotifyOnCallParticipant>.Empty);
|
||||
}
|
||||
|
||||
return ResolveAt(schedule, evaluationTime ?? _timeProvider.GetUtcNow());
|
||||
}
|
||||
|
||||
public NotifyOnCallResolution ResolveAt(
|
||||
NotifyOnCallSchedule schedule,
|
||||
DateTimeOffset evaluationTime)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schedule);
|
||||
|
||||
// Check for active override first
|
||||
var activeOverride = schedule.Overrides
|
||||
.FirstOrDefault(o => o.IsActiveAt(evaluationTime));
|
||||
|
||||
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)
|
||||
{
|
||||
_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);
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user