epoll-j

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

... ... @@ -40,6 +40,7 @@ class _MyAppState extends State<MyApp> {
void initState() {
AutoTrack()
.config(AutoTrackConfig( // 其余配置可查看AutoTrackConfig类
samplingRate: 0.9, // 采样率
host: 'http://localhost:3000/api/track',
eventHandler: (model) => {
// 事件触发会调用此方法,可自行处理
... ... @@ -90,6 +91,20 @@ class _MyAppState extends State<MyApp> {
}
```
##### 更新采样率
```dart
AutoTrack().updateSampleRate(0.5);
```
##### 登录后更新用户id
```dart
AutoTrack().updateUserId('userId');
```
##### 采样错误信息
```dart
FlutterError.onError = (details) {
Track.instance.reportError(details, details.stack!);
};
```
#### 具体使用
```dart
... ...
... ... @@ -19,9 +19,8 @@ class _MyAppState extends State<MyApp> {
void initState() {
AutoTrack()
.config(AutoTrackConfig(
eventHandler: (model) => {
print('event handler ${model.type}')
},
samplingRate: 0.9, // 采样率
eventHandler: (model) => {print('event handler ${model.type}')},
pageConfigs: [
AutoTrackPageConfig<PageA>(
pageID: 'page_a',
... ... @@ -34,8 +33,15 @@ class _MyAppState extends State<MyApp> {
.enableDrag()
.enableIgnoreNullKey()
.enableLog();
super.initState();
// AutoTrack().updateSampleRate(0.5); 更新采样率
// AutoTrack().updateUserId('xxxxxx'); 用户登录后设置用户id
//
// 采样错误信息
// FlutterError.onError = (details) {
// Track.instance.reportError(details, details.stack!);
// };
}
@override
... ...
... ... @@ -11,6 +11,7 @@ class AutoTrackConfig {
AutoTrackConfig({
this.host, // 数据上报地址
this.uploadInterval, // 数据上报间隔
this.samplingRate = 1, // 采样率
this.appKey = '', // 数据上报时根据key和secret生成签名
this.appSecret = '',
this.signature, // 签名生成方法,默认使用sha256对key、时间戳和secret进行签名
... ... @@ -40,6 +41,9 @@ class AutoTrackConfig {
String? userId;
String? uniqueId;
/// 采样率,默认 1 (100%)
double samplingRate;
int? uploadInterval;
Function? signature;
... ...
... ... @@ -62,6 +62,10 @@ class AutoTrackConfigManager {
_config.userId = userId;
}
void updateSampleRate(double rate) {
_config.samplingRate = rate;
}
void updatePageConfigs(List<AutoTrackPageConfig> pageConfigs) {
_config.pageConfigs = pageConfigs;
}
... ...
import 'dart:async';
import 'dart:math';
import 'package:auto_track/auto_track/config/manager.dart';
import 'package:auto_track/auto_track/utils/track_model.dart';
... ... @@ -39,6 +40,12 @@ class AutoTrackQueue {
_queue.clear();
final config = AutoTrackConfigManager.instance.config;
final host = config.host;
if (config.samplingRate != 1) {
if (Random().nextDouble() > config.samplingRate) {
// 不在采样范围不上传
return;
}
}
if (host != null) {
final t = DateTime.now().millisecondsSinceEpoch;
dio.post(host, data: {
... ...
... ... @@ -19,6 +19,10 @@ class AutoTrack {
AutoTrackConfigManager.instance.updateUserId(id);
}
void updateSampleRate(double rate) {
AutoTrackConfigManager.instance.updateSampleRate(rate);
}
AutoTrack config(AutoTrackConfig? config) {
if (config != null) {
AutoTrackConfigManager.instance.updateConfig(config);
... ...
name: auto_track
description: "Auto Track Plugin"
version: 0.0.5
version: 0.0.6
homepage: https://github.com/epoll-j/auto_track_plugin
environment:
... ...