-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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> |
This file contains 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,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); | ||
} |
This file contains 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