forked from NikolayIT/OpenJudgeSystem
-
Notifications
You must be signed in to change notification settings - Fork 12
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
deyordanov
wants to merge
4
commits into
v2-development
Choose a base branch
from
1726-archive-old-submissions
base: v2-development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,77 @@ | ||
namespace OJS.Data.Models.Submissions | ||
{ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using AutoMapper; | ||
using OJS.Data.Validation; | ||
using OJS.Services.Infrastructure.Models.Mapping; | ||
using OJS.Workers.Common.Models; | ||
|
||
[Table("Submissions")] | ||
public class ArchivedSubmission : IMapExplicitly, IEquatable<ArchivedSubmission> | ||
{ | ||
[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; } = []; | ||
|
||
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(); | ||
|
||
public void RegisterMappings(IProfileExpression configuration) | ||
=> configuration.CreateMap<Submission, ArchivedSubmission>() | ||
.ForMember(d => d.IsHardDeletedFromMainDatabase, opt => opt.MapFrom(s => false)); | ||
} | ||
} |
This file contains hidden or 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,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!; | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
116 changes: 116 additions & 0 deletions
116
...JS.Services.Administration.Business/Implementations/ArchivedSubmissionsBusinessService.cs
This file contains hidden or 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,116 @@ | ||
namespace OJS.Services.Administration.Business.Implementations; | ||
|
||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using DocumentFormat.OpenXml.Vml; | ||
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) | ||
{ | ||
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 | ||
.MapCollection<ArchivedSubmission>() | ||
.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; | ||
var allSubmissionsForArchive = this | ||
.GetSubmissionsForArchiving() | ||
.OrderBy(x => x.Id) | ||
.InBatches(GlobalConstants.BatchOperationsChunkSize, limit); | ||
|
||
foreach (var submissionsForArchiveBatch in allSubmissionsForArchive) | ||
{ | ||
var submissionsForArchives = submissionsForArchiveBatch | ||
.MapCollection<ArchivedSubmission>() | ||
.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); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.