boost_report_manager.dart 3.4 KB

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