summary refs log tree commit diff
path: root/testFrontend/SafeNSound.FakeUser/MonitorService.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-06-03 21:28:05 +0200
committerRory& <root@rory.gay>2025-06-03 21:28:05 +0200
commit784cb47ceb905abacc3a9bd66d5818fb88ea95a4 (patch)
tree0af6e4ca40d8c5a7ea607379cbe9506bd3280e04 /testFrontend/SafeNSound.FakeUser/MonitorService.cs
parentAdd preparation and build instructions (diff)
downloadnodejs-final-assignment-784cb47ceb905abacc3a9bd66d5818fb88ea95a4.tar.xz
testFrontend: add activity for almost all endpoints
Diffstat (limited to 'testFrontend/SafeNSound.FakeUser/MonitorService.cs')
-rw-r--r--testFrontend/SafeNSound.FakeUser/MonitorService.cs90
1 files changed, 74 insertions, 16 deletions
diff --git a/testFrontend/SafeNSound.FakeUser/MonitorService.cs b/testFrontend/SafeNSound.FakeUser/MonitorService.cs

index 57d90a5..fa2022a 100644 --- a/testFrontend/SafeNSound.FakeUser/MonitorService.cs +++ b/testFrontend/SafeNSound.FakeUser/MonitorService.cs
@@ -1,14 +1,21 @@ +using System.Net; using ArcaneLibs.Extensions; namespace SafeNSound.FakeUser; -public class MonitorService(ILogger<MonitorService> logger, UserStore userStore): IHostedService { - private Task? _getAllAlarmsTask, _assignBudgetTask; +public class MonitorService(ILogger<MonitorService> logger, UserStore userStore) : IHostedService { + private Task? _getAllAlarmsTask, + _assignBudgetTask, + _getAlarmForRandomUserTask, + _rotateMonitoredUsersTask; + private readonly CancellationTokenSource _cts = new(); - + public async Task StartAsync(CancellationToken cancellationToken) { _getAllAlarmsTask = GetAllAlarms(_cts.Token); _assignBudgetTask = AssignBudget(_cts.Token); + _getAlarmForRandomUserTask = GetAlarmForRandomUser(_cts.Token); + _rotateMonitoredUsersTask = RotateMonitoredUsers(_cts.Token); } private async Task GetAllAlarms(CancellationToken cancellationToken) { @@ -16,37 +23,88 @@ public class MonitorService(ILogger<MonitorService> logger, UserStore userStore) try { var user = userStore.GetRandomMonitor(); var alarms = await user.Client!.GetAllAlarms(); - if(alarms.Count > 0) + if (alarms.Count > 0) { logger.LogInformation("Monitor {UserId} has outstanding alarms: {Alarm}", user.Auth.Username, alarms.ToJson(indent: false)); - // else - // logger.LogInformation("Monitor {UserId} found no alarms to query", user.Auth.Username); + await user.Client!.GetAlarm(alarms.Keys.First()); + await user.Client!.DeleteAlarm(alarms.Keys.First()); + } + + await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); + } + catch (Exception ex) { + logger.LogError(ex, "Error querying alarm list"); + } + } + } + + private async Task GetAlarmForRandomUser(CancellationToken cancellationToken) { + while (!cancellationToken.IsCancellationRequested) { + try { + var user = userStore.GetRandomMonitor(); + var watchedUserIds = await user.Client!.GetAssignedUsers(); + if (watchedUserIds.Count == 0) { + logger.LogInformation("Monitor {UserId} has no assigned users", user.Auth.Username); + continue; + } + + await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); + } + catch (Exception ex) { + logger.LogError(ex, "Error querying alarm for user"); + } + } + } + + private async Task RotateMonitoredUsers(CancellationToken cancellationToken) { + while (!cancellationToken.IsCancellationRequested) { + try { + var user = userStore.GetRandomMonitor(); + var watchedUserIds = (await user.Client!.GetAssignedUsers()).ToArray(); + if (watchedUserIds.Length == 0) { + logger.LogInformation("Monitor {UserId} has no assigned users", user.Auth.Username); + } + else { + var idToRemove = Random.Shared.GetItems(watchedUserIds, 1).First(); + await user.Client!.RemoveAssignedUser(idToRemove); + } + + string idToAdd; + do { + idToAdd = userStore.GetRandomUserOfAnyType().WhoAmI!.UserId; + } while (watchedUserIds.Contains(idToAdd)); + + await user.Client!.AddAssignedUser(idToAdd); + await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); - } catch (Exception ex) { - logger.LogError(ex, "Error querying alarm"); + } + catch (Exception ex) { + logger.LogError(ex, "Error rotating monitored users"); } } } - + private async Task AssignBudget(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { try { var user = userStore.GetRandomMonitor(); // var alarms = await user.Client!.GetAllAlarms(); // if(alarms.Count > 0) - // logger.LogInformation("Monitor {UserId} has outstanding alarms: {Alarm}", user.Auth.Username, alarms.ToJson(indent: false)); + // logger.LogInformation("Monitor {UserId} has outstanding alarms: {Alarm}", user.Auth.Username, alarms.ToJson(indent: false)); // else - // logger.LogInformation("Monitor {UserId} found no alarms to query", user.Auth.Username); - await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); - - } catch (Exception ex) { - logger.LogError(ex, "Error querying alarm"); + // logger.LogInformation("Monitor {UserId} found no alarms to query", user.Auth.Username); + await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); + } + catch (Exception ex) { + logger.LogError(ex, "Error assigning budget"); } - } } public async Task StopAsync(CancellationToken cancellationToken) { await _cts.CancelAsync(); await _getAllAlarmsTask!; + await _assignBudgetTask!; + await _getAlarmForRandomUserTask!; + await _rotateMonitoredUsersTask!; } } \ No newline at end of file