From 74c785571333fbfa93d1f32b464f878f7dac6b8e Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Wed, 22 Feb 2017 04:09:08 +0300 Subject: [PATCH] more pretty schema control --- .../queued/include/queued/QueuedDatabase.h | 9 -- .../include/queued/QueuedDatabaseSchema.h | 85 +++++++++++++++++++ sources/queued/include/queued/QueuedDebug.h | 4 + .../include/queued/QueuedProcessManager.h | 23 +++++ sources/queued/src/QueuedDatabase.cpp | 14 ++- sources/queued/src/QueuedProcessManager.cpp | 3 + 6 files changed, 125 insertions(+), 13 deletions(-) create mode 100644 sources/queued/include/queued/QueuedDatabaseSchema.h diff --git a/sources/queued/include/queued/QueuedDatabase.h b/sources/queued/include/queued/QueuedDatabase.h index 4a01cd0..9ecebf5 100644 --- a/sources/queued/include/queued/QueuedDatabase.h +++ b/sources/queued/include/queued/QueuedDatabase.h @@ -29,8 +29,6 @@ #include -typedef QHash QueuedDBSchema; - /** * @brief queued adaptor to databases */ @@ -39,13 +37,6 @@ class QueuedDatabase : public QObject Q_OBJECT Q_PROPERTY(QString path READ path) - // TODO add fields SQL parameters (type whatever), Qt specific types - const QueuedDBSchema DBSchema = { - {"users", - {"_id", "name", "uid", "gid", "password_sha512", "email", "cpu", "gpu", - "memory", "gpumemory", "storage"}}, - {"tasks", {"_id", "userId", "command", "arguments", "workDirectory"}}}; - public: /** * @brief QueuedDatabase class constructor diff --git a/sources/queued/include/queued/QueuedDatabaseSchema.h b/sources/queued/include/queued/QueuedDatabaseSchema.h new file mode 100644 index 0000000..2569ebc --- /dev/null +++ b/sources/queued/include/queued/QueuedDatabaseSchema.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2016 Evgeniy Alekseev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + */ +/** + * @file QueuedDatabaseSchema.h + * Header of Queued library + * @author Evgeniy Alekseev + * @copyright MIT + * @bug https://github.com/arcan1s/queued/issues + */ + + +#ifndef QUEUEDDATABASESCHEMA_H +#define QUEUEDDATABASESCHEMA_H + +#include +#include + + +/** + * @struct QueuedDBField + * @brief describes database column + * @var name + * column name + * @var sqlDescription + * description to create column + * @var type + * Qt type of column for cast + */ +typedef struct { + QString name; + QString sqlDescription; + QVariant::Type type; +} QueuedDBField; +/** + * @typedef QueuedDBSchema + * custom map for database schemas descriptions + */ +typedef QHash> QueuedDBSchema; + +/** + * @defgroup QueuedDB + * @brief Queued database related constants + */ +namespace QueuedDB +{ +const QueuedDBSchema DBSchema = { + {"users", + {{"_id", + {"_id", "INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE", QVariant::LongLong}}, + {"name", {"name", "TEXT NOT NULL DEFAULT '0'", QVariant::String}}, + {"uid", {"uid", "INT NOT NULL DEFAULT 0", QVariant::UInt}}, + {"gid", {"gid", "INT NOT NULL DEFAULT 0", QVariant::UInt}}, + {"passwordSHA512", {"passwordSHA512", "TEXT", QVariant::String}}, + {"email", {"email", "TEXT", QVariant::String}}, + {"cpu", {"cpu", "INT", QVariant::LongLong}}, + {"gpu", {"gpu", "INT", QVariant::LongLong}}, + {"memory", {"memory", "INT", QVariant::LongLong}}, + {"gpumemory", {"gpumemory", "INT", QVariant::LongLong}}, + {"storage", {"storage", "INT", QVariant::LongLong}}, + {"permissions", {"permissions", "INT", QVariant::LongLong}}}}, + {"tasks", + {{"_id", + {"_id", "INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE", QVariant::LongLong}}, + {"userId", {"userId", "INT NOT NULL DEFAULT 0", QVariant::LongLong}}, + {"command", {"command", "TEXT", QVariant::String}}, + {"arguments", {"arguments", "TEXT", QVariant::String}}, + {"workDirectory", {"workDirectory", "TEXT", QVariant::String}}, + {"nice", {"nice", "INT", QVariant::Int}}, + {"startTime", {"startTime", "INT", QVariant::LongLong}}, + {"endTime", {"endTime", "INT", QVariant::LongLong}}}}}; +}; + +#endif /* QUEUEDDATABASESCHEMA_H */ diff --git a/sources/queued/include/queued/QueuedDebug.h b/sources/queued/include/queued/QueuedDebug.h index bf46864..8cd3836 100644 --- a/sources/queued/include/queued/QueuedDebug.h +++ b/sources/queued/include/queued/QueuedDebug.h @@ -48,6 +48,10 @@ Q_DECLARE_LOGGING_CATEGORY(LOG_PL) */ Q_DECLARE_LOGGING_CATEGORY(LOG_SERV) +/** + * @defgroup QueuedDebug + * @brief Queued debug functions + */ namespace QueuedDebug { /** diff --git a/sources/queued/include/queued/QueuedProcessManager.h b/sources/queued/include/queued/QueuedProcessManager.h index c52e7f0..caae4d7 100644 --- a/sources/queued/include/queued/QueuedProcessManager.h +++ b/sources/queued/include/queued/QueuedProcessManager.h @@ -24,13 +24,22 @@ #ifndef QUEUEDPROCESSMANAGER_H #define QUEUEDPROCESSMANAGER_H +#include #include #include #include "queued/QueuedProcess.h" +/** + * @typedef QueuedProcessMap + * map of indices to QueuedProcess pointers + */ typedef QHash QueuedProcessMap; +/** + * @typedef QueuedProcessConnectionMap + * map of indices to related QMetaObject::Connection + */ typedef QHash QueuedProcessConnectionMap; /** @@ -92,6 +101,20 @@ public: */ void remove(const long long _index); +signals: + /** + * @brief signal which will be called on task start + * @param _index task index + * @param _time task start time + */ + void taskStartTimeReceived(const long long _index, const QDateTime _time); + /** + * @brief signal which will be called on task end + * @param _index task index + * @param _time task stop time + */ + void taskStopTimeReceived(const long long _index, const QDateTime _time); + private slots: /** * @brief slot for catching finished tasks diff --git a/sources/queued/src/QueuedDatabase.cpp b/sources/queued/src/QueuedDatabase.cpp index bbbc597..337a364 100644 --- a/sources/queued/src/QueuedDatabase.cpp +++ b/sources/queued/src/QueuedDatabase.cpp @@ -27,6 +27,8 @@ #include #include +#include "queued/QueuedDatabaseSchema.h" + /** * @fn QueuedDatabase @@ -80,7 +82,8 @@ void QueuedDatabase::open(const QString _hostname, const int _port, void QueuedDatabase::checkDatabase() { QStringList tables = m_database.tables(); - for (auto table : DBSchema.keys()) { + QStringList schemaTables = QueuedDB::DBSchema.keys(); + for (auto &table : schemaTables) { // create table if does not exist if (!tables.contains(table)) createTable(table); @@ -113,13 +116,16 @@ void QueuedDatabase::createSchema(const QString _table) columns.append(record.fieldName(i)); // check and append if any - for (auto column : DBSchema[_table]) { + QStringList schemaColumns = QueuedDB::DBSchema[_table].keys(); + for (auto &column : schemaColumns) { if (columns.contains(column)) continue; + QueuedDBField field = QueuedDB::DBSchema[_table][column]; QSqlQuery query - = m_database.exec(QString("ALTER TABLE '%1' add column `%2`") + = m_database.exec(QString("ALTER TABLE '%1' ADD `%2` %3") .arg(_table) - .arg(column)); + .arg(column) + .arg(field.sqlDescription)); QSqlError error = query.lastError(); if (error.isValid()) qCCritical(LOG_LIB) << "Could not insert column" << column diff --git a/sources/queued/src/QueuedProcessManager.cpp b/sources/queued/src/QueuedProcessManager.cpp index 613342f..bd61ccf 100644 --- a/sources/queued/src/QueuedProcessManager.cpp +++ b/sources/queued/src/QueuedProcessManager.cpp @@ -142,5 +142,8 @@ void QueuedProcessManager::taskFinished(const int _exitCode, qCDebug(LOG_LIB) << "Process" << _index << "finished with code" << _exitCode << "and status" << _exitStatus; + emit(taskStopTimeReceived(_index, QDateTime::currentDateTimeUtc())); // TODO implementation + // TODO emit signal for new task here + // emit(taskStartTimeReceived(_index, QDateTime::currentDateTimeUtc())); }