Skip to content

Commit

Permalink
Pregenerate ALL profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Simyon264 committed Jul 6, 2024
1 parent 7c2d623 commit 9a20907
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 40 deletions.
23 changes: 1 addition & 22 deletions ReplayBrowser/Services/LeaderboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,15 @@ public class LeaderboardService : IHostedService, IDisposable
private readonly Ss14ApiHelper _apiHelper;
private readonly IServiceScopeFactory _scopeFactory;
private readonly AccountService _accountService;
private readonly ProfilePregeneratorService _profilePregeneratorService;

public LeaderboardService(IMemoryCache cache, Ss14ApiHelper apiHelper, IServiceScopeFactory factory, AccountService accountService, ProfilePregeneratorService profilePregeneratorService)
public LeaderboardService(IMemoryCache cache, Ss14ApiHelper apiHelper, IServiceScopeFactory factory, AccountService accountService)
{
_cache = cache;
_apiHelper = apiHelper;
_scopeFactory = factory;
_accountService = accountService;
_profilePregeneratorService = profilePregeneratorService;
}


public Task StartAsync(CancellationToken cancellationToken)
{
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromHours(6));
Expand Down Expand Up @@ -398,24 +395,6 @@ public async Task<LeaderboardData> GetLeaderboard(RangeOption rangeOption, strin
stopwatch.Stop();
Log.Information("Calculating leaderboard took {Time}ms", stopwatch.ElapsedMilliseconds);

// We loop through every leaderboard and add the player to the permanent pregenerator list if they are not already on it.
foreach (var leaderboard in leaderboards)
{
foreach (var player in leaderboard.Value.Data)
{
if (player.Value.Player == null)
continue;

if (player.Value.Player.PlayerGuid == null)
continue;

if (_profilePregeneratorService.AlwaysGenerateProfiles.Contains((Guid)player.Value.Player.PlayerGuid))
continue;

_profilePregeneratorService.AlwaysGenerateProfiles.Add((Guid)player.Value.Player.PlayerGuid);
}
}

// Save leaderboard to cache (its expensive as fuck to calculate)
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromHours(5));
Expand Down
24 changes: 6 additions & 18 deletions ReplayBrowser/Services/ProfilePregeneratorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public class ProfilePregeneratorService : IHostedService
/// </summary>
private readonly List<Guid> _generatedProfiles = new();

/// <summary>
/// List of profiles which get generated even if they are not on a watched profile list. (example being leaderboards)
/// </summary>
public List<Guid> AlwaysGenerateProfiles = new List<Guid>();

/// <summary>
/// A var which tracks the progress of the pregeneration.
/// </summary>
Expand All @@ -45,9 +40,7 @@ public ProfilePregeneratorService(IServiceScopeFactory scopeFactory, ReplayParse
public Task StartAsync(CancellationToken cancellationToken)
{
_replayParserService.OnReplaysFinishedParsing += OnReplaysParsed;

// In one minute, start the pregeneration.
Task.Delay(TimeSpan.FromMinutes(1), cancellationToken).ContinueWith(t => QueuePregeneration(), cancellationToken);
QueuePregeneration();
return Task.CompletedTask;
}

Expand Down Expand Up @@ -102,17 +95,12 @@ private async void PregenerateProfiles()
using var scope = _scopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<ReplayDbContext>();
var replayHelper = scope.ServiceProvider.GetRequiredService<ReplayHelper>();
var profilesToGenerate = new List<Guid>();
profilesToGenerate.AddRange(AlwaysGenerateProfiles);

await foreach (var account in dbContext.Accounts)
{
foreach (var profile in account.SavedProfiles)
{
if (profilesToGenerate.Contains(profile)) continue;
profilesToGenerate.Add(profile);
}
}
var profilesToGenerate = dbContext.Players
.Select(x => x.PlayerGuid)
.Where(x => !_generatedProfiles.Contains(x))
.Distinct()
.ToList();

// Remove every profile already found in _generatedProfiles
profilesToGenerate = profilesToGenerate.Except(_generatedProfiles).ToList();
Expand Down

0 comments on commit 9a20907

Please sign in to comment.