diff --git a/Cajetan.OF-DL/Worker.cs b/Cajetan.OF-DL/Worker.cs index 45593ae..e044d7e 100644 --- a/Cajetan.OF-DL/Worker.cs +++ b/Cajetan.OF-DL/Worker.cs @@ -252,7 +252,61 @@ internal class Worker(IServiceProvider serviceProvider) private async Task UpdateUserInfoAsync() { + await _dbService.CreateUsersDb([]); + await _dbService.InitializeUserInfoTablesAsync(); + Dictionary users = await _dbService.GetUsersAsync(); + Console.WriteLine(); + + Log.Information("Updating User Info for '{UserCount}' users", users.Count); + AnsiConsole.Markup($"[green]Updating User Info for '{users.Count}' users\n[/]"); + + await AnsiConsole.Progress() + .Columns(new ProgressBarColumn(), new PercentageColumn(), new TaskDescriptionColumn { Alignment = Justify.Left }) + .StartAsync(RunUpdateAsync); + + async Task RunUpdateAsync(ProgressContext context) + { + ProgressTask? updateTask = null; + + int maxUsernameLength = users.Keys.Max(s => s.Length); + + foreach ((string username, long userId) in users) + { + string description = $"Updating '{username}'".PadRight(11 + maxUsernameLength); + double prevValue = updateTask?.Value ?? 0; + + updateTask = context.AddTask(description, true, users.Count); + updateTask.Value = prevValue; + + using (LogContext.PushProperty("Username", username)) + using (LogContext.PushProperty("UserId", userId)) + using (LogContext.PushProperty("UserNum", prevValue + 1)) + using (LogContext.PushProperty("UserTotal", users.Count)) + { + try + { + Log.Information("[{UserNum:0} of {UserTotal}] Updating User Info for for: {Username:l}"); + UserEntities.UserInfo? userInfo = await _apiService.GetDetailedUserInfoAsync($"/users/{username}"); + await _dbService.UpdateUserInfoAsync(userInfo); + + updateTask.Description = $"{description} - COMPLETE"; + } + catch (Exception ex) + { + Log.Warning(ex, "[{UserNum:0} of {UserTotal}] Failed to update User Info for: {Username:l}"); + AnsiConsole.Markup($"[red]Failed to update User Info for '{username}'\n[/]"); + + updateTask.Description = $"{description} - FAILED: {ex.Message}"; + } + finally + { + updateTask.Increment(1); + updateTask.StopTask(); + } + } + } + } } private async Task> GetUsersFromSpecificListsAsync(UserListResult allUsersAndLists, string[] listNames)