public async Task UpdateMaintenanceJobs(GlobalSettings global = null) { if (global == null) { global = await _settingsHelper.GetGlobal(); } await UpdateCleanupJob(global); }
public async Task <CheckerSettings> SetSettingsAsync([FromBody] CheckerSettings value) { var global = await _settingsHelper.GetGlobal(); await _settingsHelper.Set(value); if (global.TimeZoneId != value.Global.TimeZoneId) { _logger.LogInformation($"TimeZoneId changed from {global.TimeZoneId} to {value.Global.TimeZoneId}, updating schedules"); await _manager.UpdateSchedules(); } await _manager.UpdateMaintenanceJobs(value.Global); return(await GetSettingsAsync()); }
public async Task Execute(IJobExecutionContext context) { var global = await _settingsHelper.GetGlobal(); if (!global.ResultAggregateDays.HasValue) { _logger.LogWarning("ResultAggregateDays is unset, cannot run cleanup job"); return; } if (!global.ResultRetentionMonths.HasValue) { _logger.LogWarning("ResultRetentionMonths is unset, cannot run cleanup job"); return; } var resultsToAggregate = await _checkResults.GetAll() .Where(x => x.DTS < DateTimeOffset.UtcNow.AddDays(-global.ResultAggregateDays.Value)) .GroupBy(x => new { x.DTS.UtcDateTime.Hour, x.DTS.UtcDateTime.Date, x.Status, x.CheckID }) .Where(x => x.Count() > 1) .ToListAsync(); foreach (var group in resultsToAggregate) { var timeMSAverage = Convert.ToInt32(group.Average(x => x.TimeMS)); var ticks = group.Select(x => x.DTS.UtcTicks); var avgTicks = ticks.Select(i => i / ticks.Count()).Sum() + ticks.Select(i => i % ticks.Count()).Sum() / ticks.Count(); var dateAverage = new DateTimeOffset(avgTicks, TimeSpan.Zero); _checkResults.Add(new CheckResult { CheckID = group.Key.CheckID, DTS = dateAverage, Status = group.Key.Status, TimeMS = timeMSAverage }); } _checkResults.DeleteRange(resultsToAggregate.SelectMany(x => x)); await _checkResults.SaveChangesAsync(); var resultsToDelete = await _checkResults.GetAll() .Where(x => x.DTS < DateTimeOffset.UtcNow.AddMonths(-global.ResultRetentionMonths.Value)) .ToListAsync(); _checkResults.DeleteRange(resultsToDelete); await _checkResults.SaveChangesAsync(); }
protected override async Task SendNotification(NotificationType type, string message, string failedAfter = null) { var global = await _settingsHelper.GetGlobal(); if (string.IsNullOrEmpty(global.Url)) { throw new Exception("Global Url setting is required for Slack notification"); } ; var url = $"{global.Url}/details/{_check.ID}"; message = $"Check <{url}|{_check.Name}> completed in {_result.TimeMS}ms with result status: {_result.Status.Name.ToString()} (Type: {_result.Status.Type.Name})"; var channelID = _notification.GetOptions <Options>().ChannelID; _logger.Info($"Sending slack notification to channel ID {channelID}: {message}"); await _slackHelper.SendMessage(channelID, message); }
public async Task <string> GetToken(User user) { var claims = new[] { new Claim("username", user.Username), new Claim("isWindowsUser", user.IsWindowsUser.ToString()) }; var key = await GetSecurityKey(); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var global = await _settingsHelper.GetGlobal(); var token = new JwtSecurityToken( claims: claims, notBefore: DateTime.UtcNow, expires: DateTime.UtcNow.AddDays(global.LoginExpireAfterDays), signingCredentials: creds); return(_tokenHandler.WriteToken(token)); }