Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Dec 21, 2024
1 parent aa2b0d1 commit 78cfa68
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 196 deletions.
2 changes: 1 addition & 1 deletion JL.Core/Audio/AudioUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public static async Task GetAndPlayAudio(string foundSpelling, string? reading)
{
await Utils.Frontend.StopTextToSpeech().ConfigureAwait(false);
Utils.Frontend.PlayAudio(audioResponse.AudioData, audioResponse.AudioFormat);
Stats.IncrementStat(StatType.TimesPlayedAudio);
StatsUtils.IncrementStat(StatType.TimesPlayedAudio);
}
}

Expand Down
6 changes: 3 additions & 3 deletions JL.Core/Config/ConfigDBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ FOREIGN KEY (profile_id) REFERENCES profile (id) ON DELETE CASCADE
if (defaultProfileExists)
{
ProfileUtils.CurrentProfileId = ProfileDBUtils.GetCurrentProfileIdFromDB(connection, ProfileUtils.DefaultProfileId);
Stats.LifetimeStats = StatsDBUtils.GetStatsFromDB(connection, ProfileUtils.DefaultProfileId)!;
StatsUtils.LifetimeStats = StatsDBUtils.GetStatsFromDB(connection, ProfileUtils.DefaultProfileId)!;
}

ProfileDBUtils.InsertGlobalProfile(connection);
StatsDBUtils.InsertStats(connection, Stats.LifetimeStats, ProfileUtils.GlobalProfileId);
StatsDBUtils.InsertStats(connection, StatsUtils.LifetimeStats, ProfileUtils.GlobalProfileId);
}

if (!defaultProfileExists)
{
ProfileDBUtils.InsertDefaultProfile(connection);
StatsDBUtils.InsertStats(connection, Stats.ProfileLifetimeStats, ProfileUtils.CurrentProfileId);
StatsDBUtils.InsertStats(connection, StatsUtils.ProfileLifetimeStats, ProfileUtils.CurrentProfileId);
}
}

Expand Down
6 changes: 3 additions & 3 deletions JL.Core/Config/CoreConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ public void ApplyPreferences(SqliteConnection connection)

if (TrackTermLookupCounts)
{
if (Stats.ProfileLifetimeStats.TermLookupCountDict.Count > 0)
if (StatsUtils.ProfileLifetimeStats.TermLookupCountDict.Count > 0)
{
StatsDBUtils.UpdateProfileLifetimeStats(connection);
}

if (Stats.LifetimeStats.TermLookupCountDict.Count > 0)
if (StatsUtils.LifetimeStats.TermLookupCountDict.Count > 0)
{
StatsDBUtils.UpdateLifetimeStats(connection);
}
Expand All @@ -107,7 +107,7 @@ public void ApplyPreferences(SqliteConnection connection)
TrackTermLookupCounts = ConfigDBManager.GetValueFromConfig(connection, TrackTermLookupCounts, nameof(TrackTermLookupCounts), bool.TryParse);
if (!TrackTermLookupCounts)
{
Stats.SessionStats.TermLookupCountDict.Clear();
StatsUtils.SessionStats.TermLookupCountDict.Clear();
}

AnkiIntegration = ConfigDBManager.GetValueFromConfig(connection, AnkiIntegration, nameof(AnkiIntegration), bool.TryParse);
Expand Down
16 changes: 8 additions & 8 deletions JL.Core/Config/StatsDBUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ public static void UpdateLifetimeStats()

public static void UpdateLifetimeStats(SqliteConnection connection)
{
UpdateStats(connection, Stats.LifetimeStats, ProfileUtils.GlobalProfileId);
UpdateStats(connection, StatsUtils.LifetimeStats, ProfileUtils.GlobalProfileId);
if (CoreConfigManager.Instance.TrackTermLookupCounts)
{
UpsertTermLookupCounts(connection, Stats.LifetimeStats.TermLookupCountDict, ProfileUtils.GlobalProfileId);
Stats.LifetimeStats.TermLookupCountDict.Clear();
UpsertTermLookupCounts(connection, StatsUtils.LifetimeStats.TermLookupCountDict, ProfileUtils.GlobalProfileId);
StatsUtils.LifetimeStats.TermLookupCountDict.Clear();
}
}

Expand All @@ -150,18 +150,18 @@ public static void UpdateProfileLifetimeStats()

public static void UpdateProfileLifetimeStats(SqliteConnection connection)
{
UpdateStats(connection, Stats.ProfileLifetimeStats, ProfileUtils.CurrentProfileId);
UpdateStats(connection, StatsUtils.ProfileLifetimeStats, ProfileUtils.CurrentProfileId);
if (CoreConfigManager.Instance.TrackTermLookupCounts)
{
UpsertTermLookupCounts(connection, Stats.ProfileLifetimeStats.TermLookupCountDict, ProfileUtils.CurrentProfileId);
Stats.ProfileLifetimeStats.TermLookupCountDict.Clear();
UpsertTermLookupCounts(connection, StatsUtils.ProfileLifetimeStats.TermLookupCountDict, ProfileUtils.CurrentProfileId);
StatsUtils.ProfileLifetimeStats.TermLookupCountDict.Clear();
}
}

public static void SetStatsFromDB(SqliteConnection connection)
{
Stats.LifetimeStats = GetStatsFromDB(connection, ProfileUtils.GlobalProfileId)!;
Stats.ProfileLifetimeStats = GetStatsFromDB(connection, ProfileUtils.CurrentProfileId)!;
StatsUtils.LifetimeStats = GetStatsFromDB(connection, ProfileUtils.GlobalProfileId)!;
StatsUtils.ProfileLifetimeStats = GetStatsFromDB(connection, ProfileUtils.CurrentProfileId)!;
}

internal static void ResetAllTermLookupCounts(int profileId)
Expand Down
8 changes: 5 additions & 3 deletions JL.Core/Mining/MiningUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,10 @@ public static class MiningUtils
case JLField.Audio:
case JLField.Image:
case JLField.LocalTime:
default:
return null;

default:
throw new ArgumentOutOfRangeException(null, field, "Invalid JLField");
}
}

Expand Down Expand Up @@ -728,7 +730,7 @@ public static async Task MineToFile(LookupResult lookupResult, string currentTex

await File.AppendAllTextAsync(filePath, lineToMine.ToString()).ConfigureAwait(false);

Stats.IncrementStat(StatType.CardsMined);
StatsUtils.IncrementStat(StatType.CardsMined);

Utils.Frontend.Alert(AlertLevel.Success, $"Mined {lookupResult.PrimarySpelling}");
Utils.Logger.Information("Mined {PrimarySpelling}", lookupResult.PrimarySpelling);
Expand Down Expand Up @@ -954,7 +956,7 @@ public static async Task Mine(LookupResult lookupResult, string currentText, str
await AnkiConnect.Sync().ConfigureAwait(false);
}

Stats.IncrementStat(StatType.CardsMined);
StatsUtils.IncrementStat(StatType.CardsMined);
}

/// <summary>
Expand Down
153 changes: 0 additions & 153 deletions JL.Core/Statistics/Stats.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.Json.Serialization;
using JL.Core.Config;

namespace JL.Core.Statistics;

Expand All @@ -14,156 +13,4 @@ public sealed class Stats
public ulong Imoutos { get; set; }

[JsonIgnore] public Dictionary<string, int> TermLookupCountDict { get; } = new(StringComparer.Ordinal);

[JsonIgnore] public static Stats SessionStats { get; set; } = new();
[JsonIgnore] public static Stats ProfileLifetimeStats { get; set; } = new();
[JsonIgnore] public static Stats LifetimeStats { get; set; } = new();

public static void IncrementStat(StatType type, long amount = 1)
{
switch (type)
{
case StatType.Characters:
{
bool positive = amount >= 0;
ulong unsignedAmount = positive
? (ulong)amount
: (ulong)-amount;

if (positive)
{
SessionStats.Characters += unsignedAmount;
ProfileLifetimeStats.Characters += unsignedAmount;
LifetimeStats.Characters += unsignedAmount;
}
else
{
SessionStats.Characters -= unsignedAmount;
ProfileLifetimeStats.Characters -= unsignedAmount;
LifetimeStats.Characters -= unsignedAmount;
}

break;
}

case StatType.Lines:
{
bool positive = amount >= 0;
ulong unsignedAmount = positive
? (ulong)amount
: (ulong)-amount;

if (positive)
{
SessionStats.Lines += unsignedAmount;
ProfileLifetimeStats.Lines += unsignedAmount;
LifetimeStats.Lines += unsignedAmount;
}
else
{
SessionStats.Lines -= unsignedAmount;
ProfileLifetimeStats.Lines -= unsignedAmount;
LifetimeStats.Lines -= unsignedAmount;
}

break;
}

case StatType.Time:
{
SessionStats.Time = SessionStats.Time.Add(TimeSpan.FromTicks(amount));
ProfileLifetimeStats.Time = ProfileLifetimeStats.Time.Add(TimeSpan.FromTicks(amount));
LifetimeStats.Time = LifetimeStats.Time.Add(TimeSpan.FromTicks(amount));

break;
}

case StatType.CardsMined:
{
ulong unsignedAmount = (ulong)amount;
SessionStats.CardsMined += unsignedAmount;
ProfileLifetimeStats.CardsMined += unsignedAmount;
LifetimeStats.CardsMined += unsignedAmount;

break;
}

case StatType.TimesPlayedAudio:
{
ulong unsignedAmount = (ulong)amount;
SessionStats.TimesPlayedAudio += unsignedAmount;
ProfileLifetimeStats.TimesPlayedAudio += unsignedAmount;
LifetimeStats.TimesPlayedAudio += unsignedAmount;

break;
}

case StatType.NumberOfLookups:
{
ulong unsignedAmount = (ulong)amount;
SessionStats.NumberOfLookups += unsignedAmount;
ProfileLifetimeStats.NumberOfLookups += unsignedAmount;
LifetimeStats.NumberOfLookups += unsignedAmount;

break;
}

case StatType.Imoutos:
{
ulong unsignedAmount = (ulong)amount;
SessionStats.Imoutos += unsignedAmount;
ProfileLifetimeStats.Imoutos += unsignedAmount;
LifetimeStats.Imoutos += unsignedAmount;

break;
}

default:
throw new ArgumentOutOfRangeException(nameof(type), type, "Invalid StatType");
}
}

public static void ResetStats(StatsMode statsMode)
{
Stats stats = statsMode switch
{
StatsMode.Lifetime => LifetimeStats,
StatsMode.Profile => ProfileLifetimeStats,
StatsMode.Session => SessionStats,
_ => throw new ArgumentOutOfRangeException(nameof(statsMode), statsMode, "Invalid StatsMode")
};

stats.Characters = 0;
stats.Lines = 0;
stats.Time = TimeSpan.Zero;
stats.CardsMined = 0;
stats.TimesPlayedAudio = 0;
stats.Imoutos = 0;

stats.TermLookupCountDict.Clear();
if (statsMode is StatsMode.Profile or StatsMode.Lifetime)
{
StatsDBUtils.ResetAllTermLookupCounts(statsMode is StatsMode.Profile ? ProfileUtils.CurrentProfileId : ProfileUtils.GlobalProfileId);
}
}

public static void IncrementTermLookupCount(string primarySpelling)
{
IncrementLookupStat(SessionStats, primarySpelling);
IncrementLookupStat(ProfileLifetimeStats, primarySpelling);
IncrementLookupStat(LifetimeStats, primarySpelling);
}

private static void IncrementLookupStat(Stats stats, string primarySpelling)
{
Dictionary<string, int> lookupStatsDict = stats.TermLookupCountDict;
if (lookupStatsDict.TryGetValue(primarySpelling, out int count))
{
lookupStatsDict[primarySpelling] = count + 1;
}
else
{
lookupStatsDict[primarySpelling] = 1;
}
}
}
Loading

0 comments on commit 78cfa68

Please sign in to comment.