public Report Save(Fact[] facts) { if (facts.Length == 0 || string.IsNullOrWhiteSpace(facts[0].SourceCode)) { Console.WriteLine("facts or SourceCode is empty"); return null; } try { Report report = null; using (var transaction = _unitOfWork.BeginTransaction()) { report = new Report { Id = 0, CreatedAt = DateTime.Now, SourceCode = facts[0].SourceCode, IsLast = true }; var reportId = _reportRepository.Save(report); foreach (var fact in facts) fact.ReportId = reportId; _factRepository.InsertMany(facts); transaction.Commit(); } Console.WriteLine("{0} facts saved", facts.Length); return report; } catch (Exception e) { Console.WriteLine($"Report saving failed: {e.Message}"); } return null; }
private void SendNewReportNotification(Report report, Fact[] facts) { if (!NeedToSendNotification(report, facts)) { return; } using (var scope = _objectFactory.CreateScope()) { var notificationService = scope.Resolve<INotificationService>(); notificationService.SendNewReport(report, facts); } }
private bool NeedToSendNotification(Report report, Fact[] facts) { using (var scope = _objectFactory.CreateScope()) { var unitOfWork = scope.Resolve<IUnitOfWork>(); var previousReportDateSql = QueryStore.PreviousReportBySourceCode(); var previousReportDate = unitOfWork.Session.Query(previousReportDateSql, new {report.SourceCode}); var currentCount = Enum.GetValues(typeof (FactLevel)) .Cast<FactLevel>() .ToDictionary(l => (int)l, l => facts.Count(f => f.Level == l)); var previousCount = Enum.GetValues(typeof (FactLevel)) .Cast<FactLevel>() .ToDictionary(l => (int) l, l => previousReportDate.Count(f => f.Level == (int) l)); foreach (var current in currentCount) { if (previousCount[current.Key] != current.Value) { return true; } } return false; } }
public void SendNewReport(Report report, Fact[] facts) { var sourceCode = report.SourceCode; var recievers = _config.Notification.Recievers.Cast<Reciever>() .Where(r => r.SourceList.Count == 0 || r.SourceList.Contains(sourceCode)); var levelCount = new ReportInfo { CreatedAt = report.CreatedAt, Level25 = facts.Count(f => f.Level == FactLevel.Normal), Level50 = facts.Count(f => f.Level == FactLevel.Warning), Level75 = facts.Count(f => f.Level == FactLevel.Error), Level100 = facts.Count(f => f.Level == FactLevel.Critical), CustomStyles = CustomStyles(report.CreatedAt) }; foreach (var reciever in recievers) { var data = new List<KeyValuePair<string, ReportInfo>> { new KeyValuePair<string, ReportInfo>(sourceCode, levelCount) }; SourceInfoEmailTemplate template = new SourceInfoEmailTemplate() { SourceCodeData = data }; var emailBody = template.TransformText(); var info = new NotificationSenderInfo { Body = emailBody, Recipient = reciever.Name, Subject = string.Format("[{0}] new important events", report.SourceCode ) }; SendEmail(info); } }