Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SearchEvents.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Content;
6using Waher.Events;
12
14{
16 {
17 public SearchEvents()
18 : base("/SearchEvents")
19 {
20 }
21
22 public override bool HandlesSubPaths => false;
23 public override bool UserSessions => true;
24 public bool AllowsPOST => true;
25
26 public async Task POST(HttpRequest Request, HttpResponse Response)
27 {
28 Gateway.AssertUserAuthenticated(Request, "Admin.Data.Events");
29
30 if (!Request.HasData)
31 throw new BadRequestException();
32
33 object Obj = await Request.DecodeDataAsync();
34 if (!(Obj is Dictionary<string, object> Form))
35 throw new BadRequestException();
36
37 if (!Form.TryGetValue("offset", out Obj) || !(Obj is int Offset) ||
38 !Form.TryGetValue("maxCount", out Obj) || !(Obj is int MaxCount) ||
39 !Form.TryGetValue("from", out Obj) || !(Obj is string FromStr) || !XML.TryParse(FromStr, out DateTime From) ||
40 !Form.TryGetValue("to", out Obj) || !(Obj is string ToStr) || !XML.TryParse(ToStr, out DateTime To) ||
41 !Form.TryGetValue("object", out Obj) || !(Obj is string Object) ||
42 !Form.TryGetValue("actor", out Obj) || !(Obj is string Actor) ||
43 !Form.TryGetValue("eventId", out Obj) || !(Obj is string EventId) ||
44 !Form.TryGetValue("module", out Obj) || !(Obj is string Module) ||
45 !Form.TryGetValue("facility", out Obj) || !(Obj is string Facility) ||
46 !Form.TryGetValue("debug", out Obj) || !(Obj is bool Debug) ||
47 !Form.TryGetValue("informational", out Obj) || !(Obj is bool Informational) ||
48 !Form.TryGetValue("notice", out Obj) || !(Obj is bool Notice) ||
49 !Form.TryGetValue("warning", out Obj) || !(Obj is bool Warning) ||
50 !Form.TryGetValue("error", out Obj) || !(Obj is bool Error) ||
51 !Form.TryGetValue("critical", out Obj) || !(Obj is bool Critical) ||
52 !Form.TryGetValue("alert", out Obj) || !(Obj is bool Alert) ||
53 !Form.TryGetValue("emergency", out Obj) || !(Obj is bool Emergency) ||
54 !Form.TryGetValue("minor", out Obj) || !(Obj is bool Minor) ||
55 !Form.TryGetValue("medium", out Obj) || !(Obj is bool Medium) ||
56 !Form.TryGetValue("major", out Obj) || !(Obj is bool Major))
57 {
58 throw new BadRequestException();
59 }
60
61 if (To < From)
62 {
63 DateTime Temp = To;
64 To = From;
65 From = Temp;
66 }
67
68 if (Offset == 0)
69 {
70 List<KeyValuePair<string, object>> Tags = new List<KeyValuePair<string, object>>()
71 {
72 new KeyValuePair<string, object>("From", From.ToString()),
73 new KeyValuePair<string, object>("To", To.ToString()),
74 };
75
76 if (!string.IsNullOrEmpty(Object))
77 Tags.Add(new KeyValuePair<string, object>("Object", Object));
78
79 if (!string.IsNullOrEmpty(Actor))
80 Tags.Add(new KeyValuePair<string, object>("Actor", Actor));
81
82 if (!string.IsNullOrEmpty(EventId))
83 Tags.Add(new KeyValuePair<string, object>("EventId", EventId));
84
85 if (!string.IsNullOrEmpty(Module))
86 Tags.Add(new KeyValuePair<string, object>("Module", Module));
87
88 if (!string.IsNullOrEmpty(Facility))
89 Tags.Add(new KeyValuePair<string, object>("Facility", Facility));
90
91 if (!(Debug && Informational && Notice && Warning && Error && Critical && Alert && Emergency))
92 {
93 if (Debug)
94 Tags.Add(new KeyValuePair<string, object>("Debug", Debug));
95
96 if (Informational)
97 Tags.Add(new KeyValuePair<string, object>("Informational", Informational));
98
99 if (Notice)
100 Tags.Add(new KeyValuePair<string, object>("Notice", Notice));
101
102 if (Warning)
103 Tags.Add(new KeyValuePair<string, object>("Warning", Warning));
104
105 if (Error)
106 Tags.Add(new KeyValuePair<string, object>("Error", Error));
107
108 if (Critical)
109 Tags.Add(new KeyValuePair<string, object>("Critical", Critical));
110
111 if (Alert)
112 Tags.Add(new KeyValuePair<string, object>("Alert", Alert));
113
114 if (Emergency)
115 Tags.Add(new KeyValuePair<string, object>("Emergency", Emergency));
116 }
117
118 if (!(Minor && Medium && Major))
119 {
120 if (Minor)
121 Tags.Add(new KeyValuePair<string, object>("Minor", Minor));
122
123 if (Medium)
124 Tags.Add(new KeyValuePair<string, object>("Medium", Medium));
125
126 if (Major)
127 Tags.Add(new KeyValuePair<string, object>("Major", Major));
128 }
129
130 Log.Informational("Searching event log.",
131 string.Empty,
132 Request.RemoteEndPoint,
133 Tags.ToArray());
134 }
135
136 List<Filter> Filters = new List<Filter>()
137 {
138 new FilterFieldGreaterOrEqualTo("Timestamp", From),
139 new FilterFieldLesserOrEqualTo("Timestamp", To)
140 };
141
142 AddFilter(Filters, "Object", Object);
143 AddFilter(Filters, "Actor", Actor);
144 AddFilter(Filters, "EventId", EventId);
145 AddFilter(Filters, "Module", Module);
146 AddFilter(Filters, "Facility", Facility);
147
148 if (!(Debug && Informational && Notice && Warning && Error && Critical && Alert && Emergency))
149 {
150 List<Filter> Ors = new List<Filter>();
151
152 AddFilter(Ors, "Type", Debug, EventType.Debug);
153 AddFilter(Ors, "Type", Informational, EventType.Informational);
154 AddFilter(Ors, "Type", Notice, EventType.Notice);
155 AddFilter(Ors, "Type", Warning, EventType.Warning);
156 AddFilter(Ors, "Type", Error, EventType.Error);
157 AddFilter(Ors, "Type", Critical, EventType.Critical);
158 AddFilter(Ors, "Type", Alert, EventType.Alert);
159 AddFilter(Ors, "Type", Emergency, EventType.Emergency);
160
161 Filters.Add(new FilterOr(Ors.ToArray()));
162 }
163
164 if (!(Minor && Medium && Major))
165 {
166 List<Filter> Ors = new List<Filter>();
167
168 AddFilter(Ors, "Level", Minor, EventLevel.Minor);
169 AddFilter(Ors, "Level", Medium, EventLevel.Medium);
170 AddFilter(Ors, "Level", Major, EventLevel.Major);
171
172 Filters.Add(new FilterOr(Ors.ToArray()));
173 }
174
175 bool More = false;
176 List<Dictionary<string, object>> Events = new List<Dictionary<string, object>>();
177
178 foreach (PersistedEvent Event in await Database.Find<PersistedEvent>(Offset, MaxCount + 1, new FilterAnd(Filters.ToArray()), "-Timestamp"))
179 {
180 if (MaxCount-- > 0)
181 {
182 Events.Add(new Dictionary<string, object>()
183 {
184 { "timestamp", Event.Timestamp },
185 { "type", Event.Type },
186 { "level", Event.Level },
187 { "message", Event.Message },
188 { "object", Event.Object },
189 { "actor", Event.Actor },
190 { "eventId", Event.EventId },
191 { "module", Event.Module },
192 { "facility", Event.Facility },
193 { "stackTrace", Event.StackTrace },
194 { "tags", TagsToJson(Event.Tags) }
195 });
196 }
197 else
198 {
199 More = true;
200 break;
201 }
202 }
203
204 Dictionary<string, object> Result = new Dictionary<string, object>()
205 {
206 { "more" , More },
207 { "events", Events.ToArray() }
208 };
209
210 await Response.Return(Result);
211 }
212
213 public static void AddFilter(List<Filter> Filters, string ParameterName, string Value)
214 {
215 if (!string.IsNullOrEmpty(Value))
216 {
217 if (Value.IndexOf('*') >= 0)
218 Filters.Add(new FilterFieldLikeRegEx(ParameterName, CommonTypes.RegexStringEncode(Value.Replace("*", "__WILDCARD__")).Replace("__WILDCARD__", ".*")));
219 else
220 Filters.Add(new FilterFieldEqualTo(ParameterName, Value));
221 }
222 }
223
224 public static void AddFilter(List<Filter> Filters, string ParameterName, bool Value, Enum EnumValue)
225 {
226 if (Value)
227 Filters.Add(new FilterFieldEqualTo(ParameterName, EnumValue));
228 }
229
230 private static Dictionary<string, object>[] TagsToJson(PersistedTag[] Tags)
231 {
232 if (Tags is null)
233 return null;
234
235 int i, c = Tags.Length;
236 Dictionary<string, object>[] Result = new Dictionary<string, object>[c];
237
238 for (i = 0; i < c; i++)
239 {
240 Result[i] = new Dictionary<string, object>()
241 {
242 { "name", Tags[i].Name },
243 { "value", Tags[i].Value }
244 };
245 }
246
247 return Result;
248 }
249
250 }
251}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string RegexStringEncode(string s)
Encodes a string for inclusion in a regular expression.
Definition: CommonTypes.cs:813
Helps with common XML-related tasks.
Definition: XML.cs:19
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:744
Class representing an event.
Definition: Event.cs:10
KeyValuePair< string, object >[] Tags
Variable set of tags providing event-specific information.
Definition: Event.cs:166
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Informational(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an informational event.
Definition: Log.cs:334
Class representing a persisted event.
Class representing a persisted tag.
Definition: PersistedTag.cs:10
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
Definition: Gateway.cs:3041
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:18
string RemoteEndPoint
Remote end-point.
Definition: HttpRequest.cs:195
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:95
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:247
This filter selects objects that conform to all child-filters provided.
Definition: FilterAnd.cs:10
This filter selects objects that have a named field equal to a given value.
This filter selects objects that have a named field greater or equal to a given value.
This filter selects objects that have a named field lesser or equal to a given value.
This filter selects objects that have a named field matching a given regular expression.
This filter selects objects that conform to any of the child-filters provided.
Definition: FilterOr.cs:10
bool AllowsPOST
If the POST method is allowed.
Definition: SearchEvents.cs:24
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
Definition: SearchEvents.cs:26
POST Interface for HTTP resources.
EventLevel
Event level.
Definition: EventLevel.cs:7
EventType
Type of event.
Definition: EventType.cs:7