Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public API_MatchHistoryController(IDbContextFactory<AppDbContext> dbFactory)
_dbFactory = dbFactory;
}

[HttpGet("{startingMatchID}")]
[HttpGet("{since}")]
// TODO: Move to Authorize for this
public async Task<APIResult> GetHistorySince([FromHeader(Name = "X-Api-Key")] string apiKey, Int64 startingMatchID)
public async Task<APIResult> GetHistorySince([FromHeader(Name = "X-Api-Key")] string apiKey, DateTime since)
{
RouteHandler_Get_MatchHistory_Result result = new RouteHandler_Get_MatchHistory_Result();

Expand All @@ -141,10 +141,10 @@ public async Task<APIResult> GetHistorySince([FromHeader(Name = "X-Api-Key")] st
return result;
}

const Int64 maxLobbiesPerRequest = 99; // actually 100, but query is <=
const int maxLobbiesPerRequest = 500;

await using var db = await _dbFactory.CreateDbContextAsync();
result.matches = await Database.MatchHistory.GetMatchesInRange(db, startingMatchID, startingMatchID + maxLobbiesPerRequest);
result.matches = await Database.MatchHistory.GetMatchesSince(db, since, maxLobbiesPerRequest);

return result;
}
Expand Down
13 changes: 9 additions & 4 deletions GenOnlineService/Database/Database.MatchHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,16 +659,21 @@ await db.MatchHistory



public static async Task<MatchHistoryCollection> GetMatchesInRange(
AppDbContext db, long startID, long endID)
public static async Task<MatchHistoryCollection> GetMatchesSince(
AppDbContext db, DateTime since, int maxLobbiesPerRequest)
{
MatchHistoryCollection collection = new();

since = DateTime.SpecifyKind(since, DateTimeKind.Utc);

try
{
// Single query fetches metadata + all slot columns — no concurrent reader issue.
var rows = await db.MatchHistory
.Where(m => m.MatchId >= startID && m.MatchId <= endID && m.Finished)
.Where(m => m.Finished && m.TimeFinished >= since)
.OrderBy(m => m.TimeFinished)
.ThenBy(m => m.MatchId)
.Take(maxLobbiesPerRequest)
.Select(m => new
{
m.MatchId,
Expand Down Expand Up @@ -733,7 +738,7 @@ public static async Task<MatchHistoryCollection> GetMatchesInRange(
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] GetMatchesInRange failed: {ex.Message}");
Console.WriteLine($"[ERROR] GetMatchesSince failed: {ex.Message}");
SentrySdk.CaptureException(ex);
}

Expand Down
Loading