private async Task SaveIncidentAsync(int componentId, IWatcherCheckResult result, bool notify = false) { var date = _configuration.DateTimeProvider().Date; var componentStatus = result.IsValid ? ComponentStatus.Operational : ComponentStatus.MajorOutage; var incidentStatus = result.IsValid ? IncidentStatus.Fixed : IncidentStatus.Identified; var name = $"{result.WatcherName} check is {(result.IsValid ? "valid" : "invalid")}"; var incidents = await _cachetService.GetIncidentsAsync(componentId); var incident = incidents.FirstOrDefault(x => x.ComponentId == componentId && x.CreatedAt?.Date == date); var message = result.Description; if (incident == null) { incident = await _cachetService.CreateIncidentAsync(name, message, incidentStatus, notify : notify, componentId : componentId, componentStatus : componentStatus); } else { incident = await _cachetService.UpdateIncidentAsync(incident.Id, name, message, incidentStatus, notify : notify, componentId : componentId, componentStatus : componentStatus); } }
private async Task SaveIncidentAsync(int componentId, IWatcherCheckResult result, bool notify = false, bool saveValidIncident = false, bool updateIfStatusIsTheSame = false) { var date = _configuration.DateTimeProvider().Date; var componentStatus = result.IsValid ? ComponentStatus.Operational : ComponentStatus.MajorOutage; var incidentStatus = result.IsValid ? IncidentStatus.Fixed : IncidentStatus.Identified; var incidents = await _cachetService.GetIncidentsAsync(componentId); var existingIncidentStatus = incidents.FirstOrDefault(x => x.ComponentId == componentId)?.Status; //If there's neither failure nor previous incident reported, do not report a valid service check. if (!saveValidIncident && result.IsValid && existingIncidentStatus == null) { return; } if (!updateIfStatusIsTheSame && existingIncidentStatus == incidentStatus) { return; } var name = $"{result.WatcherName} check is {(result.IsValid ? "valid" : "invalid")}"; var incident = incidents.FirstOrDefault(x => x.ComponentId == componentId && x.CreatedAt?.Date == date); var message = result.Description; if (incident == null) { incident = await _cachetService.CreateIncidentAsync(name, message, incidentStatus, notify : notify, componentId : componentId, componentStatus : componentStatus); } else { incident = await _cachetService.UpdateIncidentAsync(incident.Id, name, message, incidentStatus, notify : notify, componentId : componentId, componentStatus : componentStatus); } }