forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventStatisticsSink.cs
More file actions
113 lines (98 loc) · 3.42 KB
/
EventStatisticsSink.cs
File metadata and controls
113 lines (98 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Waher.Events.Statistics
{
/// <summary>
/// Calculates statistics on incoming events.
/// </summary>
public class EventStatisticsSink : EventSink
{
private Dictionary<string, Statistic> perActor = new Dictionary<string, Statistic>();
private Dictionary<string, Statistic> perEventId = new Dictionary<string, Statistic>();
private Dictionary<string, Statistic> perFacility = new Dictionary<string, Statistic>();
private Dictionary<string, Statistic> perModule = new Dictionary<string, Statistic>();
private Dictionary<string, Statistic> perLevel = new Dictionary<string, Statistic>();
private Dictionary<string, Statistic> perType = new Dictionary<string, Statistic>();
private Dictionary<string, Statistic> perStackTrace = new Dictionary<string, Statistic>();
private DateTime lastStat = DateTime.MinValue;
private object synchObj = new object();
/// <summary>
/// Calculates statistics on incoming events.
/// </summary>
/// <param name="ObjectID">Object ID of event sink.</param>
public EventStatisticsSink(string ObjectID)
: base(ObjectID)
{
this.lastStat = DateTime.Now;
}
/// <summary>
/// Gets statistics of events logged since last call to <see cref="GetStatisticsSinceLast"/>.
/// </summary>
/// <returns>Event statistics.</returns>
public EventStatistics GetStatisticsSinceLast()
{
EventStatistics Result;
DateTime TP = DateTime.Now;
lock (this.synchObj)
{
Result = new EventStatistics()
{
PerActor = this.perActor,
PerEventId = this.perEventId,
PerFacility = this.perFacility,
PerModule = this.perModule,
PerLevel = this.perLevel,
PerType = this.perType,
PerStackTrace = this.perStackTrace,
LastStat = this.lastStat,
CurrentStat = TP
};
this.perActor = new Dictionary<string, Statistic>();
this.perEventId = new Dictionary<string, Statistic>();
this.perFacility = new Dictionary<string, Statistic>();
this.perModule = new Dictionary<string, Statistic>();
this.perLevel = new Dictionary<string, Statistic>();
this.perType = new Dictionary<string, Statistic>();
this.perStackTrace = new Dictionary<string, Statistic>();
this.lastStat = TP;
}
return Result;
}
/// <summary>
/// Queues an event to be output.
/// </summary>
/// <param name="Event">Event to queue.</param>
public override Task Queue(Event Event)
{
string s;
lock (this.synchObj)
{
this.IncLocked(Event.Type.ToString(), this.perType);
this.IncLocked(Event.Level.ToString(), this.perLevel);
if (!string.IsNullOrEmpty(s = Event.Actor))
this.IncLocked(s, this.perActor);
if (!string.IsNullOrEmpty(s = Event.EventId))
this.IncLocked(s, this.perEventId);
if (!string.IsNullOrEmpty(s = Event.Facility))
this.IncLocked(s, this.perFacility);
if (!string.IsNullOrEmpty(s = Event.Module))
this.IncLocked(s, this.perModule);
if ((Event.Type == EventType.Critical || Event.Type == EventType.Alert || Event.Type == EventType.Emergency) &&
!string.IsNullOrEmpty(Event.StackTrace))
{
this.IncLocked(Event.StackTrace, this.perStackTrace);
}
}
return Task.CompletedTask;
}
private void IncLocked(string Key, Dictionary<string, Statistic> PerKey)
{
if (PerKey.TryGetValue(Key, out Statistic Nr))
Nr.Inc();
else
PerKey[Key] = new Statistic(1);
}
}
}