mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-23 09:16:54 +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,
|
||||
}
|
||||
|
||||
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 {
|
||||
mailserver,
|
||||
|
|
|
@ -39,16 +39,20 @@ class HetznerMetricsCubit extends Cubit<HetznerMetricsState> {
|
|||
}
|
||||
|
||||
void load(final Period period) async {
|
||||
final HetznerMetricsLoaded newState = await repository.getMetrics(period);
|
||||
timer = Timer(
|
||||
Duration(seconds: newState.stepInSeconds.toInt()),
|
||||
() => load(newState.period),
|
||||
);
|
||||
|
||||
try {
|
||||
final HetznerMetricsLoaded newState = await repository.getMetrics(period);
|
||||
timer = Timer(
|
||||
Duration(seconds: newState.stepInSeconds.toInt()),
|
||||
() => load(newState.period),
|
||||
);
|
||||
emit(newState);
|
||||
} on StateError {
|
||||
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';
|
||||
|
||||
class MetricsLoadException implements Exception {
|
||||
MetricsLoadException(this.message);
|
||||
final String message;
|
||||
}
|
||||
|
||||
class HetznerMetricsRepository {
|
||||
Future<HetznerMetricsLoaded> getMetrics(final Period period) async {
|
||||
final DateTime end = DateTime.now();
|
||||
|
@ -31,6 +36,10 @@ class HetznerMetricsRepository {
|
|||
final cpuMetricsData = results[0]['metrics'];
|
||||
final networkMetricsData = results[1]['metrics'];
|
||||
|
||||
if (cpuMetricsData == null || networkMetricsData == null) {
|
||||
throw MetricsLoadException('Metrics data is null');
|
||||
}
|
||||
|
||||
return HetznerMetricsLoaded(
|
||||
period: period,
|
||||
start: start,
|
||||
|
|
Loading…
Reference in a new issue