ability to merge syscall information duplicates

This commit is contained in:
13xforever 2019-09-08 21:31:38 +05:00
parent 1ed8f5f321
commit bc17f6d97c
2 changed files with 67 additions and 4 deletions

View File

@ -88,16 +88,24 @@ namespace CompatBot.Commands
{
try
{
var result = await SyscallInfoProvider.FixAsync().ConfigureAwait(false);
await ctx.RespondAsync("Fixing invalid function names...").ConfigureAwait(false);
var result = await SyscallInfoProvider.FixInvalidFunctionNamesAsync().ConfigureAwait(false);
if (result.funcs > 0)
await ctx.RespondAsync($"Successfully fixed {result.funcs} function name{(result.funcs == 1 ? "" : "s")} and {result.links} game link{(result.links == 1 ? "" : "s")}").ConfigureAwait(false);
else
await ctx.RespondAsync("No invalid syscall functions detected").ConfigureAwait(false);
await ctx.RespondAsync("Fixing duplicates...").ConfigureAwait(false);
result = await SyscallInfoProvider.FixDuplicatesAsync().ConfigureAwait(false);
if (result.funcs > 0)
await ctx.RespondAsync($"Successfully merged {result.funcs} function{(result.funcs == 1 ? "" : "s")} and {result.links} game link{(result.links == 1 ? "" : "s")}").ConfigureAwait(false);
else
await ctx.RespondAsync("No duplicate function entries found").ConfigureAwait(false);
}
catch (Exception e)
{
Config.Log.Warn(e, "Failed to fix syscall info");
await ctx.ReactWithAsync(Config.Reactions.Failure, "Failed to fix syscall information").ConfigureAwait(false);
await ctx.ReactWithAsync(Config.Reactions.Failure, "Failed to fix syscall information", true).ConfigureAwait(false);
}
}

View File

@ -47,10 +47,10 @@ namespace CompatBot.Database.Providers
}
}
public static async Task<(int funcs, int links)> FixAsync()
public static async Task<(int funcs, int links)> FixInvalidFunctionNamesAsync()
{
int funcs = 0, links = 0;
var syscallStats = new TSyscallStats();
int funcs = 0, links = 0;
using (var db = new ThumbnailDb())
{
var funcsToFix = new List<SyscallInfo>(0);
@ -58,6 +58,9 @@ namespace CompatBot.Database.Providers
{
funcsToFix = await db.SyscallInfo.Where(sci => sci.Function.Contains('(')).ToListAsync().ConfigureAwait(false);
funcs = funcsToFix.Count;
if (funcs == 0)
return (0, 0);
foreach (var sci in funcsToFix)
{
var productIds = await db.SyscallToProductMap.AsNoTracking().Where(m => m.SyscallInfoId == sci.Id).Select(m => m.Product.ProductCode).Distinct().ToListAsync().ConfigureAwait(false);
@ -98,5 +101,57 @@ namespace CompatBot.Database.Providers
}
return (funcs, links);
}
public static async Task<(int funcs, int links)> FixDuplicatesAsync()
{
int funcs = 0, links = 0;
using (var db = new ThumbnailDb())
{
var duplicateFunctionNames = await db.SyscallInfo.Where(sci => db.SyscallInfo.Count(isci => isci.Function == sci.Function && isci.Module == sci.Module) > 1).Distinct().ToListAsync().ConfigureAwait(false);
if (duplicateFunctionNames.Count == 0)
return (0, 0);
if (await Limiter.WaitAsync(1000, Config.Cts.Token))
{
try
{
foreach (var dupFunc in duplicateFunctionNames)
{
var dups = db.SyscallInfo.Where(sci => sci.Function == dupFunc.Function && sci.Module == dupFunc.Module).ToList();
if (dups.Count < 2)
continue;
var mostCommonDup = dups.Select(dup => (dup, count: db.SyscallToProductMap.Count(scm => scm.SyscallInfoId == dup.Id))).OrderByDescending(stat => stat.count).First().dup;
var dupsToRemove = dups.Where(df => df.Id != mostCommonDup.Id).ToList();
funcs += dupsToRemove.Count;
foreach (var dupToRemove in dupsToRemove)
{
var mappings = db.SyscallToProductMap.Where(scm => scm.SyscallInfoId == dupToRemove.Id).ToList();
links += mappings.Count;
foreach (var mapping in mappings)
{
if (!db.SyscallToProductMap.Any(scm => scm.ProductId == mapping.ProductId && scm.SyscallInfoId == mostCommonDup.Id))
db.SyscallToProductMap.Add(new SyscallToProductMap {ProductId = mapping.ProductId, SyscallInfoId = mostCommonDup.Id});
}
}
await db.SaveChangesAsync().ConfigureAwait(false);
db.SyscallInfo.RemoveRange(dupsToRemove);
await db.SaveChangesAsync().ConfigureAwait(false);
}
await db.SaveChangesAsync().ConfigureAwait(false);
}
catch (Exception e)
{
Config.Log.Warn(e, "Failed to remove duplicate syscall entries");
throw;
}
finally
{
Limiter.Release();
}
}
}
return (funcs, links);
}
}
}