tstor/ui/lib/screens/torrent_stats.dart
2024-08-15 11:23:44 +03:00

62 lines
1.6 KiB
Dart

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:graphql/client.dart';
import 'package:tstor_ui/api/client.dart';
import 'package:tstor_ui/api/torrent_stats.graphql.dart';
class TorrentStatsScreen extends StatefulWidget {
const TorrentStatsScreen({super.key});
@override
State<TorrentStatsScreen> createState() => _TorrentStatsScreenState();
}
class _TorrentStatsScreenState extends State<TorrentStatsScreen> {
Future<LineChartData> _totalStats() async {
final since = DateTime.now().subtract(Duration(hours: 1));
final data = await client.query$TorrentTotalStats(
Options$Query$TorrentTotalStats(
variables: Variables$Query$TorrentTotalStats(
since: since,
),
fetchPolicy: FetchPolicy.networkOnly,
),
);
return LineChartData(
lineBarsData: [
LineChartBarData(
spots: data.parsedData!.torrentDaemon!.statsHistory
.map(
(e) => FlSpot(
since.difference(e.timestamp).inSeconds.toDouble(),
e.activePeers.toDouble(),
),
)
.toList(),
),
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Torrent Stats"),
),
body: FutureBuilder(
future: _totalStats(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(),
);
}
return LineChart(snapshot.data!);
},
),
);
}
}