-
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.
feat: department-summary-weekly-worker (#668)
- [x] New feature - [ ] Bug fix - [ ] High impact **Description of work:** [AB#51133](https://statoil-proview.visualstudio.com/787035c2-8cf2-4d73-a83e-bb0e6d937eec/_workitems/edit/51133) This takes the old report creation code and moves it to the new function app. The ResourceOwnerReportDataCreator.cs code remains untouched. **Testing:** - [ ] Can be tested - [ ] Automatic tests created / updated - [x] Local tests are passing I did some simple testing with hardcoded messages as the queue is not set up yet. **Checklist:** - [x] Considered automated tests - [x] Considered updating specification / documentation - [x] Considered work items - [x] Considered security - [x] Performed developer testing - [x] Checklist finalized / ready for review
- Loading branch information
1 parent
c0cea7e
commit 1008b27
Showing
8 changed files
with
484 additions
and
26 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
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.