Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework load status and add failover implementation #585

Closed
wants to merge 14 commits into from
Next Next commit
Start rework load service
  • Loading branch information
PaulPickhardt committed Jun 5, 2024
commit 8977497443d19b9416f6c1189929eca8ecf706ce
29 changes: 29 additions & 0 deletions lib/home/models/node_workload.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class NodeWorkload {
/// The current load status of the ingress node.
final double ingress;

/// The current load status of the worker node.
final double worker;

/// The current load status of the stateful node.
final double stateful;

/// The timestamp of the current load status data.
final DateTime timestamp;

NodeWorkload({required this.ingress, required this.worker, required this.stateful, required this.timestamp});

factory NodeWorkload.fromJson(Map<String, dynamic> json) => NodeWorkload(
ingress: json['ingress'],
worker: json['worker'],
stateful: json['stateful'],
timestamp: DateTime.fromMillisecondsSinceEpoch(json['timestamp']),
);

Map<String, dynamic> toJson() => {
'ingress': ingress,
'worker': worker,
'stateful': stateful,
'timestamp': timestamp.millisecondsSinceEpoch,
};
}
21 changes: 12 additions & 9 deletions lib/home/services/load.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:priobike/home/models/node_workload.dart';
import 'package:priobike/http.dart';
import 'package:priobike/logging/logger.dart';
import 'package:priobike/main.dart';
@@ -11,9 +12,6 @@ class LoadStatus with ChangeNotifier {
/// If the service is currently loading the status history.
bool isLoading = false;

/// The warning that should be displayed.
String? text;

/// If there exists a warning.
bool hasWarning = false;

@@ -31,10 +29,16 @@ class LoadStatus with ChangeNotifier {
final settings = getIt<Settings>();
final baseUrl = settings.backend.path;

final url = "https://$baseUrl/load-service/static/load_response.json";
final url = "https://$baseUrl/load-service/load.json";
final endpoint = Uri.parse(url);

final response = await Http.get(endpoint).timeout(const Duration(seconds: 4));
// FIXME Do we want basic auth here? Needs to be placed in auth service then.
const user = "";
const pw = "";
String basicAuth = 'Basic ${base64.encode(utf8.encode('$user:$pw'))}';

final response = await Http.get(endpoint, headers: <String, String>{'authorization': basicAuth})
.timeout(const Duration(seconds: 4));
if (response.statusCode != 200) {
isLoading = false;
notifyListeners();
@@ -44,12 +48,12 @@ class LoadStatus with ChangeNotifier {

final json = jsonDecode(response.body);

if (json["warning"]) {
final nodeWorkload = NodeWorkload.fromJson(json);

if (nodeWorkload.stateful > 0.8) {
hasWarning = true;
text = json["response_text"];
} else {
hasWarning = false;
text = null;
}

isLoading = false;
@@ -85,7 +89,6 @@ class LoadStatus with ChangeNotifier {
/// Reset the status.
Future<void> reset() async {
hasWarning = false;
text = null;
isLoading = false;
notifyListeners();
}
6 changes: 3 additions & 3 deletions lib/home/views/load_status.dart
Original file line number Diff line number Diff line change
@@ -30,8 +30,8 @@ class LoadStatusViewState extends State<LoadStatusView> {
context: context,
builder: (BuildContext context) {
return DialogLayout(
title: "Mehr Nutzende als normalerweise",
text: loadStatus.text ?? "",
title: "Hohe Auslastung",
text: "Das System seht zurzeit unter hoher Auslastung.",
actions: [
BigButtonTertiary(
label: "Schließen",
@@ -58,7 +58,7 @@ class LoadStatusViewState extends State<LoadStatusView> {
children: [
Flexible(
child: Content(
text: "Mehr Nutzende als normalerweise",
text: "Hohe Auslastung",
context: context,
),
),