Skip to content

Commit

Permalink
chore: diagnose sync issues with sync.log file (AppFlowy-IO#6950)
Browse files Browse the repository at this point in the history
* chore: filter sync log

* chore: filter sync log

* chore: enable/disable sync log

* chore: enable/disable sync log

* chore: observer document and folder

* fix: integration test

---------

Co-authored-by: Lucas.Xu <[email protected]>
  • Loading branch information
appflowy and LucasXu0 authored Dec 10, 2024
1 parent 2c88653 commit d2b2f17
Show file tree
Hide file tree
Showing 19 changed files with 252 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import 'data_migration/data_migration_test_runner.dart'
as data_migration_test_runner;
import 'document/document_test_runner.dart' as document_test_runner;
import 'set_env.dart' as preset_af_cloud_env_test;
import 'sidebar/sidebar_move_page_test.dart' as sidebar_move_page_test;
import 'uncategorized/uncategorized_test_runner.dart'
as uncategorized_test_runner;
import 'workspace/workspace_test_runner.dart' as workspace_test_runner;
import 'data_migration/data_migration_test_runner.dart'
as data_migration_test_runner;
import 'set_env.dart' as preset_af_cloud_env_test;

Future<void> main() async {
preset_af_cloud_env_test.main();

data_migration_test_runner.main();

// uncategorized
uncategorized_test_runner.main();

Expand Down
1 change: 1 addition & 0 deletions frontend/appflowy_flutter/lib/core/config/kv_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class KVKeys {

static const String kCloudType = 'kCloudType';
static const String kAppflowyCloudBaseURL = 'kAppFlowyCloudBaseURL';
static const String kAppFlowyEnableSyncTrace = 'kAppFlowyEnableSyncTrace';

/// The key for saving the text scale factor.
///
Expand Down
3 changes: 3 additions & 0 deletions frontend/appflowy_flutter/lib/env/backend_env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class AppFlowyCloudConfiguration {
required this.base_url,
required this.ws_base_url,
required this.gotrue_url,
required this.enable_sync_trace,
});

factory AppFlowyCloudConfiguration.fromJson(Map<String, dynamic> json) =>
Expand All @@ -47,6 +48,7 @@ class AppFlowyCloudConfiguration {
final String base_url;
final String ws_base_url;
final String gotrue_url;
final bool enable_sync_trace;

Map<String, dynamic> toJson() => _$AppFlowyCloudConfigurationToJson(this);

Expand All @@ -55,6 +57,7 @@ class AppFlowyCloudConfiguration {
base_url: '',
ws_base_url: '',
gotrue_url: '',
enable_sync_trace: false,
);
}

Expand Down
21 changes: 21 additions & 0 deletions frontend/appflowy_flutter/lib/env/cloud_env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ class AppFlowyCloudSharedEnv {
base_url: Env.afCloudUrl,
ws_base_url: await _getAppFlowyCloudWSUrl(Env.afCloudUrl),
gotrue_url: await _getAppFlowyCloudGotrueUrl(Env.afCloudUrl),
enable_sync_trace: false,
);

return AppFlowyCloudSharedEnv(
Expand Down Expand Up @@ -241,12 +242,14 @@ Future<AppFlowyCloudConfiguration> configurationFromUri(
base_url: "$baseUrl:8000",
ws_base_url: "ws://${baseUri.host}:8000/ws/v1",
gotrue_url: "$baseUrl:9999",
enable_sync_trace: true,
);
} else {
return AppFlowyCloudConfiguration(
base_url: baseUrl,
ws_base_url: await _getAppFlowyCloudWSUrl(baseUrl),
gotrue_url: await _getAppFlowyCloudGotrueUrl(baseUrl),
enable_sync_trace: await getSyncLogEnabled(),
);
}
}
Expand All @@ -271,6 +274,24 @@ Future<String> getAppFlowyCloudUrl() async {
return result ?? kAppflowyCloudUrl;
}

Future<bool> getSyncLogEnabled() async {
final result =
await getIt<KeyValueStorage>().get(KVKeys.kAppFlowyEnableSyncTrace);

if (result == null) {
return false;
}

return result.toLowerCase() == "true";
}

Future<void> setSyncLogEnabled(bool enable) async {
await getIt<KeyValueStorage>().set(
KVKeys.kAppFlowyEnableSyncTrace,
enable.toString().toLowerCase(),
);
}

Future<String> _getAppFlowyCloudWSUrl(String baseURL) async {
try {
final uri = Uri.parse(baseURL);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:appflowy/env/cloud_env.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/workspace/application/settings/cloud_setting_listener.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
Expand All @@ -14,7 +15,7 @@ class AppFlowyCloudSettingBloc
extends Bloc<AppFlowyCloudSettingEvent, AppFlowyCloudSettingState> {
AppFlowyCloudSettingBloc(CloudSettingPB setting)
: _listener = UserCloudConfigListener(),
super(AppFlowyCloudSettingState.initial(setting)) {
super(AppFlowyCloudSettingState.initial(setting, false)) {
_dispatch();
}

Expand All @@ -31,6 +32,10 @@ class AppFlowyCloudSettingBloc
(event, emit) async {
await event.when(
initial: () async {
await getSyncLogEnabled().then((value) {
emit(state.copyWith(isSyncLogEnabled: value));
});

_listener.start(
onSettingChanged: (result) {
if (isClosed) {
Expand All @@ -48,6 +53,10 @@ class AppFlowyCloudSettingBloc
final config = UpdateCloudConfigPB.create()..enableSync = isEnable;
await UserEventSetCloudConfig(config).send();
},
enableSyncLog: (isEnable) async {
await setSyncLogEnabled(isEnable);
emit(state.copyWith(isSyncLogEnabled: isEnable));
},
didReceiveSetting: (CloudSettingPB setting) {
emit(
state.copyWith(
Expand All @@ -67,6 +76,8 @@ class AppFlowyCloudSettingEvent with _$AppFlowyCloudSettingEvent {
const factory AppFlowyCloudSettingEvent.initial() = _Initial;
const factory AppFlowyCloudSettingEvent.enableSync(bool isEnable) =
_EnableSync;
const factory AppFlowyCloudSettingEvent.enableSyncLog(bool isEnable) =
_EnableSyncLog;
const factory AppFlowyCloudSettingEvent.didReceiveSetting(
CloudSettingPB setting,
) = _DidUpdateSetting;
Expand All @@ -77,12 +88,17 @@ class AppFlowyCloudSettingState with _$AppFlowyCloudSettingState {
const factory AppFlowyCloudSettingState({
required CloudSettingPB setting,
required bool showRestartHint,
required bool isSyncLogEnabled,
}) = _AppFlowyCloudSettingState;

factory AppFlowyCloudSettingState.initial(CloudSettingPB setting) =>
factory AppFlowyCloudSettingState.initial(
CloudSettingPB setting,
bool isSyncLogEnabled,
) =>
AppFlowyCloudSettingState(
setting: setting,
showRestartHint: setting.serverUrl.isNotEmpty,
isSyncLogEnabled: isSyncLogEnabled,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ class SpaceBloc extends Bloc<SpaceEvent, SpaceState> {

if (openFirstPage) {
if (currentSpace != null) {
add(SpaceEvent.open(currentSpace));
if (!isClosed) {
add(SpaceEvent.open(currentSpace));
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class AppFlowyCloudViewSetting extends StatelessWidget {
return Column(
children: [
const AppFlowyCloudEnableSync(),
const AppFlowyCloudSyncLogEnabled(),
const VSpace(12),
RestartButton(
onClick: () {
Expand Down Expand Up @@ -123,6 +124,7 @@ class CustomAppFlowyCloudView extends StatelessWidget {
final List<Widget> children = [];
children.addAll([
const AppFlowyCloudEnableSync(),
const AppFlowyCloudSyncLogEnabled(),
const VSpace(40),
]);

Expand Down Expand Up @@ -331,6 +333,47 @@ class AppFlowyCloudEnableSync extends StatelessWidget {
}
}

class AppFlowyCloudSyncLogEnabled extends StatelessWidget {
const AppFlowyCloudSyncLogEnabled({super.key});

@override
Widget build(BuildContext context) {
return BlocBuilder<AppFlowyCloudSettingBloc, AppFlowyCloudSettingState>(
builder: (context, state) {
return Row(
children: [
FlowyText.medium(LocaleKeys.settings_menu_enableSyncLog.tr()),
const Spacer(),
Toggle(
value: state.isSyncLogEnabled,
onChanged: (value) {
if (value) {
showCancelAndConfirmDialog(
context: context,
title: LocaleKeys.settings_menu_enableSyncLog.tr(),
description:
LocaleKeys.settings_menu_enableSyncLogWarning.tr(),
confirmLabel: LocaleKeys.button_confirm.tr(),
onConfirm: () {
context
.read<AppFlowyCloudSettingBloc>()
.add(AppFlowyCloudSettingEvent.enableSyncLog(value));
},
);
} else {
context
.read<AppFlowyCloudSettingBloc>()
.add(AppFlowyCloudSettingEvent.enableSyncLog(value));
}
},
),
],
);
},
);
}
}

class BillingGateGuard extends StatelessWidget {
const BillingGateGuard({required this.builder, super.key});

Expand Down
4 changes: 3 additions & 1 deletion frontend/resources/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,8 @@
"syncSetting": "Sync Setting",
"cloudSettings": "Cloud Settings",
"enableSync": "Enable sync",
"enableSyncLog": "Enable sync logging",
"enableSyncLogWarning": "Thank you for helping diagnose sync issues. This will log your document edits to a local file. Please quit and reopen the app after enabling",
"enableEncrypt": "Encrypt data",
"cloudURL": "Base URL",
"invalidCloudURLScheme": "Invalid Scheme",
Expand Down Expand Up @@ -2904,4 +2906,4 @@
"permissionDenied": "No permission to open this file",
"unknownError": "File open failed"
}
}
}
42 changes: 28 additions & 14 deletions frontend/rust-lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/rust-lib/event-integration-test/src/user_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ pub async fn use_localhost_af_cloud() {
base_url,
ws_base_url,
gotrue_url,
enable_sync_trace: true,
maximum_upload_file_size_in_bytes: None,
}
.write_env();
Expand Down
Loading

0 comments on commit d2b2f17

Please sign in to comment.