mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-02 10:53:44 +00:00
Staff can make models again with other processing options than the UGC
settings' (ray backend, hidden-face method, denoising) and every make records
what made it, for comparing the options.
- migration mysql 99 / sqlite 82 (ugc_process_options): ugc.process_options
(picked for the next make, cleared once made), ugc.made_options (what made
the current files) and ugc_process_runs (every successful make: options,
wall and CPU time, hidden faces', occlusion's and icon's time, bricks,
triangles before and after)
- IUgc: ResetUgcModelProcessing and ResetPropertyUgcModelProcessing take the
options; PendingModel carries them; RecordUgcModelRun, GetUgcRunSummaries;
list entries have madeOptions and processOptions
- the UGC server applies a job's options over its settings and records the run
- /api/ugc/reprocess takes options ("embree fast oidn"); /api/ugc/options
lists the choices, the settings' defaults and averages per combination
- /reprocessproperty [builtin|embree|hiprt] [toolbox|fast] [off|oidn], any
order, all optional
Check: run the migration on MySQL and SQLite; /reprocessproperty embree fast
on a property, then the models' made_options and ugc_process_runs rows;
/api/ugc/options.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
253 lines
14 KiB
C++
253 lines
14 KiB
C++
#include "SQLiteDatabase.h"
|
|
|
|
#include <chrono>
|
|
#include <cmath>
|
|
|
|
namespace {
|
|
IUgc::ProcessInfo ReadUgcProcessInfo(CppSQLite3Query& result, bool modular) {
|
|
IUgc::ProcessInfo info;
|
|
info.id = result.getInt64Field("id");
|
|
info.characterId = result.getInt64Field("character_id");
|
|
info.characterName = result.getStringField("character_name", "");
|
|
info.state = static_cast<IUgc::eProcessState>(result.getIntField("is_optimized"));
|
|
info.attempts = static_cast<uint32_t>(result.getIntField("process_attempts"));
|
|
info.processedAt = result.getInt64Field("processed_at");
|
|
info.error = result.getStringField("process_error", "");
|
|
if (modular) info.details = result.getStringField("ldf_config", "");
|
|
else info.processAfter = result.getInt64Field("process_after");
|
|
if (!modular) info.bakeAo = result.getIntField("bake_ao") != 0;
|
|
return info;
|
|
}
|
|
|
|
int64_t UnixNow() {
|
|
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
|
}
|
|
}
|
|
|
|
IUgc::Model ReadModel(CppSQLite3Query& result) {
|
|
IUgc::Model model;
|
|
|
|
int blobSize{};
|
|
const auto* blob = result.getBlobField("lxfml", blobSize);
|
|
model.lxfmlData << std::string(reinterpret_cast<const char*>(blob), blobSize);
|
|
model.id = result.getInt64Field("ugcID");
|
|
model.modelID = result.getInt64Field("modelID");
|
|
|
|
return model;
|
|
}
|
|
|
|
std::vector<IUgc::Model> SQLiteDatabase::GetUgcModels(const LWOOBJID& propertyId) {
|
|
auto [_, result] = ExecuteSelect(
|
|
"SELECT lxfml, u.id AS ugcID, pc.id AS modelID FROM ugc AS u JOIN properties_contents AS pc ON u.id = pc.ugc_id WHERE lot = 14 AND property_id = ? AND pc.ugc_id IS NOT NULL;",
|
|
propertyId);
|
|
|
|
std::vector<IUgc::Model> toReturn;
|
|
|
|
while (!result.eof()) {
|
|
toReturn.push_back(ReadModel(result));
|
|
result.nextRow();
|
|
}
|
|
|
|
return toReturn;
|
|
}
|
|
|
|
std::vector<IUgc::Model> SQLiteDatabase::GetAllUgcModels() {
|
|
auto [_, result] = ExecuteSelect("SELECT u.id AS ugcID, pc.id AS modelID, lxfml FROM ugc AS u JOIN properties_contents AS pc ON pc.ugc_id = u.id WHERE pc.lot = 14 AND pc.ugc_id IS NOT NULL;");
|
|
|
|
std::vector<IUgc::Model> models;
|
|
while (!result.eof()) {
|
|
models.push_back(ReadModel(result));
|
|
result.nextRow();
|
|
}
|
|
|
|
return models;
|
|
}
|
|
|
|
void SQLiteDatabase::RemoveUnreferencedUgcModels() {
|
|
ExecuteDelete("DELETE FROM ugc WHERE id NOT IN (SELECT ugc_id FROM properties_contents WHERE ugc_id IS NOT NULL);");
|
|
}
|
|
|
|
void SQLiteDatabase::InsertNewUgcModel(
|
|
std::stringstream& sd0Data, // cant be const sad
|
|
const uint64_t blueprintId,
|
|
const uint32_t accountId,
|
|
const LWOOBJID characterId,
|
|
const int64_t processAfter) {
|
|
const std::istream stream(sd0Data.rdbuf());
|
|
ExecuteInsert(
|
|
"INSERT INTO `ugc`(`id`, `account_id`, `character_id`, `is_optimized`, `lxfml`, `bake_ao`, `filename`, `process_after`) VALUES (?,?,?,?,?,?,?,?)",
|
|
blueprintId,
|
|
accountId,
|
|
characterId,
|
|
0,
|
|
&stream,
|
|
false,
|
|
"weedeater.lxfml",
|
|
processAfter
|
|
);
|
|
// The owner is still building: their other models waiting for their quiet period wait longer
|
|
if (processAfter > 0) ExecuteUpdate("UPDATE ugc SET process_after = ? WHERE character_id = ? AND is_optimized = 0 AND process_after > 0 AND process_after < ?;", processAfter, characterId, processAfter);
|
|
}
|
|
|
|
void SQLiteDatabase::DeleteUgcModelData(const LWOOBJID& modelId) {
|
|
ExecuteDelete("DELETE FROM ugc WHERE id = ?;", modelId);
|
|
ExecuteDelete("DELETE FROM properties_contents WHERE ugc_id = ?;", modelId);
|
|
}
|
|
|
|
void SQLiteDatabase::UpdateUgcModelData(const LWOOBJID& modelId, std::stringstream& lxfml, const int64_t processAfter) {
|
|
const std::istream stream(lxfml.rdbuf());
|
|
// The UGC server makes the model's files again
|
|
ExecuteUpdate("UPDATE ugc SET lxfml = ?, is_optimized = 0, process_attempts = 0, process_error = '', process_after = ? WHERE id = ?;", &stream, processAfter, modelId);
|
|
}
|
|
|
|
std::optional<IUgc::Model> SQLiteDatabase::GetUgcModel(const LWOOBJID ugcId) {
|
|
// LEFT JOIN: a model that is in someone's inventory rather than placed on a property still exists
|
|
auto [_, result] = ExecuteSelect("SELECT u.id AS ugcID, pc.id AS modelID, lxfml FROM ugc AS u LEFT JOIN properties_contents AS pc ON pc.ugc_id = u.id WHERE u.id = ? LIMIT 1;", ugcId);
|
|
|
|
std::optional<IUgc::Model> toReturn = std::nullopt;
|
|
if (!result.eof()) {
|
|
toReturn = ReadModel(result);
|
|
}
|
|
|
|
return toReturn;
|
|
}
|
|
|
|
std::vector<IUgc::PendingModel> SQLiteDatabase::GetUgcModelsToProcess(const uint32_t limit, const bool priorityOnly) {
|
|
auto [_, result] = ExecuteSelect("SELECT id, lxfml, process_attempts, priority, process_options FROM ugc WHERE is_optimized = 0 AND process_after <= ? AND (? = 0 OR priority = 1) "
|
|
"ORDER BY priority DESC, process_attempts ASC, id DESC LIMIT ?;", UnixNow(), priorityOnly ? 1 : 0, limit);
|
|
std::vector<IUgc::PendingModel> models;
|
|
while (!result.eof()) {
|
|
auto& model = models.emplace_back();
|
|
model.id = result.getInt64Field("id");
|
|
int blobSize{};
|
|
const auto* blob = result.getBlobField("lxfml", blobSize);
|
|
model.lxfml.assign(reinterpret_cast<const char*>(blob), blobSize);
|
|
model.attempts = static_cast<uint32_t>(result.getIntField("process_attempts"));
|
|
model.priority = result.getIntField("priority") != 0;
|
|
model.options = result.getStringField("process_options", "");
|
|
result.nextRow();
|
|
}
|
|
return models;
|
|
}
|
|
|
|
void SQLiteDatabase::SetUgcModelProcessed(const LWOOBJID id, const eProcessState state, const uint32_t attempts, const std::string_view error, const bool bakeAo) {
|
|
ExecuteUpdate("UPDATE ugc SET is_optimized = ?, process_attempts = ?, process_error = ?, bake_ao = ?, processed_at = ?, priority = CASE WHEN ? = 0 THEN priority ELSE 0 END, "
|
|
"process_options = CASE WHEN ? = 0 THEN process_options ELSE '' END WHERE id = ?;",
|
|
static_cast<int32_t>(state), attempts, error, bakeAo, UnixNow(), static_cast<int32_t>(state), static_cast<int32_t>(state), id);
|
|
}
|
|
|
|
std::optional<IUgc::ProcessInfo> SQLiteDatabase::GetUgcProcessInfo(const LWOOBJID id) {
|
|
auto [_, result] = ExecuteSelect(
|
|
"SELECT u.id, u.character_id, c.name AS character_name, u.is_optimized, u.process_attempts, u.processed_at, u.process_error, u.bake_ao, u.process_after "
|
|
"FROM ugc AS u LEFT JOIN charinfo AS c ON c.id = u.character_id WHERE u.id = ? LIMIT 1;", id);
|
|
if (result.eof()) return std::nullopt;
|
|
return ReadUgcProcessInfo(result, false);
|
|
}
|
|
|
|
uint64_t SQLiteDatabase::ResetUgcModelProcessing(const std::optional<LWOOBJID> id, const bool failedOnly, const std::string_view options) {
|
|
if (id) return ExecuteUpdate("UPDATE ugc SET is_optimized = 0, process_attempts = 0, process_error = '', process_after = 0, process_options = ? WHERE id = ?;", options, *id);
|
|
if (failedOnly) return ExecuteUpdate("UPDATE ugc SET is_optimized = 0, process_attempts = 0, process_error = '', process_after = 0, process_options = ? WHERE is_optimized = 2;", options);
|
|
return ExecuteUpdate("UPDATE ugc SET is_optimized = 0, process_attempts = 0, process_error = '', process_after = 0, process_options = ?;", options);
|
|
}
|
|
|
|
IUgc::ProcessTotals SQLiteDatabase::GetUgcProcessTotals(const bool modular) {
|
|
const std::string sql = modular
|
|
? "SELECT COUNT(*) AS made, SUM(CASE WHEN process_ms > 0 THEN 1 ELSE 0 END) AS timed, COALESCE(SUM(process_ms), 0) AS ms, COALESCE(SUM(process_cpu_ms), 0) AS cpu, "
|
|
"COALESCE(MAX(process_ms), 0) AS maxms, COALESCE(AVG(NULLIF(process_memory_kb, 0)), 0) AS memavg, COALESCE(MAX(process_memory_kb), 0) AS memmax, "
|
|
"0 AS bricks, 0 AS tris, 0 AS before_tris, 0 AS after_tris FROM ugc_modular_build WHERE is_optimized = 1;"
|
|
: "SELECT COUNT(*) AS made, SUM(CASE WHEN process_ms > 0 THEN 1 ELSE 0 END) AS timed, COALESCE(SUM(process_ms), 0) AS ms, COALESCE(SUM(process_cpu_ms), 0) AS cpu, "
|
|
"COALESCE(MAX(process_ms), 0) AS maxms, COALESCE(AVG(NULLIF(process_memory_kb, 0)), 0) AS memavg, COALESCE(MAX(process_memory_kb), 0) AS memmax, "
|
|
"COALESCE(SUM(brick_count), 0) AS bricks, COALESCE(SUM(triangle_count), 0) AS tris, "
|
|
"COALESCE(SUM(CASE WHEN triangle_count_before > 0 THEN triangle_count_before ELSE 0 END), 0) AS before_tris, "
|
|
"COALESCE(SUM(CASE WHEN triangle_count_before > 0 THEN triangle_count ELSE 0 END), 0) AS after_tris FROM ugc WHERE is_optimized = 1;";
|
|
auto [_, result] = ExecuteSelect(sql);
|
|
IUgc::ProcessTotals totals;
|
|
if (result.eof()) return totals;
|
|
const auto field = [&](const char* name) { return static_cast<uint64_t>(std::llround(result.getFloatField(name, 0.0))); };
|
|
totals = { field("made"), field("timed"), field("ms"), field("cpu"), field("maxms"), field("memavg"), field("memmax"), field("bricks"), field("tris"), field("before_tris"), field("after_tris") };
|
|
return totals;
|
|
}
|
|
|
|
uint64_t SQLiteDatabase::ResetPropertyUgcModelProcessing(const LWOOBJID propertyId, const std::string_view options) {
|
|
return ExecuteUpdate("UPDATE ugc SET is_optimized = 0, process_attempts = 0, process_error = '', process_after = 0, priority = 1, process_options = ? "
|
|
"WHERE id IN (SELECT ugc_id FROM properties_contents WHERE property_id = ? AND ugc_id IS NOT NULL);", options, propertyId);
|
|
}
|
|
|
|
void SQLiteDatabase::RecordUgcModelRun(const ProcessRun& run) {
|
|
ExecuteInsert("INSERT INTO ugc_process_runs (ugc_id, options, made_at, process_ms, process_cpu_ms, hsr_ms, ao_ms, icon_ms, bricks, triangles_before, triangles) "
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", run.id, run.options, UnixNow(), run.milliseconds, run.cpuMilliseconds, run.hsrMilliseconds,
|
|
run.aoMilliseconds, run.iconMilliseconds, run.bricks, run.trianglesBefore, run.triangles);
|
|
ExecuteUpdate("UPDATE ugc SET made_options = ? WHERE id = ?;", run.options, run.id);
|
|
}
|
|
|
|
std::vector<IUgc::RunSummary> SQLiteDatabase::GetUgcRunSummaries() {
|
|
auto [_, result] = ExecuteSelect("SELECT options, COUNT(*) AS runs, COUNT(DISTINCT ugc_id) AS models, SUM(process_ms) AS ms, SUM(process_cpu_ms) AS cpu, "
|
|
"SUM(hsr_ms) AS hsr, SUM(ao_ms) AS ao, SUM(icon_ms) AS icon, SUM(bricks) AS bricks, SUM(triangles_before) AS before_tris, SUM(triangles) AS tris "
|
|
"FROM ugc_process_runs GROUP BY options ORDER BY runs DESC, options;");
|
|
std::vector<RunSummary> summaries;
|
|
while (!result.eof()) {
|
|
const auto field = [&](const char* name) { return static_cast<uint64_t>(std::llround(result.getFloatField(name, 0.0))); };
|
|
summaries.push_back({ result.getStringField("options", ""), field("runs"), field("models"), field("ms"), field("cpu"), field("hsr"), field("ao"),
|
|
field("icon"), field("bricks"), field("before_tris"), field("tris") });
|
|
result.nextRow();
|
|
}
|
|
return summaries;
|
|
}
|
|
|
|
std::vector<IUgc::ProcessInfo> SQLiteDatabase::GetUgcProcessList(const std::optional<eProcessState> state, const std::string_view search, const uint32_t offset, const uint32_t limit) {
|
|
const std::string select =
|
|
"SELECT u.id, u.character_id, c.name AS character_name, u.is_optimized, u.process_attempts, u.processed_at, u.process_error, u.bake_ao, u.process_after "
|
|
"FROM ugc AS u LEFT JOIN charinfo AS c ON c.id = u.character_id ";
|
|
std::vector<IUgc::ProcessInfo> list;
|
|
// Every filter always bound, off when its first value says so
|
|
const int32_t stateValue = state ? static_cast<int32_t>(*state) : -1;
|
|
const std::string text(search);
|
|
const std::string pattern = "%" + text + "%";
|
|
const std::string where = "WHERE (? < 0 OR u.is_optimized = ?) AND (? = '' OR CAST(u.id AS CHAR) = ? OR c.name LIKE ?) ";
|
|
auto [_, result] = ExecuteSelect(select + where + "ORDER BY u.id DESC LIMIT ? OFFSET ?;", stateValue, stateValue, text, text, pattern, limit, offset);
|
|
while (!result.eof()) {
|
|
list.push_back(ReadUgcProcessInfo(result, false));
|
|
result.nextRow();
|
|
}
|
|
return list;
|
|
}
|
|
|
|
std::vector<std::pair<IUgc::eProcessState, uint64_t>> SQLiteDatabase::GetUgcProcessCounts() {
|
|
auto [_, result] = ExecuteSelect("SELECT is_optimized, COUNT(*) AS count FROM ugc GROUP BY is_optimized;");
|
|
std::vector<std::pair<eProcessState, uint64_t>> counts;
|
|
while (!result.eof()) {
|
|
counts.emplace_back(static_cast<eProcessState>(result.getIntField("is_optimized")), static_cast<uint64_t>(result.getInt64Field("count")));
|
|
result.nextRow();
|
|
}
|
|
return counts;
|
|
}
|
|
|
|
void SQLiteDatabase::ExpediteUgcModel(const LWOOBJID id) {
|
|
ExecuteUpdate("UPDATE ugc SET process_after = 0 WHERE id = ? AND is_optimized = 0 AND process_after > 0;", id);
|
|
}
|
|
|
|
void SQLiteDatabase::ExpediteUgcModels(const LWOOBJID characterId) {
|
|
ExecuteUpdate("UPDATE ugc SET process_after = 0 WHERE character_id = ? AND is_optimized = 0 AND process_after > 0;", characterId);
|
|
}
|
|
|
|
void SQLiteDatabase::SetUgcModelStats(const LWOOBJID id, const uint32_t bricks, const uint32_t triangles, const uint32_t trianglesBefore) {
|
|
ExecuteUpdate("UPDATE ugc SET brick_count = ?, triangle_count = ?, triangle_count_before = ? WHERE id = ?;", bricks, triangles, trianglesBefore, id);
|
|
}
|
|
|
|
void SQLiteDatabase::SetUgcFileChecksum(const eFileOwner owner, const LWOOBJID storageId, const std::string_view file, const std::string_view md5, const uint32_t size) {
|
|
ExecuteInsert("INSERT INTO ugc_file_checksums (kind, storage_id, file, md5, size) VALUES (?, ?, ?, ?, ?) "
|
|
"ON CONFLICT(kind, storage_id, file) DO UPDATE SET md5 = excluded.md5, size = excluded.size;", static_cast<int32_t>(owner), storageId, file, md5, size);
|
|
}
|
|
|
|
std::optional<IUgc::FileChecksum> SQLiteDatabase::GetUgcFileChecksum(const LWOOBJID blueprintId, const std::string_view file) {
|
|
auto [_, result] = ExecuteSelect("SELECT md5, size FROM ugc_file_checksums WHERE kind = 0 AND storage_id = ? AND file = ? "
|
|
"UNION ALL SELECT f.md5, f.size FROM ugc_modular_build b JOIN ugc_file_checksums f ON f.kind = 1 AND f.storage_id = b.combination_id AND f.file = ? "
|
|
"WHERE b.ugc_id = ? AND b.combination_id != 0 LIMIT 1;", blueprintId, file, file, blueprintId);
|
|
if (result.eof()) return std::nullopt;
|
|
return IUgc::FileChecksum{ std::string(result.getStringField("md5", "")), static_cast<uint32_t>(result.getInt64Field("size")) };
|
|
}
|
|
|
|
void SQLiteDatabase::SetUgcModelProcessStats(const LWOOBJID id, const ProcessStats& stats) {
|
|
ExecuteUpdate("UPDATE ugc SET process_ms = ?, process_cpu_ms = ?, process_memory_kb = ? WHERE id = ?;", stats.milliseconds, stats.cpuMilliseconds, stats.memoryKb, id);
|
|
}
|