oc-front/lib/models/logs.dart
2024-08-08 08:42:32 +02:00

116 lines
3.1 KiB
Dart

import 'package:oc_front/models/abstract.dart';
import 'package:json_string/json_string.dart';
class LogResults extends SerializerDeserializer<LogResults> {
String? status;
LogsResult? data;
LogResults({
this.status,
this.data,
});
String getID() {
return "";
}
@override deserialize(dynamic json) {
try { json = json as Map<String, dynamic>;
} catch (e) { return LogResults(); }
return LogResults(
status: json.containsKey("status") ? json["status"] : "",
data: json.containsKey("data") ? LogsResult().deserialize(json["data"]) : null,
);
}
@override Map<String, dynamic> serialize() {
return { };
}
}
class LogsResult extends SerializerDeserializer<LogsResult> {
List<Logs> result;
LogsResult({
this.result = const [],
});
String getID() {
return "";
}
@override deserialize(dynamic json) {
try { json = json as Map<String, dynamic>;
} catch (e) { return LogsResult(); }
return LogsResult(
result: json.containsKey("result") ? fromListJson(json["result"], Logs()) : [],
);
}
@override Map<String, dynamic> serialize() {
return { };
}
}
class Logs extends SerializerDeserializer<Logs> {
String? level;
List<Log> logs = [];
Logs({
this.level,
});
String getID() {
return "";
}
@override deserialize(dynamic json) {
try { json = json as Map<String, dynamic>;
} catch (e) { return Logs(); }
return Logs(
level: json.containsKey("stream") && (json["stream"] as Map<String, dynamic>).containsKey("level") ? json["stream"]["level"] : "",
);
}
@override Map<String, dynamic> serialize() {
return { };
}
}
class Log extends SerializerDeserializer<Log> {
DateTime? timestamp;
String? message;
String? level;
Map<String, dynamic> map = {};
Log({
this.timestamp,
this.message,
this.level
});
String getID() {
return "";
}
String getMessage(String mess) {
var jsonString = mess;
try {
var j = JsonString(mess.replaceAll("\\", "")).decodedValue as Map<String, dynamic>;
map = j;
if (j["Status"] == "Pending") {
jsonString = "${j["Name"]} : [${j["Namespace"]}] Status: ${j["Status"]}... \nCreated at ${j["Created"].toString().replaceAllMapped(RegExp(r'\(\w+\)'), (match) { return ''; }).replaceAllMapped(RegExp(r'\+\w+'), (match) { return ''; })}";
} else {
jsonString = "${j["Name"]} : [${j["Namespace"]}] ${j["Status"]} ${j["Progress"]} (${j["Duration"].toString().replaceAll("seconds", "s")})\nStarted at ${j["Created"].toString().replaceAllMapped(RegExp(r'\(\w+\)'), (match) { return ''; }).replaceAllMapped(RegExp(r'\+\w+'), (match) { return ''; })}";
}
} on JsonFormatException catch (e) { /* */ }
message = jsonString;
return jsonString;
}
@override deserialize(dynamic json) {
try { json = json as List<dynamic>;
} catch (e) { return Log(); }
return Log(
timestamp: json.isNotEmpty ? DateTime.parse(json[0]) : null,
message: json.length > 1 ? getMessage(json[1].toString()) : null,
);
}
@override Map<String, dynamic> serialize() { return { }; }
}