Skip to content

Commit

Permalink
Show all alerts in email message, not just the new ones
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-cameron committed Apr 9, 2018
1 parent ebebb8e commit 1194da5
Showing 1 changed file with 78 additions and 98 deletions.
176 changes: 78 additions & 98 deletions MOE.Common/Business/WatchDog/WatchDogScan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public class WatchDogScan
new ConcurrentBag<MOE.Common.Models.Signal>();
public ConcurrentBag<MOE.Common.Models.Signal> signalsNoRecords =
new ConcurrentBag<MOE.Common.Models.Signal>();
ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent> ForceOffErrors =
ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent> ForceOffErrors =
new ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent>();
public ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent> MaxOutErrors =
public ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent> MaxOutErrors =
new ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent>();
public ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent> LowHitCountErrors =
public ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent> LowHitCountErrors =
new ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent>();
public ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent> MissingRecords =
public ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent> MissingRecords =
new ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent>();
public ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent> StuckPedErrors =
public ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent> StuckPedErrors =
new ConcurrentBag<MOE.Common.Models.SPMWatchDogErrorEvent>();
public List<SPMWatchDogErrorEvent> RecordsFromTheDayBefore = new List<SPMWatchDogErrorEvent>();

Expand Down Expand Up @@ -64,8 +64,8 @@ private void CheckAllSignals(List<Models.Signal> signals)
ParallelOptions options = new ParallelOptions();
options.MaxDegreeOfParallelism = Settings.MaxDegreeOfParallelism;

Parallel.ForEach(signals, options, signal =>
//foreach(var signal in signals)
Parallel.ForEach(signals, options, signal =>
//foreach(var signal in signals)
{
MOE.Common.Business.AnalysisPhaseCollection APcollection =
new MOE.Common.Business.AnalysisPhaseCollection(signal.SignalID,
Expand All @@ -90,9 +90,9 @@ private void CheckSignalsWithData()
DateTime AnalysisEnd = ScanDate.Date + endHour;
ParallelOptions options = new ParallelOptions();
options.MaxDegreeOfParallelism = Settings.MaxDegreeOfParallelism;

Parallel.ForEach(signalsWithRecords, options, signal =>
{
{
CheckForLowDetectorHits(signal);
}
);
Expand All @@ -104,7 +104,7 @@ private void CheckForRecords(List<MOE.Common.Models.Signal> signals)
ParallelOptions options = new ParallelOptions();
options.MaxDegreeOfParallelism = Settings.MaxDegreeOfParallelism;
Parallel.ForEach(signals, options, signal =>
{
{
if (Settings.WeekdayOnly && ScanDate.DayOfWeek == DayOfWeek.Monday)
{
CheckSignalRecordCount(ScanDate.AddDays(-2), signal);
Expand Down Expand Up @@ -154,7 +154,7 @@ private void CreateAndSendEmail()
{
message.To.Add(user.Email);
}
message.To.Add(Settings.DefaultEmailAddress);
message.To.Add(Settings.DefaultEmailAddress);
message.Subject = "ATSPM Alerts for " + ScanDate.ToShortDateString();
message.From = new System.Net.Mail.MailAddress(Settings.FromEmailAddress);
string missingErrors = SortAndAddToMessage(MissingRecords);
Expand Down Expand Up @@ -221,7 +221,7 @@ private void CreateAndSendEmail()
if (LowHitCountErrors.Count > 0 && countErrors != "")
{
message.Body += " \n --The following signals had unusually low advanced detection counts on ";
if (Settings.WeekdayOnly && ScanDate.DayOfWeek == DayOfWeek.Monday)
if (Settings.WeekdayOnly && ScanDate.DayOfWeek == DayOfWeek.Monday)
{

message.Body += ScanDate.AddDays(-3).ToShortDateString() + " between ";
Expand Down Expand Up @@ -282,7 +282,7 @@ private void CheckForLowDetectorHits(MOE.Common.Models.Signal signal)
DateTime end = new DateTime();
if (Settings.WeekdayOnly && ScanDate.DayOfWeek == DayOfWeek.Monday)
{
start = ScanDate.AddDays(-3).Date.AddHours(Settings.PreviousDayPMPeakStart);
start = ScanDate.AddDays(-3).Date.AddHours(Settings.PreviousDayPMPeakStart);
end = ScanDate.AddDays(-3).Date.AddHours(Settings.PreviousDayPMPeakEnd);
}
else
Expand All @@ -300,7 +300,7 @@ private void CheckForLowDetectorHits(MOE.Common.Models.Signal signal)
error.Phase = detector.Approach.ProtectedPhaseNumber;
error.TimeStamp = ScanDate;
error.Direction = detector.Approach.DirectionType.Description;
error.Message = "Count: "+ currentVolume.ToString();
error.Message = "Count: " + currentVolume.ToString();
error.ErrorCode = 2;
if (!LowHitCountErrors.Contains(error))
{
Expand All @@ -314,28 +314,28 @@ private void CheckForLowDetectorHits(MOE.Common.Models.Signal signal)
MOE.Common.Models.Repositories.IApplicationEventRepository er =
MOE.Common.Models.Repositories.ApplicationEventRepositoryFactory.Create();

er.QuickAdd("SPMWatchDog", "Program", "CheckForLowDetectorHits",
er.QuickAdd("SPMWatchDog", "Program", "CheckForLowDetectorHits",
MOE.Common.Models.ApplicationEvent.SeverityLevels.Medium, detector.DetectorID + "-" + ex.Message);
}
}
//);
}

private void CheckForStuckPed(MOE.Common.Business.AnalysisPhase phase, MOE.Common.Models.Signal signal)
private void CheckForStuckPed(MOE.Common.Business.AnalysisPhase phase, MOE.Common.Models.Signal signal)
{
if (phase.PedestrianEvents.Count > Settings.MaximumPedestrianEvents)
{
MOE.Common.Models.SPMWatchDogErrorEvent error = new MOE.Common.Models.SPMWatchDogErrorEvent();
error.SignalID = signal.SignalID;
error.Phase = phase.PhaseNumber;
error.TimeStamp = ScanDate;
error.Direction = phase.Direction??"";
error.Direction = phase.Direction ?? "";
error.Message = phase.PedestrianEvents.Count.ToString() +
" Pedestrian Activations";
error.ErrorCode = 3;
if (!StuckPedErrors.Contains(error))
{
Console.WriteLine("Signal " + signal.SignalID + phase.PedestrianEvents.Count.ToString() +
Console.WriteLine("Signal " + signal.SignalID + phase.PedestrianEvents.Count.ToString() +
" Pedestrian Activations");
StuckPedErrors.Add(error);
}
Expand All @@ -344,13 +344,13 @@ private void CheckForStuckPed(MOE.Common.Business.AnalysisPhase phase, MOE.Comm

private void CheckForForceOff(MOE.Common.Business.AnalysisPhase phase, MOE.Common.Models.Signal signal)
{
if (phase.PercentForceOffs > Settings.PercentThreshold && phase.TerminationEvents.Where(t=>t.EventCode !=7).Count() > Settings.MinPhaseTerminations)
if (phase.PercentForceOffs > Settings.PercentThreshold && phase.TerminationEvents.Where(t => t.EventCode != 7).Count() > Settings.MinPhaseTerminations)
{
MOE.Common.Models.SPMWatchDogErrorEvent error = new MOE.Common.Models.SPMWatchDogErrorEvent();
error.SignalID = signal.SignalID;
error.Phase = phase.PhaseNumber;
error.TimeStamp = ScanDate;
error.Direction = phase.Direction??"";
error.Direction = phase.Direction ?? "";
error.Message = "Force Offs " + Math.Round(phase.PercentForceOffs * 100, 1).ToString() + "%";
error.ErrorCode = 4;
if (!ForceOffErrors.Contains(error))
Expand All @@ -369,36 +369,36 @@ private void CheckForForceOff(MOE.Common.Business.AnalysisPhase phase, MOE.Commo
error.SignalID = signal.SignalID;
error.Phase = phase.PhaseNumber;
error.TimeStamp = ScanDate;
error.Direction = phase.Direction??"";
error.Direction = phase.Direction ?? "";
error.Message = "Max Outs " + Math.Round(phase.PercentMaxOuts * 100, 1).ToString() + "%";
error.ErrorCode = 5;
if (MaxOutErrors.Count == 0 || !MaxOutErrors.Contains(error))
{
Console.WriteLine("Signal " + signal.SignalID + "Has MaxOut Errors");
MaxOutErrors.Add(error);
}

if (MaxOutErrors.Count == 0 || !MaxOutErrors.Contains(error))
{
Console.WriteLine("Signal " + signal.SignalID + "Has MaxOut Errors");
MaxOutErrors.Add(error);
}


}
}

private void SendMessage( System.Net.Mail.MailMessage message)
private void SendMessage(System.Net.Mail.MailMessage message)
{
MOE.Common.Models.Repositories.IApplicationEventRepository er =
MOE.Common.Models.Repositories.ApplicationEventRepositoryFactory.Create();
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(Settings.EmailServer);
try
{
Console.WriteLine("Sent message to: " + message.To.ToString() + "\nMessage text: " + message.Body + "\n");
smtp.Send(message);
System.Threading.Thread.Sleep(5000);
er.QuickAdd("SPMWatchDog", "Program", "SendMessage",
MOE.Common.Models.ApplicationEvent.SeverityLevels.Information,
"Email Sent Successfully to: " + message.To.ToString());
Console.WriteLine("Sent message to: " + message.To.ToString() + "\nMessage text: " + message.Body + "\n");
smtp.Send(message);
System.Threading.Thread.Sleep(5000);
er.QuickAdd("SPMWatchDog", "Program", "SendMessage",
MOE.Common.Models.ApplicationEvent.SeverityLevels.Information,
"Email Sent Successfully to: " + message.To.ToString());
}
catch(Exception ex)
{
catch (Exception ex)
{
er.QuickAdd("SPMWatchDog", "Program", "SendMessage",
MOE.Common.Models.ApplicationEvent.SeverityLevels.Medium, ex.Message);
}
Expand All @@ -408,72 +408,52 @@ private string SortAndAddToMessage(ConcurrentBag<MOE.Common.Models.SPMWatchDogEr
{
MOE.Common.Models.Repositories.ISPMWatchDogErrorEventRepository watchDogErrorEventRepository =
MOE.Common.Models.Repositories.SPMWatchDogErrorEventRepositoryFactory.Create();
List<MOE.Common.Models.SPMWatchDogErrorEvent> SortedErrors =
MOE.Common.Models.Repositories.ISignalsRepository signalRepository =
MOE.Common.Models.Repositories.SignalsRepositoryFactory.Create();
List<MOE.Common.Models.SPMWatchDogErrorEvent> SortedErrors =
errors.OrderBy(x => x.SignalID).ThenBy(x => x.Phase).ToList();

string ErrorMessage = "";
string ErrorMessage = "";

foreach (MOE.Common.Models.SPMWatchDogErrorEvent error in SortedErrors)
foreach (MOE.Common.Models.SPMWatchDogErrorEvent error in SortedErrors)
{
var signal = signalRepository.GetSignalBySignalID(error.SignalID);
ErrorMessage += error.SignalID.ToString();
ErrorMessage += " - ";
ErrorMessage += signal.PrimaryName;
ErrorMessage += " & ";
ErrorMessage += signal.SecondaryName;
if (error.Phase > 0)
{
//List<SPMWatchDogErrorEvent> RecordsFromTheDayBefore = new List<SPMWatchDogErrorEvent>();
//compare to error log to see if this was failing yesterday
if (Settings.WeekdayOnly && ScanDate.DayOfWeek == DayOfWeek.Monday)
{
RecordsFromTheDayBefore =
watchDogErrorEventRepository.GetSPMWatchDogErrorEventsBetweenDates(ScanDate.AddDays(-3), ScanDate.AddDays(-2).AddMinutes(-1));
}
else
{
RecordsFromTheDayBefore =
watchDogErrorEventRepository.GetSPMWatchDogErrorEventsBetweenDates(ScanDate.AddDays(-1), ScanDate.AddMinutes(-1));
}
ErrorMessage += " - Phase ";
ErrorMessage += error.Phase;
}
ErrorMessage += " (" + error.Message + ")";
ErrorMessage += "\n";
}
try
{
watchDogErrorEventRepository.AddList(errors.ToList());

if (FindMatchingErrorInErrorTable(error) == false)
}
catch (DbEntityValidationException ex)
{
foreach (var entityValidationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in entityValidationErrors.ValidationErrors)
{

MOE.Common.Models.Repositories.ISignalsRepository signalRepository =
MOE.Common.Models.Repositories.SignalsRepositoryFactory.Create();
var signal = signalRepository.GetSignalBySignalID(error.SignalID);
// Add to email if it was not failing yesterday
ErrorMessage += error.SignalID.ToString();
ErrorMessage += " - ";
ErrorMessage += signal.PrimaryName;
ErrorMessage += " & ";
ErrorMessage += signal.SecondaryName;
if (error.Phase > 0)
{
ErrorMessage += " - Phase ";
ErrorMessage += error.Phase;
}
ErrorMessage += " (" + error.Message + ")";
ErrorMessage += "\n";
//}

Console.WriteLine("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
}
}
try
{
watchDogErrorEventRepository.AddList(errors.ToList());
}

}
catch (DbEntityValidationException ex)
{
foreach (var entityValidationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in entityValidationErrors.ValidationErrors)
{
Console.WriteLine("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
}
}
}
return ErrorMessage;

}

return ErrorMessage;

}

static private int FindChannel(string SignalID, int Phase)
{

MOE.Common.Models.Repositories.ISignalsRepository smh = MOE.Common.Models.Repositories.SignalsRepositoryFactory.Create();
MOE.Common.Models.Signal sig = smh.GetSignalBySignalID(SignalID);

Expand All @@ -495,13 +475,13 @@ static private int FindChannel(string SignalID, int Phase)
private bool FindMatchingErrorInErrorTable(SPMWatchDogErrorEvent error)
{
var MatchingRecord = (from r in RecordsFromTheDayBefore
where error.SignalID == r.SignalID
&& error.DetectorID == r.DetectorID
&& error.ErrorCode == r.ErrorCode
&& error.Phase == r.Phase
select r).FirstOrDefault();
where error.SignalID == r.SignalID
&& error.DetectorID == r.DetectorID
&& error.ErrorCode == r.ErrorCode
&& error.Phase == r.Phase
select r).FirstOrDefault();

if(MatchingRecord != null)
if (MatchingRecord != null)
{
return true;
}
Expand Down Expand Up @@ -536,6 +516,6 @@ static private string FindDetector(MOE.Common.Models.Signal Signal, int Channel)
}
}


}
}

0 comments on commit 1194da5

Please sign in to comment.