Skip to content

Commit

Permalink
feat: Culling
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed Oct 14, 2022
1 parent b34f96e commit b7567e6
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
16 changes: 16 additions & 0 deletions HuTao.Culling/HuTao.Culling.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\HuTao.Bot\HuTao.Bot.csproj" />
<ProjectReference Include="..\HuTao.Data\HuTao.Data.csproj" />
<ProjectReference Include="..\HuTao.Services\HuTao.Services.csproj" />
</ItemGroup>

</Project>
84 changes: 84 additions & 0 deletions HuTao.Culling/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using HuTao.Data;
using HuTao.Data.Config;
using HuTao.Data.Models.Logging;
using HuTao.Services.Linking;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Serilog;

namespace HuTao.Culling;

public class Program
{
private static IServiceProvider? _services;
private HuTaoContext? _db;

public async Task CullMessagesInternalAsync(DateTimeOffset oldest)
{
var messageSet = _db!.Set<MessageLog>();
Log.Information("Getting messages to delete");
var messages = await messageSet
.Include(m => m.Embeds)
.Include(m => m.Components)
.Include(m => m.Attachments)
.Where(m => oldest > m.LogDate)
.Take(10000)
.OrderBy(m => m.LogDate)
.ToListAsync();
Log.Information("Culling messages older than {Oldest}, total {Count}", oldest, messages.Count);

var count = 0;
foreach (var message in messages)
{
if (count % 1000 == 0)
Log.Information("Deleting message {MessageId}", message.MessageId);

if (count % 10000 == 0)
{
Log.Information("Saving changes...");
await _db.SaveChangesAsync();
Log.Information("Finished saving");
Log.Information("Deleted {Count} messages so far...", count);
}

Remove(message);
}

Log.Information("Deleted {Count} messages", count);
Log.Information("Final saving changes...");
await _db.SaveChangesAsync();
Log.Information("Deleted {Count} messages", count);

void Remove(MessageLog message)
{
_db.TryRemove(message.Embeds);
_db.TryRemove(message.Components);
_db.RemoveRange(message.Attachments);
_db.Remove(message);

count++;
}
}

public static Task Main(string[] args) => new Program().StartAsync(args);

public async Task StartAsync(string[] args)
{
_services = ConfigureServices();
_db = _services.GetRequiredService<HuTaoContext>();

var days = int.Parse(args.First());
var oldest = DateTimeOffset.Now.ToUniversalTime() - TimeSpan.FromDays(days);
await CullMessagesInternalAsync(oldest);
}

private static ServiceProvider ConfigureServices() =>
new ServiceCollection()
.AddLogging(l => l.AddSerilog())
.AddDbContext<HuTaoContext>(ContextOptions)
.BuildServiceProvider();

private static void ContextOptions(DbContextOptionsBuilder optionsBuilder) => optionsBuilder
.UseLazyLoadingProxies()
.UseNpgsql(HuTaoConfig.Configuration.HuTaoContext);
}
6 changes: 6 additions & 0 deletions HuTao.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HuTao.Services", "HuTao.Ser
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HuTao.Data", "HuTao.Data\HuTao.Data.csproj", "{DE501463-C094-4550-82F3-9513C576E5B2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HuTao.Culling", "HuTao.Culling\HuTao.Culling.csproj", "{CD984E6D-8385-441C-A37B-33F2FFD0BFBD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{DE501463-C094-4550-82F3-9513C576E5B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE501463-C094-4550-82F3-9513C576E5B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE501463-C094-4550-82F3-9513C576E5B2}.Release|Any CPU.Build.0 = Release|Any CPU
{CD984E6D-8385-441C-A37B-33F2FFD0BFBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD984E6D-8385-441C-A37B-33F2FFD0BFBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD984E6D-8385-441C-A37B-33F2FFD0BFBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD984E6D-8385-441C-A37B-33F2FFD0BFBD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit b7567e6

Please sign in to comment.