Skip to content

Commit

Permalink
Merge branch 'master' into chore/summary/adjust-recipients
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Fusion.Summary.Functions/Deployment/disabled-functions.json
  • Loading branch information
Jonathanio123 committed Aug 22, 2024
2 parents f6a054c + 9bc94fd commit 83884a0
Show file tree
Hide file tree
Showing 12 changed files with 552 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public Task PutDepartmentAsync(ApiResourceOwnerDepartment departments,
/// </summary>
public Task<ApiWeeklySummaryReport?> GetLatestWeeklyReportAsync(string departmentSapId,
CancellationToken cancellationToken = default);

public Task PutWeeklySummaryReportAsync(string departmentSapId, ApiWeeklySummaryReport report,
CancellationToken cancellationToken = default);
}

#region Models
Expand Down Expand Up @@ -65,7 +68,7 @@ public record ApiWeeklySummaryReport
public record ApiPersonnelMoreThan100PercentFTE
{
public string FullName { get; set; } = "-";
public int FTE { get; set; } = -1;
public double FTE { get; set; } = -1;
}

public record ApiEndingPosition
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using Fusion.Resources.Functions.Common.Extensions;
using Fusion.Resources.Functions.Common.Integration.Http;

namespace Fusion.Resources.Functions.Common.ApiClients;
Expand Down Expand Up @@ -47,12 +48,10 @@ public async Task PutDepartmentAsync(ApiResourceOwnerDepartment department,
public async Task<ApiWeeklySummaryReport?> GetLatestWeeklyReportAsync(string departmentSapId,
CancellationToken cancellationToken = default)
{
// Get the date of the last monday or today if today is monday
// So the weekly report is based on the week that has passed
var lastMonday = GetCurrentOrLastMondayDate();
var lastMonday = DateTime.UtcNow.GetPreviousWeeksMondayDate();

var queryString =
$"summary-reports/{departmentSapId}/weekly?$filter=Period eq '{lastMonday.Date:O}'&$top=1";
$"resource-owners-summary-reports/{departmentSapId}/weekly?$filter=Period eq '{lastMonday.Date:O}'&$top=1";

using var response = await summaryClient.GetAsync(queryString, cancellationToken);
if (!response.IsSuccessStatusCode)
Expand All @@ -65,26 +64,13 @@ public async Task PutDepartmentAsync(ApiResourceOwnerDepartment department,
cancellationToken: cancellationToken))?.Items?.FirstOrDefault();
}

private static DateTime GetCurrentOrLastMondayDate()
public async Task PutWeeklySummaryReportAsync(string departmentSapId, ApiWeeklySummaryReport report,
CancellationToken cancellationToken = default)
{
var date = DateTime.UtcNow;
switch (date.DayOfWeek)
{
case DayOfWeek.Sunday:
return date.AddDays(-6);
case DayOfWeek.Monday:
return date;
case DayOfWeek.Tuesday:
case DayOfWeek.Wednesday:
case DayOfWeek.Thursday:
case DayOfWeek.Friday:
case DayOfWeek.Saturday:
default:
{
var daysUntilMonday = (int)date.DayOfWeek - 1;

return date.AddDays(-daysUntilMonday);
}
}
using var body = new JsonContent(JsonSerializer.Serialize(report, jsonSerializerOptions));

// Error logging is handled by http middleware => FunctionHttpMessageHandler
using var _ = await summaryClient.PutAsync($"resource-owners-summary-reports/{departmentSapId}/weekly", body,
cancellationToken);
}
}
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);
}
}
}
}
30 changes: 30 additions & 0 deletions src/Fusion.Summary.Api/ConfigurationExtensions.cs
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);
}
}
19 changes: 19 additions & 0 deletions src/Fusion.Summary.Api/Database/SqlTokenProvider.cs
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/");
}
}
1 change: 1 addition & 0 deletions src/Fusion.Summary.Api/Fusion.Summary.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Include="Fusion.AspNetCore" Version="8.0.4"/>
<PackageReference Include="Fusion.AspNetCore.FluentAuthorization" Version="8.0.0" />
<PackageReference Include="Fusion.AspNetCore.Versioning" Version="8.1.0"/>
<PackageReference Include="Fusion.Infrastructure.Database" Version="8.0.5"/>
<PackageReference Include="Fusion.Integration" Version="8.0.0" />
<PackageReference Include="Fusion.Integration.Authorization" Version="8.0.0" />
<PackageReference Include="MediatR" Version="12.3.0" />
Expand Down
26 changes: 18 additions & 8 deletions src/Fusion.Summary.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using FluentValidation.AspNetCore;
using Fusion.AspNetCore.Versioning;
using Fusion.Resources.Api.Middleware;
using Fusion.Summary.Api;
using Fusion.Summary.Api.Database;
using Fusion.Summary.Api.Middleware;
using Microsoft.AspNetCore.Authentication.JwtBearer;
Expand All @@ -13,24 +14,30 @@

var builder = WebApplication.CreateBuilder(args);

builder.Configuration
.AddJsonFile("/app/secrets/appsettings.secrets.yaml", optional: true)
.AddJsonFile("/app/static/config/env.json", optional: true, reloadOnChange: true);
if (Environment.GetEnvironmentVariable("INTEGRATION_TEST_RUN") != "true")
{
builder.Configuration
.AddJsonFile("/app/secrets/appsettings.secrets.yaml", optional: true)
.AddJsonFile("/app/config/appsettings.json", optional: true); // to be able to override settings by using a config map in kubernetes

builder.AddKeyVault();
}

var azureAdClientId = builder.Configuration["AzureAd:ClientId"];
var azureAdClientSecret = builder.Configuration["AzureAd:ClientSecret"];
var certThumbprint = builder.Configuration["Config:CertThumbprint"];
var environment = builder.Configuration["Environment"];
var fusionEnvironment = builder.Configuration["FUSION_ENVIRONMENT"];
var databaseConnectionString = builder.Configuration.GetConnectionString(nameof(SummaryDbContext));
var databaseConnectionString = builder.Configuration.GetConnectionString(nameof(SummaryDbContext))!;

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddHealthChecks()
.AddCheck("liveness", () => HealthCheckResult.Healthy())
.AddCheck("db", () => HealthCheckResult.Healthy(), tags: ["ready"]);
// TODO: Add a real health check, when database is added
// TODO: Add a real health check, when database is added in deployment pipelines and PR pipelines
// .AddDbContextCheck<DatabaseContext>("db", tags: new[] { "ready" });


builder.Services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
Expand All @@ -54,17 +61,20 @@
builder.Services.AddFusionIntegration(f =>
{
f.AddFusionAuthorization();
f.UseServiceInformation("Fusion.Summary.Api", "Dev");
f.UseServiceInformation("Fusion.Summary.Api", environment);
f.UseDefaultEndpointResolver(fusionEnvironment ?? "ci");
f.UseDefaultTokenProvider(opts =>
{
opts.ClientId = azureAdClientId ?? throw new InvalidOperationException("Missing AzureAd:ClientId");
opts.ClientSecret = azureAdClientSecret;
opts.CertificateThumbprint = certThumbprint;
});
});

builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddDbContext<SummaryDbContext>(options => options.UseSqlServer(databaseConnectionString));
builder.Services.AddSqlDbContext<SummaryDbContext>(databaseConnectionString, enablePullRequestEnv: false)
.AddSqlTokenProvider<SqlTokenProvider>()
.AddAccessTokenSupport();
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
builder.Services.AddFluentValidationAutoValidation().AddValidatorsFromAssembly(typeof(Program).Assembly);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"environment": "pr",
"disabledFunctions": [
"weekly-department-recipients-sync",
"weekly-department-summary-sender"
"weekly-department-summary-sender",
"weekly-department-summary-worker"
]
}
]
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.15.1"/>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
</ItemGroup>
<ItemGroup>
Expand Down
Loading

0 comments on commit 83884a0

Please sign in to comment.