Added a query plan recorder for debugging purposes
This commit is contained in:
@@ -28,7 +28,6 @@ extern "C"
|
||||
}
|
||||
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "core/ILogger.hpp"
|
||||
|
||||
@@ -32,6 +32,7 @@ add_library(lmsdatabase STATIC
|
||||
impl/IdType.cpp
|
||||
impl/Migration.cpp
|
||||
impl/Object.cpp
|
||||
impl/QueryPlanRecorder.cpp
|
||||
impl/Session.cpp
|
||||
impl/SqlQuery.cpp
|
||||
impl/Transaction.cpp
|
||||
|
||||
@@ -206,7 +206,7 @@ namespace lms::db
|
||||
LMS_LOG(DB, INFO, "Performing quick database check...");
|
||||
|
||||
// Quick check is just a simple integrity check
|
||||
bool quickCheckPassed{ checkDbIntegrity(*connection, IntegrityCheckType::Quick, [&](std::string_view error) {
|
||||
const bool quickCheckPassed{ checkDbIntegrity(*connection, IntegrityCheckType::Quick, [&](std::string_view error) {
|
||||
LMS_LOG(DB, ERROR, "Quick check error: " << error);
|
||||
}) };
|
||||
|
||||
@@ -222,7 +222,7 @@ namespace lms::db
|
||||
|
||||
LMS_LOG(DB, INFO, "Checking database integrity...");
|
||||
|
||||
bool integrityCheckPassed{ checkDbIntegrity(*connection, IntegrityCheckType::Full, [&](std::string_view error) {
|
||||
const bool integrityCheckPassed{ checkDbIntegrity(*connection, IntegrityCheckType::Full, [&](std::string_view error) {
|
||||
LMS_LOG(DB, ERROR, "Integrity check error: " << error);
|
||||
}) };
|
||||
|
||||
@@ -238,7 +238,7 @@ namespace lms::db
|
||||
|
||||
LMS_LOG(DB, INFO, "Checking foreign key constraints...");
|
||||
|
||||
bool foreignKeyConstraintsPassed{ checkDbForeignKeyConstraints(*connection, [&](std::string_view table, long long rowId, std::string_view referredTable) {
|
||||
const bool foreignKeyConstraintsPassed{ checkDbForeignKeyConstraints(*connection, [&](std::string_view table, long long rowId, std::string_view referredTable) {
|
||||
LMS_LOG(DB, ERROR, "Foreign key constraint failed in table '" << table << "', rowid = " << rowId << ", referred table = '" << referredTable << "'");
|
||||
}) };
|
||||
|
||||
|
||||
@@ -36,8 +36,6 @@ namespace lms::db
|
||||
public:
|
||||
Db(const std::filesystem::path& dbPath, std::size_t connectionCount);
|
||||
|
||||
Session& getTLSSession() override;
|
||||
|
||||
void executeSql(const std::string& sql);
|
||||
|
||||
private:
|
||||
@@ -46,6 +44,8 @@ namespace lms::db
|
||||
|
||||
friend class Session;
|
||||
|
||||
Session& getTLSSession() override;
|
||||
|
||||
core::RecursiveSharedMutex& getMutex() { return _sharedMutex; }
|
||||
Wt::Dbo::SqlConnectionPool& getConnectionPool() { return *_connectionPool; }
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "QueryPlanRecorder.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
#include <Wt/Dbo/SqlStatement.h>
|
||||
#include <Wt/Dbo/Transaction.h>
|
||||
|
||||
#include "core/ILogger.hpp"
|
||||
|
||||
namespace lms::db
|
||||
{
|
||||
std::unique_ptr<IQueryPlanRecorder> createQueryPlanRecorder()
|
||||
{
|
||||
return std::make_unique<QueryPlanRecorder>();
|
||||
}
|
||||
|
||||
QueryPlanRecorder::QueryPlanRecorder()
|
||||
{
|
||||
LMS_LOG(DB, INFO, "Recording database query plans");
|
||||
}
|
||||
|
||||
QueryPlanRecorder::~QueryPlanRecorder() = default;
|
||||
|
||||
void QueryPlanRecorder::visitQueryPlans(const QueryPlanVisitor& visitor) const
|
||||
{
|
||||
const std::shared_lock lock{ _mutex };
|
||||
|
||||
for (const auto& [query, plan] : _queryPlans)
|
||||
visitor(query, plan);
|
||||
}
|
||||
|
||||
void QueryPlanRecorder::recordQueryPlanIfNeeded(Wt::Dbo::Session& session, const std::string& query)
|
||||
{
|
||||
{
|
||||
const std::shared_lock lock{ _mutex };
|
||||
|
||||
if (_queryPlans.contains(query))
|
||||
return;
|
||||
}
|
||||
|
||||
Wt::Dbo::Transaction transaction{ session };
|
||||
|
||||
Wt::Dbo::SqlConnection* connection{ transaction.connection() };
|
||||
auto statement{ connection->prepareStatement("EXPLAIN QUERY PLAN " + query) };
|
||||
statement->execute();
|
||||
|
||||
std::map<int, std::string> entries{ { 0, "" } };
|
||||
std::map<int, std::vector<int>> relationships;
|
||||
|
||||
std::string detail;
|
||||
while (statement->nextRow())
|
||||
{
|
||||
detail.clear();
|
||||
|
||||
int id{};
|
||||
int parent{};
|
||||
int unused{};
|
||||
|
||||
if (statement->getResult(0, &id)
|
||||
&& statement->getResult(1, &parent)
|
||||
&& statement->getResult(2, &unused)
|
||||
&& statement->getResult(3, &detail, static_cast<int>(detail.capacity())))
|
||||
{
|
||||
entries.emplace(id, detail);
|
||||
relationships[parent].push_back(id);
|
||||
}
|
||||
}
|
||||
|
||||
// format
|
||||
std::string result;
|
||||
std::function<void(int, unsigned)> formatQuery = [&](int id, unsigned level) -> void {
|
||||
for (std::size_t i{}; i < level; ++i)
|
||||
result += '\t';
|
||||
|
||||
result += entries.at(id);
|
||||
result += '\n';
|
||||
auto itChildren = relationships.find(id);
|
||||
if (itChildren == relationships.end())
|
||||
return;
|
||||
|
||||
for (int child : itChildren->second)
|
||||
formatQuery(child, level + 1);
|
||||
};
|
||||
|
||||
formatQuery(0, 0);
|
||||
|
||||
{
|
||||
const std::unique_lock lock{ _mutex };
|
||||
_queryPlans.try_emplace(query, std::move(result));
|
||||
}
|
||||
}
|
||||
} // namespace lms::db
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <shared_mutex>
|
||||
#include <string>
|
||||
|
||||
#include <Wt/Dbo/Session.h>
|
||||
|
||||
#include "database/IQueryPlanRecorder.hpp"
|
||||
|
||||
namespace lms::db
|
||||
{
|
||||
class QueryPlanRecorder : public IQueryPlanRecorder
|
||||
{
|
||||
public:
|
||||
QueryPlanRecorder();
|
||||
~QueryPlanRecorder() override;
|
||||
QueryPlanRecorder(const QueryPlanRecorder&) = delete;
|
||||
QueryPlanRecorder& operator=(const QueryPlanRecorder&) = delete;
|
||||
|
||||
void visitQueryPlans(const QueryPlanVisitor& visitor) const override;
|
||||
|
||||
void recordQueryPlanIfNeeded(Wt::Dbo::Session& session, const std::string& query);
|
||||
|
||||
private:
|
||||
mutable std::shared_mutex _mutex;
|
||||
std::map<std::string, std::string> _queryPlans;
|
||||
};
|
||||
} // namespace lms::db
|
||||
@@ -19,22 +19,39 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <Wt/Dbo/Dbo.h>
|
||||
#include <Wt/Dbo/Call.h>
|
||||
#include <Wt/Dbo/Query.h>
|
||||
#include <Wt/Dbo/Session.h>
|
||||
#include <Wt/Dbo/collection.h>
|
||||
#include <Wt/WDateTime.h>
|
||||
|
||||
#include "core/ITraceLogger.hpp"
|
||||
#include "core/Service.hpp"
|
||||
#include "database/Types.hpp"
|
||||
|
||||
#include "QueryPlanRecorder.hpp"
|
||||
|
||||
namespace lms::db::utils
|
||||
{
|
||||
#define ESCAPE_CHAR_STR "\\"
|
||||
static inline constexpr char escapeChar{ '\\' };
|
||||
std::string escapeLikeKeyword(std::string_view keywords);
|
||||
|
||||
Wt::WDateTime normalizeDateTime(const Wt::WDateTime& dateTime);
|
||||
|
||||
namespace details
|
||||
{
|
||||
template<typename Query>
|
||||
void recordQueryPlanIfNeeded(const Query& query)
|
||||
{
|
||||
if (IQueryPlanRecorder * recorder{ core::Service<IQueryPlanRecorder>::get() })
|
||||
static_cast<QueryPlanRecorder*>(recorder)->recordQueryPlanIfNeeded(query.session(), query.asString());
|
||||
}
|
||||
} // namespace details
|
||||
|
||||
template<typename Query>
|
||||
void applyRange(Query& query, std::optional<Range> range)
|
||||
{
|
||||
@@ -82,13 +99,18 @@ namespace lms::db::utils
|
||||
template<typename Query, typename UnaryFunc>
|
||||
void forEachQueryResult(const Query& query, UnaryFunc&& func)
|
||||
{
|
||||
details::recordQueryPlanIfNeeded(query);
|
||||
|
||||
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "ForEachQueryResult", "Query", query.asString());
|
||||
|
||||
forEachResult(query.resultList(), std::forward<UnaryFunc>(func));
|
||||
}
|
||||
|
||||
template<typename T, typename Query>
|
||||
std::vector<T> fetchQueryResults(const Query& query)
|
||||
{
|
||||
details::recordQueryPlanIfNeeded(query);
|
||||
|
||||
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "FetchQueryResults", "Query", query.asString());
|
||||
|
||||
auto collection{ query.resultList() };
|
||||
@@ -98,6 +120,8 @@ namespace lms::db::utils
|
||||
template<typename Query>
|
||||
std::vector<typename QueryResultType<Query>::type> fetchQueryResults(const Query& query)
|
||||
{
|
||||
details::recordQueryPlanIfNeeded(query);
|
||||
|
||||
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "FetchQueryResults", "Query", query.asString());
|
||||
|
||||
auto collection{ query.resultList() };
|
||||
@@ -107,6 +131,8 @@ namespace lms::db::utils
|
||||
template<typename Query>
|
||||
auto fetchQuerySingleResult(const Query& query)
|
||||
{
|
||||
details::recordQueryPlanIfNeeded(query);
|
||||
|
||||
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "FetchQuerySingleResult", "Query", query.asString());
|
||||
return query.resultValue();
|
||||
}
|
||||
@@ -183,6 +209,4 @@ namespace lms::db::utils
|
||||
call.run();
|
||||
}
|
||||
}
|
||||
|
||||
Wt::WDateTime normalizeDateTime(const Wt::WDateTime& dateTime);
|
||||
} // namespace lms::db::utils
|
||||
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
|
||||
#include "database/objects/Artwork.hpp"
|
||||
|
||||
#include <Wt/Dbo/WtSqlTraits.h>
|
||||
|
||||
#include "database/Session.hpp"
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace lms::db
|
||||
{
|
||||
// Due to technical limitations, query plans are recorded globally across all databases.
|
||||
// As a result, this class is implemented as a singleton rather than being owned per DB instance.
|
||||
class IQueryPlanRecorder
|
||||
{
|
||||
public:
|
||||
virtual ~IQueryPlanRecorder() = default;
|
||||
|
||||
using QueryPlanVisitor = std::function<void(std::string_view query, std::string_view plan)>;
|
||||
virtual void visitQueryPlans(const QueryPlanVisitor& visitor) const = 0;
|
||||
};
|
||||
|
||||
std::unique_ptr<IQueryPlanRecorder> createQueryPlanRecorder();
|
||||
} // namespace lms::db
|
||||
@@ -14,10 +14,12 @@ add_executable(lms
|
||||
ui/State.cpp
|
||||
ui/Tooltip.cpp
|
||||
ui/Utils.cpp
|
||||
ui/admin/debug/Database.cpp
|
||||
ui/admin/debug/Tracing.cpp
|
||||
ui/admin/DebugToolsView.cpp
|
||||
ui/admin/InitWizardView.cpp
|
||||
ui/admin/MediaLibrariesView.cpp
|
||||
ui/admin/MediaLibraryModal.cpp
|
||||
ui/admin/TracingView.cpp
|
||||
ui/admin/ScannerController.cpp
|
||||
ui/admin/ScannerReportResource.cpp
|
||||
ui/admin/ScanSettingsView.cpp
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
#include "core/SystemPaths.hpp"
|
||||
#include "core/WtLogger.hpp"
|
||||
#include "database/IDb.hpp"
|
||||
#include "database/IQueryPlanRecorder.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "image/Image.hpp"
|
||||
#include "services/artwork/IArtworkService.hpp"
|
||||
@@ -315,6 +316,10 @@ namespace lms
|
||||
|
||||
core::IOContextRunner ioContextRunner{ ioContext, getThreadCount(), "Misc" };
|
||||
|
||||
core::Service<db::IQueryPlanRecorder> queryPlanRecorder;
|
||||
if (config->getBool("db-record-query-plans", false))
|
||||
queryPlanRecorder.assign(db::createQueryPlanRecorder());
|
||||
|
||||
// Connection pool size must be twice the number of threads: we have at least 2 io pools with getThreadCount() each and they all may access the database
|
||||
auto database{ db::createDb(config->getPath("working-dir", "/var/lms") / "lms.db", getThreadCount() * 2) };
|
||||
{
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "core/ITraceLogger.hpp"
|
||||
#include "core/Service.hpp"
|
||||
#include "database/IDb.hpp"
|
||||
#include "database/IQueryPlanRecorder.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Artist.hpp"
|
||||
#include "database/objects/Cluster.hpp"
|
||||
@@ -51,11 +52,11 @@
|
||||
#include "NotificationContainer.hpp"
|
||||
#include "PlayQueue.hpp"
|
||||
#include "SettingsView.hpp"
|
||||
#include "admin/DebugToolsView.hpp"
|
||||
#include "admin/InitWizardView.hpp"
|
||||
#include "admin/MediaLibrariesView.hpp"
|
||||
#include "admin/ScanSettingsView.hpp"
|
||||
#include "admin/ScannerController.hpp"
|
||||
#include "admin/TracingView.hpp"
|
||||
#include "admin/UserView.hpp"
|
||||
#include "admin/UsersView.hpp"
|
||||
#include "common/Template.hpp"
|
||||
@@ -75,6 +76,8 @@ namespace lms::ui
|
||||
const std::string appRoot{ Wt::WApplication::appRoot() };
|
||||
|
||||
auto res{ std::make_shared<Wt::WMessageResourceBundle>() };
|
||||
res->use(appRoot + "admin-db");
|
||||
res->use(appRoot + "admin-debugtools");
|
||||
res->use(appRoot + "admin-initwizard");
|
||||
res->use(appRoot + "admin-medialibraries");
|
||||
res->use(appRoot + "admin-medialibrary");
|
||||
@@ -132,7 +135,7 @@ namespace lms::ui
|
||||
IdxAdminScanner,
|
||||
IdxAdminUsers,
|
||||
IdxAdminUser,
|
||||
IdxAdminTracing,
|
||||
IdxAdminDebugTools,
|
||||
};
|
||||
|
||||
void handlePathChange(Wt::WStackedWidget& stack, bool isAdmin)
|
||||
@@ -158,7 +161,7 @@ namespace lms::ui
|
||||
{ "/admin/scanner", IdxAdminScanner, true, Wt::WString::tr("Lms.Admin.ScannerController.scanner") },
|
||||
{ "/admin/users", IdxAdminUsers, true, Wt::WString::tr("Lms.Admin.Users.users") },
|
||||
{ "/admin/user", IdxAdminUser, true, std::nullopt },
|
||||
{ "/admin/tracing", IdxAdminTracing, true, Wt::WString::tr("Lms.Admin.Tracing.tracing") },
|
||||
{ "/admin/debug-tools", IdxAdminDebugTools, true, Wt::WString::tr("Lms.Admin.DebugTools.debug-tools") },
|
||||
};
|
||||
|
||||
LMS_LOG(UI, DEBUG, "Internal path changed to '" << wApp->internalPath() << "'");
|
||||
@@ -461,11 +464,13 @@ namespace lms::ui
|
||||
navbar->bindNew<Wt::WAnchor>("scan-settings", Wt::WLink{ Wt::LinkType::InternalPath, "/admin/scan-settings" }, Wt::WString::tr("Lms.Admin.menu-scan-settings"));
|
||||
navbar->bindNew<Wt::WAnchor>("scanner", Wt::WLink{ Wt::LinkType::InternalPath, "/admin/scanner" }, Wt::WString::tr("Lms.Admin.menu-scanner"));
|
||||
navbar->bindNew<Wt::WAnchor>("users", Wt::WLink{ Wt::LinkType::InternalPath, "/admin/users" }, Wt::WString::tr("Lms.Admin.menu-users"));
|
||||
// Hide the entry if the trace logger is not enabled
|
||||
if (core::Service<core::tracing::ITraceLogger>::get())
|
||||
navbar->bindNew<Wt::WAnchor>("tracing", Wt::WLink{ Wt::LinkType::InternalPath, "/admin/tracing" }, Wt::WString::tr("Lms.Admin.menu-tracing"));
|
||||
else
|
||||
navbar->bindEmpty("tracing");
|
||||
// Hide the entry if no debug service is enabled
|
||||
if (core::Service<core::tracing::ITraceLogger>::get()
|
||||
|| core::Service<db::IQueryPlanRecorder>::get())
|
||||
{
|
||||
navbar->setCondition("if-debug-tools", true);
|
||||
navbar->bindNew<Wt::WAnchor>("debug-tools", Wt::WLink{ Wt::LinkType::InternalPath, "/admin/debug-tools" }, Wt::WString::tr("Lms.Admin.menu-debug-tools"));
|
||||
}
|
||||
}
|
||||
|
||||
// Contents
|
||||
@@ -486,7 +491,7 @@ namespace lms::ui
|
||||
mainStack->addNew<ScannerController>();
|
||||
mainStack->addNew<UsersView>();
|
||||
mainStack->addNew<UserView>();
|
||||
mainStack->addNew<TracingView>();
|
||||
mainStack->addNew<DebugToolsView>();
|
||||
}
|
||||
|
||||
explore->getPlayQueueController().setMaxTrackCountToEnqueue(_playQueue->getCapacity());
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "DebugToolsView.hpp"
|
||||
|
||||
#include "admin/debug/Database.hpp"
|
||||
#include "debug/Database.hpp"
|
||||
#include "debug/Tracing.hpp"
|
||||
|
||||
namespace lms::ui
|
||||
{
|
||||
DebugToolsView::DebugToolsView()
|
||||
: Wt::WTemplate{ Wt::WString::tr("Lms.Admin.DebugTools.template") }
|
||||
{
|
||||
addFunction("tr", &Wt::WTemplate::Functions::tr);
|
||||
|
||||
bindNew<Tracing>("tracing");
|
||||
bindNew<Database>("db");
|
||||
}
|
||||
} // namespace lms::ui
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Wt/WTemplate.h>
|
||||
|
||||
namespace lms::ui
|
||||
{
|
||||
class DebugToolsView : public Wt::WTemplate
|
||||
{
|
||||
public:
|
||||
DebugToolsView();
|
||||
};
|
||||
} // namespace lms::ui
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "Database.hpp"
|
||||
|
||||
#include <Wt/Http/Response.h>
|
||||
#include <Wt/Utils.h>
|
||||
#include <Wt/WDateTime.h>
|
||||
#include <Wt/WPushButton.h>
|
||||
#include <Wt/WResource.h>
|
||||
|
||||
#include "core/ITraceLogger.hpp"
|
||||
#include "core/String.hpp"
|
||||
#include "database/IQueryPlanRecorder.hpp"
|
||||
|
||||
namespace lms::ui
|
||||
{
|
||||
namespace
|
||||
{
|
||||
class QueryPlansReportResource : public Wt::WResource
|
||||
{
|
||||
public:
|
||||
QueryPlansReportResource(const db::IQueryPlanRecorder& recorder)
|
||||
: _recorder{ recorder }
|
||||
{
|
||||
}
|
||||
|
||||
~QueryPlansReportResource()
|
||||
{
|
||||
beingDeleted();
|
||||
}
|
||||
QueryPlansReportResource(const QueryPlansReportResource&) = delete;
|
||||
QueryPlansReportResource& operator=(const QueryPlansReportResource&) = delete;
|
||||
|
||||
private:
|
||||
void handleRequest(const Wt::Http::Request&, Wt::Http::Response& response)
|
||||
{
|
||||
response.setMimeType("application/text");
|
||||
|
||||
auto encodeHttpHeaderField = [](const std::string& fieldName, const std::string& fieldValue) {
|
||||
// This implements RFC 5987
|
||||
return fieldName + "*=UTF-8''" + Wt::Utils::urlEncode(fieldValue);
|
||||
};
|
||||
|
||||
const std::string cdp{ encodeHttpHeaderField("filename", "LMS_db_query_plans_" + core::stringUtils::toISO8601String(Wt::WDateTime::currentDateTime()) + ".txt") };
|
||||
response.addHeader("Content-Disposition", "attachment; " + cdp);
|
||||
|
||||
_recorder.visitQueryPlans([&](std::string_view query, std::string_view plan) {
|
||||
response.out() << query << '\n';
|
||||
response.out() << plan << "\n-------------------------\n";
|
||||
});
|
||||
}
|
||||
|
||||
const db::IQueryPlanRecorder& _recorder;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Database::Database()
|
||||
: Wt::WTemplate{ Wt::WString::tr("Lms.Admin.DebugTools.Db.template") }
|
||||
{
|
||||
addFunction("tr", &Wt::WTemplate::Functions::tr);
|
||||
|
||||
Wt::WPushButton* dumpBtn{ bindNew<Wt::WPushButton>("export-query-plans-btn", Wt::WString::tr("Lms.Admin.DebugTools.Db.export-query-plans")) };
|
||||
|
||||
if (const auto* recorder{ core::Service<db::IQueryPlanRecorder>::get() })
|
||||
{
|
||||
Wt::WLink link{ std::make_shared<QueryPlansReportResource>(*recorder) };
|
||||
link.setTarget(Wt::LinkTarget::NewWindow);
|
||||
dumpBtn->setLink(link);
|
||||
}
|
||||
else
|
||||
dumpBtn->setEnabled(false);
|
||||
}
|
||||
|
||||
} // namespace lms::ui
|
||||
@@ -23,9 +23,9 @@
|
||||
|
||||
namespace lms::ui
|
||||
{
|
||||
class TracingView : public Wt::WTemplate
|
||||
class Database : public Wt::WTemplate
|
||||
{
|
||||
public:
|
||||
TracingView();
|
||||
Database();
|
||||
};
|
||||
} // namespace lms::ui
|
||||
@@ -17,7 +17,7 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "TracingView.hpp"
|
||||
#include "Tracing.hpp"
|
||||
|
||||
#include <Wt/Http/Response.h>
|
||||
#include <Wt/Utils.h>
|
||||
@@ -73,12 +73,12 @@ namespace lms::ui
|
||||
};
|
||||
} // namespace
|
||||
|
||||
TracingView::TracingView()
|
||||
: Wt::WTemplate{ Wt::WString::tr("Lms.Admin.Tracing.template") }
|
||||
Tracing::Tracing()
|
||||
: Wt::WTemplate{ Wt::WString::tr("Lms.Admin.DebugTools.Tracing.template") }
|
||||
{
|
||||
addFunction("tr", &Wt::WTemplate::Functions::tr);
|
||||
|
||||
Wt::WPushButton* dumpBtn{ bindNew<Wt::WPushButton>("export-btn", Wt::WString::tr("Lms.Admin.Tracing.export-current-buffer")) };
|
||||
Wt::WPushButton* dumpBtn{ bindNew<Wt::WPushButton>("export-btn", Wt::WString::tr("Lms.Admin.DebugTools.Tracing.export-current-buffer")) };
|
||||
|
||||
if (auto traceLogger{ core::Service<core::tracing::ITraceLogger>::get() })
|
||||
{
|
||||
@@ -89,4 +89,5 @@ namespace lms::ui
|
||||
else
|
||||
dumpBtn->setEnabled(false);
|
||||
}
|
||||
|
||||
} // namespace lms::ui
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Wt/WTemplate.h>
|
||||
|
||||
namespace lms::ui
|
||||
{
|
||||
class Tracing : public Wt::WTemplate
|
||||
{
|
||||
public:
|
||||
Tracing();
|
||||
};
|
||||
} // namespace lms::ui
|
||||
Reference in New Issue
Block a user