-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuCleanupHistory.pas
More file actions
503 lines (436 loc) · 14.2 KB
/
uCleanupHistory.pas
File metadata and controls
503 lines (436 loc) · 14.2 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
unit uCleanupHistory;
{
清理历史记录管理模块
功能:
- 保存清理操作历史到JSON文件
- 加载和查询历史记录
- 生成清理报告
作者: AI助手
版本: 1.0
日期: 2025-12-01
}
interface
uses
System.SysUtils, System.Classes, System.IOUtils, System.DateUtils,
System.JSON, System.Generics.Collections, System.Math, System.StrUtils;
type
// 清理类型枚举
TCleanupType = (
ctRecycleBin, // 回收站
ctTempFiles, // 临时文件
ctBrowserCache, // 浏览器缓存
ctWindowsUpdate, // Windows更新缓存
ctSystemLogs, // 系统日志
ctPrefetch, // 预取文件
ctSmartCleanup // 智能清理(综合)
);
// 单条清理历史记录
TCleanupHistoryEntry = record
ID: string; // 唯一标识(GUID)
Timestamp: TDateTime; // 清理时间
CleanupType: TCleanupType; // 清理类型
TypeName: string; // 类型名称(中文)
FilesDeleted: Integer; // 删除文件数
SpaceFreed: Int64; // 释放空间(字节)
Success: Boolean; // 是否成功
ErrorMessage: string; // 错误信息
Duration: Integer; // 耗时(毫秒)
Details: TArray<string>; // 详细信息
function ToJSON: TJSONObject;
procedure FromJSON(AJson: TJSONObject);
function GetSpaceFreedStr: string;
function GetDurationStr: string;
end;
// 清理历史管理器
TCleanupHistoryManager = class
private
FHistoryFile: string;
FHistory: TList<TCleanupHistoryEntry>;
FMaxEntries: Integer;
procedure LoadFromFile;
procedure SaveToFile;
function GetHistoryFilePath: string;
public
constructor Create;
destructor Destroy; override;
// 添加历史记录
procedure AddEntry(const AEntry: TCleanupHistoryEntry); overload;
procedure AddEntry(AType: TCleanupType; AFilesDeleted: Integer;
ASpaceFreed: Int64; ASuccess: Boolean; const AErrorMessage: string = '';
ADuration: Integer = 0; const ADetails: TArray<string> = nil); overload;
// 查询历史记录
function GetAllEntries: TArray<TCleanupHistoryEntry>;
function GetEntriesByType(AType: TCleanupType): TArray<TCleanupHistoryEntry>;
function GetEntriesByDateRange(AStartDate, AEndDate: TDateTime): TArray<TCleanupHistoryEntry>;
function GetRecentEntries(ACount: Integer = 20): TArray<TCleanupHistoryEntry>;
// 统计信息
function GetTotalSpaceFreed: Int64;
function GetTotalFilesDeleted: Integer;
function GetEntryCount: Integer;
// 清理操作
procedure ClearHistory;
procedure PruneOldEntries(AKeepDays: Integer = 30);
// 导出报告
function ExportToJSON(const AFileName: string): Boolean;
function ExportToText(const AFileName: string): Boolean;
function GenerateReportText: string;
// 属性
property MaxEntries: Integer read FMaxEntries write FMaxEntries;
property HistoryFile: string read FHistoryFile;
end;
// 全局函数 - 获取清理类型名称
function CleanupTypeToString(AType: TCleanupType): string;
function StringToCleanupType(const AStr: string): TCleanupType;
// 全局单例
function CleanupHistory: TCleanupHistoryManager;
implementation
var
GCleanupHistory: TCleanupHistoryManager = nil;
function CleanupHistory: TCleanupHistoryManager;
begin
if not Assigned(GCleanupHistory) then
GCleanupHistory := TCleanupHistoryManager.Create;
Result := GCleanupHistory;
end;
function CleanupTypeToString(AType: TCleanupType): string;
begin
case AType of
ctRecycleBin: Result := '回收站清理';
ctTempFiles: Result := '临时文件清理';
ctBrowserCache: Result := '浏览器缓存清理';
ctWindowsUpdate: Result := 'Windows更新缓存清理';
ctSystemLogs: Result := '系统日志清理';
ctPrefetch: Result := '预取文件清理';
ctSmartCleanup: Result := '智能清理';
else
Result := '未知类型';
end;
end;
function StringToCleanupType(const AStr: string): TCleanupType;
begin
if AStr = '回收站清理' then Result := ctRecycleBin
else if AStr = '临时文件清理' then Result := ctTempFiles
else if AStr = '浏览器缓存清理' then Result := ctBrowserCache
else if AStr = 'Windows更新缓存清理' then Result := ctWindowsUpdate
else if AStr = '系统日志清理' then Result := ctSystemLogs
else if AStr = '预取文件清理' then Result := ctPrefetch
else if AStr = '智能清理' then Result := ctSmartCleanup
else Result := ctTempFiles; // 默认
end;
{ TCleanupHistoryEntry }
function TCleanupHistoryEntry.ToJSON: TJSONObject;
var
DetailsArr: TJSONArray;
I: Integer;
begin
Result := TJSONObject.Create;
Result.AddPair('id', ID);
Result.AddPair('timestamp', DateToISO8601(Timestamp));
Result.AddPair('cleanupType', Integer(CleanupType));
Result.AddPair('typeName', TypeName);
Result.AddPair('filesDeleted', TJSONNumber.Create(FilesDeleted));
Result.AddPair('spaceFreed', TJSONNumber.Create(SpaceFreed));
Result.AddPair('success', TJSONBool.Create(Success));
Result.AddPair('errorMessage', ErrorMessage);
Result.AddPair('duration', TJSONNumber.Create(Duration));
DetailsArr := TJSONArray.Create;
for I := 0 to High(Details) do
DetailsArr.Add(Details[I]);
Result.AddPair('details', DetailsArr);
end;
procedure TCleanupHistoryEntry.FromJSON(AJson: TJSONObject);
var
DetailsArr: TJSONArray;
I: Integer;
begin
ID := AJson.GetValue<string>('id', '');
Timestamp := ISO8601ToDate(AJson.GetValue<string>('timestamp', ''));
CleanupType := TCleanupType(AJson.GetValue<Integer>('cleanupType', 0));
TypeName := AJson.GetValue<string>('typeName', '');
FilesDeleted := AJson.GetValue<Integer>('filesDeleted', 0);
SpaceFreed := AJson.GetValue<Int64>('spaceFreed', 0);
Success := AJson.GetValue<Boolean>('success', False);
ErrorMessage := AJson.GetValue<string>('errorMessage', '');
Duration := AJson.GetValue<Integer>('duration', 0);
SetLength(Details, 0);
if AJson.TryGetValue<TJSONArray>('details', DetailsArr) then
begin
SetLength(Details, DetailsArr.Count);
for I := 0 to DetailsArr.Count - 1 do
Details[I] := DetailsArr.Items[I].Value;
end;
end;
function TCleanupHistoryEntry.GetSpaceFreedStr: string;
begin
if SpaceFreed < 1024 then
Result := Format('%d B', [SpaceFreed])
else if SpaceFreed < 1024 * 1024 then
Result := Format('%.2f KB', [SpaceFreed / 1024])
else if SpaceFreed < 1024 * 1024 * 1024 then
Result := Format('%.2f MB', [SpaceFreed / (1024 * 1024)])
else
Result := Format('%.2f GB', [SpaceFreed / (1024 * 1024 * 1024)]);
end;
function TCleanupHistoryEntry.GetDurationStr: string;
begin
if Duration < 1000 then
Result := Format('%d 毫秒', [Duration])
else if Duration < 60000 then
Result := Format('%.1f 秒', [Duration / 1000])
else
Result := Format('%.1f 分钟', [Duration / 60000]);
end;
{ TCleanupHistoryManager }
constructor TCleanupHistoryManager.Create;
begin
inherited Create;
FHistory := TList<TCleanupHistoryEntry>.Create;
FMaxEntries := 500; // 最多保存500条记录
FHistoryFile := GetHistoryFilePath;
LoadFromFile;
end;
destructor TCleanupHistoryManager.Destroy;
begin
SaveToFile;
FHistory.Free;
inherited Destroy;
end;
function TCleanupHistoryManager.GetHistoryFilePath: string;
begin
Result := TPath.Combine(ExtractFilePath(ParamStr(0)), 'cleanup_history.json');
end;
procedure TCleanupHistoryManager.LoadFromFile;
var
JsonStr: string;
JsonArr: TJSONArray;
JsonObj: TJSONObject;
I: Integer;
Entry: TCleanupHistoryEntry;
begin
FHistory.Clear;
if not TFile.Exists(FHistoryFile) then
Exit;
try
JsonStr := TFile.ReadAllText(FHistoryFile, TEncoding.UTF8);
JsonArr := TJSONObject.ParseJSONValue(JsonStr) as TJSONArray;
if Assigned(JsonArr) then
begin
try
for I := 0 to JsonArr.Count - 1 do
begin
JsonObj := JsonArr.Items[I] as TJSONObject;
Entry.FromJSON(JsonObj);
FHistory.Add(Entry);
end;
finally
JsonArr.Free;
end;
end;
except
// 忽略加载错误,使用空历史
end;
end;
procedure TCleanupHistoryManager.SaveToFile;
var
JsonArr: TJSONArray;
I: Integer;
begin
JsonArr := TJSONArray.Create;
try
for I := 0 to FHistory.Count - 1 do
JsonArr.AddElement(FHistory[I].ToJSON);
TFile.WriteAllText(FHistoryFile, JsonArr.Format(2), TEncoding.UTF8);
finally
JsonArr.Free;
end;
end;
procedure TCleanupHistoryManager.AddEntry(const AEntry: TCleanupHistoryEntry);
var
Entry: TCleanupHistoryEntry;
begin
Entry := AEntry;
if Entry.ID = '' then
Entry.ID := TGUID.NewGuid.ToString;
if Entry.TypeName = '' then
Entry.TypeName := CleanupTypeToString(Entry.CleanupType);
FHistory.Insert(0, Entry); // 最新的在前面
// 限制历史记录数量
while FHistory.Count > FMaxEntries do
FHistory.Delete(FHistory.Count - 1);
SaveToFile;
end;
procedure TCleanupHistoryManager.AddEntry(AType: TCleanupType; AFilesDeleted: Integer;
ASpaceFreed: Int64; ASuccess: Boolean; const AErrorMessage: string;
ADuration: Integer; const ADetails: TArray<string>);
var
Entry: TCleanupHistoryEntry;
begin
Entry.ID := TGUID.NewGuid.ToString;
Entry.Timestamp := Now;
Entry.CleanupType := AType;
Entry.TypeName := CleanupTypeToString(AType);
Entry.FilesDeleted := AFilesDeleted;
Entry.SpaceFreed := ASpaceFreed;
Entry.Success := ASuccess;
Entry.ErrorMessage := AErrorMessage;
Entry.Duration := ADuration;
Entry.Details := ADetails;
AddEntry(Entry);
end;
function TCleanupHistoryManager.GetAllEntries: TArray<TCleanupHistoryEntry>;
begin
Result := FHistory.ToArray;
end;
function TCleanupHistoryManager.GetEntriesByType(AType: TCleanupType): TArray<TCleanupHistoryEntry>;
var
ResultList: TList<TCleanupHistoryEntry>;
Entry: TCleanupHistoryEntry;
begin
ResultList := TList<TCleanupHistoryEntry>.Create;
try
for Entry in FHistory do
begin
if Entry.CleanupType = AType then
ResultList.Add(Entry);
end;
Result := ResultList.ToArray;
finally
ResultList.Free;
end;
end;
function TCleanupHistoryManager.GetEntriesByDateRange(AStartDate, AEndDate: TDateTime): TArray<TCleanupHistoryEntry>;
var
ResultList: TList<TCleanupHistoryEntry>;
Entry: TCleanupHistoryEntry;
begin
ResultList := TList<TCleanupHistoryEntry>.Create;
try
for Entry in FHistory do
begin
if (Entry.Timestamp >= AStartDate) and (Entry.Timestamp <= AEndDate) then
ResultList.Add(Entry);
end;
Result := ResultList.ToArray;
finally
ResultList.Free;
end;
end;
function TCleanupHistoryManager.GetRecentEntries(ACount: Integer): TArray<TCleanupHistoryEntry>;
var
I, Count: Integer;
begin
Count := Min(ACount, FHistory.Count);
SetLength(Result, Count);
for I := 0 to Count - 1 do
Result[I] := FHistory[I];
end;
function TCleanupHistoryManager.GetTotalSpaceFreed: Int64;
var
Entry: TCleanupHistoryEntry;
begin
Result := 0;
for Entry in FHistory do
Result := Result + Entry.SpaceFreed;
end;
function TCleanupHistoryManager.GetTotalFilesDeleted: Integer;
var
Entry: TCleanupHistoryEntry;
begin
Result := 0;
for Entry in FHistory do
Result := Result + Entry.FilesDeleted;
end;
function TCleanupHistoryManager.GetEntryCount: Integer;
begin
Result := FHistory.Count;
end;
procedure TCleanupHistoryManager.ClearHistory;
begin
FHistory.Clear;
SaveToFile;
end;
procedure TCleanupHistoryManager.PruneOldEntries(AKeepDays: Integer);
var
CutoffDate: TDateTime;
I: Integer;
begin
CutoffDate := Now - AKeepDays;
for I := FHistory.Count - 1 downto 0 do
begin
if FHistory[I].Timestamp < CutoffDate then
FHistory.Delete(I);
end;
SaveToFile;
end;
function TCleanupHistoryManager.ExportToJSON(const AFileName: string): Boolean;
var
JsonArr: TJSONArray;
I: Integer;
begin
Result := False;
JsonArr := TJSONArray.Create;
try
for I := 0 to FHistory.Count - 1 do
JsonArr.AddElement(FHistory[I].ToJSON);
TFile.WriteAllText(AFileName, JsonArr.Format(2), TEncoding.UTF8);
Result := True;
finally
JsonArr.Free;
end;
end;
function TCleanupHistoryManager.ExportToText(const AFileName: string): Boolean;
begin
Result := False;
try
TFile.WriteAllText(AFileName, GenerateReportText, TEncoding.UTF8);
Result := True;
except
end;
end;
function TCleanupHistoryManager.GenerateReportText: string;
var
SB: TStringBuilder;
Entry: TCleanupHistoryEntry;
I: Integer;
begin
SB := TStringBuilder.Create;
try
SB.AppendLine('========================================');
SB.AppendLine(' MoveC 清理历史报告');
SB.AppendLine('========================================');
SB.AppendLine('');
SB.AppendLine(Format('生成时间: %s', [FormatDateTime('yyyy-mm-dd hh:nn:ss', Now)]));
SB.AppendLine(Format('总记录数: %d', [FHistory.Count]));
SB.AppendLine(Format('累计释放空间: %.2f MB', [GetTotalSpaceFreed / (1024 * 1024)]));
SB.AppendLine(Format('累计删除文件: %d 个', [GetTotalFilesDeleted]));
SB.AppendLine('');
SB.AppendLine('----------------------------------------');
SB.AppendLine('详细记录:');
SB.AppendLine('----------------------------------------');
for I := 0 to FHistory.Count - 1 do
begin
Entry := FHistory[I];
SB.AppendLine('');
SB.AppendLine(Format('[%d] %s', [I + 1, FormatDateTime('yyyy-mm-dd hh:nn:ss', Entry.Timestamp)]));
SB.AppendLine(Format(' 类型: %s', [Entry.TypeName]));
SB.AppendLine(Format(' 状态: %s', [IfThen(Entry.Success, '成功', '失败')]));
SB.AppendLine(Format(' 删除文件: %d 个', [Entry.FilesDeleted]));
SB.AppendLine(Format(' 释放空间: %s', [Entry.GetSpaceFreedStr]));
SB.AppendLine(Format(' 耗时: %s', [Entry.GetDurationStr]));
if Entry.ErrorMessage <> '' then
SB.AppendLine(Format(' 错误: %s', [Entry.ErrorMessage]));
end;
SB.AppendLine('');
SB.AppendLine('========================================');
SB.AppendLine(' 报告结束');
SB.AppendLine('========================================');
Result := SB.ToString;
finally
SB.Free;
end;
end;
initialization
finalization
if Assigned(GCleanupHistory) then
FreeAndNil(GCleanupHistory);
end.