[Core] Use FirstOrDefaultAsync when saving statistics.

This commit is contained in:
2024-05-11 02:53:54 +01:00
parent 169d44dee7
commit 6f66e0f13b
3 changed files with 57 additions and 48 deletions

View File

@@ -93,7 +93,7 @@ public static class Statistics
} }
/// <summary>Saves statistics to disk</summary> /// <summary>Saves statistics to disk</summary>
public static async Task SaveStats() public static async Task SaveStatsAsync()
{ {
try try
{ {
@@ -107,11 +107,11 @@ public static class Statistics
AaruConsole.WriteException(ex); AaruConsole.WriteException(ex);
} }
if(Settings.Settings.Current.Stats is { ShareStats: true }) await SubmitStats(); if(Settings.Settings.Current.Stats is { ShareStats: true }) await SubmitStatsAsync();
} }
/// <summary>Submits statistics to Aaru.Server</summary> /// <summary>Submits statistics to Aaru.Server</summary>
static async Task SubmitStats() static async Task SubmitStatsAsync()
{ {
await using var ctx = AaruContext.Create(Settings.Settings.LocalDbPath); await using var ctx = AaruContext.Create(Settings.Settings.LocalDbPath);
@@ -381,12 +381,13 @@ public static class Statistics
{ {
foreach(string nvs in ctx.Commands.Where(c => !c.Synchronized).Select(c => c.Name).Distinct()) foreach(string nvs in ctx.Commands.Where(c => !c.Synchronized).Select(c => c.Name).Distinct())
{ {
Command existing = ctx.Commands.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? Command existing =
new Command await ctx.Commands.FirstOrDefaultAsync(c => c.Synchronized && c.Name == nvs) ??
{ new Command
Name = nvs, {
Synchronized = true Name = nvs,
}; Synchronized = true
};
existing.Count += (ulong)ctx.Commands.LongCount(c => !c.Synchronized && c.Name == nvs); existing.Count += (ulong)ctx.Commands.LongCount(c => !c.Synchronized && c.Name == nvs);
ctx.Commands.Update(existing); ctx.Commands.Update(existing);
@@ -398,12 +399,13 @@ public static class Statistics
{ {
foreach(string nvs in ctx.Filesystems.Where(c => !c.Synchronized).Select(c => c.Name).Distinct()) foreach(string nvs in ctx.Filesystems.Where(c => !c.Synchronized).Select(c => c.Name).Distinct())
{ {
Filesystem existing = ctx.Filesystems.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? Filesystem existing =
new Filesystem await ctx.Filesystems.FirstOrDefaultAsync(c => c.Synchronized && c.Name == nvs) ??
{ new Filesystem
Name = nvs, {
Synchronized = true Name = nvs,
}; Synchronized = true
};
existing.Count += (ulong)ctx.Filesystems.LongCount(c => !c.Synchronized && c.Name == nvs); existing.Count += (ulong)ctx.Filesystems.LongCount(c => !c.Synchronized && c.Name == nvs);
@@ -417,7 +419,7 @@ public static class Statistics
{ {
foreach(string nvs in ctx.Filters.Where(c => !c.Synchronized).Select(c => c.Name).Distinct()) foreach(string nvs in ctx.Filters.Where(c => !c.Synchronized).Select(c => c.Name).Distinct())
{ {
Filter existing = ctx.Filters.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? Filter existing = await ctx.Filters.FirstOrDefaultAsync(c => c.Synchronized && c.Name == nvs) ??
new Filter new Filter
{ {
Name = nvs, Name = nvs,
@@ -434,12 +436,13 @@ public static class Statistics
{ {
foreach(string nvs in ctx.MediaFormats.Where(c => !c.Synchronized).Select(c => c.Name).Distinct()) foreach(string nvs in ctx.MediaFormats.Where(c => !c.Synchronized).Select(c => c.Name).Distinct())
{ {
MediaFormat existing = ctx.MediaFormats.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? MediaFormat existing =
new MediaFormat await ctx.MediaFormats.FirstOrDefaultAsync(c => c.Synchronized && c.Name == nvs) ??
{ new MediaFormat
Name = nvs, {
Synchronized = true Name = nvs,
}; Synchronized = true
};
existing.Count += (ulong)ctx.MediaFormats.LongCount(c => !c.Synchronized && c.Name == nvs); existing.Count += (ulong)ctx.MediaFormats.LongCount(c => !c.Synchronized && c.Name == nvs);
@@ -453,12 +456,13 @@ public static class Statistics
{ {
foreach(string nvs in ctx.Partitions.Where(c => !c.Synchronized).Select(c => c.Name).Distinct()) foreach(string nvs in ctx.Partitions.Where(c => !c.Synchronized).Select(c => c.Name).Distinct())
{ {
Partition existing = ctx.Partitions.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? Partition existing =
new Partition await ctx.Partitions.FirstOrDefaultAsync(c => c.Synchronized && c.Name == nvs) ??
{ new Partition
Name = nvs, {
Synchronized = true Name = nvs,
}; Synchronized = true
};
existing.Count += (ulong)ctx.Partitions.LongCount(c => !c.Synchronized && c.Name == nvs); existing.Count += (ulong)ctx.Partitions.LongCount(c => !c.Synchronized && c.Name == nvs);
@@ -471,12 +475,13 @@ public static class Statistics
{ {
foreach(string nvs in ctx.Versions.Where(c => !c.Synchronized).Select(c => c.Name).Distinct()) foreach(string nvs in ctx.Versions.Where(c => !c.Synchronized).Select(c => c.Name).Distinct())
{ {
Version existing = ctx.Versions.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? Version existing =
new Version await ctx.Versions.FirstOrDefaultAsync(c => c.Synchronized && c.Name == nvs) ??
{ new Version
Name = nvs, {
Synchronized = true Name = nvs,
}; Synchronized = true
};
existing.Count += (ulong)ctx.Versions.LongCount(c => !c.Synchronized && c.Name == nvs); existing.Count += (ulong)ctx.Versions.LongCount(c => !c.Synchronized && c.Name == nvs);
ctx.Versions.Update(existing); ctx.Versions.Update(existing);
@@ -491,7 +496,9 @@ public static class Statistics
if(ctx.Medias.Any(c => !c.Synchronized && c.Type == media && c.Real)) if(ctx.Medias.Any(c => !c.Synchronized && c.Type == media && c.Real))
{ {
Database.Models.Media existing = Database.Models.Media existing =
ctx.Medias.FirstOrDefault(c => c.Synchronized && c.Type == media && c.Real) ?? await ctx.Medias.FirstOrDefaultAsync(c => c.Synchronized &&
c.Type == media &&
c.Real) ??
new Database.Models.Media new Database.Models.Media
{ {
Synchronized = true, Synchronized = true,
@@ -511,7 +518,9 @@ public static class Statistics
{ {
Database.Models.Media existing = Database.Models.Media existing =
ctx.Medias.FirstOrDefault(c => c.Synchronized && c.Type == media && !c.Real) ?? await ctx.Medias.FirstOrDefaultAsync(c => c.Synchronized &&
c.Type == media &&
!c.Real) ??
new Database.Models.Media new Database.Models.Media
{ {
Synchronized = true, Synchronized = true,
@@ -551,9 +560,9 @@ public static class Statistics
.Distinct()) .Distinct())
{ {
OperatingSystem existing = OperatingSystem existing =
ctx.OperatingSystems.FirstOrDefault(c => c.Synchronized && await ctx.OperatingSystems.FirstOrDefaultAsync(c => c.Synchronized &&
c.Name == osName && c.Name == osName &&
c.Version == osVersion) ?? c.Version == osVersion) ??
new OperatingSystem new OperatingSystem
{ {
Synchronized = true, Synchronized = true,
@@ -587,9 +596,9 @@ public static class Statistics
.Distinct()) .Distinct())
{ {
RemoteApplication existing = RemoteApplication existing =
ctx.RemoteApplications.FirstOrDefault(c => c.Synchronized && await ctx.RemoteApplications.FirstOrDefaultAsync(c => c.Synchronized &&
c.Name == remoteAppName && c.Name == remoteAppName &&
c.Version == remoteAppVersion) ?? c.Version == remoteAppVersion) ??
new RemoteApplication new RemoteApplication
{ {
Synchronized = true, Synchronized = true,
@@ -618,7 +627,7 @@ public static class Statistics
.Distinct()) .Distinct())
{ {
RemoteArchitecture existing = RemoteArchitecture existing =
ctx.RemoteArchitectures.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? await ctx.RemoteArchitectures.FirstOrDefaultAsync(c => c.Synchronized && c.Name == nvs) ??
new RemoteArchitecture new RemoteArchitecture
{ {
Name = nvs, Name = nvs,
@@ -645,9 +654,9 @@ public static class Statistics
.Distinct()) .Distinct())
{ {
RemoteOperatingSystem existing = RemoteOperatingSystem existing =
ctx.RemoteOperatingSystems.FirstOrDefault(c => c.Synchronized && await ctx.RemoteOperatingSystems.FirstOrDefaultAsync(c => c.Synchronized &&
c.Name == remoteOsName && c.Name == remoteOsName &&
c.Version == remoteOsVersion) ?? c.Version == remoteOsVersion) ??
new RemoteOperatingSystem new RemoteOperatingSystem
{ {
Synchronized = true, Synchronized = true,

View File

@@ -296,7 +296,7 @@ public sealed class SplashWindowViewModel(SplashWindow view) : ViewModelBase
Task.Run(async () => Task.Run(async () =>
{ {
await Statistics.SaveStats(); await Statistics.SaveStatsAsync();
Dispatcher.UIThread.Post(LoadMainWindow); Dispatcher.UIThread.Post(LoadMainWindow);
}); });

View File

@@ -250,7 +250,7 @@ class MainClass
int ret = await rootCommand.InvokeAsync(args); int ret = await rootCommand.InvokeAsync(args);
await Statistics.SaveStats(); await Statistics.SaveStatsAsync();
if(!rootCommand.Parse(args).RootCommandResult.GetValueForOption(pauseOption)) return ret; if(!rootCommand.Parse(args).RootCommandResult.GetValueForOption(pauseOption)) return ret;