Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SearchEventsCommand.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using Waher.Events;
11using Waher.Things;
14
16{
21 {
27 : base(ReportNode)
28 {
29 }
30
31 [Page(2, "Pagination", 200)]
32 [Header(3, "Offset:")]
33 [ToolTip(4, "Skip this number of objects in the result set.")]
34 [Required]
35 [Range(0, int.MaxValue)]
36 public int Offset = 0;
37
38 [Page(2, "Pagination", 200)]
39 [Header(5, "Max Count:")]
40 [ToolTip(6, "Maximum number of objects to return.")]
41 [Required]
42 [Range(1, int.MaxValue)]
43 public int MaxCount = 1000;
44
45 [Page(7, "Time", 150)]
46 [Header(8, "From:")]
47 [ToolTip(9, "Search for events from this point in time.")]
48 public DateTime From = CurrentMinute.AddHours(-1);
49
50 [Page(7, "Time", 150)]
51 [Header(10, "To:")]
52 [ToolTip(11, "Search for events to this point in time.")]
53 public DateTime To = CurrentMinute.AddMinutes(1);
54
55 [Page(12, "Scope", 100)]
56 [Header(13, "Object:")]
57 [ToolTip(14, "Return events related to this object. Use asterisks (*) as wildcard characters.")]
58 public string Object = string.Empty;
59
60 [Page(12, "Scope", 100)]
61 [Header(15, "Actor:")]
62 [ToolTip(16, "Return events related to this actor. Use asterisks (*) as wildcard characters.")]
63 public string Actor = string.Empty;
64
65 [Page(12, "Scope", 100)]
66 [Header(17, "Event ID:")]
67 [ToolTip(18, "Return events with this Event ID. Use asterisks (*) as wildcard characters.")]
68 public string EventId = string.Empty;
69
70 [Page(12, "Scope", 100)]
71 [Header(19, "Module:")]
72 [ToolTip(20, "Return events related to this module. Use asterisks (*) as wildcard characters.")]
73 public string Module = string.Empty;
74
75 [Page(12, "Scope", 100)]
76 [Header(21, "Facility:")]
77 [ToolTip(22, "Return events related to this facility. Use asterisks (*) as wildcard characters.")]
78 public string Facility = string.Empty;
79
80 [Page(23, "Types", 110)]
81 [Header(24, "Debug")]
82 [ToolTip(25, "Check this box to include debug messages in the result.")]
83 public bool Debug = true;
84
85 [Page(23, "Types", 110)]
86 [Header(26, "Information")]
87 [ToolTip(27, "Check this box to include information messages in the result.")]
88 public bool Informational = true;
89
90 [Page(23, "Types", 110)]
91 [Header(28, "Notice")]
92 [ToolTip(29, "Check this box to include notification messages in the result.")]
93 public bool Notice = true;
94
95 [Page(23, "Types", 110)]
96 [Header(30, "Warning")]
97 [ToolTip(31, "Check this box to include warning messages in the result.")]
98 public bool Warning = true;
99
100 [Page(23, "Types", 110)]
101 [Header(32, "Error")]
102 [ToolTip(33, "Check this box to include error messages in the result.")]
103 public bool Error = true;
104
105 [Page(23, "Types", 110)]
106 [Header(34, "Critical")]
107 [ToolTip(35, "Check this box to include critical messages in the result.")]
108 public bool Critical = true;
109
110 [Page(23, "Types", 110)]
111 [Header(36, "Alert")]
112 [ToolTip(37, "Check this box to include alert messages in the result.")]
113 public bool Alert = true;
114
115 [Page(23, "Types", 110)]
116 [Header(38, "Emergency")]
117 [ToolTip(39, "Check this box to include emergency messages in the result.")]
118 public bool Emergency = true;
119
120 [Page(40, "Levels", 120)]
121 [Header(41, "Minor")]
122 [ToolTip(42, "Check this box to include minor events in the result.")]
123 public bool Minor = true;
124
125 [Page(40, "Levels", 120)]
126 [Header(43, "Medium")]
127 [ToolTip(44, "Check this box to include medium events in the result.")]
128 public bool Medium = true;
129
130 [Page(40, "Levels", 120)]
131 [Header(45, "Major")]
132 [ToolTip(46, "Check this box to include major events in the result.")]
133 public bool Major = true;
134
138 public static DateTime CurrentMinute
139 {
140 get
141 {
142 DateTime TP = DateTime.Now;
143 return new DateTime(TP.Year, TP.Month, TP.Day, TP.Hour, TP.Minute, 0);
144 }
145 }
146
151 public override Task<string> GetNameAsync(Language Language)
152 {
153 return Language.GetStringAsync(typeof(SearchEventsCommand), 1, "Search...");
154 }
155
160 public override ICommand Copy()
161 {
163 {
164 Offset = this.Offset,
165 MaxCount = this.MaxCount,
166 From = this.From,
167 To = this.To,
168 Object = this.Object,
169 Actor = this.Actor,
170 EventId = this.EventId,
171 Module = this.Module,
172 Facility = this.Facility,
173 Debug = this.Debug,
174 Informational = this.Informational,
175 Notice = this.Notice,
176 Warning = this.Warning,
177 Error = this.Error,
178 Critical = this.Critical,
179 Alert = this.Alert,
180 Emergency = this.Emergency,
181 Minor = this.Minor,
182 Medium = this.Medium,
183 Major = this.Major
184 };
185 }
186
193 {
194 this.DoSearch(Query, Language);
195 return Task.CompletedTask;
196 }
197
198 private async void DoSearch(Query Query, Language Language)
199 {
200 try
201 {
202 await Query.Start();
203 await Query.SetTitle(await Language.GetStringAsync(typeof(SearchEventsCommand), 47, "Events"));
204
205 if (this.To < this.From)
206 {
207 DateTime Temp = this.To;
208 this.To = this.From;
209 this.From = Temp;
210 }
211
213 {
214 new FilterFieldGreaterOrEqualTo("Timestamp", this.From),
215 new FilterFieldLesserOrEqualTo("Timestamp", this.To)
216 };
217
218 WebServices.SearchEvents.AddFilter(Filters, "Object", this.Object);
219 WebServices.SearchEvents.AddFilter(Filters, "Actor", this.Actor);
220 WebServices.SearchEvents.AddFilter(Filters, "EventId", this.EventId);
221 WebServices.SearchEvents.AddFilter(Filters, "Module", this.Module);
222 WebServices.SearchEvents.AddFilter(Filters, "Facility", this.Facility);
223
224 if (!(this.Debug && this.Informational && this.Notice && this.Warning && this.Error && this.Critical && this.Alert && this.Emergency))
225 {
226 List<Filter> Ors = new List<Filter>();
227
228 WebServices.SearchEvents.AddFilter(Ors, "Type", this.Debug, EventType.Debug);
229 WebServices.SearchEvents.AddFilter(Ors, "Type", this.Informational, EventType.Informational);
230 WebServices.SearchEvents.AddFilter(Ors, "Type", this.Notice, EventType.Notice);
231 WebServices.SearchEvents.AddFilter(Ors, "Type", this.Warning, EventType.Warning);
232 WebServices.SearchEvents.AddFilter(Ors, "Type", this.Error, EventType.Error);
233 WebServices.SearchEvents.AddFilter(Ors, "Type", this.Critical, EventType.Critical);
234 WebServices.SearchEvents.AddFilter(Ors, "Type", this.Alert, EventType.Alert);
235 WebServices.SearchEvents.AddFilter(Ors, "Type", this.Emergency, EventType.Emergency);
236
237 Filters.Add(new FilterOr(Ors.ToArray()));
238 }
239
240 if (!(this.Minor && this.Medium && this.Major))
241 {
242 List<Filter> Ors = new List<Filter>();
243
244 WebServices.SearchEvents.AddFilter(Ors, "Level", this.Minor, EventLevel.Minor);
245 WebServices.SearchEvents.AddFilter(Ors, "Level", this.Medium, EventLevel.Medium);
246 WebServices.SearchEvents.AddFilter(Ors, "Level", this.Major, EventLevel.Major);
247
248 Filters.Add(new FilterOr(Ors.ToArray()));
249 }
250
251 await Query.SetStatus(await Language.GetStringAsync(typeof(SearchEventsCommand), 48, "Searching database..."));
252 await Query.BeginSection(await Language.GetStringAsync(typeof(SearchEventsCommand), 49, "Search Result"));
253 await Query.NewTable("Events", await Language.GetStringAsync(typeof(SearchEventsCommand), 47, "Events"),
254 new Column("Timestamp", await Language.GetStringAsync(typeof(SearchEventsCommand), 50, "Timestamp"),
255 null, null, null, null, ColumnAlignment.Left, null),
256 new Column("Type", await Language.GetStringAsync(typeof(SearchEventsCommand), 51, "Type"),
257 null, null, null, null, ColumnAlignment.Left, null),
258 new Column("Level", await Language.GetStringAsync(typeof(SearchEventsCommand), 52, "Level"),
259 null, null, null, null, ColumnAlignment.Left, null),
260 new Column("Object", await Language.GetStringAsync(typeof(SearchEventsCommand), 53, "Object"),
261 null, null, null, null, ColumnAlignment.Left, null),
262 new Column("Actor", await Language.GetStringAsync(typeof(SearchEventsCommand), 54, "Actor"),
263 null, null, null, null, ColumnAlignment.Left, null),
264 new Column("EventId", await Language.GetStringAsync(typeof(SearchEventsCommand), 55, "EventId"),
265 null, null, null, null, ColumnAlignment.Left, null),
266 new Column("Module", await Language.GetStringAsync(typeof(SearchEventsCommand), 56, "Module"),
267 null, null, null, null, ColumnAlignment.Left, null),
268 new Column("Facility", await Language.GetStringAsync(typeof(SearchEventsCommand), 57, "Facility"),
269 null, null, null, null, ColumnAlignment.Left, null),
270 new Column("Message", await Language.GetStringAsync(typeof(SearchEventsCommand), 58, "Message"),
271 null, null, null, null, ColumnAlignment.Left, null));
272
273 foreach (PersistedEvent Event in await Database.Find<PersistedEvent>(this.Offset, this.MaxCount + 1, new FilterAnd(Filters.ToArray()), "-Timestamp"))
274 {
275 if (this.MaxCount-- <= 0)
276 break;
277
278 StringBuilder sb = new StringBuilder(Event.Message);
279
280 if (!(Event.Tags is null) && Event.Tags.Length > 0)
281 {
282 sb.AppendLine();
283
284 foreach (PersistedTag Tag in Event.Tags)
285 {
286 sb.AppendLine();
287 sb.Append(Tag.Name);
288 sb.Append('=');
289 sb.Append(Tag.Value?.ToString());
290 }
291 }
292
293 if (!string.IsNullOrEmpty(Event.StackTrace))
294 {
295 sb.AppendLine();
296 sb.AppendLine();
297 sb.AppendLine(Event.StackTrace);
298 }
299
300 await Query.NewRecords("Events",
301 new Record(Event.Timestamp, Event.Type.ToString(), Event.Level.ToString(), Event.Object, Event.Actor,
302 Event.EventId, Event.Module, Event.Facility, sb.ToString()));
303 }
304
305 await Query.TableDone("Events");
306 await Query.EndSection();
307 }
308 catch (Exception ex)
309 {
310 await Query.LogMessage(ex);
311 }
312 finally
313 {
314 await Query.Done();
315 }
316 }
317
318 }
319}
Class representing an event.
Definition: Event.cs:11
string Message
Free-text event message.
Definition: Event.cs:132
EventType Type
Type of event.
Definition: Event.cs:122
string Object
Object related to the event.
Definition: Event.cs:137
EventLevel Level
Event Level.
Definition: Event.cs:127
string Actor
Actor responsible for the action causing the event.
Definition: Event.cs:142
string Module
Module where the event is reported.
Definition: Event.cs:157
DateTime Timestamp
Timestamp of event.
Definition: Event.cs:117
KeyValuePair< string, object >[] Tags
Variable set of tags providing event-specific information.
Definition: Event.cs:167
string EventId
Computer-readable Event ID identifying type of even.
Definition: Event.cs:147
string Facility
Facility can be either a facility in the network sense or in the system sense.
Definition: Event.cs:152
string StackTrace
Stack Trace of event.
Definition: Event.cs:162
Class representing a persisted event.
Class representing a persisted tag.
Definition: PersistedTag.cs:10
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 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 conform to any of the child-filters provided.
Definition: FilterOr.cs:10
ReportNode Report
Report node.
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.
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
SearchEventsCommand(SearchEventLogNode ReportNode)
Query command that searches the event log for events.
override ICommand Copy()
Creates a copy of the command object.
static DateTime CurrentMinute
Returns the current time, omitting seconds and fractions of seconds.
override Task< string > GetNameAsync(Language Language)
Gets the displayable name of the command.
override Task StartQueryExecutionAsync(Query Query, Language Language)
Starts the execution of a query.
Abstract base class for commands executing a server administrator report.
Defines a column in a table.
Definition: Column.cs:30
Class handling the reception of data from a query.
Definition: Query.cs:12
Task TableDone(string TableId)
Reports a table as being complete.
Definition: Query.cs:327
Task EndSection()
Ends a section. Each call to BeginSection(string) must be followed by a call to EndSection().
Definition: Query.cs:504
Task Start()
Starts query execution.
Definition: Query.cs:213
Task NewRecords(string TableId, params Record[] Records)
Reports a new set of records in a table.
Definition: Query.cs:300
Task BeginSection(string Header)
Begins a new section. Sections can be nested. Each call to BeginSection(string) must be followed by a...
Definition: Query.cs:483
Task NewTable(string TableId, string TableName, params Column[] Columns)
Defines a new table in the query output.
Definition: Query.cs:272
Task LogMessage(Exception Exception)
Logs an Exception as a query message.
Definition: Query.cs:381
async Task Done()
Query execution completed.
Definition: Query.cs:241
Task SetStatus(string Status)
Sets the current status of the query execution.
Definition: Query.cs:457
Task SetTitle(string Title)
Sets the title of the report.
Definition: Query.cs:430
Defines a record in a table.
Definition: Record.cs:9
Interface for commands.
Definition: ICommand.cs:32
EventLevel
Event level.
Definition: EventLevel.cs:7
EventType
Type of event.
Definition: EventType.cs:7
ColumnAlignment
Column alignment.
Definition: Column.cs:9