From 00503db65c9ae87b9106e1a247d3efb9f6cedb78 Mon Sep 17 00:00:00 2001 From: Liplum Date: Thu, 26 Sep 2024 11:49:15 +0800 Subject: [PATCH] [stats] build payloads --- lib/backend/stats/entity/feature.dart | 14 +++++++++++++- lib/backend/stats/entity/route.dart | 13 ++++++++++++- lib/backend/stats/entity/stats.dart | 3 +++ lib/backend/stats/service/stats.dart | 17 ++++++++++++++++- 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 lib/backend/stats/entity/stats.dart diff --git a/lib/backend/stats/entity/feature.dart b/lib/backend/stats/entity/feature.dart index 51a92010..09b85ddf 100644 --- a/lib/backend/stats/entity/feature.dart +++ b/lib/backend/stats/entity/feature.dart @@ -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; @@ -15,4 +17,14 @@ class StatsFeatureUsage { this.result = "", required this.time, }); + + @override + Map toPayload() => { + "category": "app_feature", + "data": { + "feature": feature, + "time": time.toIso8601String(), + "result": result, + } + }; } diff --git a/lib/backend/stats/entity/route.dart b/lib/backend/stats/entity/route.dart index 769e307a..426c906c 100644 --- a/lib/backend/stats/entity/route.dart +++ b/lib/backend/stats/entity/route.dart @@ -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; @@ -13,4 +15,13 @@ class StatsRoute { required this.route, required this.time, }); + + @override + Map toPayload() => { + "category": "app_route", + "data": { + "route": route, + "time": time.toIso8601String(), + } + }; } diff --git a/lib/backend/stats/entity/stats.dart b/lib/backend/stats/entity/stats.dart new file mode 100644 index 00000000..1c856b50 --- /dev/null +++ b/lib/backend/stats/entity/stats.dart @@ -0,0 +1,3 @@ +abstract class StatsEntry { + Map toPayload(); +} diff --git a/lib/backend/stats/service/stats.dart b/lib/backend/stats/service/stats.dart index 598b652d..b24b9502 100644 --- a/lib/backend/stats/service/stats.dart +++ b/lib/backend/stats/service/stats.dart @@ -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 uploadStats(List 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, + ); + } +}