Skip to content

Commit 7c9be18

Browse files
committed
Added GetUploadQueueStats.
1 parent e8de1f2 commit 7c9be18

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

PowerSync/PowerSync.Common/Client/PowerSyncDatabase.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,30 @@ await Database.WriteTransaction(async tx =>
420420
Closed = true;
421421
}
422422

423+
private record UploadQueueStatsResult(int size, int count);
424+
/// <summary>
425+
/// Get upload queue size estimate and count.
426+
/// </summary>
427+
public async Task<UploadQueueStats> GetUploadQueueStats(bool includeSize = false)
428+
{
429+
if (includeSize)
430+
{
431+
var result = await Database.Get<UploadQueueStatsResult>(
432+
$"SELECT SUM(cast(data as blob) + 20) as size, count(*) as count FROM {PSInternalTable.CRUD}"
433+
);
434+
435+
return new UploadQueueStats(result.count, result.size);
436+
}
437+
else
438+
{
439+
var result = await Database.Get<UploadQueueStatsResult>(
440+
$"SELECT count(*) as count FROM {PSInternalTable.CRUD}"
441+
);
442+
return new UploadQueueStats(result.count);
443+
}
444+
}
445+
446+
423447
/// <summary>
424448
/// Get a batch of crud data to upload.
425449
/// <para />

Tests/PowerSync/PowerSync.Common.Tests/Client/PowerSyncDatabaseTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ namespace PowerSync.Common.Tests.Client;
66

77
using PowerSync.Common.Client;
88

9+
/// <summary>
10+
/// dotnet test -v n --framework net8.0 --filter "PowerSyncDatabaseTests"
11+
/// </summary>
912
public class PowerSyncDatabaseTests : IAsyncLifetime
1013
{
1114
private PowerSyncDatabase db = default!;
@@ -454,4 +457,18 @@ await db.WriteTransaction(async tx =>
454457
var afterTx = await db.GetAll<object>("SELECT * FROM assets");
455458
Assert.Equal(2, afterTx.Length);
456459
}
460+
461+
[Fact(Timeout = 5000)]
462+
public async Task GetUploadQueueStatsTest()
463+
{
464+
for (var i = 0; i < 100; i++)
465+
{
466+
await db.Execute("INSERT INTO assets (id) VALUES(?)", ["0" + i + "-writelock"]);
467+
}
468+
469+
var stats = await db.GetUploadQueueStats(true);
470+
Assert.Equal(100, stats.Count);
471+
Assert.NotNull(stats.Size);
472+
}
473+
457474
}

0 commit comments

Comments
 (0)