Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmppStatistics.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
7using Waher.Things;
10
12{
13 public class XmppStatistics : ICommand
14 {
15 [Page(1, "Time", 150)]
16 [Header(2, "From:")]
17 [ToolTip(3, "Search for records from this point in time.")]
18 public DateTime From = DateTime.MinValue;
19
20 [Page(1, "Time", 150)]
21 [Header(4, "To:")]
22 [ToolTip(5, "Search for records to this point in time.")]
23 public DateTime To = DateTime.MaxValue;
24
25 public string CommandID => "Statistics";
26 public CommandType Type => CommandType.Query;
27 public string SortCategory => "Protocols";
28 public string SortKey => "Statistics";
29
30 public Task<string> GetNameAsync(Language Language)
31 {
32 return Language.GetStringAsync(typeof(XmppStatistics), 6, "Statistics...");
33 }
34
35 public Task<bool> CanExecuteAsync(RequestOrigin Caller)
36 {
37 return XmppServerModule.IsAdmin(Caller.From);
38 }
39
40 public ICommand Copy()
41 {
42 return new XmppStatistics()
43 {
44 From = this.From,
45 To = this.To
46 };
47 }
48
49 public Task ExecuteCommandAsync()
50 {
51 throw new NotSupportedException();
52 }
53
55 {
56 return Task.FromResult(string.Empty);
57 }
58
60 {
61 return Task.FromResult(string.Empty);
62 }
63
65 {
66 return Task.FromResult(string.Empty);
67 }
68
70 {
71 this.Execute(Query, Language);
72 return Task.CompletedTask;
73 }
74
75 private async void Execute(Query Query, Language Language)
76 {
77 try
78 {
79 await Query.Start();
80 await Query.SetTitle(await Language.GetStringAsync(typeof(XmppStatistics), 15, "Requests"));
81
82 if (this.To < this.From)
83 {
84 DateTime Temp = this.To;
85 this.To = this.From;
86 this.From = Temp;
87 }
88
89 List<Filter> Filters = new List<Filter>();
90 IEnumerable<XmppStatistic> Records;
91
92 if (this.From > DateTime.MinValue)
93 Filters.Add(new FilterFieldGreaterOrEqualTo("Timestamp", this.From));
94
95 if (this.To < DateTime.MaxValue)
96 Filters.Add(new FilterFieldLesserOrEqualTo("Timestamp", this.To));
97
98 await Query.SetStatus(await Language.GetStringAsync(typeof(XmppStatistics), 8, "Searching database..."));
99
100 switch (Filters.Count)
101 {
102 case 0:
103 Records = await Database.Find<XmppStatistic>("Timestamp");
104 break;
105
106 case 1:
107 Records = await Database.Find<XmppStatistic>(Filters[0], "Timestamp");
108 break;
109
110 default:
111 Records = await Database.Find<XmppStatistic>(new FilterAnd(Filters.ToArray()), "Timestamp");
112 break;
113 }
114
115 string Header = await Language.GetStringAsync(typeof(XmppStatistics), 17, "XMPP Statistics");
116 await Query.BeginSection(Header);
117 await Query.NewTable("Statistics", Header, new Column[]
118 {
119 new Column("Start", await Language.GetStringAsync(typeof(XmppStatistics), 10, "Start"),
120 null, null, null, null, ColumnAlignment.Left, null),
121 new Column("Timestamp", await Language.GetStringAsync(typeof(XmppStatistics), 11, "Timestamp"),
122 null, null, null, null, ColumnAlignment.Left, null),
123 new Column("Requests", await Language.GetStringAsync(typeof(XmppStatistics), 16, "#Stanzas"),
124 null, null, null, null, ColumnAlignment.Left, null),
125 new Column("Rx", await Language.GetStringAsync(typeof(XmppStatistics), 13, "#Rx"),
126 null, null, null, null, ColumnAlignment.Left, null),
127 new Column("Tx", await Language.GetStringAsync(typeof(XmppStatistics), 14, "#Tx"),
128 null, null, null, null, ColumnAlignment.Left, null)
129 });
130
131 foreach (XmppStatistic Rec in Records)
132 {
133 await Query.NewRecords("Statistics", new Record(new object[]
134 {
135 Rec.Start,
136 Rec.Timestamp,
137 Rec.NrStanzas,
138 Rec.NrBytesRx,
139 Rec.NrBytesTx
140 }));
141 }
142
143 await Query.TableDone("Statistics");
144 await Query.EndSection();
145 }
146 catch (Exception ex)
147 {
148 await Query.LogMessage(ex);
149 }
150 finally
151 {
152 await Query.Done();
153 }
154 }
155
156 }
157}
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 greater or equal to a given value.
This filter selects objects that have a named field lesser or equal to a given value.
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
Task< string > GetConfirmationStringAsync(Language Language)
Gets a confirmation string, if any, of the command. If no confirmation is necessary,...
Task< bool > CanExecuteAsync(RequestOrigin Caller)
If the command can be executed by the caller.
ICommand Copy()
Creates a copy of the command object.
Task< string > GetSuccessStringAsync(Language Language)
Gets a success string, if any, of the command. If no specific success string is available,...
Task< string > GetNameAsync(Language Language)
Gets the name of data source.
Task< string > GetFailureStringAsync(Language Language)
Gets a failure string, if any, of the command. If no specific failure string is available,...
Task StartQueryExecutionAsync(Query Query, Language Language)
Starts the execution of a query.
Service Module hosting the XMPP broker and its components.
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:318
Task EndSection()
Ends a section. Each call to BeginSection(string) must be followed by a call to EndSection().
Definition: Query.cs:495
Task Start()
Starts query execution.
Definition: Query.cs:204
Task NewRecords(string TableId, params Record[] Records)
Reports a new set of records in a table.
Definition: Query.cs:291
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:474
Task NewTable(string TableId, string TableName, params Column[] Columns)
Defines a new table in the query output.
Definition: Query.cs:263
Task LogMessage(Exception Exception)
Logs an Exception as a query message.
Definition: Query.cs:372
async Task Done()
Query execution completed.
Definition: Query.cs:232
Task SetStatus(string Status)
Sets the current status of the query execution.
Definition: Query.cs:448
Task SetTitle(string Title)
Sets the title of the report.
Definition: Query.cs:421
Defines a record in a table.
Definition: Record.cs:9
Tokens available in request.
Definition: RequestOrigin.cs:9
string From
Address of caller.
Interface for commands.
Definition: ICommand.cs:32
ColumnAlignment
Column alignment.
Definition: Column.cs:9
CommandType
Command type.
Definition: ICommand.cs:11