Skip to content

Commit

Permalink
[stats] build payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Sep 26, 2024
1 parent 0bc4947 commit 00503db
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
14 changes: 13 additions & 1 deletion lib/backend/stats/entity/feature.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:objectbox/objectbox.dart';

import 'stats.dart';

@Entity()
class StatsFeatureUsage {
class StatsFeatureUsage implements StatsEntry {
@Id()
int id;
final String feature;
Expand All @@ -15,4 +17,14 @@ class StatsFeatureUsage {
this.result = "",
required this.time,
});

@override
Map<String, dynamic> toPayload() => {
"category": "app_feature",
"data": {
"feature": feature,
"time": time.toIso8601String(),
"result": result,
}
};
}
13 changes: 12 additions & 1 deletion lib/backend/stats/entity/route.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:objectbox/objectbox.dart';

import 'stats.dart';

@Entity()
class StatsRoute {
class StatsRoute implements StatsEntry {
@Id()
int id;
final String route;
Expand All @@ -13,4 +15,13 @@ class StatsRoute {
required this.route,
required this.time,
});

@override
Map<String, dynamic> toPayload() => {
"category": "app_route",
"data": {
"route": route,
"time": time.toIso8601String(),
}
};
}
3 changes: 3 additions & 0 deletions lib/backend/stats/entity/stats.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abstract class StatsEntry {
Map<String, dynamic> toPayload();
}
17 changes: 16 additions & 1 deletion lib/backend/stats/service/stats.dart
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
class StatsService {}
import 'package:dio/dio.dart';
import 'package:mimir/init.dart';

import '../entity/stats.dart';

class StatsService {
Dio get _dio => Init.mimirDio;

Future<void> uploadStats(List<StatsEntry> list) async {
final payloads = list.map((entry) => entry.toPayload()).toList(growable: false);
final res = await _dio.post(
"https://stats.mysit.life/v1/log",
data: payloads,
);
}
}

0 comments on commit 00503db

Please sign in to comment.