Skip to content

Added submissions archives. #1656

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

Open
wants to merge 1 commit into
base: v2-development
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions Common/OJS.Common/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ namespace OJS.Common.Extensions;

public static class EnumerableExtensions
{
public static T? MaxOrDefault<T>(this IEnumerable<T> enumerable)
=> enumerable.DefaultIfEmpty()
.Max();
public static IEnumerable<IEnumerable<T>> InBatches<T>(this IEnumerable<T> queryable, int size)
{
var current = queryable.AsQueryable();
while (current.Any())
{
var batch = current.Take(size);
yield return batch;
current = current.Skip(size);
}
}
}
96 changes: 96 additions & 0 deletions Data/OJS.Data.Models/Submissions/ArchivedSubmission.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
namespace OJS.Data.Models.Submissions
{
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq.Expressions;
using OJS.Data.Validation;
using OJS.Workers.Common.Models;

[Table("Submissions")]
public class ArchivedSubmission

Check notice on line 11 in Data/OJS.Data.Models/Submissions/ArchivedSubmission.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Data/OJS.Data.Models/Submissions/ArchivedSubmission.cs#L11

Implement 'IEquatable<ArchivedSubmission>'.
{
public static Expression<Func<Submission, ArchivedSubmission>> FromSubmission =>
submission => new ArchivedSubmission
{
Id = submission.Id,
ParticipantId = submission.ParticipantId,
ProblemId = submission.ProblemId,
SubmissionTypeId = submission.SubmissionTypeId,
Content = submission.Content,
FileExtension = submission.FileExtension,
SolutionSkeleton = submission.SolutionSkeleton,
StartedExecutionOn = submission.StartedExecutionOn,
CompletedExecutionOn = submission.CompletedExecutionOn,
IpAddress = submission.IpAddress,
WorkerName = submission.WorkerName,
ExceptionType = submission.ExceptionType,
Processed = submission.Processed,
Points = submission.Points,
ProcessingComment = submission.ProcessingComment,
TestRunsCache = submission.TestRunsCache,
CreatedOn = submission.CreatedOn,
ModifiedOn = submission.ModifiedOn,
IsHardDeletedFromMainDatabase = false,
};
Comment on lines +13 to +35
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure it has all the properties.. legacy and alpha might have slight differences already.


[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }

public int ParticipantId { get; set; }

public int ProblemId { get; set; }

public int? SubmissionTypeId { get; set; }

public byte[] Content { get; set; } = Array.Empty<byte>();

public string? FileExtension { get; set; }

public byte[]? SolutionSkeleton { get; set; }

public DateTime? StartedExecutionOn { get; set; }

public DateTime? CompletedExecutionOn { get; set; }

[StringLength(ConstraintConstants.IpAddressMaxLength)]
[Column(TypeName = "varchar")]
public string? IpAddress { get; set; }

[StringLength(ConstraintConstants.Submission.WorkerNameMaxLength)]
public string? WorkerName { get; set; }

public ExceptionType? ExceptionType { get; set; }

public bool Processed { get; set; }

public int Points { get; set; }

public string? ProcessingComment { get; set; }

public string? TestRunsCache { get; set; }

public DateTime CreatedOn { get; set; }

public DateTime? ModifiedOn { get; set; }

public bool IsHardDeletedFromMainDatabase { get; set; }

[NotMapped]
public bool IsBinaryFile => !string.IsNullOrWhiteSpace(this.FileExtension);

[NotMapped]
public string ContentAsString
=> this.IsBinaryFile ? string.Empty : this.Content.ToString();

public override bool Equals(object? obj)
=> obj is ArchivedSubmission other && this.Equals(other);

public bool Equals(ArchivedSubmission? other)
=> other != null && this.Id == other.Id;

public override int GetHashCode()
=> this.Id.GetHashCode();
}
}
9 changes: 9 additions & 0 deletions Data/OJS.Data/ArchivesDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace OJS.Data;

using Microsoft.EntityFrameworkCore;
using OJS.Data.Models.Submissions;

public class ArchivesDbContext(DbContextOptions<ArchivesDbContext> options) : DbContext(options)
{
public DbSet<ArchivedSubmission> Submissions { get; set; } = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ public static void ConfigureServices(
.AddHttpClients(configuration)
.AddTransient(typeof(IDataService<>), typeof(AdministrationDataService<>))
.AddTransient<ITransactionsProvider, TransactionsProvider<OjsDbContext>>()
.AddTransient<IArchivesDataService, ArchivesDataService>()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this autoregistered?

.AddTransient<AdministrationExceptionMiddleware>()
.AddHangfireServer(configuration, AppName, [AdministrationQueueName])
.AddMessageQueue<Program>(configuration)
.ConfigureGlobalDateFormat()
.ConfigureCorsPolicy(configuration)
.AddIdentityDatabase<OjsDbContext, UserProfile, Role, UserInRole>(configuration)
.AddArchivesDatabase(configuration)
.AddResiliencePipelines()
.AddMemoryCache()
.AddDistributedCaching(configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ namespace OJS.Servers.Infrastructure.Extensions
using System.Security.Claims;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using OJS.Data;
using OpenAI;
using RabbitMQ.Client;
using static OJS.Common.GlobalConstants;
Expand Down Expand Up @@ -199,6 +201,35 @@ public static IServiceCollection AddHangfireServer(
return services;
}

/// <summary>
/// Adds the archives database context to the service collection.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="configuration">The configuration.</param>
public static IServiceCollection AddArchivesDatabase(
this IServiceCollection services,
IConfiguration configuration)
{
var defaultConnectionString = configuration.GetConnectionString(DefaultDbConnectionName);

// Modify the connection string to use a different database name for archives
var builder = new SqlConnectionStringBuilder(defaultConnectionString);
builder.InitialCatalog = $"{builder.InitialCatalog}Archives";
var connectionString = builder.ConnectionString;

services
.AddDbContext<ArchivesDbContext>(options =>
{
options.UseSqlServer(connectionString);
});

services
.AddHealthChecks()
.AddSqlServer(connectionString, name: "archives-db");

return services;
}

public static IServiceCollection AddSwaggerDocs(
this IServiceCollection services,
string name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Common\OJS.Common\OJS.Common.csproj" />
<ProjectReference Include="..\..\..\Data\OJS.Data\OJS.Data.csproj" />
<ProjectReference Include="..\..\..\Services\Common\OJS.Services.Common\OJS.Services.Common.csproj" />
<ProjectReference Include="..\..\..\Services\Infrastructure\OJS.Services.Infrastructure\OJS.Services.Infrastructure.csproj" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Servers/UI/OJS.Servers.Ui/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static void Main(string[] args)
.AddTransient(typeof(IDataService<>), typeof(DataService<>))
.AddTransient<ITransactionsProvider, TransactionsProvider<OjsDbContext>>()
.AddIdentityDatabase<OjsDbContext, UserProfile, Role, UserInRole>(configuration)
.AddArchivesDatabase(configuration)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now we don't need this db in ui

.AddResiliencePipelines()
.AddOpenAiClient(configuration)
.AddMemoryCache()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
namespace OJS.Services.Administration.Business.Implementations;

using System.Linq;
using System.Threading.Tasks;
using OJS.Common;
using OJS.Data.Models.Submissions;
using OJS.Services.Administration.Data;
using OJS.Services.Common;
using OJS.Services.Common.Data;
using OJS.Services.Infrastructure;
using OJS.Services.Infrastructure.Extensions;

public class ArchivedSubmissionsBusinessService : IArchivedSubmissionsBusinessService
{
private readonly ISubmissionsDataService submissionsData;
private readonly IArchivesDataService archivesData;
private readonly IDatesService dates;

public ArchivedSubmissionsBusinessService(
ISubmissionsDataService submissionsData,
IArchivesDataService archivesData,
IDatesService dates)
{
this.submissionsData = submissionsData;
this.archivesData = archivesData;
this.dates = dates;
}

public async Task<int> ArchiveOldSubmissionsDailyBatch(int limit, int maxSubBatchSize)
{
await this.archivesData.CreateDatabaseIfNotExists();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use MigrateDatabase in Program.cs like for the OjsDbContext.


var leftoverSubmissionsFromBatchSplitting = limit % maxSubBatchSize;
var numberOfIterations = limit / maxSubBatchSize;
if(leftoverSubmissionsFromBatchSplitting > 0)
{
numberOfIterations++;
}

var archived = 0;

for (var i = 0; i < numberOfIterations; i++)
{
var curBatchSize = maxSubBatchSize;
var isLastIteration = i == (numberOfIterations - 1);
if(leftoverSubmissionsFromBatchSplitting > 0 && isLastIteration)
{
curBatchSize = leftoverSubmissionsFromBatchSplitting;
}

var allSubmissionsForArchive = this
.GetSubmissionsForArchiving()
.OrderBy(x => x.Id)
.InBatches(GlobalConstants.BatchOperationsChunkSize, curBatchSize);

foreach (var submissionsForArchiveBatch in allSubmissionsForArchive)
{
var submissionsForArchives = submissionsForArchiveBatch
.Select(ArchivedSubmission.FromSubmission)
.ToList();

if(submissionsForArchives.Count == 0)
{
break;
}

archived += await this.archivesData.AddMany(submissionsForArchives);
await this.archivesData.SaveChanges();
}

await this.submissionsData.HardDeleteArchived(curBatchSize);
}

return archived;
}

public async Task<int> ArchiveOldSubmissionsWithLimit(int limit)
{
var archived = 0;
await this.archivesData.CreateDatabaseIfNotExists();

var allSubmissionsForArchive = this
.GetSubmissionsForArchiving()
.OrderBy(x => x.Id)
.InBatches(GlobalConstants.BatchOperationsChunkSize, limit);

foreach (var submissionsForArchiveBatch in allSubmissionsForArchive)
{
var submissionsForArchives = submissionsForArchiveBatch
.Select(ArchivedSubmission.FromSubmission)
.ToList();

if(submissionsForArchives.Count == 0)
{
break;
}

archived += await this.archivesData.AddMany(submissionsForArchives);
await this.archivesData.SaveChanges();
}

return archived;
}

public async Task<int> HardDeleteArchivedByLimit(int limit)
=> await this.submissionsData.HardDeleteArchived(limit);

private IQueryable<Submission> GetSubmissionsForArchiving()
{
var now = this.dates.GetUtcNow();
var bestSubmissionCutoffDate = now.AddYears(-GlobalConstants.BestSubmissionEligibleForArchiveAgeInYears);
var nonBestSubmissionCutoffDate = now.AddYears(-GlobalConstants.NonBestSubmissionEligibleForArchiveAgeInYears);

return this.submissionsData
.GetAllCreatedBeforeDateAndNonBestCreatedBeforeDate(
bestSubmissionCutoffDate,
nonBestSubmissionCutoffDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ public class RecurringBackgroundJobsBusinessService : IRecurringBackgroundJobsBu
private readonly ISubmissionsForProcessingBusinessService submissionsForProcessing;
private readonly IParticipantsBusinessService participantsBusinessService;
private readonly IParticipantScoresBusinessService participantScoresBusiness;
private readonly IArchivedSubmissionsBusinessService archivedSubmissionsBusiness;
private readonly IBusControl bus;
private readonly ILogger<RecurringBackgroundJobsBusinessService> logger;

public RecurringBackgroundJobsBusinessService(
ISubmissionsForProcessingBusinessService submissionsForProcessing,
IParticipantsBusinessService participantsBusinessService,
IParticipantScoresBusinessService participantScoresBusiness,
IArchivedSubmissionsBusinessService archivedSubmissionsBusiness,
IBusControl bus,
ILogger<RecurringBackgroundJobsBusinessService> logger)
{
this.submissionsForProcessing = submissionsForProcessing;
this.participantsBusinessService = participantsBusinessService;
this.participantScoresBusiness = participantScoresBusiness;
this.archivedSubmissionsBusiness = archivedSubmissionsBusiness;
this.bus = bus;
this.logger = logger;
}
Expand Down Expand Up @@ -77,5 +80,37 @@ public async Task<object> NormalizeAllPointsThatExceedAllowedLimit()

return "Successfully normalized all points that exceed allowed limit";
}

public async Task<object> ArchiveOldSubmissionsDailyBatch()
{
const int archiveDailyBatchLimit = 500_000;
const int archiveMaxSubBatchSize = 10_000;

var archivedCount = await this.archivedSubmissionsBusiness.ArchiveOldSubmissionsDailyBatch(
archiveDailyBatchLimit,
archiveMaxSubBatchSize);

return $"Successfully archived {archivedCount} submissions.";
}

public async Task<object> ArchiveOldSubmissionsWithLimit()
{
const int archiveYearlyBatchLimit = 25_000;

var archivedCount = await this.archivedSubmissionsBusiness.ArchiveOldSubmissionsWithLimit(
archiveYearlyBatchLimit);

return $"Successfully archived {archivedCount} submissions in yearly batch.";
}

public async Task<object> HardDeleteArchivedSubmissions()
{
const int archiveSingleBatchLimit = 25_000;

var hardDeletedCount = await this.archivedSubmissionsBusiness.HardDeleteArchivedByLimit(
archiveSingleBatchLimit);

return $"Successfully hard deleted {hardDeletedCount} archived submissions.";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ Task UpdateBySubmissionAndPoints(
int submissionPoints,
Participant participant,
bool shouldSaveChanges = true);

Task RemoveSubmissionIdsBySubmissionIds(IEnumerable<int> submissionIds);
}
}
Loading
Loading