Skip to content

Commit 756967f

Browse files
authored
Merge pull request #30 from powersync-ja/chore/upload-queue-stats
(chore): Added GetUploadQueueStats.
2 parents e8de1f2 + c2817cd commit 756967f

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

PowerSync/PowerSync.Common/Client/PowerSyncDatabase.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,33 @@ 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+
return await ReadTransaction(async tx =>
430+
{
431+
if (includeSize)
432+
{
433+
var result = await tx.Get<UploadQueueStatsResult>(
434+
$"SELECT SUM(cast(data as blob) + 20) as size, count(*) as count FROM {PSInternalTable.CRUD}"
435+
);
436+
437+
return new UploadQueueStats(result.count, result.size);
438+
}
439+
else
440+
{
441+
var result = await tx.Get<UploadQueueStatsResult>(
442+
$"SELECT count(*) as count FROM {PSInternalTable.CRUD}"
443+
);
444+
return new UploadQueueStats(result.count);
445+
}
446+
});
447+
}
448+
449+
423450
/// <summary>
424451
/// Get a batch of crud data to upload.
425452
/// <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)