[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,7 +381,8 @@ 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 =
await ctx.Commands.FirstOrDefaultAsync(c => c.Synchronized && c.Name == nvs) ??
new Command new Command
{ {
Name = nvs, Name = nvs,
@@ -398,7 +399,8 @@ 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 =
await ctx.Filesystems.FirstOrDefaultAsync(c => c.Synchronized && c.Name == nvs) ??
new Filesystem new Filesystem
{ {
Name = nvs, 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,7 +436,8 @@ 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 =
await ctx.MediaFormats.FirstOrDefaultAsync(c => c.Synchronized && c.Name == nvs) ??
new MediaFormat new MediaFormat
{ {
Name = nvs, Name = nvs,
@@ -453,7 +456,8 @@ 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 =
await ctx.Partitions.FirstOrDefaultAsync(c => c.Synchronized && c.Name == nvs) ??
new Partition new Partition
{ {
Name = nvs, Name = nvs,
@@ -471,7 +475,8 @@ 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 =
await ctx.Versions.FirstOrDefaultAsync(c => c.Synchronized && c.Name == nvs) ??
new Version new Version
{ {
Name = nvs, Name = nvs,
@@ -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,7 +560,7 @@ 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
@@ -587,7 +596,7 @@ 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
@@ -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,7 +654,7 @@ 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

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;