From 18cdc9418356776ca6f9964ffe9b7894271fa85a Mon Sep 17 00:00:00 2001 From: "Leon Etienne (ubuntu wsl)" Date: Mon, 28 Sep 2020 15:32:22 +0200 Subject: [PATCH] Fixed bullshittery in LogHistory --- Tubio/DownloadManager.cpp | 8 +- Tubio/Framework.cpp | 2 + Tubio/LogHistory.cpp | 44 ++++++++--- Tubio/LogHistory.h | 1 + Tubio_rest.postman_collection.json | 113 +++++++++++++++++------------ 5 files changed, 104 insertions(+), 64 deletions(-) diff --git a/Tubio/DownloadManager.cpp b/Tubio/DownloadManager.cpp index b6f63b1..9dadffb 100644 --- a/Tubio/DownloadManager.cpp +++ b/Tubio/DownloadManager.cpp @@ -271,16 +271,16 @@ JsonArray DownloadManager::GetAlltimeCacheAsJson(time_t max_age, std::size_t max JsonArray cutArr; // If max_num is -1 (would mean inifnite) it would underflow to size_t::max - for (std::size_t i = 0; ((i < arr.Size()) && (i < max_num)); i++) + for (std::size_t i = 0; ((i < arr.Size()) && (cutArr.Size() < max_num)); i++) { - // If max_age is > 0, we have to check against the max age - if (max_age > 0) + // If max_age is >= 0, we have to check against the max age + if (max_age >= 0) { if (arr[i].AsJson.DoesExist("queued_timestamp")) { if (arr[i]["queued_timestamp"].GetDataType() == JDType::INT) { - if ((time(0) - arr[i]["queued_timestamp"].AsInt) < max_age) + if ((time(0) - arr[i]["queued_timestamp"].AsInt) <= max_age) { cutArr += arr[i]; } diff --git a/Tubio/Framework.cpp b/Tubio/Framework.cpp index bf434d8..256b4f0 100644 --- a/Tubio/Framework.cpp +++ b/Tubio/Framework.cpp @@ -14,6 +14,8 @@ Framework::Framework() log->cout << "Starting Tubio server..."; log->Flush(); + LogHistory::Init(); + httpServer = new HttpServer(); PostInit(); diff --git a/Tubio/LogHistory.cpp b/Tubio/LogHistory.cpp index 0965e91..98fd1ad 100644 --- a/Tubio/LogHistory.cpp +++ b/Tubio/LogHistory.cpp @@ -7,11 +7,16 @@ void LogHistory::PreInit() history = new std::vector(); lastSave = time(0); // now didHistoryChangeSinceLastSave = false; - LoadSaveFileCache(); return; } +void LogHistory::Init() +{ + LoadSaveFileCache(); + return; +} + void LogHistory::PostExit() { Save(); @@ -31,6 +36,7 @@ void LogHistory::PostExit() void LogHistory::Update() { + XGConfig::logging.autosave_interval = 20; if ((time(0) - lastSave > XGConfig::logging.autosave_interval) && (didHistoryChangeSinceLastSave)) { // Mutex gets reset in Save(); @@ -61,12 +67,19 @@ void LogHistory::Save() { LoadSaveFileCache(); JasonPP::Json savefile; + saveFileCache.Merge(newJsonLogs.AsArray); savefile.SetArrayData(saveFileCache); - savefile.AsArray.Merge(newJsonLogs.AsArray); ofs.open(XGConfig::logging.logfile_json); ofs << savefile.Render(); ofs.close(); - + + // Clear loghistory vector + for (std::size_t i = 0; i < history->size(); i++) + { + delete history->at(i); + history->at(i) = nullptr; + } + history->clear(); } lastSave = time(0); @@ -108,29 +121,33 @@ void LogHistory::LoadSaveFileCache() JasonPP::JsonArray LogHistory::GetCompleteLogHistoryAsJson(time_t max_age, std::size_t max_num) { - JasonPP::JsonArray arr - ; + JasonPP::JsonArray arr; for (std::size_t i = 0; i < history->size(); i++) { arr += history->at(i)->GetAsJson(); } - arr.Merge(saveFileCache); - arr.Sort("timestamp", JasonPP::JSON_ARRAY_SORT_MODE::NUM_DESC); - if ((max_age == -1) && (max_num == (std::size_t)-1)) return arr; + arr.Merge(saveFileCache); + + if ((max_age == -1) && (max_num == (std::size_t) - 1)) + { + arr.Sort("timestamp", JasonPP::JSON_ARRAY_SORT_MODE::NUM_DESC); + return arr; + } JasonPP::JsonArray cutArr; - for (std::size_t i = 0; ((i < arr.Size()) && (i < max_num)); i++) + // If max_num is -1 (would mean inifnite) it would underflow to size_t::max + for (std::size_t i = 0; ((i < arr.Size()) && (cutArr.Size() < max_num)); i++) { - // If max_age is > 0, we have to check against the max age - if (max_age > 0) + // If max_age is >= 0, we have to check against the max age + if (max_age >= 0) { if (arr[i].AsJson.DoesExist("timestamp")) { if (arr[i]["timestamp"].GetDataType() == JasonPP::JDType::INT) { - if ((time(0) - arr[i]["timestamp"].AsInt) < max_age) + if ((time(0) - arr[i]["timestamp"].AsInt) <= max_age) { cutArr += arr[i]; } @@ -142,6 +159,7 @@ JasonPP::JsonArray LogHistory::GetCompleteLogHistoryAsJson(time_t max_age, std:: cutArr += arr[i]; } } + cutArr.Sort("timestamp", JasonPP::JSON_ARRAY_SORT_MODE::NUM_DESC); return cutArr; } @@ -149,6 +167,8 @@ bool LogHistory::ClearLogHistory() { FileSystem::Delete(XGConfig::logging.logfile_json); FileSystem::Delete(XGConfig::logging.logfile_text); + saveFileCache.Clear(); + history->clear(); Save(); diff --git a/Tubio/LogHistory.h b/Tubio/LogHistory.h index 670ff0a..e0fdcf6 100644 --- a/Tubio/LogHistory.h +++ b/Tubio/LogHistory.h @@ -29,6 +29,7 @@ namespace Logging { public: static void PreInit(); + static void Init(); static void PostExit(); static void Update(); diff --git a/Tubio_rest.postman_collection.json b/Tubio_rest.postman_collection.json index c931053..f0e0ea6 100644 --- a/Tubio_rest.postman_collection.json +++ b/Tubio_rest.postman_collection.json @@ -39,7 +39,7 @@ "header": [], "body": { "mode": "raw", - "raw": "{\r\n \"request\": \"queue_download\",\r\n \"video_url\": \"https://de.pornhub.com/view_video.php?viewkey=ph5f4e4a744bcc2\",\r\n \"mode\": \"video\"\r\n}", + "raw": "{\r\n \"request\": \"queue_download\",\r\n \"video_url\": \"https://youtu.be/OF-thWTJcu0\",\r\n \"mode\": \"video\"\r\n}", "options": { "raw": { "language": "json" @@ -66,7 +66,7 @@ "header": [], "body": { "mode": "raw", - "raw": "{\r\n \"request\": \"queue_download\",\r\n \"video_url\": \"https://youtu.be/eJsTaFRsa_k\",\r\n \"mode\": \"audio\"\r\n}", + "raw": "{\r\n \"request\": \"queue_download\",\r\n \"video_url\": \"https://youtu.be/wgfNsek8xkc\",\r\n \"mode\": \"audio\"\r\n}", "options": { "raw": { "language": "json" @@ -93,7 +93,7 @@ "header": [], "body": { "mode": "raw", - "raw": "{\r\n \"request\": \"fetch_session_cache\",\r\n \"max_age\": -1,\r\n \"max_num:\": 5\r\n}", + "raw": "{\r\n \"request\": \"fetch_session_cache\",\r\n \"max_age\": -1,\r\n \"max_num\": -1\r\n}", "options": { "raw": { "language": "json" @@ -114,13 +114,13 @@ "response": [] }, { - "name": "FetAlltimeCache", + "name": "FetchAlltimeCache", "request": { "method": "POST", "header": [], "body": { "mode": "raw", - "raw": "{\r\n \"request\": \"fetch_session_cache\"\r\n}", + "raw": "{\r\n \"request\": \"fetch_alltime_cache\"\r\n}", "options": { "raw": { "language": "json" @@ -174,47 +174,7 @@ "header": [], "body": { "mode": "raw", - "raw": "{\r\n \"request\": \"free_console\"\r\n}", - "options": { - "raw": { - "language": "json" - } - } - }, - "url": { - "raw": "" - } - }, - "response": [] - }, - { - "name": "ShowConsole", - "request": { - "method": "POST", - "header": [], - "body": { - "mode": "raw", - "raw": "{\r\n \"request\": \"free_console\"\r\n}", - "options": { - "raw": { - "language": "json" - } - } - }, - "url": { - "raw": "" - } - }, - "response": [] - }, - { - "name": "get_server_os_name", - "request": { - "method": "POST", - "header": [], - "body": { - "mode": "raw", - "raw": "{\r\n \"request\": \"get_os\"\r\n}", + "raw": "{\r\n \"request\": \"hide_console\"\r\n}", "options": { "raw": { "language": "json" @@ -237,6 +197,63 @@ }, "response": [] }, + { + "name": "ShowConsole", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"request\": \"show_console\"\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "192.168.1.12:6969/api", + "host": [ + "192", + "168", + "1", + "12" + ], + "port": "6969", + "path": [ + "api" + ] + } + }, + "response": [] + }, + { + "name": "get_server_os_name", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"request\": \"get_os_name\"\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:6969/api", + "host": [ + "localhost" + ], + "port": "6969", + "path": [ + "api" + ] + } + }, + "response": [] + }, { "name": "FetchSessionLogs", "request": { @@ -244,7 +261,7 @@ "header": [], "body": { "mode": "raw", - "raw": "{\r\n \"request\": \"fetch_logs\"\r\n \"max_age\": -1,\r\n \"max_num\": -1\r\n}", + "raw": "{\r\n \"request\": \"fetch_session_logs\"\r\n}", "options": { "raw": { "language": "json" @@ -271,7 +288,7 @@ "header": [], "body": { "mode": "raw", - "raw": "{\r\n \"request\": \"fetch_logs\"\r\n \"max_age\": -1,\r\n \"max_num\": -1\r\n}", + "raw": "{\r\n \"request\": \"fetch_alltime_logs\",\r\n \"max_age\": -1,\r\n \"max_num\": -1\r\n}", "options": { "raw": { "language": "json"