public Entities.CheckBatch ToEntity(Runner.CheckBatch checkBatch) { return(new Entities.CheckBatch { Time = TimeUtilities.DateTimeOffsetToLong(checkBatch.Time), Duration = TimeUtilities.TimeSpanToLong(checkBatch.Duration) }); }
public Entities.CheckResult ToEntity(Runner.CheckResult checkResult) { return(new Entities.CheckResult { Type = ToEntity(checkResult.Type), Message = checkResult.Message, Time = TimeUtilities.DateTimeOffsetToLong(checkResult.Time), Duration = TimeUtilities.TimeSpanToLong(checkResult.Duration) }); }
public void DateTimeOffset_WithUtcNow_RoundTrips() { // Arrange var expected = DateTimeOffset.UtcNow; // Act var intermediate = TimeUtilities.DateTimeOffsetToLong(expected); var actual = TimeUtilities.LongToDateTimeOffset(intermediate); // Assert Assert.Equal(expected, actual); }
public void DateTimeOffset_WithArbitrary_RoundTrips() { // Arrange var expected = new DateTimeOffset(2016, 1, 2, 3, 4, 5, 6, TimeSpan.Zero); // Act var intermediate = TimeUtilities.DateTimeOffsetToLong(expected); var actual = TimeUtilities.LongToDateTimeOffset(intermediate); // Assert Assert.Equal(expected, actual); }
private CheckResult GetCheckResult(int minutesDelta, CheckResultType type) { return(new CheckResult { Check = new Check { Name = CheckName }, CheckBatch = new CheckBatch(), Type = type, Time = TimeUtilities.DateTimeOffsetToLong(UtcNow.AddMinutes(minutesDelta)) }); }
private async Task <CheckResultAndHealth> GetCheckResultAndHealthAsync(string checkName, CancellationToken token) { var latestCheckResult = await _context .CheckResults .Where(x => x.Check.Name == checkName) .OrderByDescending(x => x.Time) .FirstOrDefaultAsync(); if (latestCheckResult == null) { // No check results exist. return(null); } if (latestCheckResult.Type != Entities.CheckResultType.Failure) { // Not currently failing. return(new CheckResultAndHealth { CheckResult = latestCheckResult, IsHealthy = true }); } var timeThreshold = TimeUtilities.DateTimeOffsetToLong(_systemClock.UtcNow - DurationThreshold); var oldestFailures = await _context .CheckResults .Where(x => x.Check.Name == checkName && x.Time > timeThreshold && x.Type == Entities.CheckResultType.Failure) .OrderBy(x => x.Time) .Take(CountThreshold) .ToListAsync(); if (oldestFailures.Count < CountThreshold) { // Not enough failures to act on. return(null); } var oldestFailure = oldestFailures.First(); return(new CheckResultAndHealth { CheckResult = oldestFailure, IsHealthy = false }); }
public async Task <Heartbeat> CreateHeartbeatAsync(string heartGroupName, string heartName, CancellationToken token) { var now = _systemClock.UtcNow; var heart = await _context .Hearts .Include(x => x.HeartGroup) .FirstOrDefaultAsync(x => x.HeartGroup.Name == heartGroupName && x.Name == heartName, token); if (heart == null) { var heartGroup = await _context .HeartGroups .FirstOrDefaultAsync(x => x.Name == heartGroupName, token); if (heartGroup == null) { heartGroup = new Entities.HeartGroup { Name = heartGroupName }; } heart = new Entities.Heart { HeartGroup = heartGroup, Name = heartName }; } var heartbeat = new Entities.Heartbeat { Heart = heart, Time = TimeUtilities.DateTimeOffsetToLong(now) }; _context.Heartbeats.Add(heartbeat); await _context.SaveChangesAsync(token); return(_entityMapper.ToBusiness(heartbeat)); }
public async Task <CheckNotification> CheckForNotificationAsync(string checkName, CancellationToken token) { var checkResultAndHealth = await GetCheckResultAndHealthAsync(checkName, token); if (checkResultAndHealth == null) { // Don't notify when there is no check result to act on. return(null); } var notification = await GetNotificationAsync(checkName, token); if (notification == null) { if (checkResultAndHealth.IsHealthy) { // Don't notify when the check is healthy and there is no previous notification. return(null); } notification = new Entities.CheckNotification { CheckId = checkResultAndHealth.CheckResult.CheckId, Version = 0 }; _context.CheckNotifications.Add(notification); } else if (notification.IsHealthy == checkResultAndHealth.IsHealthy) { // Don't notify the health status is not changing. return(null); } notification.CheckResultId = checkResultAndHealth.CheckResult.CheckResultId; notification.CheckResult = checkResultAndHealth.CheckResult; notification.Time = TimeUtilities.DateTimeOffsetToLong(_systemClock.UtcNow); notification.IsHealthy = checkResultAndHealth.IsHealthy; notification.Version++; // Add the notification record var record = new Entities.CheckNotificationRecord { CheckId = notification.CheckId, CheckResultId = notification.CheckResultId, Time = notification.Time, IsHealthy = notification.IsHealthy, Version = notification.Version, CheckNotification = notification }; _context.CheckNotificationRecords.Add(record); try { await _context.SaveChangesAsync(); return(_entityMapper.ToBusiness(notification)); } catch (DbUpdateConcurrencyException) { // This means another caller updated the notification already. return(null); } }