mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-28 17:08:29 +01:00
Report the local sync counters in bug reports
This commit is contained in:
@@ -21,6 +21,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:powersync/powersync.dart' show SyncStatus;
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import 'package:wger/core/error_dialogs.dart' show CopyToClipboardButton;
|
||||
import 'package:wger/core/errors.dart' show buildGithubIssueUrl;
|
||||
import 'package:wger/core/formatting/formatting.dart';
|
||||
import 'package:wger/core/logs.dart';
|
||||
@@ -105,7 +106,23 @@ class SyncStatusDialog extends ConsumerWidget {
|
||||
return ValueListenableBuilder<bool>(
|
||||
valueListenable: ref.watch(syncWatchdogProvider).stalled,
|
||||
builder: (context, stalled, _) => AlertDialog(
|
||||
title: Text(i18n.syncStatusDialogTitle),
|
||||
// The report button below only appears once something is visibly
|
||||
// wrong; copying the snapshot has to work in every state, a sync
|
||||
// that looks idle is exactly the case we need reports for.
|
||||
title: Row(
|
||||
children: [
|
||||
Expanded(child: Text(i18n.syncStatusDialogTitle)),
|
||||
CopyToClipboardButton(
|
||||
text: formatSyncDiagnostics(
|
||||
syncState,
|
||||
pendingUploads: pendingUploads,
|
||||
server: serverCategory(serverUrl),
|
||||
local: ref.watch(localSyncStateProvider).value,
|
||||
),
|
||||
iconOnly: true,
|
||||
),
|
||||
],
|
||||
),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
@@ -230,6 +247,7 @@ class SyncStatusDialog extends ConsumerWidget {
|
||||
if (stalled || syncState.anyError != null)
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
final local = await collectLocalSyncState();
|
||||
final url = buildGithubIssueUrl(
|
||||
issueTitle: 'Sync error',
|
||||
issueErrorMessage:
|
||||
@@ -240,6 +258,7 @@ class SyncStatusDialog extends ConsumerWidget {
|
||||
syncState,
|
||||
pendingUploads: pendingUploads,
|
||||
server: serverCategory(serverUrl),
|
||||
local: local,
|
||||
),
|
||||
);
|
||||
try {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:powersync/powersync.dart'
|
||||
show CredentialsException, PowerSyncProtocolException, SyncResponseException, SyncStatus;
|
||||
@@ -75,11 +76,55 @@ String? serverCategory(String? serverUrl) {
|
||||
};
|
||||
}
|
||||
|
||||
/// Local counters that tell a blocked apply apart from data that never
|
||||
/// arrives. Null fields mean the value is not in the database (yet).
|
||||
class LocalSyncState {
|
||||
const LocalSyncState({
|
||||
this.targetRequestId,
|
||||
this.lastSeenRequestId,
|
||||
this.lastAppliedRequestId,
|
||||
this.maxLastOp,
|
||||
this.opsSinceCheckpoint,
|
||||
this.oplogRows,
|
||||
});
|
||||
|
||||
/// While the target is greater than [lastSeenRequestId], the client waits
|
||||
/// for the service to confirm a local write and applies nothing.
|
||||
final int? targetRequestId;
|
||||
final int? lastSeenRequestId;
|
||||
final int? lastAppliedRequestId;
|
||||
|
||||
/// Highest bucket position stored locally. Advances while data arrives,
|
||||
/// even when no checkpoint can be applied.
|
||||
final int? maxLastOp;
|
||||
|
||||
/// Operations downloaded since the last applied checkpoint.
|
||||
final int? opsSinceCheckpoint;
|
||||
|
||||
final int? oplogRows;
|
||||
}
|
||||
|
||||
/// Reads [LocalSyncState] in a single round trip. The subqueries keep the
|
||||
/// result to one row even when a table is empty.
|
||||
const _localStateQuery = '''
|
||||
SELECT
|
||||
(SELECT CAST(value AS INTEGER) FROM ps_kv WHERE key = 'target_checkpoint_request_id')
|
||||
AS target_request_id,
|
||||
(SELECT CAST(value AS INTEGER) FROM ps_kv WHERE key = 'last_seen_checkpoint_request_id')
|
||||
AS last_seen_request_id,
|
||||
(SELECT CAST(value AS INTEGER) FROM ps_kv WHERE key = 'last_applied_checkpoint_request_id')
|
||||
AS last_applied_request_id,
|
||||
(SELECT CAST(max(last_op) AS INTEGER) FROM ps_buckets) AS max_last_op,
|
||||
(SELECT CAST(sum(count_since_last) AS INTEGER) FROM ps_buckets) AS ops_since_checkpoint,
|
||||
(SELECT count(*) FROM ps_oplog) AS oplog_rows
|
||||
''';
|
||||
|
||||
/// Renders a compact sync-state summary for bug reports.
|
||||
String formatSyncDiagnostics(
|
||||
SyncStatus status, {
|
||||
required int pendingUploads,
|
||||
String? server,
|
||||
LocalSyncState? local,
|
||||
}) {
|
||||
final buffer = StringBuffer();
|
||||
if (server != null) {
|
||||
@@ -92,6 +137,19 @@ String formatSyncDiagnostics(
|
||||
)
|
||||
..writeln('last successful sync: ${status.lastSyncedAt?.toUtc().toIso8601String() ?? 'never'}')
|
||||
..writeln('pending uploads: $pendingUploads');
|
||||
if (local != null) {
|
||||
String value(int? v) => v?.toString() ?? '-';
|
||||
buffer
|
||||
..writeln(
|
||||
'checkpoint request: target ${value(local.targetRequestId)}, '
|
||||
'seen ${value(local.lastSeenRequestId)}, applied ${value(local.lastAppliedRequestId)}',
|
||||
)
|
||||
..writeln(
|
||||
'local buckets: last op ${value(local.maxLastOp)}, '
|
||||
'ops since checkpoint ${value(local.opsSinceCheckpoint)}, '
|
||||
'oplog rows ${value(local.oplogRows)}',
|
||||
);
|
||||
}
|
||||
if (status.downloadProgress case final progress?) {
|
||||
buffer.writeln(
|
||||
'download progress: ${progress.downloadedOperations} / ${progress.totalOperations}',
|
||||
@@ -107,6 +165,38 @@ String formatSyncDiagnostics(
|
||||
return buffer.toString().trimRight();
|
||||
}
|
||||
|
||||
/// Local counters for the sync status dialog, so it can offer them without
|
||||
/// building a whole bug report.
|
||||
final localSyncStateProvider = FutureProvider.autoDispose<LocalSyncState?>(
|
||||
(ref) => collectLocalSyncState(),
|
||||
);
|
||||
|
||||
/// Reads the local sync counters, or null when the database is missing or
|
||||
/// does not answer.
|
||||
///
|
||||
/// Never throws: a report must not fail over a diagnostic, and the internal
|
||||
/// tables read here are not part of PowerSync's public API.
|
||||
Future<LocalSyncState?> collectLocalSyncState() async {
|
||||
final db = builtPowerSyncInstance;
|
||||
if (db == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
final row = await db.get(_localStateQuery);
|
||||
return LocalSyncState(
|
||||
targetRequestId: row['target_request_id'] as int?,
|
||||
lastSeenRequestId: row['last_seen_request_id'] as int?,
|
||||
lastAppliedRequestId: row['last_applied_request_id'] as int?,
|
||||
maxLastOp: row['max_last_op'] as int?,
|
||||
opsSinceCheckpoint: row['ops_since_checkpoint'] as int?,
|
||||
oplogRows: row['oplog_rows'] as int?,
|
||||
);
|
||||
} catch (e, s) {
|
||||
_logger.warning('Could not read the local sync state', e, s);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Snapshot of the current sync state for bug reports, or null when the
|
||||
/// PowerSync database has not been initialised.
|
||||
///
|
||||
@@ -124,6 +214,7 @@ Future<String?> collectSyncDiagnostics({String? serverUrl}) async {
|
||||
db.currentStatus,
|
||||
pendingUploads: queue.count,
|
||||
server: serverCategory(serverUrl),
|
||||
local: await collectLocalSyncState(),
|
||||
);
|
||||
} catch (e, s) {
|
||||
_logger.warning('Could not collect sync diagnostics', e, s);
|
||||
|
||||
@@ -29,6 +29,7 @@ import 'package:powersync/powersync.dart'
|
||||
SyncResponseException,
|
||||
SyncStatus,
|
||||
UpdateType;
|
||||
import 'package:wger/core/error_dialogs.dart' show CopyToClipboardButton;
|
||||
import 'package:wger/core/network/network_provider.dart';
|
||||
import 'package:wger/core/widgets/sync_status_dialog.dart';
|
||||
import 'package:wger/database/powersync/powersync.dart'
|
||||
@@ -172,6 +173,17 @@ void main() {
|
||||
expect(find.byType(LinearProgressIndicator), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('offers the snapshot for copying even when nothing looks wrong', (tester) async {
|
||||
// The state we keep getting reports about: no error, not stalled, so
|
||||
// the report button stays hidden and copying is the only way out.
|
||||
await _pumpDialog(tester, buildSyncStatus());
|
||||
|
||||
expect(find.text('Report issue'), findsNothing);
|
||||
final button = tester.widget<CopyToClipboardButton>(find.byType(CopyToClipboardButton));
|
||||
expect(button.text, contains('last successful sync: never'));
|
||||
expect(button.text, contains('pending uploads: 0'));
|
||||
});
|
||||
|
||||
testWidgets('renders the placeholder while the sync state is still unknown', (tester) async {
|
||||
await _pumpDialog(tester, buildUninitializedSyncStatus());
|
||||
|
||||
|
||||
@@ -66,6 +66,45 @@ void main() {
|
||||
expect(text, contains('error (Connection error): SocketException'));
|
||||
});
|
||||
|
||||
test('omits the local counters when they could not be read', () {
|
||||
final text = formatSyncDiagnostics(buildSyncStatus(connected: true), pendingUploads: 0);
|
||||
|
||||
expect(text, isNot(contains('checkpoint request')));
|
||||
expect(text, isNot(contains('local buckets')));
|
||||
});
|
||||
|
||||
test('renders the local counters, showing a closed apply gate', () {
|
||||
final text = formatSyncDiagnostics(
|
||||
buildSyncStatus(connected: true),
|
||||
pendingUploads: 0,
|
||||
local: const LocalSyncState(
|
||||
targetRequestId: 9,
|
||||
lastSeenRequestId: 4,
|
||||
lastAppliedRequestId: 3,
|
||||
maxLastOp: 1738524,
|
||||
opsSinceCheckpoint: 166,
|
||||
oplogRows: 15973,
|
||||
),
|
||||
);
|
||||
|
||||
expect(text, contains('checkpoint request: target 9, seen 4, applied 3'));
|
||||
expect(
|
||||
text,
|
||||
contains('local buckets: last op 1738524, ops since checkpoint 166, oplog rows 15973'),
|
||||
);
|
||||
});
|
||||
|
||||
test('marks missing local counters instead of dropping the line', () {
|
||||
final text = formatSyncDiagnostics(
|
||||
buildSyncStatus(connected: true),
|
||||
pendingUploads: 0,
|
||||
local: const LocalSyncState(targetRequestId: 3),
|
||||
);
|
||||
|
||||
expect(text, contains('checkpoint request: target 3, seen -, applied -'));
|
||||
expect(text, contains('local buckets: last op -, ops since checkpoint -, oplog rows -'));
|
||||
});
|
||||
|
||||
test('clamps oversized error messages', () {
|
||||
final text = formatSyncDiagnostics(
|
||||
buildSyncStatus(downloadError: Exception('x' * 5000)),
|
||||
@@ -81,6 +120,7 @@ void main() {
|
||||
group('collectSyncDiagnostics', () {
|
||||
test('returns null while the PowerSync database is not initialised', () async {
|
||||
expect(await collectSyncDiagnostics(), isNull);
|
||||
expect(await collectLocalSyncState(), isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user