Skip to content

Commit

Permalink
Refactor session ID check to handle large batches
Browse files Browse the repository at this point in the history
Refactored `CheckIfSessionsAlreadyExists` to process session IDs in batches of 20, improving performance and avoiding query size limitations. The new implementation splits session IDs, constructs and executes queries for each batch, and combines results into a single list of unique existing session IDs.
  • Loading branch information
FrodeHus committed Dec 19, 2024
1 parent 980ed98 commit 02dfc23
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions BeyondTrustConnector/AccessSessionUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,23 @@ public async Task Run([TimerTrigger("0 */15 * * * *", RunOnStartup = false)] Tim

private static async Task<List<string?>> CheckIfSessionsAlreadyExists(QueryService queryService, IEnumerable<XElement> sessions)
{
var sessionIds = sessions.Select(s => s.Attribute("lsid")!.Value).Distinct().Aggregate(string.Empty, (current, next) => current += $"'{next}',").TrimEnd(',');
var existingSessionResult = await queryService.QueryWorkspace($"BeyondTrustAccessSession_CL | where SessionId in ({sessionIds}) | project SessionId");
var existingSessions = existingSessionResult?.Table.Rows.Select(r => r[0].ToString()).ToList() ?? [];
return existingSessions;
var sessionIds = sessions.Select(s => s.Attribute("lsid")!.Value).Distinct().ToList();
var existingSessions = new List<string?>();

const int batchSize = 20;
for (int i = 0; i < sessionIds.Count; i += batchSize)
{
var batch = sessionIds.Skip(i).Take(batchSize);
var batchIds = string.Join(",", batch.Select(id => $"'{id}'"));
var query = $"BeyondTrustAccessSession_CL | where SessionId in ({batchIds}) | project SessionId";
var existingSessionResult = await queryService.QueryWorkspace(query);
if (existingSessionResult?.Table.Rows is not null)
{
existingSessions.AddRange(existingSessionResult.Table.Rows.Select(r => r[0].ToString()));
}
}

return existingSessions.Distinct().ToList();
}

private static List<Dictionary<string, object>> GetUserDetails(XElement session, string ns)
Expand Down

0 comments on commit 02dfc23

Please sign in to comment.