2022-10-16 00:51:10 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2022-07-14 13:34:08 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart';
|
2021-04-10 03:04:23 +00:00
|
|
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/hetzner_metrics.dart';
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/hetzner_metrics/hetzner_metrics_cubit.dart';
|
2021-04-10 03:04:23 +00:00
|
|
|
|
2022-09-16 08:07:37 +00:00
|
|
|
class MetricsLoadException implements Exception {
|
|
|
|
MetricsLoadException(this.message);
|
|
|
|
final String message;
|
|
|
|
}
|
|
|
|
|
2021-04-10 03:04:23 +00:00
|
|
|
class HetznerMetricsRepository {
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<HetznerMetricsLoaded> getMetrics(final Period period) async {
|
|
|
|
final DateTime end = DateTime.now();
|
2021-04-10 03:04:23 +00:00
|
|
|
DateTime start;
|
|
|
|
|
|
|
|
switch (period) {
|
|
|
|
case Period.hour:
|
2022-05-24 18:55:39 +00:00
|
|
|
start = end.subtract(const Duration(hours: 1));
|
2021-04-10 03:04:23 +00:00
|
|
|
break;
|
|
|
|
case Period.day:
|
2022-05-24 18:55:39 +00:00
|
|
|
start = end.subtract(const Duration(days: 1));
|
2021-04-10 03:04:23 +00:00
|
|
|
break;
|
|
|
|
case Period.month:
|
2022-05-24 18:55:39 +00:00
|
|
|
start = end.subtract(const Duration(days: 15));
|
2021-04-10 03:04:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-13 23:13:56 +00:00
|
|
|
final HetznerApi api = HetznerApi(
|
2022-10-16 00:51:10 +00:00
|
|
|
/// TODO: Hetzner hardcode (???)
|
2022-10-13 23:13:56 +00:00
|
|
|
hasLogger: false,
|
2022-10-16 00:51:10 +00:00
|
|
|
region: getIt<ApiConfigModel>().serverLocation,
|
2022-10-13 23:13:56 +00:00
|
|
|
);
|
2021-04-10 03:04:23 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final List<Map<String, dynamic>> results = await Future.wait([
|
2021-04-10 03:04:23 +00:00
|
|
|
api.getMetrics(start, end, 'cpu'),
|
|
|
|
api.getMetrics(start, end, 'network'),
|
|
|
|
]);
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final cpuMetricsData = results[0]['metrics'];
|
|
|
|
final networkMetricsData = results[1]['metrics'];
|
2021-04-10 03:04:23 +00:00
|
|
|
|
2022-09-16 08:07:37 +00:00
|
|
|
if (cpuMetricsData == null || networkMetricsData == null) {
|
|
|
|
throw MetricsLoadException('Metrics data is null');
|
|
|
|
}
|
|
|
|
|
2021-04-10 03:04:23 +00:00
|
|
|
return HetznerMetricsLoaded(
|
|
|
|
period: period,
|
|
|
|
start: start,
|
|
|
|
end: end,
|
2022-05-24 18:55:39 +00:00
|
|
|
stepInSeconds: cpuMetricsData['step'],
|
2021-04-10 03:04:23 +00:00
|
|
|
cpu: timeSeriesSerializer(cpuMetricsData, 'cpu'),
|
|
|
|
ppsIn: timeSeriesSerializer(networkMetricsData, 'network.0.pps.in'),
|
|
|
|
ppsOut: timeSeriesSerializer(networkMetricsData, 'network.0.pps.out'),
|
|
|
|
bandwidthIn:
|
|
|
|
timeSeriesSerializer(networkMetricsData, 'network.0.bandwidth.in'),
|
|
|
|
bandwidthOut: timeSeriesSerializer(
|
|
|
|
networkMetricsData,
|
|
|
|
'network.0.bandwidth.out',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<TimeSeriesData> timeSeriesSerializer(
|
2022-06-05 22:40:34 +00:00
|
|
|
final Map<String, dynamic> json,
|
|
|
|
final String type,
|
|
|
|
) {
|
2022-06-05 19:36:32 +00:00
|
|
|
final List list = json['time_series'][type]['values'];
|
2022-06-05 22:40:34 +00:00
|
|
|
return list
|
|
|
|
.map((final el) => TimeSeriesData(el[0], double.parse(el[1])))
|
|
|
|
.toList();
|
2021-04-10 03:04:23 +00:00
|
|
|
}
|