Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(summary): Add configuration value to disable sending of notification and adjusted logging #702

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class SummaryHttpHandler : FunctionHttpMessageHandler
private readonly IOptions<HttpClientsOptions> options;

public SummaryHttpHandler(ILoggerFactory logger, ITokenProvider tokenProvider, IServiceDiscovery serviceDiscovery, IOptions<HttpClientsOptions> options)
: base(logger.CreateLogger<OrgHttpHandler>(), tokenProvider, serviceDiscovery)
: base(logger.CreateLogger<SummaryHttpHandler>(), tokenProvider, serviceDiscovery)
{
this.options = options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<WeeklyDepartmentSummarySender> logger, IConfiguration configuration)
Expand All @@ -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;
}

/// <summary>
/// This function retrieves the latest weekly summary report for each department and sends a notification to the
/// resource owners and delegate resource owners.
/// <para>
/// Set the configuration key <c>departmentFilter</c> to filter the departments to send notifications to. Is set to
/// ["PRD"] by default.
/// </para>
/// <para>
/// Set the configuration key <c>isSendingNotificationEnabled</c> 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.
/// </para>
/// <para>
/// Set the configuration key <c>weekly-department-summary-sender-parallelism</c> 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.
/// </para>
/// </summary>
[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();

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion src/Fusion.Summary.Functions/host.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
Loading