-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into chore/summary/adjust-recipients
# Conflicts: # src/Fusion.Summary.Functions/Deployment/disabled-functions.json
- Loading branch information
Showing
12 changed files
with
552 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/Fusion.Resources.Functions.Common/Extensions/DateTimeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace Fusion.Resources.Functions.Common.Extensions; | ||
|
||
public static class DateTimeExtensions | ||
{ | ||
/// <summary> | ||
/// Returns a new DateTime object with the date set to monday last week. | ||
/// </summary> | ||
public static DateTime GetPreviousWeeksMondayDate(this DateTime date) | ||
{ | ||
switch (date.DayOfWeek) | ||
{ | ||
case DayOfWeek.Sunday: | ||
return date.AddDays(-6); | ||
case DayOfWeek.Monday: | ||
return date.AddDays(-7); | ||
case DayOfWeek.Tuesday: | ||
case DayOfWeek.Wednesday: | ||
case DayOfWeek.Thursday: | ||
case DayOfWeek.Friday: | ||
case DayOfWeek.Saturday: | ||
default: | ||
{ | ||
// Calculate days until previous monday | ||
// Go one week back and then remove the days until the monday | ||
var daysUntilLastWeeksMonday = (1 - (int)date.DayOfWeek) - 7; | ||
|
||
return date.AddDays(daysUntilLastWeeksMonday); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Azure.Identity; | ||
|
||
namespace Fusion.Summary.Api; | ||
|
||
public static class ConfigurationExtensions | ||
{ | ||
public static void AddKeyVault(this WebApplicationBuilder builder) | ||
{ | ||
var configuration = builder.Configuration; | ||
var clientId = configuration["AzureAd:ClientId"]; | ||
var tenantId = configuration["AzureAd:TenantId"]; | ||
var clientSecret = configuration["AzureAd:ClientSecret"]; | ||
var keyVaultUrl = configuration["KEYVAULT_URL"]; | ||
|
||
if (string.IsNullOrWhiteSpace(keyVaultUrl)) | ||
{ | ||
Console.WriteLine("Skipping key vault as url is null, empty or whitespace."); | ||
return; | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(clientSecret)) | ||
{ | ||
Console.WriteLine("Skipping key vault as clientSecret is null, empty or whitespace."); | ||
return; | ||
} | ||
|
||
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret); | ||
configuration.AddAzureKeyVault(new Uri(keyVaultUrl), credential); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Fusion.Infrastructure.Database; | ||
using Fusion.Integration.Configuration; | ||
|
||
namespace Fusion.Summary.Api.Database; | ||
|
||
public class SqlTokenProvider : ISqlTokenProvider | ||
{ | ||
private readonly IFusionTokenProvider fusionTokenProvider; | ||
|
||
public SqlTokenProvider(IFusionTokenProvider fusionTokenProvider) | ||
{ | ||
this.fusionTokenProvider = fusionTokenProvider; | ||
} | ||
|
||
public Task<string> GetAccessTokenAsync() | ||
{ | ||
return fusionTokenProvider.GetApplicationTokenAsync("https://database.windows.net/"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/Fusion.Summary.Functions/Functions/WeeklyDepartmentSummaryWorker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Azure.Messaging.ServiceBus; | ||
using Fusion.Integration.Profile; | ||
using Fusion.Resources.Functions.Common.ApiClients; | ||
using Fusion.Resources.Functions.Common.Extensions; | ||
using Fusion.Summary.Functions.ReportCreator; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Azure.WebJobs.ServiceBus; | ||
using Microsoft.Extensions.Logging; | ||
using JsonSerializer = System.Text.Json.JsonSerializer; | ||
|
||
namespace Fusion.Summary.Functions.Functions; | ||
|
||
public class WeeklyDepartmentSummaryWorker | ||
{ | ||
private readonly IResourcesApiClient _resourceClient; | ||
private readonly ISummaryApiClient _summaryApiClient; | ||
private readonly ILogger<WeeklyDepartmentSummaryWorker> _logger; | ||
|
||
public WeeklyDepartmentSummaryWorker(IResourcesApiClient resourceClient, ILogger<WeeklyDepartmentSummaryWorker> logger, ISummaryApiClient summaryApiClient) | ||
{ | ||
_resourceClient = resourceClient; | ||
_logger = logger; | ||
_summaryApiClient = summaryApiClient; | ||
} | ||
|
||
[FunctionName("weekly-department-summary-worker")] | ||
public async Task RunAsync( | ||
[ServiceBusTrigger("%department_summary_weekly_queue%", Connection = "AzureWebJobsServiceBus")] | ||
ServiceBusReceivedMessage message, ServiceBusMessageActions messageReceiver) | ||
{ | ||
try | ||
{ | ||
var dto = await JsonSerializer.DeserializeAsync<ApiResourceOwnerDepartment>(message.Body.ToStream()); | ||
|
||
if (string.IsNullOrEmpty(dto.FullDepartmentName)) | ||
throw new Exception("FullDepartmentIdentifier not valid."); | ||
|
||
await CreateAndStoreReportAsync(dto); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Error while processing message"); | ||
throw; | ||
} | ||
finally | ||
{ | ||
// Complete the message regardless of outcome. | ||
await messageReceiver.CompleteMessageAsync(message); | ||
} | ||
} | ||
|
||
private async Task CreateAndStoreReportAsync(ApiResourceOwnerDepartment message) | ||
{ | ||
var departmentRequests = (await _resourceClient.GetAllRequestsForDepartment(message.FullDepartmentName)).ToList(); | ||
|
||
var departmentPersonnel = (await _resourceClient.GetAllPersonnelForDepartment(message.FullDepartmentName)) | ||
.Where(per => | ||
per.AccountType != FusionAccountType.Consultant.ToString() && | ||
per.AccountType != FusionAccountType.External.ToString()) | ||
.ToList(); | ||
|
||
// Check if the department has personnel, abort if not | ||
if (departmentPersonnel.Count == 0) | ||
{ | ||
_logger.LogInformation("Department contains no personnel, no need to store report"); | ||
return; | ||
} | ||
|
||
var report = await BuildSummaryReportAsync(departmentPersonnel, departmentRequests, message.DepartmentSapId); | ||
|
||
await _summaryApiClient.PutWeeklySummaryReportAsync(message.DepartmentSapId, report); | ||
} | ||
|
||
|
||
private static Task<ApiWeeklySummaryReport> BuildSummaryReportAsync( | ||
List<IResourcesApiClient.InternalPersonnelPerson> personnel, | ||
List<IResourcesApiClient.ResourceAllocationRequest> requests, | ||
string departmentSapId) | ||
{ | ||
var report = new ApiWeeklySummaryReport() | ||
{ | ||
Period = DateTime.UtcNow.GetPreviousWeeksMondayDate(), | ||
DepartmentSapId = departmentSapId, | ||
PositionsEnding = ResourceOwnerReportDataCreator | ||
.GetPersonnelPositionsEndingWithNoFutureAllocation(personnel) | ||
.Select(ep => new ApiEndingPosition() | ||
{ | ||
EndDate = ep.EndDate.GetValueOrDefault(DateTime.MinValue), | ||
FullName = ep.FullName | ||
}) | ||
.ToArray(), | ||
PersonnelMoreThan100PercentFTE = ResourceOwnerReportDataCreator | ||
.GetPersonnelAllocatedMoreThan100Percent(personnel) | ||
.Select(ep => new ApiPersonnelMoreThan100PercentFTE() | ||
{ | ||
FullName = ep.FullName, | ||
FTE = ep.TotalWorkload | ||
}) | ||
.ToArray(), | ||
NumberOfPersonnel = ResourceOwnerReportDataCreator.GetTotalNumberOfPersonnel(personnel).ToString(), | ||
CapacityInUse = ResourceOwnerReportDataCreator.GetCapacityInUse(personnel).ToString(), | ||
NumberOfRequestsLastPeriod = ResourceOwnerReportDataCreator.GetNumberOfRequestsLastWeek(requests).ToString(), | ||
NumberOfOpenRequests = ResourceOwnerReportDataCreator.GetNumberOfOpenRequests(requests).ToString(), | ||
NumberOfRequestsStartingInLessThanThreeMonths = ResourceOwnerReportDataCreator.GetNumberOfRequestsStartingInLessThanThreeMonths(requests).ToString(), | ||
NumberOfRequestsStartingInMoreThanThreeMonths = ResourceOwnerReportDataCreator.GetNumberOfRequestsStartingInMoreThanThreeMonths(requests).ToString(), | ||
AverageTimeToHandleRequests = ResourceOwnerReportDataCreator.GetAverageTimeToHandleRequests(requests).ToString(), | ||
AllocationChangesAwaitingTaskOwnerAction = ResourceOwnerReportDataCreator.GetAllocationChangesAwaitingTaskOwnerAction(requests).ToString(), | ||
ProjectChangesAffectingNextThreeMonths = ResourceOwnerReportDataCreator.CalculateDepartmentChangesLastWeek(personnel).ToString() | ||
}; | ||
|
||
return Task.FromResult(report); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.