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

Refactor paritioning date range method and unit tests for the same #168

Merged
merged 1 commit into from
Dec 6, 2024
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
2 changes: 1 addition & 1 deletion src/Altinn.Auth.AuditLog/AuditLogHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static WebApplication Create(string[] args)
services.Configure<KeyVaultSettings>(config.GetSection("kvSetting"));
builder.AddAuditLogPersistence();
builder.Services.AddSingleton<PartitionCreationHostedService>();
//builder.Services.AddHostedService(sp => sp.GetRequiredService<PartitionCreationHostedService>());
builder.Services.AddHostedService(sp => sp.GetRequiredService<PartitionCreationHostedService>());
services.AddSingleton<IAuthenticationEventService, AuthenticationEventService>();
services.AddSingleton<IAuthorizationEventService, AuthorizationEventService>();
services.Configure<PostgreSQLSettings>(config.GetSection("PostgreSQLSettings"));
Expand Down
31 changes: 20 additions & 11 deletions src/Altinn.Auth.AuditLog/Services/PartitionCreationHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private void CreateMonthlyPartitionFromTimer()
newJob = Task.Run(async () =>
{
var token = _stoppingCts!.Token;

try
{
await CreateMonthlyPartition(token);
Expand Down Expand Up @@ -123,38 +123,47 @@ private void CreateMonthlyPartitionFromTimer()
}

private async Task CreateMonthlyPartition(CancellationToken cancellationToken)
{
var partitions = GetPartitionsForCurrentAndAdjacentMonths();

await _partitionManagerRepository.CreatePartitions(partitions, cancellationToken);
}

internal IReadOnlyList<Partition> GetPartitionsForCurrentAndAdjacentMonths()
{
string authenticationSchemaName = "authentication";
string authzSchemaName = "authz";

// Get current dateonly
var now = DateOnly.FromDateTime(_timeProvider.GetUtcNow().UtcDateTime);


// Define the date ranges for the past, current and next month partitions
var currentMonthStartDate = now;
var currentMonthEndDate = currentMonthStartDate.AddMonths(1);
DateOnly pastMonthStartDate = now.AddMonths(-1);
DateOnly pastMonthEndDate = now;
DateOnly nextMonthStartDate = now.AddMonths(1);
DateOnly nextMonthEndDate = nextMonthStartDate.AddMonths(1);
var (currentMonthStartDate, currentMonthEndDate) = GetMonthStartAndEndDate(now);
var (pastMonthStartDate, pastMonthEndDate) = GetMonthStartAndEndDate(now.AddMonths(-1));
var (nextMonthStartDate, nextMonthEndDate) = GetMonthStartAndEndDate(now.AddMonths(1));

// Create partition names
var pastMonthPartitionName = $"eventlogv1_y{pastMonthStartDate.Year}m{pastMonthStartDate.Month:D2}";
var currentMonthPartitionName = $"eventlogv1_y{currentMonthStartDate.Year}m{currentMonthStartDate.Month:D2}";
var nextMonthPartitionName = $"eventlogv1_y{nextMonthStartDate.Year}m{nextMonthStartDate.Month:D2}";

// List of partitions for both schemas
IReadOnlyList<Partition> partitions = [
return new List<Partition>
{
new Partition { SchemaName = authenticationSchemaName, Name = pastMonthPartitionName, StartDate = pastMonthStartDate, EndDate = pastMonthEndDate },
new Partition { SchemaName = authenticationSchemaName, Name = currentMonthPartitionName, StartDate = currentMonthStartDate, EndDate = currentMonthEndDate },
new Partition { SchemaName = authenticationSchemaName, Name = nextMonthPartitionName, StartDate = nextMonthStartDate, EndDate = nextMonthEndDate },
new Partition { SchemaName = authzSchemaName, Name = pastMonthPartitionName, StartDate = pastMonthStartDate, EndDate = pastMonthEndDate },
new Partition { SchemaName = authzSchemaName, Name = currentMonthPartitionName, StartDate = currentMonthStartDate, EndDate = currentMonthEndDate },
new Partition { SchemaName = authzSchemaName, Name = nextMonthPartitionName, StartDate = nextMonthStartDate, EndDate = nextMonthEndDate }
];
};
}

await _partitionManagerRepository.CreatePartitions(partitions, cancellationToken);
internal (DateOnly startDate, DateOnly endDate) GetMonthStartAndEndDate(DateOnly date)
{
DateOnly startDate = new DateOnly(date.Year, date.Month, 1);
DateOnly endDate = startDate.AddMonths(1).AddDays(-1);
return (startDate, endDate);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private HttpClient CreateEventClient()
return client;
}

[Fact(Skip = "Ignored")]
[Fact]
public async Task CreateAuthenticationEvent_Ok()
{
using var client = CreateEventClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private HttpClient CreateEventClient()
return client;
}

[Fact(Skip = "Ignored")]
[Fact]
public async Task CreateAuthorizationEvent_Ok()
{
using var client = CreateEventClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using Testcontainers.PostgreSql;
using static System.Net.Mime.MediaTypeNames;

namespace Altinn.Auth.AuditLog.Tests;
namespace Altinn.Auth.AuditLog.Tests.Integration;

public class PartitionCreationHostedServiceIntegrationTests(DbFixture dbFixture, WebApplicationFixture webApplicationFixture)
: WebApplicationTests(dbFixture, webApplicationFixture)
Expand All @@ -29,7 +29,7 @@ protected Task WaitForPartitionJob()
return HostedService.RunningJob;
}

[Fact(Skip = "Ignored")]
[Fact]
public async Task ExecuteAsync_CreatesCurrentMonthPartition_OnlyOnce()
{
TimeProvider.Advance(TimeSpan.FromDays(1) + TimeSpan.FromHours(1));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Altinn.Auth.AuditLog.Core.Models;
using Altinn.Auth.AuditLog.Services;
using Microsoft.Extensions.Time.Testing;
using System;
using System.Collections.Generic;
using Xunit;

namespace Altinn.Auth.AuditLog.Tests;
public class PartitionCreationHostedServiceTests
{
[Fact]
public void GetPartitionsForCurrentAndAdjacentMonths_ReturnsCorrectPartitions()
{
// Arrange
var timeProvider = new FakeTimeProvider(new DateTime(2023, 10, 15));
var service = new PartitionCreationHostedService(null, null, timeProvider);

// Act
var partitions = service.GetPartitionsForCurrentAndAdjacentMonths();

// Assert
Assert.Equal(6, partitions.Count);
Assert.Contains(partitions, p => p.Name == "eventlogv1_y2023m09" && p.StartDate == new DateOnly(2023, 9, 1) && p.EndDate == new DateOnly(2023, 9, 30));
Assert.Contains(partitions, p => p.Name == "eventlogv1_y2023m10" && p.StartDate == new DateOnly(2023, 10, 1) && p.EndDate == new DateOnly(2023, 10, 31));
Assert.Contains(partitions, p => p.Name == "eventlogv1_y2023m11" && p.StartDate == new DateOnly(2023, 11, 1) && p.EndDate == new DateOnly(2023, 11, 30));
}

[Theory]
[InlineData(2023, 10, 15, 2023, 10, 1, 2023, 10, 31)]
[InlineData(2023, 2, 1, 2023, 2, 1, 2023, 2, 28)]
[InlineData(2024, 2, 1, 2024, 2, 1, 2024, 2, 29)] // Leap year
public void GetMonthStartAndEndDate_ReturnsCorrectDates(int year, int month, int day, int expectedStartYear, int expectedStartMonth, int expectedStartDay, int expectedEndYear, int expectedEndMonth, int expectedEndDay)
{
// Arrange
var service = new PartitionCreationHostedService(null, null, null);
var date = new DateOnly(year, month, day);

// Act
var (startDate, endDate) = service.GetMonthStartAndEndDate(date);

// Assert
Assert.Equal(new DateOnly(expectedStartYear, expectedStartMonth, expectedStartDay), startDate);
Assert.Equal(new DateOnly(expectedEndYear, expectedEndMonth, expectedEndDay), endDate);
}

[Fact]
public void GetPartitionsForCurrentAndAdjacentMonths_CrossYearBoundary_ReturnsCorrectPartitions()
{
// Arrange
var timeProvider = new FakeTimeProvider(new DateTime(2023, 12, 15));
var service = new PartitionCreationHostedService(null, null, timeProvider);

// Act
var partitions = service.GetPartitionsForCurrentAndAdjacentMonths();

// Assert
Assert.Equal(6, partitions.Count);
Assert.Contains(partitions, p => p.Name == "eventlogv1_y2023m11" && p.StartDate == new DateOnly(2023, 11, 1) && p.EndDate == new DateOnly(2023, 11, 30));
Assert.Contains(partitions, p => p.Name == "eventlogv1_y2023m12" && p.StartDate == new DateOnly(2023, 12, 1) && p.EndDate == new DateOnly(2023, 12, 31));
Assert.Contains(partitions, p => p.Name == "eventlogv1_y2024m01" && p.StartDate == new DateOnly(2024, 1, 1) && p.EndDate == new DateOnly(2024, 1, 31));
}
}
Loading