Fixed bullshittery in LogHistory

This commit is contained in:
Leon Etienne (ubuntu wsl) 2020-09-28 15:32:22 +02:00
parent 3caf06b5e1
commit 18cdc94183
5 changed files with 104 additions and 64 deletions

View File

@ -271,16 +271,16 @@ JsonArray DownloadManager::GetAlltimeCacheAsJson(time_t max_age, std::size_t max
JsonArray cutArr; JsonArray cutArr;
// If max_num is -1 (would mean inifnite) it would underflow to size_t::max // 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 is >= 0, we have to check against the max age
if (max_age > 0) if (max_age >= 0)
{ {
if (arr[i].AsJson.DoesExist("queued_timestamp")) if (arr[i].AsJson.DoesExist("queued_timestamp"))
{ {
if (arr[i]["queued_timestamp"].GetDataType() == JDType::INT) 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]; cutArr += arr[i];
} }

View File

@ -14,6 +14,8 @@ Framework::Framework()
log->cout << "Starting Tubio server..."; log->cout << "Starting Tubio server...";
log->Flush(); log->Flush();
LogHistory::Init();
httpServer = new HttpServer(); httpServer = new HttpServer();
PostInit(); PostInit();

View File

@ -7,11 +7,16 @@ void LogHistory::PreInit()
history = new std::vector<LogEntry*>(); history = new std::vector<LogEntry*>();
lastSave = time(0); // now lastSave = time(0); // now
didHistoryChangeSinceLastSave = false; didHistoryChangeSinceLastSave = false;
LoadSaveFileCache();
return; return;
} }
void LogHistory::Init()
{
LoadSaveFileCache();
return;
}
void LogHistory::PostExit() void LogHistory::PostExit()
{ {
Save(); Save();
@ -31,6 +36,7 @@ void LogHistory::PostExit()
void LogHistory::Update() void LogHistory::Update()
{ {
XGConfig::logging.autosave_interval = 20;
if ((time(0) - lastSave > XGConfig::logging.autosave_interval) && (didHistoryChangeSinceLastSave)) if ((time(0) - lastSave > XGConfig::logging.autosave_interval) && (didHistoryChangeSinceLastSave))
{ {
// Mutex gets reset in Save(); // Mutex gets reset in Save();
@ -61,12 +67,19 @@ void LogHistory::Save()
{ {
LoadSaveFileCache(); LoadSaveFileCache();
JasonPP::Json savefile; JasonPP::Json savefile;
saveFileCache.Merge(newJsonLogs.AsArray);
savefile.SetArrayData(saveFileCache); savefile.SetArrayData(saveFileCache);
savefile.AsArray.Merge(newJsonLogs.AsArray);
ofs.open(XGConfig::logging.logfile_json); ofs.open(XGConfig::logging.logfile_json);
ofs << savefile.Render(); ofs << savefile.Render();
ofs.close(); 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); 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 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++) for (std::size_t i = 0; i < history->size(); i++)
{ {
arr += history->at(i)->GetAsJson(); 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; 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 is >= 0, we have to check against the max age
if (max_age > 0) if (max_age >= 0)
{ {
if (arr[i].AsJson.DoesExist("timestamp")) if (arr[i].AsJson.DoesExist("timestamp"))
{ {
if (arr[i]["timestamp"].GetDataType() == JasonPP::JDType::INT) 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]; cutArr += arr[i];
} }
@ -142,6 +159,7 @@ JasonPP::JsonArray LogHistory::GetCompleteLogHistoryAsJson(time_t max_age, std::
cutArr += arr[i]; cutArr += arr[i];
} }
} }
cutArr.Sort("timestamp", JasonPP::JSON_ARRAY_SORT_MODE::NUM_DESC);
return cutArr; return cutArr;
} }
@ -149,6 +167,8 @@ bool LogHistory::ClearLogHistory()
{ {
FileSystem::Delete(XGConfig::logging.logfile_json); FileSystem::Delete(XGConfig::logging.logfile_json);
FileSystem::Delete(XGConfig::logging.logfile_text); FileSystem::Delete(XGConfig::logging.logfile_text);
saveFileCache.Clear();
history->clear();
Save(); Save();

View File

@ -29,6 +29,7 @@ namespace Logging
{ {
public: public:
static void PreInit(); static void PreInit();
static void Init();
static void PostExit(); static void PostExit();
static void Update(); static void Update();

View File

@ -39,7 +39,7 @@
"header": [], "header": [],
"body": { "body": {
"mode": "raw", "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": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
@ -66,7 +66,7 @@
"header": [], "header": [],
"body": { "body": {
"mode": "raw", "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": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
@ -93,7 +93,7 @@
"header": [], "header": [],
"body": { "body": {
"mode": "raw", "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": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
@ -114,13 +114,13 @@
"response": [] "response": []
}, },
{ {
"name": "FetAlltimeCache", "name": "FetchAlltimeCache",
"request": { "request": {
"method": "POST", "method": "POST",
"header": [], "header": [],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\r\n \"request\": \"fetch_session_cache\"\r\n}", "raw": "{\r\n \"request\": \"fetch_alltime_cache\"\r\n}",
"options": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
@ -174,47 +174,7 @@
"header": [], "header": [],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\r\n \"request\": \"free_console\"\r\n}", "raw": "{\r\n \"request\": \"hide_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}",
"options": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
@ -237,6 +197,63 @@
}, },
"response": [] "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", "name": "FetchSessionLogs",
"request": { "request": {
@ -244,7 +261,7 @@
"header": [], "header": [],
"body": { "body": {
"mode": "raw", "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": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
@ -271,7 +288,7 @@
"header": [], "header": [],
"body": { "body": {
"mode": "raw", "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": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"