import 'package:oc_front/models/abstract.dart'; import 'package:json_string/json_string.dart'; class LogResults extends SerializerDeserializer { String? status; LogsResult? data; LogResults({ this.status, this.data, }); String getID() { return ""; } @override deserialize(dynamic json) { try { json = json as Map; } catch (e) { return LogResults(); } return LogResults( status: json.containsKey("status") ? json["status"] : "", data: json.containsKey("data") ? LogsResult().deserialize(json["data"]) : null, ); } @override Map serialize() { return { }; } } class LogsResult extends SerializerDeserializer { List result; LogsResult({ this.result = const [], }); String getID() { return ""; } @override deserialize(dynamic json) { try { json = json as Map; } catch (e) { return LogsResult(); } return LogsResult( result: json.containsKey("result") ? fromListJson(json["result"], Logs()) : [], ); } @override Map serialize() { return { }; } } class Logs extends SerializerDeserializer { String? level; List logs = []; Logs({ this.level, }); String getID() { return ""; } @override deserialize(dynamic json) { try { json = json as Map; } catch (e) { return Logs(); } return Logs( level: json.containsKey("stream") && (json["stream"] as Map).containsKey("level") ? json["stream"]["level"] : "", ); } @override Map serialize() { return { }; } } class Log extends SerializerDeserializer { DateTime? timestamp; String? message; String? level; Map 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; 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; } 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 serialize() { return { }; } }