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;
3using System.Threading.Tasks;
4using Waher.Content;
6using Waher.Events;
13
15{
17 {
18 public SearchEvents()
19 : base("/SearchEvents")
20 {
21 }
22
23 public override bool HandlesSubPaths => false;
24 public override bool UserSessions => true;
25 public bool AllowsPOST => true;
26
27 public async Task POST(HttpRequest Request, HttpResponse Response)
28 {
29 Gateway.AssertUserAuthenticated(Request, "Admin.Data.Events");
30
31 if (!Request.HasData)
32 {
33 await Response.SendResponse(new BadRequestException());
34 return;
35 }
36
37 ContentResponse Content = await Request.DecodeDataAsync();
38 if (Content.HasError || !(Content.Decoded is Dictionary<string, object> Form))
39 {
40 await Response.SendResponse(new BadRequestException());
41 return;
42 }
43
44 if (!Form.TryGetValue("offset", out object Obj) || !(Obj is int Offset) ||
45 !Form.TryGetValue("maxCount", out Obj) || !(Obj is int MaxCount) ||
46 !Form.TryGetValue("from", out Obj) || !(Obj is string FromStr) || !XML.TryParse(FromStr, out DateTime From) ||
47 !Form.TryGetValue("to", out Obj) || !(Obj is string ToStr) || !XML.TryParse(ToStr, out DateTime To) ||
48 !Form.TryGetValue("object", out Obj) || !(Obj is string Object) ||
49 !Form.TryGetValue("actor", out Obj) || !(Obj is string Actor) ||
50 !Form.TryGetValue("eventId", out Obj) || !(Obj is string EventId) ||
51 !Form.TryGetValue("module", out Obj) || !(Obj is string Module) ||
52 !Form.TryGetValue("facility", out Obj) || !(Obj is string Facility) ||
53 !Form.TryGetValue("debug", out Obj) || !(Obj is bool Debug) ||
54 !Form.TryGetValue("informational", out Obj) || !(Obj is bool Informational) ||
55 !Form.TryGetValue("notice", out Obj) || !(Obj is bool Notice) ||
56 !Form.TryGetValue("warning", out Obj) || !(Obj is bool Warning) ||
57 !Form.TryGetValue("error", out Obj) || !(Obj is bool Error) ||
58 !Form.TryGetValue("critical", out Obj) || !(Obj is bool Critical) ||
59 !Form.TryGetValue("alert", out Obj) || !(Obj is bool Alert) ||
60 !Form.TryGetValue("emergency", out Obj) || !(Obj is bool Emergency) ||
61 !Form.TryGetValue("minor", out Obj) || !(Obj is bool Minor) ||
62 !Form.TryGetValue("medium", out Obj) || !(Obj is bool Medium) ||
63 !Form.TryGetValue("major", out Obj) || !(Obj is bool Major))
64 {
65 await Response.SendResponse(new BadRequestException());
66 return;
67 }
68
69 if (To < From)
70 {
71 DateTime Temp = To;
72 To = From;
73 From = Temp;
74 }
75
76 if (Offset == 0)
77 {
79 {
80 new KeyValuePair<string, object>("From", From.ToString()),
81 new KeyValuePair<string, object>("To", To.ToString()),
82 };
83
84 if (!string.IsNullOrEmpty(Object))
85 Tags.Add(new KeyValuePair<string, object>("Object", Object));
86
87 if (!string.IsNullOrEmpty(Actor))
88 Tags.Add(new KeyValuePair<string, object>("Actor", Actor));
89
90 if (!string.IsNullOrEmpty(EventId))
91 Tags.Add(new KeyValuePair<string, object>("EventId", EventId));
92
93 if (!string.IsNullOrEmpty(Module))
94 Tags.Add(new KeyValuePair<string, object>("Module", Module));
95
96 if (!string.IsNullOrEmpty(Facility))
97 Tags.Add(new KeyValuePair<string, object>("Facility", Facility));
98
99 if (!(Debug && Informational && Notice && Warning && Error && Critical && Alert && Emergency))
100 {
101 if (Debug)
102 Tags.Add(new KeyValuePair<string, object>("Debug", Debug));
103
104 if (Informational)
105 Tags.Add(new KeyValuePair<string, object>("Informational", Informational));
106
107 if (Notice)
108 Tags.Add(new KeyValuePair<string, object>("Notice", Notice));
109
110 if (Warning)
111 Tags.Add(new KeyValuePair<string, object>("Warning", Warning));
112
113 if (Error)
114 Tags.Add(new KeyValuePair<string, object>("Error", Error));
115
116 if (Critical)
117 Tags.Add(new KeyValuePair<string, object>("Critical", Critical));
118
119 if (Alert)
120 Tags.Add(new KeyValuePair<string, object>("Alert", Alert));
121
122 if (Emergency)
123 Tags.Add(new KeyValuePair<string, object>("Emergency", Emergency));
124 }
125
126 if (!(Minor && Medium && Major))
127 {
128 if (Minor)
129 Tags.Add(new KeyValuePair<string, object>("Minor", Minor));
130
131 if (Medium)
132 Tags.Add(new KeyValuePair<string, object>("Medium", Medium));
133
134 if (Major)
135 Tags.Add(new KeyValuePair<string, object>("Major", Major));
136 }
137
138 Log.Informational("Searching event log.",
139 string.Empty,
140 Request.RemoteEndPoint,
141 Tags.ToArray());
142 }
143
145 {
146 new FilterFieldGreaterOrEqualTo("Timestamp", From),
147 new FilterFieldLesserOrEqualTo("Timestamp", To)
148 };
149
150 AddFilter(Filters, "Object", Object);
151 AddFilter(Filters, "Actor", Actor);
152 AddFilter(Filters, "EventId", EventId);
153 AddFilter(Filters, "Module", Module);
154 AddFilter(Filters, "Facility", Facility);
155
156 if (!(Debug && Informational && Notice && Warning && Error && Critical && Alert && Emergency))
157 {
158 List<Filter> Ors = new List<Filter>();
159
160 AddFilter(Ors, "Type", Debug, EventType.Debug);
161 AddFilter(Ors, "Type", Informational, EventType.Informational);
162 AddFilter(Ors, "Type", Notice, EventType.Notice);
163 AddFilter(Ors, "Type", Warning, EventType.Warning);
164 AddFilter(Ors, "Type", Error, EventType.Error);
165 AddFilter(Ors, "Type", Critical, EventType.Critical);
166 AddFilter(Ors, "Type", Alert, EventType.Alert);
167 AddFilter(Ors, "Type", Emergency, EventType.Emergency);
168
169 Filters.Add(new FilterOr(Ors.ToArray()));
170 }
171
172 if (!(Minor && Medium && Major))
173 {
174 List<Filter> Ors = new List<Filter>();
175
176 AddFilter(Ors, "Level", Minor, EventLevel.Minor);
177 AddFilter(Ors, "Level", Medium, EventLevel.Medium);
178 AddFilter(Ors, "Level", Major, EventLevel.Major);
179
180 Filters.Add(new FilterOr(Ors.ToArray()));
181 }
182
183 bool More = false;
185 IEnumerable<PersistedEvent> RecordSet = await Database.Find<PersistedEvent>(
186 Offset, MaxCount + 1, new FilterAnd(Filters.ToArray()), "-Timestamp");
187
188 foreach (PersistedEvent Event in RecordSet)
189 {
190 if (MaxCount-- > 0)
191 {
192 Events.Add(new Dictionary<string, object>()
193 {
194 { "timestamp", Event.Timestamp },
195 { "type", Event.Type },
196 { "level", Event.Level },
197 { "message", Event.Message },
198 { "object", Event.Object },
199 { "actor", Event.Actor },
200 { "eventId", Event.EventId },
201 { "module", Event.Module },
202 { "facility", Event.Facility },
203 { "stackTrace", Event.StackTrace },
204 { "tags", TagsToJson(Event.Tags) }
205 });
206 }
207 else
208 {
209 More = true;
210 break;
211 }
212 }
213
214 Dictionary<string, object> Result = new Dictionary<string, object>()
215 {
216 { "more" , More },
217 { "events", Events.ToArray() }
218 };
219
220 await Response.Return(Result);
221 }
222
223 public static void AddFilter(ChunkedList<Filter> Filters, string ParameterName, string Value)
224 {
225 if (!string.IsNullOrEmpty(Value))
226 {
227 if (Value.IndexOf('*') >= 0)
228 Filters.Add(new FilterFieldLikeRegEx(ParameterName, CommonTypes.RegexStringEncode(Value.Replace("*", "__WILDCARD__")).Replace("__WILDCARD__", ".*")));
229 else
230 Filters.Add(new FilterFieldEqualTo(ParameterName, Value));
231 }
232 }
233
234 public static void AddFilter(List<Filter> Filters, string ParameterName, bool Value, Enum EnumValue)
235 {
236 if (Value)
237 Filters.Add(new FilterFieldEqualTo(ParameterName, EnumValue));
238 }
239
240 private static Dictionary<string, object>[] TagsToJson(PersistedTag[] Tags)
241 {
242 if (Tags is null)
243 return null;
244
245 int i, c = Tags.Length;
246 Dictionary<string, object>[] Result = new Dictionary<string, object>[c];
247
248 for (i = 0; i < c; i++)
249 {
250 Result[i] = new Dictionary<string, object>()
251 {
252 { "name", Tags[i].Name },
253 { "value", Tags[i].Value }
254 };
255 }
256
257 return Result;
258 }
259
260 }
261}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static string RegexStringEncode(string s)
Encodes a string for inclusion in a regular expression.
Definition: CommonTypes.cs:815
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Helps with common XML-related tasks.
Definition: XML.cs:21
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:892
Class representing an event.
Definition: Event.cs:11
KeyValuePair< string, object >[] Tags
Variable set of tags providing event-specific information.
Definition: Event.cs:167
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
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:344
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:147
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:3868
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:22
string RemoteEndPoint
Remote end-point.
Definition: HttpRequest.cs:243
bool HasData
If the request has data.
Definition: HttpRequest.cs:113
async Task< ContentResponse > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:139
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
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:21
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:238
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
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
T[] ToArray()
Returns an array containing all elements of the collection.
bool AllowsPOST
If the POST method is allowed.
Definition: SearchEvents.cs:25
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
Definition: SearchEvents.cs:27
POST Interface for HTTP resources.
EventLevel
Event level.
Definition: EventLevel.cs:7
EventType
Type of event.
Definition: EventType.cs:7