Skip to content

Commit

Permalink
Reduce requests
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoessler committed Oct 4, 2024
1 parent 2a85fbe commit 314ef70
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions Guard.Core/Models/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
15 changes: 14 additions & 1 deletion Guard.WPF/Core/Installation/Updater.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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; }
Expand All @@ -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();
Expand All @@ -45,15 +53,20 @@ public class UpdateInfo
await httpClient.GetFromJsonAsync<UpdateInfo>(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;
}
Expand Down
20 changes: 20 additions & 0 deletions Guard.WPF/Core/Stats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
{
Expand Down

0 comments on commit 314ef70

Please sign in to comment.