Skip to content

Commit a2dae0a

Browse files
committed
Capturing sync progress received by core extension.
1 parent f7d6db0 commit a2dae0a

File tree

3 files changed

+36
-48
lines changed

3 files changed

+36
-48
lines changed

PowerSync/PowerSync.Common/Client/Sync/Stream/StreamingSyncImplementation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,7 @@ async Task HandleInstruction(Instruction instruction)
469469
DataFlow = new SyncDataFlowStatus
470470
{
471471
Downloading = info.Downloading != null,
472-
// TODO CL
473-
// DownloadProgress = info.Downloading?.Buckets
472+
DownloadProgress = info.Downloading?.Buckets
474473
}
475474
},
476475
new UpdateSyncStatusOptions
@@ -645,6 +644,7 @@ protected void UpdateSyncStatus(SyncStatusOptions options, UpdateSyncStatusOptio
645644
UploadError = updateOptions?.ClearUploadError == true
646645
? null
647646
: options.DataFlow?.UploadError ?? SyncStatus.DataFlowStatus.UploadError,
647+
DownloadProgress = options.DataFlow?.DownloadProgress ?? SyncStatus.DataFlowStatus.DownloadProgress,
648648
},
649649
PriorityStatusEntries = options.PriorityStatusEntries ?? SyncStatus.PriorityStatusEntries
650650
});

PowerSync/PowerSync.Common/DB/Crud/SyncProgress.cs

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using PowerSync.Common.Client.Sync.Stream;
2+
13
namespace PowerSync.Common.DB.Crud;
24

35
/// <summary>
@@ -18,25 +20,24 @@ namespace PowerSync.Common.DB.Crud;
1820
public class SyncProgress : ProgressWithOperations
1921
{
2022
public static readonly int FULL_SYNC_PRIORITY = 2147483647;
21-
22-
private InternalProgressInformation internalProgress;
23-
24-
public SyncProgress(InternalProgressInformation progress)
23+
protected Dictionary<string, BucketProgress> InternalProgress { get; }
24+
25+
public SyncProgress(Dictionary<string, BucketProgress> progress)
2526
{
26-
this.internalProgress = progress;
27+
this.InternalProgress = progress;
2728
var untilCompletion = UntilPriority(FULL_SYNC_PRIORITY);
2829

2930
TotalOperations = untilCompletion.TotalOperations;
3031
DownloadedOperations = untilCompletion.DownloadedOperations;
3132
DownloadedFraction = untilCompletion.DownloadedFraction;
3233
}
3334

34-
private ProgressWithOperations UntilPriority(int priority)
35+
public ProgressWithOperations UntilPriority(int priority)
3536
{
3637
var total = 0;
3738
var downloaded = 0;
3839

39-
foreach (var progress in internalProgress.Buckets.Values)
40+
foreach (var progress in InternalProgress.Values)
4041
{
4142
// Include higher-priority buckets, which are represented by lower numbers.
4243
if (progress.Priority <= priority)
@@ -55,43 +56,6 @@ private ProgressWithOperations UntilPriority(int priority)
5556
}
5657
}
5758

58-
/// <summary>
59-
/// Represents progress information for sync operations.
60-
/// </summary>
61-
public class InternalProgressInformation
62-
{
63-
/// <summary>
64-
/// Dictionary mapping bucket names to their progress information.
65-
/// </summary>
66-
public Dictionary<string, BucketProgressInfo> Buckets { get; set; } = new();
67-
}
68-
69-
/// <summary>
70-
/// Represents progress information for a single bucket.
71-
/// </summary>
72-
public class BucketProgressInfo
73-
{
74-
/// <summary>
75-
/// Priority of the associated buckets
76-
/// </summary>
77-
public int Priority { get; set; }
78-
79-
/// <summary>
80-
/// Total ops at last completed sync, or 0
81-
/// </summary>
82-
public int AtLast { get; set; }
83-
84-
/// <summary>
85-
/// Total ops since the last completed sync
86-
/// </summary>
87-
public int SinceLast { get; set; }
88-
89-
/// <summary>
90-
/// Total opcount for next checkpoint as indicated by service
91-
/// </summary>
92-
public int TargetCount { get; set; }
93-
}
94-
9559
/// <summary>
9660
/// Information about a progressing download made by the PowerSync SDK.
9761
///

PowerSync/PowerSync.Common/DB/Crud/SyncStatus.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace PowerSync.Common.DB.Crud;
22

3+
using PowerSync.Common.Client.Sync.Stream;
34
using Newtonsoft.Json;
45

56
public class SyncDataFlowStatus
@@ -21,20 +22,28 @@ public class SyncDataFlowStatus
2122
/// </summary>
2223
[JsonProperty("uploadError")]
2324
public Exception? UploadError { get; set; } = null;
25+
26+
27+
/// <summary>
28+
/// Internal information about how far we are downloading operations in buckets.
29+
/// </summary>
30+
public Dictionary<string, BucketProgress>? DownloadProgress { get; set; } = null;
2431
}
2532

2633
public class SyncPriorityStatus
2734
{
2835
[JsonProperty("uploading")] public int Priority { get; set; }
29-
36+
3037
[JsonProperty("lastSyncedAt")] public DateTime? LastSyncedAt { get; set; }
3138

3239
[JsonProperty("hasSynced")] public bool? HasSynced { get; set; }
3340
}
3441

3542
public class SyncStatusOptions
3643
{
37-
public SyncStatusOptions() {}
44+
public SyncStatusOptions()
45+
{
46+
}
3847

3948
public SyncStatusOptions(SyncStatusOptions options)
4049
{
@@ -93,6 +102,21 @@ public class SyncStatus(SyncStatusOptions options)
93102
.OrderBy(entry => entry.Priority)
94103
.ToArray();
95104

105+
/// <summary>
106+
/// A realtime progress report on how many operations have been downloaded and
107+
/// how many are necessary in total to complete the next sync iteration.
108+
/// </summary>
109+
public SyncProgress? DownloadProgress()
110+
{
111+
var internalProgress = Options.DataFlow?.DownloadProgress;
112+
if (internalProgress == null)
113+
{
114+
return null;
115+
}
116+
117+
return new SyncProgress(internalProgress);
118+
}
119+
96120
/// <summary>
97121
/// Reports the sync status (a pair of HasSynced and LastSyncedAt fields)
98122
/// for a specific bucket priority level.

0 commit comments

Comments
 (0)