2022-11-12 17:29:06 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2022-11-18 03:07:42 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/api_controller.dart';
|
2022-11-12 17:29:06 +00:00
|
|
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
|
|
|
|
|
|
|
import 'package:selfprivacy/logic/cubit/metrics/metrics_cubit.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/metrics.dart';
|
|
|
|
|
|
|
|
class MetricsLoadException implements Exception {
|
|
|
|
MetricsLoadException(this.message);
|
|
|
|
final String message;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MetricsRepository {
|
|
|
|
Future<MetricsLoaded> getMetrics(final Period period) async {
|
2022-11-18 03:07:42 +00:00
|
|
|
final providerApiFactory = ApiController.currentServerProviderApiFactory;
|
|
|
|
if (providerApiFactory == null) {
|
|
|
|
throw MetricsLoadException('Server Provider data is null');
|
2022-11-12 17:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final DateTime end = DateTime.now();
|
|
|
|
DateTime start;
|
|
|
|
|
|
|
|
switch (period) {
|
|
|
|
case Period.hour:
|
|
|
|
start = end.subtract(const Duration(hours: 1));
|
|
|
|
break;
|
|
|
|
case Period.day:
|
|
|
|
start = end.subtract(const Duration(days: 1));
|
|
|
|
break;
|
|
|
|
case Period.month:
|
|
|
|
start = end.subtract(const Duration(days: 15));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
final serverId = getIt<ApiConfigModel>().serverDetails!.id;
|
|
|
|
final ServerMetrics? metrics =
|
2022-11-18 03:07:42 +00:00
|
|
|
await providerApiFactory.getServerProvider().getMetrics(
|
2022-11-12 17:29:06 +00:00
|
|
|
serverId,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (metrics == null) {
|
|
|
|
throw MetricsLoadException('Metrics data is null');
|
|
|
|
}
|
|
|
|
|
|
|
|
return MetricsLoaded(
|
|
|
|
period: period,
|
|
|
|
metrics: metrics,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|