epoll-j

feat(track): 添加采样率设置

@@ -40,6 +40,7 @@ class _MyAppState extends State<MyApp> { @@ -40,6 +40,7 @@ class _MyAppState extends State<MyApp> {
40 void initState() { 40 void initState() {
41 AutoTrack() 41 AutoTrack()
42 .config(AutoTrackConfig( // 其余配置可查看AutoTrackConfig类 42 .config(AutoTrackConfig( // 其余配置可查看AutoTrackConfig类
  43 + samplingRate: 0.9, // 采样率
43 host: 'http://localhost:3000/api/track', 44 host: 'http://localhost:3000/api/track',
44 eventHandler: (model) => { 45 eventHandler: (model) => {
45 // 事件触发会调用此方法,可自行处理 46 // 事件触发会调用此方法,可自行处理
@@ -90,6 +91,20 @@ class _MyAppState extends State<MyApp> { @@ -90,6 +91,20 @@ class _MyAppState extends State<MyApp> {
90 } 91 }
91 92
92 ``` 93 ```
  94 +##### 更新采样率
  95 +```dart
  96 +AutoTrack().updateSampleRate(0.5);
  97 +```
  98 +##### 登录后更新用户id
  99 +```dart
  100 +AutoTrack().updateUserId('userId');
  101 +```
  102 +##### 采样错误信息
  103 +```dart
  104 +FlutterError.onError = (details) {
  105 + Track.instance.reportError(details, details.stack!);
  106 +};
  107 +```
93 108
94 #### 具体使用 109 #### 具体使用
95 ```dart 110 ```dart
@@ -19,9 +19,8 @@ class _MyAppState extends State<MyApp> { @@ -19,9 +19,8 @@ class _MyAppState extends State<MyApp> {
19 void initState() { 19 void initState() {
20 AutoTrack() 20 AutoTrack()
21 .config(AutoTrackConfig( 21 .config(AutoTrackConfig(
22 - eventHandler: (model) => {  
23 - print('event handler ${model.type}')  
24 - }, 22 + samplingRate: 0.9, // 采样率
  23 + eventHandler: (model) => {print('event handler ${model.type}')},
25 pageConfigs: [ 24 pageConfigs: [
26 AutoTrackPageConfig<PageA>( 25 AutoTrackPageConfig<PageA>(
27 pageID: 'page_a', 26 pageID: 'page_a',
@@ -34,8 +33,15 @@ class _MyAppState extends State<MyApp> { @@ -34,8 +33,15 @@ class _MyAppState extends State<MyApp> {
34 .enableDrag() 33 .enableDrag()
35 .enableIgnoreNullKey() 34 .enableIgnoreNullKey()
36 .enableLog(); 35 .enableLog();
37 -  
38 super.initState(); 36 super.initState();
  37 +
  38 + // AutoTrack().updateSampleRate(0.5); 更新采样率
  39 + // AutoTrack().updateUserId('xxxxxx'); 用户登录后设置用户id
  40 + //
  41 + // 采样错误信息
  42 + // FlutterError.onError = (details) {
  43 + // Track.instance.reportError(details, details.stack!);
  44 + // };
39 } 45 }
40 46
41 @override 47 @override
@@ -11,6 +11,7 @@ class AutoTrackConfig { @@ -11,6 +11,7 @@ class AutoTrackConfig {
11 AutoTrackConfig({ 11 AutoTrackConfig({
12 this.host, // 数据上报地址 12 this.host, // 数据上报地址
13 this.uploadInterval, // 数据上报间隔 13 this.uploadInterval, // 数据上报间隔
  14 + this.samplingRate = 1, // 采样率
14 this.appKey = '', // 数据上报时根据key和secret生成签名 15 this.appKey = '', // 数据上报时根据key和secret生成签名
15 this.appSecret = '', 16 this.appSecret = '',
16 this.signature, // 签名生成方法,默认使用sha256对key、时间戳和secret进行签名 17 this.signature, // 签名生成方法,默认使用sha256对key、时间戳和secret进行签名
@@ -40,6 +41,9 @@ class AutoTrackConfig { @@ -40,6 +41,9 @@ class AutoTrackConfig {
40 String? userId; 41 String? userId;
41 String? uniqueId; 42 String? uniqueId;
42 43
  44 + /// 采样率,默认 1 (100%)
  45 + double samplingRate;
  46 +
43 int? uploadInterval; 47 int? uploadInterval;
44 48
45 Function? signature; 49 Function? signature;
@@ -62,6 +62,10 @@ class AutoTrackConfigManager { @@ -62,6 +62,10 @@ class AutoTrackConfigManager {
62 _config.userId = userId; 62 _config.userId = userId;
63 } 63 }
64 64
  65 + void updateSampleRate(double rate) {
  66 + _config.samplingRate = rate;
  67 + }
  68 +
65 void updatePageConfigs(List<AutoTrackPageConfig> pageConfigs) { 69 void updatePageConfigs(List<AutoTrackPageConfig> pageConfigs) {
66 _config.pageConfigs = pageConfigs; 70 _config.pageConfigs = pageConfigs;
67 } 71 }
1 import 'dart:async'; 1 import 'dart:async';
  2 +import 'dart:math';
2 3
3 import 'package:auto_track/auto_track/config/manager.dart'; 4 import 'package:auto_track/auto_track/config/manager.dart';
4 import 'package:auto_track/auto_track/utils/track_model.dart'; 5 import 'package:auto_track/auto_track/utils/track_model.dart';
@@ -39,6 +40,12 @@ class AutoTrackQueue { @@ -39,6 +40,12 @@ class AutoTrackQueue {
39 _queue.clear(); 40 _queue.clear();
40 final config = AutoTrackConfigManager.instance.config; 41 final config = AutoTrackConfigManager.instance.config;
41 final host = config.host; 42 final host = config.host;
  43 + if (config.samplingRate != 1) {
  44 + if (Random().nextDouble() > config.samplingRate) {
  45 + // 不在采样范围不上传
  46 + return;
  47 + }
  48 + }
42 if (host != null) { 49 if (host != null) {
43 final t = DateTime.now().millisecondsSinceEpoch; 50 final t = DateTime.now().millisecondsSinceEpoch;
44 dio.post(host, data: { 51 dio.post(host, data: {
@@ -19,6 +19,10 @@ class AutoTrack { @@ -19,6 +19,10 @@ class AutoTrack {
19 AutoTrackConfigManager.instance.updateUserId(id); 19 AutoTrackConfigManager.instance.updateUserId(id);
20 } 20 }
21 21
  22 + void updateSampleRate(double rate) {
  23 + AutoTrackConfigManager.instance.updateSampleRate(rate);
  24 + }
  25 +
22 AutoTrack config(AutoTrackConfig? config) { 26 AutoTrack config(AutoTrackConfig? config) {
23 if (config != null) { 27 if (config != null) {
24 AutoTrackConfigManager.instance.updateConfig(config); 28 AutoTrackConfigManager.instance.updateConfig(config);
1 name: auto_track 1 name: auto_track
2 description: "Auto Track Plugin" 2 description: "Auto Track Plugin"
3 -version: 0.0.5 3 +version: 0.0.6
4 homepage: https://github.com/epoll-j/auto_track_plugin 4 homepage: https://github.com/epoll-j/auto_track_plugin
5 5
6 environment: 6 environment: