Some license server improvements

This commit is contained in:
WolverinDEV
2020-02-06 13:49:39 +01:00
parent 14d9936d90
commit 3e39bcc38c
9 changed files with 322 additions and 250 deletions
+41 -51
View File
@@ -59,7 +59,7 @@ bool LicenseManager::setup(std::string& error) {
SET_VERSION(0);
case 0:
logMessage("Updating database! To version 1");
logMessage(LOG_GENERAL, "Updating database! To version 1");
CTBL("CREATE TABLE IF NOT EXISTS `history_speach` (`keyId` INT, `timestamp` BIGINT, `total` BIGINT, `dead` BIGINT, `online` BIGINT, `varianz` BIGINT)");
CTBL("CREATE TABLE IF NOT EXISTS `history_online` (`keyId` INT, `timestamp` BIGINT, `server` INT, `clients` INT, `web` INT, `music` INT, `queries` INT)");
@@ -116,7 +116,7 @@ bool LicenseManager::registerLicense(const std::string& key, const shared_ptr<Li
result res;
res = command(this->database, "INSERT INTO `license` (`key`, `type`, `deleted`, `issuer`) VALUES (:key, :type, :deleted, :issuer)", variable{":key", key}, variable{":type", (uint32_t) info->type}, variable{":deleted", false}, variable{":issuer", issuer}).execute();
if(!res) {
logError("Could not register new license (" + res.fmtStr() + ")");
logError(LOG_GENERAL, "Could not register new license (" + res.fmtStr() + ")");
return false;
}
auto keyId = this->id_cache->getKeyId(key);
@@ -133,7 +133,7 @@ bool LicenseManager::registerLicense(const std::string& key, const shared_ptr<Li
variable{":end", duration_cast<milliseconds>(info->end.time_since_epoch()).count()}
).execute();
if(!res) {
logError("Could not register new license info (" + res.fmtStr() + ")");
logError(LOG_GENERAL, "Could not register new license info (" + res.fmtStr() + ")");
return false;
}
return true;
@@ -145,13 +145,13 @@ bool LicenseManager::deleteLicense(const std::string& key, bool full) {
if(keyId == 0) return false; //Never exists
auto res = command(this->database, "DELETE FROM `license` WHERE `key` = :key", variable{":key", key}).execute();
if(!res) logError("Could not delete license (" + res.fmtStr() + ")");
if(!res) logError(LOG_GENERAL, "Could not delete license (" + res.fmtStr() + ")");
res = command(this->database, "DELETE FROM `license_info` WHERE `keyId` = :key", variable{":keyId", keyId}).execute();
if(!res) logError("Could not delete license (" + res.fmtStr() + ")");
if(!res) logError(LOG_GENERAL, "Could not delete license (" + res.fmtStr() + ")");
return !!res;
} else {
auto res = command(this->database, "UPDATE `license` SET `deleted` = :true WHERE `key` = :key", variable{":true", true}, variable{":key", key}).execute();
if(!res) logError("Could not delete license (" + res.fmtStr() + ")");
if(!res) logError(LOG_GENERAL, "Could not delete license (" + res.fmtStr() + ")");
return !!res;
}
}
@@ -162,7 +162,7 @@ bool LicenseManager::validLicenseKey(const std::string& key) {
*flag = true;
return 0;
}, &valid);
if(!res) logError("Could not validate license (" + res.fmtStr() + ")");
if(!res) logError(LOG_GENERAL, "Could not validate license (" + res.fmtStr() + ")");
return !!res && valid;
}
@@ -214,7 +214,7 @@ inline std::map<std::string, std::shared_ptr<LicenseInfo>> query_license(SqlMana
return 0;
}, &result);
logTrace(LOG_GENERAL, "Query returned {} results", result.size());
if(!res) logError("Could not query license (" + res.fmtStr() + ")");
if(!res) logError(LOG_GENERAL, "Could not query license (" + res.fmtStr() + ")");
return result;
}
@@ -246,7 +246,7 @@ bool LicenseManager::logRequest(const std::string& key, const std::string& uniqu
variable{":unique_id", unique_id},
variable{":result", state}).execute();
if(!res) {
logError("Could not log license validation (" + res.fmtStr() + ")");
logError(LOG_GENERAL, "Could not log license validation (" + res.fmtStr() + ")");
return false;
}
@@ -258,7 +258,7 @@ bool LicenseManager::logRequest(const std::string& key, const std::string& uniqu
variable{":version", version}
).execute();
if(!res)
logError("Could not log license version statistic (" + res.fmtStr() + ")");
logError(LOG_GENERAL, "Could not log license version statistic (" + res.fmtStr() + ")");
res = {};
}
return true;
@@ -284,7 +284,7 @@ bool LicenseManager::logStatistic(const std::string &key, const std::string& uni
variable{":query", data.queries_online()}
).execute();
if(!res)
logError("Could not log license statistics (" + res.fmtStr() + ")");
logError(LOG_GENERAL, "Could not log license statistics (" + res.fmtStr() + ")");
res = {};
}
//SELECT * FROM `license_info` WHERE `keyId` IN (SELECT `keyId` FROM `license` WHERE `key` = '000')
@@ -299,7 +299,7 @@ bool LicenseManager::logStatistic(const std::string &key, const std::string& uni
variable{":varianz", data.speach_varianz()}
).execute();
if(!res)
logError("Could not log license statistics (" + res.fmtStr() + ")");
logError(LOG_GENERAL, "Could not log license statistics (" + res.fmtStr() + ")");
res = {};
}
return true;
@@ -347,48 +347,38 @@ std::shared_ptr<LicenseManager::UserHistory> LicenseManager::list_statistics_use
have_key = false;
have_uid = false;
for(int index = 0; index < columns; index++) {
try {
if(names[index] == "keyId") {
key_id = stoull(values[index]);
have_key = true;
} else if(names[index] == "unique_id") {
unique_id = values[index];
have_uid = true;
} else {
continue;
}
if(have_key && have_uid)
break;
} catch (std::exception& ex) {
logError("Failed to parse column " + names[index] + " => " + ex.what() + " (Value: " + values[index] + ")");
return 0;
}
if(names[index] == "keyId") {
key_id = strtol(values[index].c_str(), nullptr, 10);
if(key_id == 0) return 0; /* invalid key id */
have_key = true;
if(have_key && have_uid) goto process_tag;
} else if(names[index] == "unique_id") {
unique_id = values[index];
have_uid = true;
if(have_key && have_uid) goto process_tag;
}
}
return 0; /* key or uid haven't been found */
if(!have_key || !have_uid) return 0; //TODO: May log a warning
if(key_id == 0) return 0;
process_tag:
stats_ptr = &server_statistics[to_string(key_id) + "_" + unique_id];
}
for(int index = 0; index < columns; index++) {
try {
if(names[index] == "timestamp")
temp_stats.timestamp = system_clock::time_point() + milliseconds(stoll(values[index]));
else if(names[index] == "server")
temp_stats.servers_online = stoull(values[index]);
else if(names[index] == "clients")
temp_stats.clients_online = stoull(values[index]);
else if(names[index] == "web")
temp_stats.web_clients_online = stoull(values[index]);
else if(names[index] == "music")
temp_stats.bots_online = stoull(values[index]);
else if(names[index] == "queries")
temp_stats.queries_online = stoull(values[index]);
} catch (std::exception& ex) {
logError("Failed to parse column " + names[index] + " => " + ex.what() + " (Value: " + values[index] + ")");
return 0;
}
if(names[index] == "timestamp")
temp_stats.timestamp = system_clock::time_point() + milliseconds(stoll(values[index]));
else if(names[index] == "server")
temp_stats.servers_online = strtol(values[index].c_str(), nullptr, 10);
else if(names[index] == "clients")
temp_stats.clients_online = strtol(values[index].c_str(), nullptr, 10);
else if(names[index] == "web")
temp_stats.web_clients_online = strtol(values[index].c_str(), nullptr, 10);
else if(names[index] == "music")
temp_stats.bots_online = strtol(values[index].c_str(), nullptr, 10);
else if(names[index] == "queries")
temp_stats.queries_online = strtol(values[index].c_str(), nullptr, 10);
}
/* because the query could only be oldest to newest */
@@ -420,7 +410,7 @@ std::shared_ptr<LicenseManager::UserHistory> LicenseManager::list_statistics_use
});
if(!state) {
logError("Could not read license statistics (" + state.fmtStr() + ")");
logError(LOG_GENERAL, "Could not read license statistics (" + state.fmtStr() + ")");
return {};
}
@@ -472,7 +462,7 @@ std::deque<std::unique_ptr<LicenseManager::GlobalVersionsStatistic>> LicenseMana
else if(names[index] == "version")
info->versions[values[index]] = 1;
} catch (std::exception& ex) {
logError("Failed to parse column " + names[index] + " => " + ex.what() + " (Value: " + values[index] + ")");
logError(LOG_GENERAL, "Failed to parse column " + names[index] + " => " + ex.what() + " (Value: " + values[index] + ")");
return 0;
}
}
@@ -483,7 +473,7 @@ std::deque<std::unique_ptr<LicenseManager::GlobalVersionsStatistic>> LicenseMana
});
if(!state) {
logError("Could not read license statistics (" + state.fmtStr() + ")");
logError(LOG_GENERAL, "Could not read license statistics (" + state.fmtStr() + ")");
return {};
}