From 314ef707c2f66781a8048fe68ab0b98099cc4610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6ssler?= Date: Fri, 4 Oct 2024 18:21:51 +0200 Subject: [PATCH] Reduce requests --- Guard.Core/Models/AppSettings.cs | 1 + Guard.WPF/Core/Installation/Updater.cs | 15 ++++++++++++++- Guard.WPF/Core/Stats.cs | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Guard.Core/Models/AppSettings.cs b/Guard.Core/Models/AppSettings.cs index f7c1812..a86e40e 100644 --- a/Guard.Core/Models/AppSettings.cs +++ b/Guard.Core/Models/AppSettings.cs @@ -48,5 +48,6 @@ public class AppSettings public bool MinimizeToTray { get; set; } = false; public LockTimeSetting LockTime { get; set; } = LockTimeSetting.TenMinutes; public Version LastUsedAppVersion { get; set; } = new(0, 0); + public DateTime LastAppStartEvent { get; set; } = DateTime.MinValue; } } diff --git a/Guard.WPF/Core/Installation/Updater.cs b/Guard.WPF/Core/Installation/Updater.cs index 8ff2866..10dbc82 100644 --- a/Guard.WPF/Core/Installation/Updater.cs +++ b/Guard.WPF/Core/Installation/Updater.cs @@ -1,6 +1,5 @@ using System.Diagnostics; using System.IO; -using System.Net.Http; using System.Net.Http.Json; using System.Text.Json; using System.Windows; @@ -15,6 +14,9 @@ public class Updater private static readonly JsonSerializerOptions jsonSerializerOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; + private static DateTime lastUpdateCheck = DateTime.MinValue; + private static UpdateInfo? lastUpdateInfo = null; + public class UpdateInfoDownloadUrls { public required string Installer { get; set; } @@ -36,6 +38,12 @@ public class UpdateInfo string currentVersionString = currentVersion?.ToString() ?? ""; bool isPortable = InstallationContext.IsPortable(); + // Cache last update check for 1 hour + if (lastUpdateCheck.AddHours(1) > DateTime.Now) + { + return lastUpdateInfo; + } + try { var httpClient = HTTP.GetHttpClient(); @@ -45,15 +53,20 @@ public class UpdateInfo await httpClient.GetFromJsonAsync(url, jsonSerializerOptions) ?? throw new Exception("Failed to get update info (is null)"); + lastUpdateCheck = DateTime.Now; + Version newVersion = new(updateInfo.Version); if (newVersion.CompareTo(currentVersion) <= 0) { + lastUpdateInfo = null; return null; } + lastUpdateInfo = updateInfo; return updateInfo; } catch (Exception e) { + lastUpdateInfo = null; Log.Logger.Error("Error while checking for updates: {0}", e.Message); return null; } diff --git a/Guard.WPF/Core/Stats.cs b/Guard.WPF/Core/Stats.cs index 921d365..57c2f92 100644 --- a/Guard.WPF/Core/Stats.cs +++ b/Guard.WPF/Core/Stats.cs @@ -4,6 +4,7 @@ using System.Text.Json; using Guard.Core; using Guard.Core.Security; +using Guard.Core.Storage; using Guard.WPF.Core.Installation; namespace Guard.WPF.Core @@ -39,6 +40,19 @@ internal static async void TrackEvent(EventType type) return; } + if (type == EventType.AppStarted) + { + if ( + DateTime.Compare( + SettingsManager.Settings.LastAppStartEvent.Date, + DateTime.Now.Date + ) == 0 + ) + { + return; + } + } + try { var content = new @@ -63,6 +77,12 @@ await httpClient.PostAsJsonAsync(statsApiUrl, content, jsonSerializerOptions) $"Failed to get update info (status code: {response.StatusCode})" ); } + + if (type == EventType.AppStarted) + { + SettingsManager.Settings.LastAppStartEvent = DateTime.Now; + _ = SettingsManager.Save(); + } } catch {