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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
// <copyright file="ReportsService.cs" company="JP Dillingham">
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
// │ Copyright (c) JP Dillingham.
// │
// │ This program is free software: you can redistribute it and/or modify
// │ it under the terms of the GNU Affero General Public License as published
// │ by the Free Software Foundation, version 3.
// │
// │ This program is distributed in the hope that it will be useful,
// │ but WITHOUT ANY WARRANTY; without even the implied warranty of
// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// │ GNU Affero General Public License for more details.
// │
// │ You should have received a copy of the GNU Affero General Public License
// │ along with this program. If not, see https://www.gnu.org/licenses/.
// │
// │ This program is distributed with Additional Terms pursuant to Section 7
// │ of the AGPLv3. See the LICENSE file in the root directory of this
// │ project for the complete terms and conditions.
// │
// │ https://slskd.org
// │
// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
// │ SPDX-FileCopyrightText: JP Dillingham
// │ SPDX-License-Identifier: AGPL-3.0-only
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
// </copyright>
namespace slskd.Telemetry;
using System;
using System.Collections.Generic;
using System.Linq;
using Dapper;
using Microsoft.Data.Sqlite;
using Serilog;
using slskd.Transfers;
using Soulseek;
/// <summary>
/// Reports.
/// </summary>
public class ReportsService
{
/// <summary>
/// Initializes a new instance of the <see cref="ReportsService"/> class.
/// </summary>
public ReportsService(ConnectionStringDictionary connectionStringDictionary)
{
ConnectionStrings = connectionStringDictionary;
}
private ConnectionStringDictionary ConnectionStrings { get; }
private ILogger Log { get; } = Serilog.Log.ForContext<ReportsService>();
/// <summary>
/// Returns a summary of all transfer data grouped by direction and final transfer state. Includes only
/// completed transfers.
/// </summary>
/// <param name="start">The optional start time of the summary window.</param>
/// <param name="end">The optional end time of the summary window.</param>
/// <param name="direction">The optional direction (Upload or Download) for the summary.</param>
/// <param name="username">The optional username by which to filter results.</param>
/// <returns>A nested dictionary keyed by direction and state and containing summary information.</returns>
/// <exception cref="ArgumentException">Thrown if end time is not later than start time.</exception>
public Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>> GetTransferSummary(
DateTime? start = null,
DateTime? end = null,
TransferDirection? direction = null,
string username = null)
{
start ??= DateTime.MinValue;
end ??= DateTime.MaxValue;
if (end <= start)
{
throw new ArgumentException("End time must be later than start time");
}
var dict = new Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>()
{
{ TransferDirection.Download, new Dictionary<TransferStates, TransferSummary>() },
{ TransferDirection.Upload, new Dictionary<TransferStates, TransferSummary>() },
};
var sql = @$"
SELECT
Direction,
StateDescription,
SUM(Size) AS TotalBytes,
COUNT(*) AS Count,
COUNT(DISTINCT Username) AS DistinctUsers,
AVG(AverageSpeed) AS AverageSpeed,
AVG(strftime('%s', StartedAt) - strftime('%s', EnqueuedAt)) AS AverageWait,
AVG(strftime('%s', EndedAt) - strftime('%s', StartedAt)) AS AverageDuration
FROM Transfers
WHERE EndedAt IS NOT NULL
AND EndedAt BETWEEN @Start AND @End
AND State IN ({string.Join(", ", TransferStateCategories.Completed.Select(s => (int)s))})
{(direction is not null ? "AND Direction = @Direction" : string.Empty)}
{(username is not null ? "AND Username = @Username" : string.Empty)}
GROUP BY Direction, StateDescription
";
using var connection = new SqliteConnection(ConnectionStrings[Database.Transfers]);
var param = new
{
Direction = direction?.ToString(),
Username = username,
Start = start,
End = end,
};
var results = connection.Query<TransferSummaryRow>(sql, param);
foreach (var result in results)
{
var record = new TransferSummary
{
TotalBytes = result.TotalBytes,
Count = result.Count,
DistinctUsers = result.DistinctUsers,
AverageSpeed = result.AverageSpeed,
AverageWait = result.AverageWait,
AverageDuration = result.AverageDuration,
};
dict[result.Direction].Add(result.StateDescription & ~TransferStates.Completed, record);
}
return dict;
}
/// <summary>
/// Returns a histogram of all transfer data, aggregated into either fixed size time intervals or a fixed
/// number of evenly sized buckets, and grouped by direction and final transfer state. Includes only
/// completed transfers.
/// </summary>
/// <param name="start">The start time of the histogram window.</param>
/// <param name="end">The end time of the histogram window.</param>
/// <param name="interval">
/// The interval for the histogram. Mutually exclusive with <paramref name="buckets"/>; exactly one must
/// be specified.
/// </param>
/// <param name="buckets">
/// The number of evenly sized buckets into which the histogram window should be divided. Mutually
/// exclusive with <paramref name="interval"/>; exactly one must be specified.
/// </param>
/// <param name="direction">The optional direction (Upload or Download) by which to filter results.</param>
/// <param name="username">The optional username by which to filter results.</param>
/// <returns>A nested dictionary keyed by direction and state and containing summary information.</returns>
/// <exception cref="ArgumentException">
/// Thrown if either <paramref name="start"/> or <paramref name="end"/> is default, if both <paramref name="interval"/>
/// and <paramref name="buckets"/> are specified, if neither is specified.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if <paramref name="end"/> is not later than <paramref name="start"/>, or if <paramref name="buckets"/>
/// is outside of the allowed range.
/// </exception>
public Dictionary<DateTime, Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>> GetTransferHistogram(
DateTime start,
DateTime end,
TimeSpan? interval = null,
int? buckets = null,
TransferDirection? direction = null,
string username = null)
{
if (start == default || end == default)
{
throw new ArgumentException("Start and end times are both required");
}
if (end <= start)
{
throw new ArgumentOutOfRangeException(nameof(end), "End time must be later than start time");
}
if (interval.HasValue && buckets.HasValue)
{
throw new ArgumentException("Interval and buckets are mutually exclusive, provide one or the other, not both");
}
if (!interval.HasValue && !buckets.HasValue)
{
throw new ArgumentException("Either interval or buckets must be supplied");
}
if (buckets.HasValue && (buckets.Value > 1000 || buckets.Value < 1))
{
throw new ArgumentOutOfRangeException(nameof(buckets), buckets, "Buckets must be between 1 and 1000");
}
if (interval.HasValue && interval.Value <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(interval), interval, "Interval must be greater than zero");
}
// if interval was provided, just use that. otherwise, compute the interval from the seconds between start and end
// divided by the desired buckets
var effectiveInterval = interval ?? TimeSpan.FromSeconds(Math.Max(1, (long)(end - start).TotalSeconds / buckets!.Value));
var dict = new Dictionary<DateTime, Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>>();
// fill the dictionary with gapless intervals and empty dictionaries
// snap to Unix epoch to match the SQL bucket formula: strftime('%s', ...) / interval * interval, otherwise
// we may end up with an empty/partial first bucket
var intervalSeconds = (long)effectiveInterval.TotalSeconds;
var currentInterval = DateTime.UnixEpoch.AddSeconds((long)(start - DateTime.UnixEpoch).TotalSeconds / intervalSeconds * intervalSeconds);
while (currentInterval < end)
{
dict[currentInterval] = new Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>()
{
{ TransferDirection.Download, new Dictionary<TransferStates, TransferSummary>() },
{ TransferDirection.Upload, new Dictionary<TransferStates, TransferSummary>() },
};
currentInterval = currentInterval.Add(effectiveInterval);
}
var sql = @$"
SELECT
datetime(strftime('%s', EndedAt) / CAST(@Interval AS INTEGER) * CAST(@Interval AS INTEGER), 'unixepoch') AS Interval,
Direction,
StateDescription,
SUM(Size) AS TotalBytes,
COUNT(*) AS Count,
COUNT(DISTINCT Username) AS DistinctUsers,
COALESCE(CAST(SUM(Size) AS REAL) / NULLIF(SUM(strftime('%s', EndedAt) - strftime('%s', StartedAt)), 0), 0.0) AS AverageSpeed,
COALESCE(AVG(strftime('%s', StartedAt) - strftime('%s', EnqueuedAt)), 0.0) AS AverageWait,
COALESCE(AVG(strftime('%s', EndedAt) - strftime('%s', StartedAt)), 0.0) AS AverageDuration
FROM Transfers
WHERE EndedAt IS NOT NULL
AND EndedAt BETWEEN @Start AND @End
AND State IN ({string.Join(", ", TransferStateCategories.Completed.Select(s => (int)s))})
{(direction is not null ? "AND Direction = @Direction" : string.Empty)}
{(username is not null ? "AND Username = @Username" : string.Empty)}
GROUP BY Interval, Direction, StateDescription
ORDER BY Interval, Direction, StateDescription;
";
using var connection = new SqliteConnection(ConnectionStrings[Database.Transfers]);
var param = new
{
Direction = direction?.ToString(),
Username = username,
Start = start,
End = end,
Interval = (int)effectiveInterval.TotalSeconds,
};
var results = connection.Query<TransferSummaryRow>(sql, param);
foreach (var result in results)
{
var record = new TransferSummary
{
TotalBytes = result.TotalBytes,
Count = result.Count,
DistinctUsers = result.DistinctUsers,
AverageSpeed = result.AverageSpeed,
AverageWait = result.AverageWait,
AverageDuration = result.AverageDuration,
};
if (result.Interval.HasValue)
{
var bucketInterval = DateTime.SpecifyKind(result.Interval.Value, DateTimeKind.Utc);
if (dict.TryGetValue(bucketInterval, out var intervalDict))
{
intervalDict[result.Direction][result.StateDescription & ~TransferStates.Completed] = record;
}
}
}
return dict;
}
/// <summary>
/// Returns the top N users summaries by count, total bytes, or average speed. Only successful transfers are included in counts.
/// </summary>
/// <param name="direction">The direction (Upload or Download).</param>
/// <param name="start">The optional start time of the summary window.</param>
/// <param name="end">The optional end time of the summary window.</param>
/// <param name="sortBy">The property by which to sort.</param>
/// <param name="sortOrder">The sort order.</param>
/// <param name="limit">The number of records to return (default: 25).</param>
/// <param name="offset">The record offset (if paginating).</param>
/// <returns>A dictionary keyed by username and containing summary information.</returns>
/// <exception cref="ArgumentException">
/// Thrown if end time is not later than start time, or limit is not greater than zero.
/// </exception>
public List<TransferSummary> GetTransferLeaderboard(
TransferDirection direction,
DateTime? start = null,
DateTime? end = null,
LeaderboardSortBy sortBy = LeaderboardSortBy.Count,
SortOrder sortOrder = SortOrder.DESC,
int limit = 25,
int offset = 0)
{
start ??= DateTime.MinValue;
end ??= DateTime.MaxValue;
if (end <= start)
{
throw new ArgumentException("End time must be later than start time");
}
if (limit <= 0)
{
throw new ArgumentOutOfRangeException("Limit must be greater than zero");
}
var sql = @$"
SELECT
Username,
Direction,
State,
SUM(Size) AS TotalBytes,
COUNT(*) AS Count,
AVG(AverageSpeed) AS AverageSpeed,
AVG(strftime('%s', StartedAt) - strftime('%s', EnqueuedAt)) AS AverageWait,
AVG(strftime('%s', EndedAt) - strftime('%s', StartedAt)) AS AverageDuration
FROM Transfers
WHERE Direction = @Direction
AND State = 48
AND EndedAt IS NOT NULL
AND EndedAt BETWEEN @Start AND @End
GROUP BY Username, Direction, StateDescription
ORDER BY {sortBy} {sortOrder}, Username DESC
LIMIT @Limit
OFFSET @Offset
";
using var connection = new SqliteConnection(ConnectionStrings[Database.Transfers]);
var param = new
{
Direction = direction.ToString(),
Limit = limit,
Offset = offset,
Start = start,
End = end,
};
var results = connection.Query<TransferSummaryRow>(sql, param);
var list = results.Select(row => new TransferSummary
{
Username = row.Username,
TotalBytes = row.TotalBytes,
Count = row.Count,
AverageSpeed = row.AverageSpeed,
AverageWait = row.AverageWait,
AverageDuration = row.AverageDuration,
}).ToList();
return list;
}
/// <summary>
/// Returns a list of transfer exceptions.
/// </summary>
/// <param name="direction">The direction (Upload or Download).</param>
/// <param name="start">The optional start time of the summary window.</param>
/// <param name="end">The optional end time of the summary window.</param>
/// <param name="username">The optional username by which to filter results.</param>
/// <param name="sortOrder">The sort order.</param>
/// <param name="limit">The number of records to return (default: 25).</param>
/// <param name="offset">The record offset (if paginating).</param>
/// <returns>A list of exceptions.</returns>
/// <exception cref="ArgumentException">
/// Thrown if end time is not later than start time, or limit is not greater than zero.
/// </exception>
public List<TransferExceptionDetail> GetTransferExceptions(
TransferDirection direction,
DateTime? start = null,
DateTime? end = null,
string username = null,
SortOrder sortOrder = SortOrder.DESC,
int limit = 25,
int offset = 0)
{
start ??= DateTime.MinValue;
end ??= DateTime.MaxValue;
if (end <= start)
{
throw new ArgumentException("End time must be later than start time");
}
if (limit <= 0)
{
throw new ArgumentOutOfRangeException("Limit must be greater than zero");
}
var sql = @$"
SELECT
Id,
Username,
Direction,
Filename,
Size,
State,
RequestedAt,
EnqueuedAt,
StartedAt,
EndedAt,
BytesTransferred,
AverageSpeed,
CASE
WHEN INSTR(Exception, ':') > 0
THEN SUBSTR(Exception, INSTR(Exception, ':') + 2)
ELSE Exception
END as Exception
FROM transfers
WHERE state & ~48
AND Direction = @Direction
AND EndedAt IS NOT NULL
AND EndedAt BETWEEN @Start AND @End
{(username is not null ? "AND Username = @Username" : string.Empty)}
ORDER BY EndedAt {sortOrder}
LIMIT @Limit
OFFSET @Offset
";
using var connection = new SqliteConnection(ConnectionStrings[Database.Transfers]);
var param = new
{
Direction = direction.ToString(),
Username = username,
Start = start,
End = end,
Limit = limit,
Offset = offset,
};
var results = connection.Query<TransferExceptionDetail>(sql, param);
return results.ToList();
}
/// <summary>
/// Returns a summary of distinct transfer exceptions and the number of times they occurred.
/// </summary>
/// <param name="direction">The direction (Upload or Download) for the summary.</param>
/// <param name="start">The optional start time of the summary window.</param>
/// <param name="end">The optional end time of the summary window.</param>
/// <param name="username">The optional username by which to filter results.</param>
/// <param name="limit">The number of records to return (default: 25).</param>
/// <param name="offset">The record offset (if paginating).</param>
/// <returns>A dictionary keyed by direction and containing a list of exceptions and the number of times they occurred.</returns>
/// <exception cref="ArgumentException">
/// Thrown if end time is not later than start time, or limit is not greater than zero.
/// </exception>
public List<TransferExceptionSummary> GetTransferExceptionsPareto(
TransferDirection direction,
DateTime? start = null,
DateTime? end = null,
string username = null,
int limit = 25,
int offset = 0)
{
start ??= DateTime.MinValue;
end ??= DateTime.MaxValue;
if (end <= start)
{
throw new ArgumentException("End time must be later than start time");
}
if (limit <= 0)
{
throw new ArgumentOutOfRangeException("Limit must be greater than zero");
}
var sql = @$"
SELECT
CASE
WHEN INSTR(Exception, ':') > 0
THEN SUBSTR(Exception, INSTR(Exception, ':') + 2)
ELSE Exception
END as Exception,
COUNT(*) as Count,
COUNT(DISTINCT Username) as DistinctUsers
FROM transfers
WHERE state & ~48
AND Direction = @Direction
AND EndedAt IS NOT NULL
AND EndedAt BETWEEN @Start AND @End
{(username is not null ? "AND Username = @Username" : string.Empty)}
GROUP BY Direction,
CASE
WHEN INSTR(Exception, ':') > 0
THEN SUBSTR(Exception, INSTR(Exception, ':') + 2)
ELSE Exception
END
ORDER BY Count DESC
LIMIT @Limit
OFFSET @Offset
";
using var connection = new SqliteConnection(ConnectionStrings[Database.Transfers]);
var param = new
{
Direction = direction.ToString(),
Username = username,
Start = start,
End = end,
Limit = limit,
Offset = offset,
};
var results = connection.Query<TransferExceptionSummary>(sql, param);
return results.ToList();
}
/// <summary>
/// Returns a list of directories and the frequency at which they have been downloaded.
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="username"></param>
/// <param name="sortOrder"></param>
/// <param name="limit"></param>
/// <param name="offset"></param>
/// <returns></returns>
/// <exception cref="ArgumentException">
/// Thrown if end time is not later than start time, or limit is not greater than zero.
/// </exception>
public List<TransferDirectorySummary> GetTransferDirectoryFrequency(
DateTime? start = null,
DateTime? end = null,
string username = null,
SortOrder sortOrder = SortOrder.DESC,
int limit = 25,
int offset = 0)
{
start ??= DateTime.MinValue;
end ??= DateTime.MaxValue;
if (end <= start)
{
throw new ArgumentException("End time must be later than start time");
}
if (limit <= 0)
{
throw new ArgumentOutOfRangeException("Limit must be greater than zero");
}
var sql = @$"
WITH directories AS (
SELECT
RTRIM(
RTRIM(filename, REPLACE(REPLACE(filename, '/', ''), '\', '')), '/\'
) AS Directory,
Username
FROM transfers
WHERE Direction = 'Upload'
AND State = 48
AND EndedAt IS NOT NULL
AND EndedAt BETWEEN @Start AND @End
{(username is not null ? "AND Username = @Username" : string.Empty)}
)
SELECT Directory, COUNT(*) AS Count, COUNT(DISTINCT Username) AS DistinctUsers
FROM directories
GROUP BY Directory
ORDER BY DistinctUsers DESC, Count DESC
LIMIT @Limit
OFFSET @Offset
";
using var connection = new SqliteConnection(ConnectionStrings[Database.Transfers]);
var param = new
{
Start = start,
End = end,
Limit = limit,
Offset = offset,
Username = username,
};
var results = connection.Query<TransferDirectorySummary>(sql, param);
return results.ToList();
}
private record TransferSummaryRow
{
public DateTime? Interval { get; init; }
public string Username { get; init; }
public TransferDirection Direction { get; init; }
public TransferStates StateDescription { get; init; }
public long TotalBytes { get; init; }
public long Count { get; init; }
public long DistinctUsers { get; init; }
public double AverageSpeed { get; init; }
public double AverageWait { get; init; }
public double AverageDuration { get; init; }
}
}
|