-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathForms.Main.pas
More file actions
310 lines (249 loc) · 7.04 KB
/
Forms.Main.pas
File metadata and controls
310 lines (249 loc) · 7.04 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
unit Forms.Main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, JSON.Readers, JSON.Utils, System.TypInfo, System.JSON.Types,
System.JSON.Builders, System.Generics.Collections, System.Rtti, System.Generics.Defaults, System.Diagnostics;
type
TTGMessage = class
public
id: Integer;
&type: string;
from: string;
from_id: string;
end;
TUsersDict = TDictionary<Int64, string>;
TMessagesStat = TDictionary<Int64, Integer>;
TUserStat = class
private
FName: string;
FCount: Integer;
public
constructor Create(const AName: string; ACount: Integer);
property Name: string read FName;
property Count: Integer read FCount;
end;
TFormMain = class(TForm)
btnOpenFile: TButton;
Memo1: TMemo;
btnCount: TButton;
OpenDialog1: TOpenDialog;
procedure btnOpenFileClick(Sender: TObject);
procedure btnCountClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
procedure ReadAllMessages(const AIter: TJSONIterator);
public
{ Public declarations }
end;
var
FormMain: TFormMain;
sr1: TStreamReader;
jr1: TJsonTextReader;
ji1: TJSONIterator;
g_names: TUsersDict;
g_messages: TMessagesStat;
/// <summary>Format number using user's regional settings.</summary>
function FormatNumber(ANumber: Integer; TrimDecimal: Boolean = True): string; overload;
/// <summary>Format number using user's regional settings.</summary>
function FormatNumber(const ANumberStr: string): string; overload;
implementation
{$R *.dfm}
function FormatNumber(ANumber: Integer; TrimDecimal: Boolean = True): string;
var
decimalPos: Integer;
begin
Result := FormatNumber(IntToStr(ANumber));
if TrimDecimal then
begin
decimalPos := Result.LastIndexOf(FormatSettings.DecimalSeparator);
if decimalPos > -1 then
Result := Result.Substring(0, decimalPos);
end;
end;
function FormatNumber(const ANumberStr: string): string;
var
sLen: Integer;
begin
sLen := GetNumberFormatEx(PChar(LOCALE_NAME_USER_DEFAULT), 0, PChar(ANumberStr), nil, nil, 0);
SetLength(Result, Pred(sLen));
if sLen > 1 then
GetNumberFormatEx(PChar(LOCALE_NAME_USER_DEFAULT), 0, PChar(ANumberStr), nil, PChar(Result), sLen);
end;
function ParseTGMessage(AIter: TJSONIterator): TTGMessage;
var
m: TTGMessage;
begin
m := TTGMessage.Create;
while AIter.Next() do
begin
if ji1.Key = 'id' then
m.id := ji1.AsInteger;
if ji1.Key = 'type' then
m.&type := ji1.AsString;
// 'from' is null for deleted accounts
if ji1.Key = 'from' then
begin
if ji1.AsValue.IsEmpty then
m.from := '(Deleted Account)'
else
m.from := ji1.AsString;
end;
if ji1.Key = 'from_id' then
m.from_id := ji1.AsString;
end;
Result := m;
end;
procedure CountTGUser(const m: TTGMessage; var AMessages: TMessagesStat; var ANames: TUsersDict);
function getUserId(const m: TTGMessage): Int64;
begin
Result := -1;
if Length(m.from_id) > 4 then
begin
if m.from_id.StartsWith('user') then
Result := StrToInt64(m.from_id.Substring(4))
else
if m.from_id.StartsWith('channel') then
Exit
else
raise EArgumentOutOfRangeException.Create('Can''t parse user id "' + m.from_id + '".');
end
else
raise EArgumentOutOfRangeException.Create('Incorrect length of id "' + m.from_id + '".');
end;
begin
if m.&type = 'message' then
begin
var id := getUserId(m);
if id = -1 then
Exit;
if AMessages.ContainsKey(id) then
AMessages[id] := AMessages[id] + 1
else
begin
AMessages.Add(id, 1);
if not ANames.ContainsKey(id) then
ANames.Add(id, m.from);
end;
end;
end;
procedure TFormMain.btnOpenFileClick(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
sr1.Free;
jr1.Free;
ji1.Free;
g_names.Free;
g_messages.Free;
sr1 := TStreamReader.Create(OpenDialog1.FileName);
jr1 := TJsonTextReader.Create(sr1);
ji1 := TJSONIterator.Create(jr1);
g_names := TUsersDict.Create;
g_messages := TMessagesStat.Create;
end;
end;
procedure TFormMain.btnCountClick(Sender: TObject);
const
NameColWidth = 40;
CountColWidth = 10;
var
strFormat: string;
sw: TStopwatch;
log: TStrings;
list: TObjectList<TUserStat>;
begin
log := Memo1.Lines;
strFormat := '%-' + NameColWidth.ToString + 's%' + CountColWidth.ToString + 's';
ji1.Rewind;
g_messages.Clear;
g_names.Clear;
if ji1.Next('name') then
begin
log.Add(string('-').PadLeft(NameColWidth + CountColWidth, '-'));
log.Add('Group name: ' + ji1.AsString);
end
else
Exit;
if sr1.BaseStream is TFileStream then
log.Add('File: ' + TFileStream(sr1.BaseStream).FileName);
log.Add('Size: ' + FormatNumber(sr1.BaseStream.Size div 1024) + ' KB');
// read messages in json array
sw := TStopwatch.StartNew;
if ji1.Next('messages') then
if ji1.&Type = TJsonToken.StartArray then
ReadAllMessages(ji1);
sw.Stop;
// combine user name and messages count, sort descending by messages count
list := TObjectList<TUserStat>.Create(True);
for var m in g_messages do
list.Add(TUserStat.Create(g_names[m.Key], g_messages[m.Key]));
list.Sort(TComparer<TUserStat>.Construct(
function(const Left, Right: TUserStat): Integer
begin
Result := Right.Count - Left.Count;
end));
// print total count
var total: Integer := 0;
for var m in list do
Inc(total, m.Count);
log.BeginUpdate;
log.Add('Total messages: ' + FormatNumber(total));
log.Add('Parsing time: ' + FormatNumber(sw.ElapsedMilliseconds) + ' ms');
log.Add('');
// print table header
log.Add(Format(strFormat, ['User', 'Messages']));
log.Add(string('-').PadLeft(NameColWidth + CountColWidth, '-'));
// user name and count (split every 10 records by empty line)
for var i := 0 to Pred(list.Count) do
begin
log.Add(Format(strFormat, [list[i].Name, FormatNumber(list[i].Count)]));
if ((i + 1) mod 10) = 0 then
log.Add('');
end;
list.Free;
log.Add('');
log.Add(string('-').PadLeft(NameColWidth + CountColWidth, '-'));
log.EndUpdate;
end;
procedure TFormMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
sr1.Free;
jr1.Free;
ji1.Free;
g_names.Free;
g_messages.Free;
end;
procedure TFormMain.FormCreate(Sender: TObject);
begin
Memo1.Clear;
end;
procedure TFormMain.ReadAllMessages(const AIter: TJSONIterator);
var
tgMsg: TTGMessage;
begin
// go inside messages array
AIter.Recurse;
// iterate over all messages
while AIter.Next do
begin
if AIter.&Type = TJsonToken.StartObject then
begin
AIter.Recurse;
tgMsg := ParseTGMessage(AIter);
CountTGUser(tgMsg, g_messages, g_names);
tgMsg.Free;
AIter.Return;
end;
end;
AIter.Return;
end;
{ TUserStat }
constructor TUserStat.Create(const AName: string; ACount: Integer);
begin
FName := AName;
FCount := ACount;
end;
end.