Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PerformanceAlertMonitor.cs
1using System;
2using Waher.Events;
3
5{
11 {
12 private readonly string alertMessage;
13 private readonly string alertRecinded;
14 private readonly string emergencyMessage;
15 private readonly string emergencyRecinded;
16 private readonly double alertLevel;
17 private readonly int alertConsecutive;
18 private readonly double emergencyLevel;
19 private readonly int emergencyConsecutive;
20 private readonly int sign;
21 private int state = 0;
22 private int nrAboveAlertLevel = 0;
23 private int nrAboveEmergencyLevel = 0;
24 private int nrBelowAlertLevel = 0;
25 private int nrBelowEmergencyLevel = 0;
26
39 public PerformanceAlertMonitor(double AlertLevel, int AlertConsecutive,
40 double EmergencyLevel, int EmergencyConsecutive, string AlertMessage,
41 string AlertRecinded, string EmergencyMessage, string EmergencyRecinded)
42 {
43 this.sign = Math.Sign(EmergencyLevel - AlertLevel);
44
45 if (AlertConsecutive <= 0)
46 throw new ArgumentOutOfRangeException(nameof(AlertConsecutive), "Value must be positive.");
47
48 if (EmergencyConsecutive <= 0)
49 throw new ArgumentOutOfRangeException(nameof(EmergencyConsecutive), "Value must be positive.");
50
51 this.alertLevel = AlertLevel;
52 this.alertConsecutive = AlertConsecutive;
53 this.alertMessage = AlertMessage;
54 this.alertRecinded = AlertRecinded;
55 this.emergencyLevel = EmergencyLevel;
56 this.emergencyConsecutive = EmergencyConsecutive;
57 this.emergencyMessage = EmergencyMessage;
58 this.emergencyRecinded = EmergencyRecinded;
59 }
60
64 public bool IsNormal => this.state == 0;
65
69 public bool IsAlert => this.state >= 1;
70
74 public bool IsEmergency => this.state >= 2;
75
80 public void Sample(double Value)
81 {
82 if ((Value - this.emergencyLevel) * this.sign >= 0)
83 {
84 this.nrBelowEmergencyLevel = 0;
85 this.nrBelowAlertLevel = 0;
86
87 if (this.state < 2)
88 {
89 this.nrAboveEmergencyLevel++;
90 this.nrAboveAlertLevel++;
91
92 if (this.nrAboveEmergencyLevel >= this.emergencyConsecutive)
93 {
94 this.state = 2;
95
96 Log.Emergency(this.emergencyMessage);
97 this.Emergency.Raise(this, EventArgs.Empty);
98 }
99 }
100 }
101 else
102 {
103 this.nrAboveEmergencyLevel = 0;
104
105 if (this.state >= 2)
106 {
107 this.nrBelowEmergencyLevel++;
108 if (this.nrBelowEmergencyLevel >= this.emergencyConsecutive)
109 {
110 this.state = 1;
111
112 this.EmergencyRecinded.Raise(this, EventArgs.Empty);
113
114 if ((Value - this.alertLevel) * this.sign >= 0)
115 {
116 Log.Alert(this.alertMessage + "\r\n\r\n(" + this.emergencyRecinded + ")");
117 this.Alert.Raise(this, EventArgs.Empty);
118 }
119 else
120 Log.Notice(this.emergencyRecinded);
121 }
122 }
123
124 if ((Value - this.alertLevel) * this.sign >= 0)
125 {
126 this.nrBelowAlertLevel = 0;
127
128 if (this.state < 1)
129 {
130 this.nrAboveAlertLevel++;
131
132 if (this.nrAboveAlertLevel >= this.alertConsecutive)
133 {
134 this.state = 1;
135
136 Log.Alert(this.alertMessage);
137 this.Alert.Raise(this, EventArgs.Empty);
138 }
139 }
140 }
141 else
142 {
143 this.nrAboveAlertLevel = 0;
144
145 if (this.state >= 1)
146 {
147 this.nrBelowAlertLevel++;
148 if (this.nrBelowAlertLevel >= this.alertConsecutive)
149 {
150 this.state = 0;
151
152 Log.Notice(this.alertRecinded);
153 this.AlertRecinded.Raise(this, EventArgs.Empty);
154 }
155 }
156 }
157 }
158 }
159
163 public event EventHandler Alert;
164
168 public event EventHandler AlertRecinded;
169
173 public event EventHandler Emergency;
174
178 public event EventHandler EmergencyRecinded;
179 }
180}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Emergency(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an emergency event.
Definition: Log.cs:1447
static void Notice(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs a notice event.
Definition: Log.cs:460
static void Alert(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an alert event.
Definition: Log.cs:1237
Monitors performance samples, and alerts the operator if performance levels degrate.
EventHandler AlertRecinded
Event raised when alert monitor leaves alert state.
PerformanceAlertMonitor(double AlertLevel, int AlertConsecutive, double EmergencyLevel, int EmergencyConsecutive, string AlertMessage, string AlertRecinded, string EmergencyMessage, string EmergencyRecinded)
Monitors performance samples, and alerts the operator if performance levels degrate.
EventHandler EmergencyRecinded
Event raised when alert monitor leaves emergency state.
bool IsNormal
If not in an alert or emergency state.
EventHandler Emergency
Event raised when alert monitor goes into emergency state.
EventHandler Alert
Event raised when alert monitor goes into alert state.