diff --git a/src/Fusion.Resources.Functions.Common/Integration/Http/Handlers/SummaryHttpHandler.cs b/src/Fusion.Resources.Functions.Common/Integration/Http/Handlers/SummaryHttpHandler.cs index 81404577a..630974c8f 100644 --- a/src/Fusion.Resources.Functions.Common/Integration/Http/Handlers/SummaryHttpHandler.cs +++ b/src/Fusion.Resources.Functions.Common/Integration/Http/Handlers/SummaryHttpHandler.cs @@ -10,7 +10,7 @@ public class SummaryHttpHandler : FunctionHttpMessageHandler private readonly IOptions options; public SummaryHttpHandler(ILoggerFactory logger, ITokenProvider tokenProvider, IServiceDiscovery serviceDiscovery, IOptions options) - : base(logger.CreateLogger(), tokenProvider, serviceDiscovery) + : base(logger.CreateLogger(), tokenProvider, serviceDiscovery) { this.options = options; } diff --git a/src/Fusion.Summary.Functions/Functions/WeeklyDepartmentSummarySender.cs b/src/Fusion.Summary.Functions/Functions/WeeklyDepartmentSummarySender.cs index 7e78090fb..cbfd031ce 100644 --- a/src/Fusion.Summary.Functions/Functions/WeeklyDepartmentSummarySender.cs +++ b/src/Fusion.Summary.Functions/Functions/WeeklyDepartmentSummarySender.cs @@ -23,6 +23,7 @@ public class WeeklyDepartmentSummarySender private int _maxDegreeOfParallelism; private readonly string[] _departmentFilter; + private bool _sendingNotificationEnabled = true; // Default to true so that we don't accidentally disable sending notifications public WeeklyDepartmentSummarySender(ISummaryApiClient summaryApiClient, INotificationApiClient notificationApiClient, ILogger logger, IConfiguration configuration) @@ -34,13 +35,40 @@ public WeeklyDepartmentSummarySender(ISummaryApiClient summaryApiClient, INotifi _maxDegreeOfParallelism = int.TryParse(configuration["weekly-department-summary-sender-parallelism"], out var result) ? result : 2; _departmentFilter = configuration["departmentFilter"]?.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) ?? ["PRD"]; + + // Need to explicitly add the configuration key to the app settings to disable sending of notifications + if (int.TryParse(configuration["isSendingNotificationEnabled"], out var enabled)) + _sendingNotificationEnabled = enabled == 1; + else if (bool.TryParse(configuration["isSendingNotificationEnabled"], out var enabledBool)) + _sendingNotificationEnabled = enabledBool; } + /// + /// This function retrieves the latest weekly summary report for each department and sends a notification to the + /// resource owners and delegate resource owners. + /// + /// Set the configuration key departmentFilter to filter the departments to send notifications to. Is set to + /// ["PRD"] by default. + /// + /// + /// Set the configuration key isSendingNotificationEnabled to true/false or 1/0 to enable/disable sending of notifications. Is + /// true by default. If disabled the function will log a message and skip sending notifications. + /// + /// + /// Set the configuration key weekly-department-summary-sender-parallelism to control the number of + /// notifications to send in parallel. + /// Be mindful that the notification API might struggle with too many parallel requests. Default is 2. + /// + /// [FunctionName("weekly-department-summary-sender")] public async Task RunAsync([TimerTrigger("0 0 5 * * MON", RunOnStartup = false)] TimerInfo timerInfo) { logger.LogInformation("weekly-department-summary-sender started with department filter {DepartmentFilter}", JsonConvert.SerializeObject(_departmentFilter, Formatting.Indented)); + if (!_sendingNotificationEnabled) + logger.LogInformation("Sending of notifications is disabled"); + + // TODO: Use OData query to filter departments var departments = await summaryApiClient.GetDepartmentsAsync(); @@ -74,6 +102,7 @@ private async Task CreateAndSendNotificationsAsync(ApiResourceOwnerDepartment de if (summaryReport is null) { + // There can be valid cases where there is no summary report for a department. E.g. if the department has no personnel logger.LogWarning( "No summary report found for department {Department}. Unable to send report notification", JsonConvert.SerializeObject(department, Formatting.Indented)); @@ -101,6 +130,12 @@ private async Task CreateAndSendNotificationsAsync(ApiResourceOwnerDepartment de foreach (var azureId in reportReceivers) { + if (!_sendingNotificationEnabled) + { + logger.LogInformation("Sending of notifications is disabled. Skipping sending notification to user with AzureId {AzureId} for department {FullDepartmentName}", azureId, department.FullDepartmentName); + continue; + } + try { var result = await notificationApiClient.SendNotification(notification, azureId); diff --git a/src/Fusion.Summary.Functions/host.json b/src/Fusion.Summary.Functions/host.json index e63161d3e..d05a3bd72 100644 --- a/src/Fusion.Summary.Functions/host.json +++ b/src/Fusion.Summary.Functions/host.json @@ -10,7 +10,17 @@ }, "logging": { "logLevel": { - "default": "Information" + "default": "Information", + "System.Net.Http.HttpClient.App.Resources.LogicalHandler": "Warning", + "Fusion.Resources.Functions.Common.Integration.Http.Handlers.ResourcesHttpHandler": "Warning", + "System.Net.Http.HttpClient.App.Summary.LogicalHandler": "Warning", + "Fusion.Resources.Functions.Common.Integration.Http.Handlers.SummaryHttpHandler": "Warning" + }, + "applicationInsights": { + "samplingSettings": { + "maxTelemetryItemsPerSecond": 40, + "excludedTypes": "Exception;Request" + } } } } \ No newline at end of file