boost_report_manager.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import 'package:system_clock/system_clock.dart';
  2. import 'boost_logger.dart';
  3. import 'log/logger.dart';
  4. import 'ntp_time_service.dart';
  5. class BoostReportManager {
  6. final BoostLogger _logger = BoostLogger();
  7. static const String TAG = 'BoostReportManager';
  8. static final BoostReportManager _instance = BoostReportManager._internal();
  9. factory BoostReportManager() => _instance;
  10. BoostReportManager._internal();
  11. /// 初始化报告管理器
  12. Future<void> init() async {
  13. try {
  14. await _logger.init();
  15. // 初始化基本结构
  16. await _logger.setSection('sessionInfo', {});
  17. await _logger.setSection('targetInfo', {});
  18. await _logger.setSectionObj('generatedTime', _getCurrentTimestamp());
  19. } catch (e) {
  20. log(TAG, 'BoostReportManager init error: $e');
  21. }
  22. }
  23. int _getCurrentTimestamp() {
  24. return NtpTimeService().getCurrentTimestamp();
  25. }
  26. /// 读取历史日志
  27. Future<void> readHistoryLog() async {
  28. try {
  29. await _logger.readHistoryLog();
  30. } catch (e) {
  31. log(TAG, 'BoostReportManager readHistoryLog error: $e');
  32. }
  33. }
  34. /// 初始化会话信息
  35. Future<void> initSessionInfo({
  36. required String appSessionId,
  37. required String boostSessionId,
  38. required Map<String, String> deviceInfo,
  39. }) async {
  40. try {
  41. await _logger.setSection('sessionInfo', {
  42. 'appSessionId': appSessionId,
  43. 'boostSessionId': boostSessionId,
  44. 'success': false,
  45. 'errorCode': -1,
  46. 'boostStartTime': _getCurrentTimestamp(),
  47. 'boostStopTime': 0,
  48. 'boostDuration': 0,
  49. 'elapsedRealtime': SystemClock.elapsedRealtime().inMilliseconds,
  50. 'userDeviceInfo': {
  51. 'deviceModel': deviceInfo['deviceModel'] ?? '',
  52. 'osVersion': deviceInfo['osVersion'] ?? '',
  53. 'appVersion': deviceInfo['appVersion'] ?? '',
  54. 'networkType': deviceInfo['networkType'] ?? '',
  55. 'deviceBrand': deviceInfo['deviceBrand'] ?? '',
  56. },
  57. });
  58. } catch (e) {
  59. log(TAG, 'BoostReportManager initSessionInfo error: $e');
  60. }
  61. }
  62. /// 添加 targetInfo
  63. Future<void> addTargetInfo({
  64. required String locationSelectionType,
  65. required dynamic location,
  66. }) async {
  67. try {
  68. await _logger.updateField(
  69. 'targetInfo',
  70. 'locationSelectionType',
  71. locationSelectionType,
  72. );
  73. await _logger.updateField('targetInfo', 'location', location);
  74. } catch (e) {
  75. log(TAG, 'BoostReportManager addTargetInfo error: $e');
  76. }
  77. }
  78. /// 添加 latencyList
  79. Future<void> addLatencyList({required Map<String, int> latencyList}) async {
  80. try {
  81. await _logger.setSectionObj('latencys', latencyList);
  82. } catch (e) {
  83. log(TAG, 'BoostReportManager addLatencyList error: $e');
  84. }
  85. }
  86. // 获取 boostStartTime
  87. int getBoostStartTime() {
  88. try {
  89. final session = _logger.getCurrentSession();
  90. final sessionInfo = session['sessionInfo'] as Map<String, dynamic>;
  91. return sessionInfo['boostStartTime'] as int;
  92. } catch (e) {
  93. log(TAG, 'BoostReportManager getBoostStartTime error: $e');
  94. return 0;
  95. }
  96. }
  97. /// 获取当前报告
  98. Map<String, dynamic> getCurrentReport() {
  99. return _logger.getCurrentSession();
  100. }
  101. /// 获取核心日志文件路径
  102. Future<String?> getCoreLogFilePath() {
  103. return _logger.getCoreLogFilePath();
  104. }
  105. /// 获取报告文件路径
  106. Future<String?> getAppLogFilePath() {
  107. return _logger.getAppLogFilePath();
  108. }
  109. /// 释放资源
  110. Future<void> release() async {
  111. await _logger.release();
  112. }
  113. }