98 lines
3.2 KiB
C++
98 lines
3.2 KiB
C++
#include "AppLogDAO.h"
|
|
|
|
#include <QSqlQuery>
|
|
#include <QSqlError>
|
|
#include <QDebug>
|
|
|
|
#include "DatabaseManager.h"
|
|
#include "time/DateUtils.h"
|
|
|
|
bool AppLogDAO::insert(const AppLogEntry &entry) {
|
|
QSqlQuery q(DatabaseManager::instance().database());
|
|
q.prepare(R"(
|
|
INSERT INTO app_logs (source, device_id, message, screenshot, timestamp)
|
|
VALUES (:source, :device_id, :message, :screenshot, :timestamp)
|
|
)");
|
|
q.bindValue(":source", entry.source);
|
|
q.bindValue(":device_id", entry.deviceId);
|
|
q.bindValue(":message", entry.message);
|
|
q.bindValue(":screenshot", entry.screenshot.isEmpty()
|
|
? QVariant(QMetaType(QMetaType::QByteArray))
|
|
: QVariant(entry.screenshot));
|
|
q.bindValue(":timestamp", DateUtils::toUtcString(entry.timestamp));
|
|
if (!q.exec()) {
|
|
qWarning() << "[AppLogDAO] insert failed:" << q.lastError().text();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
QList<AppLogEntry> AppLogDAO::getLogs(int page, int pageSize) {
|
|
QList<AppLogEntry> result;
|
|
QSqlQuery q(DatabaseManager::instance().database());
|
|
q.prepare(R"(
|
|
SELECT id, source, device_id, message, timestamp
|
|
FROM app_logs
|
|
ORDER BY id DESC
|
|
LIMIT :limit OFFSET :offset
|
|
)");
|
|
q.bindValue(":limit", pageSize);
|
|
q.bindValue(":offset", page * pageSize);
|
|
if (!q.exec()) {
|
|
qWarning() << "[AppLogDAO] getLogs failed:" << q.lastError().text();
|
|
return result;
|
|
}
|
|
while (q.next()) {
|
|
AppLogEntry e;
|
|
e.id = q.value("id").toInt();
|
|
e.source = q.value("source").toString();
|
|
e.deviceId = q.value("device_id").toString();
|
|
e.message = q.value("message").toString();
|
|
e.timestamp = DateUtils::fromUtcString(q.value("timestamp").toString());
|
|
result.append(e);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int AppLogDAO::getTotalCount() {
|
|
QSqlQuery q(DatabaseManager::instance().database());
|
|
q.exec("SELECT COUNT(*) FROM app_logs");
|
|
if (q.next()) return q.value(0).toInt();
|
|
return 0;
|
|
}
|
|
|
|
QByteArray AppLogDAO::getScreenshot(int id) {
|
|
QSqlQuery q(DatabaseManager::instance().database());
|
|
q.prepare("SELECT screenshot FROM app_logs WHERE id = :id");
|
|
q.bindValue(":id", id);
|
|
if (!q.exec() || !q.next()) return {};
|
|
return q.value(0).toByteArray();
|
|
}
|
|
|
|
QList<AppLogEntry> AppLogDAO::getLogsWithScreenshots(int limit) {
|
|
QList<AppLogEntry> result;
|
|
QSqlQuery q(DatabaseManager::instance().database());
|
|
q.prepare(R"(
|
|
SELECT id, source, device_id, message, screenshot, timestamp
|
|
FROM app_logs
|
|
ORDER BY id DESC
|
|
LIMIT :limit
|
|
)");
|
|
q.bindValue(":limit", limit);
|
|
if (!q.exec()) {
|
|
qWarning() << "[AppLogDAO] getLogsWithScreenshots failed:" << q.lastError().text();
|
|
return result;
|
|
}
|
|
while (q.next()) {
|
|
AppLogEntry e;
|
|
e.id = q.value("id").toInt();
|
|
e.source = q.value("source").toString();
|
|
e.deviceId = q.value("device_id").toString();
|
|
e.message = q.value("message").toString();
|
|
e.screenshot = q.value("screenshot").toByteArray();
|
|
e.timestamp = DateUtils::fromUtcString(q.value("timestamp").toString());
|
|
result.append(e);
|
|
}
|
|
return result;
|
|
}
|