mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-02-02 14:16:58 +00:00
Catch error on metrics loading from Hetzner
This commit is contained in:
parent
e3d7f2b3d7
commit
d75c2837ca
|
@ -20,7 +20,22 @@ enum InitializingSteps {
|
||||||
checkSystemDnsAndDkimSet,
|
checkSystemDnsAndDkimSet,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Period { hour, day, month }
|
enum Period {
|
||||||
|
hour,
|
||||||
|
day,
|
||||||
|
month;
|
||||||
|
|
||||||
|
int get stepPeriodInSeconds {
|
||||||
|
switch (this) {
|
||||||
|
case Period.hour:
|
||||||
|
return 18;
|
||||||
|
case Period.day:
|
||||||
|
return 432;
|
||||||
|
case Period.month:
|
||||||
|
return 6480;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
enum ServiceTypes {
|
enum ServiceTypes {
|
||||||
mailserver,
|
mailserver,
|
||||||
|
|
|
@ -39,16 +39,20 @@ class HetznerMetricsCubit extends Cubit<HetznerMetricsState> {
|
||||||
}
|
}
|
||||||
|
|
||||||
void load(final Period period) async {
|
void load(final Period period) async {
|
||||||
|
try {
|
||||||
final HetznerMetricsLoaded newState = await repository.getMetrics(period);
|
final HetznerMetricsLoaded newState = await repository.getMetrics(period);
|
||||||
timer = Timer(
|
timer = Timer(
|
||||||
Duration(seconds: newState.stepInSeconds.toInt()),
|
Duration(seconds: newState.stepInSeconds.toInt()),
|
||||||
() => load(newState.period),
|
() => load(newState.period),
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
|
||||||
emit(newState);
|
emit(newState);
|
||||||
} on StateError {
|
} on StateError {
|
||||||
print('Tried to emit Hetzner metrics when cubit is closed');
|
print('Tried to emit Hetzner metrics when cubit is closed');
|
||||||
|
} on MetricsLoadException {
|
||||||
|
timer = Timer(
|
||||||
|
Duration(seconds: state.period.stepPeriodInSeconds),
|
||||||
|
() => load(state.period),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,11 @@ import 'package:selfprivacy/logic/models/hetzner_metrics.dart';
|
||||||
|
|
||||||
import 'package:selfprivacy/logic/cubit/hetzner_metrics/hetzner_metrics_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/hetzner_metrics/hetzner_metrics_cubit.dart';
|
||||||
|
|
||||||
|
class MetricsLoadException implements Exception {
|
||||||
|
MetricsLoadException(this.message);
|
||||||
|
final String message;
|
||||||
|
}
|
||||||
|
|
||||||
class HetznerMetricsRepository {
|
class HetznerMetricsRepository {
|
||||||
Future<HetznerMetricsLoaded> getMetrics(final Period period) async {
|
Future<HetznerMetricsLoaded> getMetrics(final Period period) async {
|
||||||
final DateTime end = DateTime.now();
|
final DateTime end = DateTime.now();
|
||||||
|
@ -31,6 +36,10 @@ class HetznerMetricsRepository {
|
||||||
final cpuMetricsData = results[0]['metrics'];
|
final cpuMetricsData = results[0]['metrics'];
|
||||||
final networkMetricsData = results[1]['metrics'];
|
final networkMetricsData = results[1]['metrics'];
|
||||||
|
|
||||||
|
if (cpuMetricsData == null || networkMetricsData == null) {
|
||||||
|
throw MetricsLoadException('Metrics data is null');
|
||||||
|
}
|
||||||
|
|
||||||
return HetznerMetricsLoaded(
|
return HetznerMetricsLoaded(
|
||||||
period: period,
|
period: period,
|
||||||
start: start,
|
start: start,
|
||||||
|
|
Loading…
Reference in a new issue