mirror of
https://github.com/arcan1s/queued.git
synced 2025-07-14 22:35:48 +00:00
add some reports
This commit is contained in:
@ -18,6 +18,8 @@
|
||||
|
||||
#include <queued/Queued.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "QueuedctlAuth.h"
|
||||
#include "QueuedctlOption.h"
|
||||
#include "QueuedctlPermissions.h"
|
||||
@ -32,7 +34,7 @@ void QueuedctlCommon::checkArgs(const QStringList &_args, const int _count,
|
||||
qCDebug(LOG_APP) << "Check args count" << _args << _count;
|
||||
|
||||
if (_args.count() != _count) {
|
||||
qWarning() << "Invalid command";
|
||||
qCWarning(LOG_APP) << "Invalid command";
|
||||
_parser.showHelp(1);
|
||||
}
|
||||
}
|
||||
@ -68,6 +70,32 @@ QString QueuedctlCommon::hashToString(const QVariantHash &_hash)
|
||||
}
|
||||
|
||||
|
||||
QString QueuedctlCommon::hashListToString(const QList<QVariantHash> &_list)
|
||||
{
|
||||
qCDebug(LOG_APP) << "Convert hash list to string" << _list;
|
||||
|
||||
if (_list.isEmpty())
|
||||
return "";
|
||||
|
||||
QStringList output;
|
||||
// get table header
|
||||
QStringList header = _list.first().keys();
|
||||
header.sort();
|
||||
output += header.join(',');
|
||||
// append rows
|
||||
for (auto &hash : _list) {
|
||||
QStringList row;
|
||||
std::for_each(header.cbegin(), header.cend(),
|
||||
[&hash, &row](const QString &column) {
|
||||
row += hash[column].toString().replace('\n', ' ');
|
||||
});
|
||||
output += row.join(',');
|
||||
}
|
||||
|
||||
return output.join('\n');
|
||||
}
|
||||
|
||||
|
||||
void QueuedctlCommon::preprocess(const QStringList &_args,
|
||||
QCommandLineParser &_parser)
|
||||
{
|
||||
@ -105,12 +133,18 @@ void QueuedctlCommon::preprocess(const QStringList &_args,
|
||||
break;
|
||||
case QueuedctlArgument::PluginList:
|
||||
break;
|
||||
case QueuedctlArgument::Report:
|
||||
QueuedctlUser::parserReport(_parser);
|
||||
break;
|
||||
case QueuedctlArgument::TaskAdd:
|
||||
QueuedctlTask::parserAdd(_parser);
|
||||
break;
|
||||
case QueuedctlArgument::TaskGet:
|
||||
QueuedctlTask::parserGet(_parser);
|
||||
break;
|
||||
case QueuedctlArgument::TaskList:
|
||||
QueuedctlTask::parserList(_parser);
|
||||
break;
|
||||
case QueuedctlArgument::TaskSet:
|
||||
QueuedctlTask::parserSet(_parser);
|
||||
break;
|
||||
@ -124,6 +158,9 @@ void QueuedctlCommon::preprocess(const QStringList &_args,
|
||||
case QueuedctlArgument::UserGet:
|
||||
QueuedctlUser::parserGet(_parser);
|
||||
break;
|
||||
case QueuedctlArgument::UserList:
|
||||
QueuedctlUser::parserList(_parser);
|
||||
break;
|
||||
case QueuedctlArgument::UserSet:
|
||||
QueuedctlUser::parserSet(_parser);
|
||||
break;
|
||||
@ -137,9 +174,9 @@ void QueuedctlCommon::preprocess(const QStringList &_args,
|
||||
void QueuedctlCommon::print(const QueuedctlResult &_result)
|
||||
{
|
||||
if (!_result.status)
|
||||
qInfo() << "Subprocess returns error";
|
||||
std::cout << "Subprocess returns error" << std::endl;
|
||||
if (!_result.output.isEmpty())
|
||||
QDebug(QtMsgType::QtInfoMsg).noquote() << _result.output;
|
||||
std::cout << qPrintable(_result.output) << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@ -240,6 +277,13 @@ QueuedctlCommon::process(QCommandLineParser &_parser, const QString &_cache,
|
||||
= QString("Could not remove plugin %1").arg(args.at(1));
|
||||
break;
|
||||
}
|
||||
case QueuedctlArgument::Report: {
|
||||
QString token = QueuedctlAuth::getToken(_cache, _user);
|
||||
result.status = true;
|
||||
result.output
|
||||
= hashListToString(QueuedctlUser::getReport(_parser, token));
|
||||
break;
|
||||
}
|
||||
case QueuedctlArgument::TaskAdd: {
|
||||
QString token = QueuedctlAuth::getToken(_cache, _user);
|
||||
auto definitions = QueuedctlTask::getDefinitions(_parser, false);
|
||||
@ -259,6 +303,13 @@ QueuedctlCommon::process(QCommandLineParser &_parser, const QString &_cache,
|
||||
: value.toString();
|
||||
break;
|
||||
}
|
||||
case QueuedctlArgument::TaskList: {
|
||||
QString token = QueuedctlAuth::getToken(_cache, _user);
|
||||
result.status = true;
|
||||
result.output
|
||||
= hashListToString(QueuedctlTask::getTasks(_parser, token));
|
||||
break;
|
||||
}
|
||||
case QueuedctlArgument::TaskSet: {
|
||||
QString token = QueuedctlAuth::getToken(_cache, _user);
|
||||
auto definitions = QueuedctlTask::getDefinitions(_parser, true);
|
||||
@ -296,6 +347,13 @@ QueuedctlCommon::process(QCommandLineParser &_parser, const QString &_cache,
|
||||
: value.toString();
|
||||
break;
|
||||
}
|
||||
case QueuedctlArgument::UserList: {
|
||||
QString token = QueuedctlAuth::getToken(_cache, _user);
|
||||
result.status = true;
|
||||
result.output
|
||||
= hashListToString(QueuedctlUser::getUsers(_parser, token));
|
||||
break;
|
||||
}
|
||||
case QueuedctlArgument::UserSet: {
|
||||
auto userId = QueuedctlUser::getUserId(args.at(1));
|
||||
QString token = QueuedctlAuth::getToken(_cache, _user);
|
||||
|
@ -33,13 +33,16 @@ enum class QueuedctlArgument {
|
||||
PluginAdd,
|
||||
PluginList,
|
||||
PluginRemove,
|
||||
Report,
|
||||
TaskAdd,
|
||||
TaskGet,
|
||||
TaskList,
|
||||
TaskSet,
|
||||
TaskStart,
|
||||
TaskStop,
|
||||
UserAdd,
|
||||
UserGet,
|
||||
UserList,
|
||||
UserSet
|
||||
};
|
||||
typedef struct {
|
||||
@ -64,19 +67,23 @@ const QHash<QString, QueuedctlArgumentInfo> QueuedctlArguments = {
|
||||
{QueuedctlArgument::PluginList, "Shows enabled plugins.", 1}},
|
||||
{"plugin-remove",
|
||||
{QueuedctlArgument::PluginRemove, "Removes plugin to load.", 2}},
|
||||
{"report", {QueuedctlArgument::Report, "Shows usage report.", 1}},
|
||||
{"task-add", {QueuedctlArgument::TaskAdd, "Adds new task.", 2}},
|
||||
{"task-get", {QueuedctlArgument::TaskGet, "Gets task properties.", 3}},
|
||||
{"task-list", {QueuedctlArgument::TaskList, "Gets tasks list.", 1}},
|
||||
{"task-set", {QueuedctlArgument::TaskSet, "Sets task properties.", 2}},
|
||||
{"task-start", {QueuedctlArgument::TaskStart, "Starts task.", 2}},
|
||||
{"task-stop", {QueuedctlArgument::TaskStop, "Stops task.", 2}},
|
||||
{"user-add", {QueuedctlArgument::UserAdd, "Adds new user.", 2}},
|
||||
{"user-get", {QueuedctlArgument::UserGet, "Gets user properties.", 3}},
|
||||
{"user-list", {QueuedctlArgument::UserList, "Gets users list.", 1}},
|
||||
{"user-set", {QueuedctlArgument::UserSet, "Sets user properties.", 2}}};
|
||||
// methods
|
||||
void checkArgs(const QStringList &_args, const int _count,
|
||||
QCommandLineParser &_parser);
|
||||
QString commandsHelp();
|
||||
QString hashToString(const QVariantHash &_hash);
|
||||
QString hashListToString(const QList<QVariantHash> &_list);
|
||||
void preprocess(const QStringList &_args, QCommandLineParser &_parser);
|
||||
void print(const QueuedctlResult &_result);
|
||||
QueuedctlResult process(QCommandLineParser &_parser, const QString &_cache,
|
||||
|
@ -72,10 +72,10 @@ QueuedctlTask::getDefinitions(const QCommandLineParser &_parser,
|
||||
if (_expandAll) {
|
||||
definitions.command = _parser.value("program");
|
||||
definitions.endTime
|
||||
= QDateTime::fromString(_parser.value("stop"), Qt::ISODate);
|
||||
= QDateTime::fromString(_parser.value("stop"), Qt::ISODateWithMs);
|
||||
definitions.gid = _parser.value("gid").toUInt();
|
||||
definitions.startTime
|
||||
= QDateTime::fromString(_parser.value("start"), Qt::ISODate);
|
||||
= QDateTime::fromString(_parser.value("start"), Qt::ISODateWithMs);
|
||||
definitions.uid = _parser.value("uid").toUInt();
|
||||
} else {
|
||||
// queuedctl -- task-add /path/to/application
|
||||
@ -98,6 +98,21 @@ QVariant QueuedctlTask::getTask(const long long _id, const QString &_property)
|
||||
}
|
||||
|
||||
|
||||
QList<QVariantHash> QueuedctlTask::getTasks(const QCommandLineParser &_parser,
|
||||
const QString &_token)
|
||||
{
|
||||
long long user = _parser.value("task-user").isEmpty()
|
||||
? -1
|
||||
: QueuedctlUser::getUserId(_parser.value("task-user"));
|
||||
QDateTime stop
|
||||
= QDateTime::fromString(_parser.value("stop"), Qt::ISODateWithMs);
|
||||
QDateTime start
|
||||
= QDateTime::fromString(_parser.value("start"), Qt::ISODateWithMs);
|
||||
|
||||
return QueuedCoreAdaptor::getTasks(user, start, stop, _token);
|
||||
}
|
||||
|
||||
|
||||
void QueuedctlTask::parserAdd(QCommandLineParser &_parser)
|
||||
{
|
||||
_parser.addPositionalArgument("program", "Command line.", "<program>");
|
||||
@ -150,6 +165,20 @@ void QueuedctlTask::parserGet(QCommandLineParser &_parser)
|
||||
}
|
||||
|
||||
|
||||
void QueuedctlTask::parserList(QCommandLineParser &_parser)
|
||||
{
|
||||
// user
|
||||
QCommandLineOption userOption("task-user", "Task user.", "task-user", "");
|
||||
_parser.addOption(userOption);
|
||||
// start
|
||||
QCommandLineOption startOption("start", "Task start time.", "start", "");
|
||||
_parser.addOption(startOption);
|
||||
// stop
|
||||
QCommandLineOption stopOption("stop", "Task stop time.", "stop", "");
|
||||
_parser.addOption(stopOption);
|
||||
}
|
||||
|
||||
|
||||
void QueuedctlTask::parserSet(QCommandLineParser &_parser)
|
||||
{
|
||||
_parser.addPositionalArgument("id", "Task ID.", "<id>");
|
||||
|
@ -29,8 +29,11 @@ long long addTask(const QueuedProcess::QueuedProcessDefinitions &_definitions,
|
||||
QueuedProcess::QueuedProcessDefinitions
|
||||
getDefinitions(const QCommandLineParser &_parser, const bool _expandAll);
|
||||
QVariant getTask(const long long _id, const QString &_property);
|
||||
QList<QVariantHash> getTasks(const QCommandLineParser &_parser,
|
||||
const QString &_token);
|
||||
void parserAdd(QCommandLineParser &_parser);
|
||||
void parserGet(QCommandLineParser &_parser);
|
||||
void parserList(QCommandLineParser &_parser);
|
||||
void parserSet(QCommandLineParser &_parser);
|
||||
void parserStart(QCommandLineParser &_parser);
|
||||
bool setTask(const long long _id,
|
||||
|
@ -18,6 +18,8 @@
|
||||
|
||||
#include <queued/Queued.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
extern "C" {
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
@ -34,6 +36,20 @@ QueuedctlUser::addUser(const QueuedUser::QueuedUserDefinitions &_definitions,
|
||||
}
|
||||
|
||||
|
||||
QList<QVariantHash> QueuedctlUser::getReport(const QCommandLineParser &_parser,
|
||||
const QString &_token)
|
||||
{
|
||||
qCDebug(LOG_APP) << "Get usage report";
|
||||
|
||||
QDateTime stop
|
||||
= QDateTime::fromString(_parser.value("stop"), Qt::ISODateWithMs);
|
||||
QDateTime start
|
||||
= QDateTime::fromString(_parser.value("start"), Qt::ISODateWithMs);
|
||||
|
||||
return QueuedCoreAdaptor::getPerformance(start, stop, _token);
|
||||
}
|
||||
|
||||
|
||||
QueuedUser::QueuedUserDefinitions
|
||||
QueuedctlUser::getDefinitions(const QCommandLineParser &_parser,
|
||||
const bool _expandAll)
|
||||
@ -47,6 +63,11 @@ QueuedctlUser::getDefinitions(const QCommandLineParser &_parser,
|
||||
definitions.password = _parser.isSet("stdin-password")
|
||||
? getPassword()
|
||||
: _parser.value("password");
|
||||
// transform to hash
|
||||
definitions.password
|
||||
= definitions.password.isEmpty()
|
||||
? ""
|
||||
: QueuedUser::hashFromPassword(definitions.password);
|
||||
// limits now
|
||||
QueuedLimits::Limits limits(
|
||||
_parser.value("limit-cpu").toLongLong(),
|
||||
@ -77,7 +98,7 @@ QString QueuedctlUser::getPassword()
|
||||
tty.c_lflag &= ~ECHO;
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &tty);
|
||||
|
||||
qInfo() << "Password";
|
||||
std::cout << "Password" << std::endl;
|
||||
QTextStream stream(stdin);
|
||||
QString password;
|
||||
stream >> password;
|
||||
@ -98,6 +119,20 @@ QVariant QueuedctlUser::getUser(const long long _id, const QString &_property)
|
||||
}
|
||||
|
||||
|
||||
QList<QVariantHash> QueuedctlUser::getUsers(const QCommandLineParser &_parser,
|
||||
const QString &_token)
|
||||
{
|
||||
QDateTime lastLogin = QDateTime::fromString(_parser.value("last-logged"),
|
||||
Qt::ISODateWithMs);
|
||||
auto permission
|
||||
= _parser.value("access").isEmpty()
|
||||
? QueuedEnums::Permission::Invalid
|
||||
: QueuedEnums::Permission(_parser.value("access").toInt());
|
||||
|
||||
return QueuedCoreAdaptor::getUsers(lastLogin, permission, _token);
|
||||
}
|
||||
|
||||
|
||||
long long QueuedctlUser::getUserId(const QString &_name)
|
||||
{
|
||||
qCDebug(LOG_APP) << "Get user ID for" << _name;
|
||||
@ -164,6 +199,29 @@ void QueuedctlUser::parserGet(QCommandLineParser &_parser)
|
||||
}
|
||||
|
||||
|
||||
void QueuedctlUser::parserList(QCommandLineParser &_parser)
|
||||
{
|
||||
// last logged in
|
||||
QCommandLineOption loggedOption("last-logged", "User last logged time.",
|
||||
"last-logged", "");
|
||||
_parser.addOption(loggedOption);
|
||||
// permissions
|
||||
QCommandLineOption accessOption("access", "User permission.", "access", "");
|
||||
_parser.addOption(accessOption);
|
||||
}
|
||||
|
||||
|
||||
void QueuedctlUser::parserReport(QCommandLineParser &_parser)
|
||||
{
|
||||
// start
|
||||
QCommandLineOption startOption("start", "Task start time.", "start", "");
|
||||
_parser.addOption(startOption);
|
||||
// stop
|
||||
QCommandLineOption stopOption("stop", "Task stop time.", "stop", "");
|
||||
_parser.addOption(stopOption);
|
||||
}
|
||||
|
||||
|
||||
void QueuedctlUser::parserSet(QCommandLineParser &_parser)
|
||||
{
|
||||
_parser.addPositionalArgument("id", "User ID.", "<id>");
|
||||
|
@ -26,13 +26,19 @@ namespace QueuedctlUser
|
||||
{
|
||||
long long addUser(const QueuedUser::QueuedUserDefinitions &_definitions,
|
||||
const QString &_token);
|
||||
QList<QVariantHash> getReport(const QCommandLineParser &_parser,
|
||||
const QString &_token);
|
||||
QueuedUser::QueuedUserDefinitions
|
||||
getDefinitions(const QCommandLineParser &_parser, const bool _expandAll);
|
||||
QString getPassword();
|
||||
QVariant getUser(const long long _id, const QString &_property);
|
||||
QList<QVariantHash> getUsers(const QCommandLineParser &_parser,
|
||||
const QString &_token);
|
||||
long long getUserId(const QString &_name);
|
||||
void parserAdd(QCommandLineParser &_parser);
|
||||
void parserGet(QCommandLineParser &_parser);
|
||||
void parserList(QCommandLineParser &_parser);
|
||||
void parserReport(QCommandLineParser &_parser);
|
||||
void parserSet(QCommandLineParser &_parser);
|
||||
bool setUser(const long long _id,
|
||||
const QueuedUser::QueuedUserDefinitions &_definitions,
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
#include <queued/Queued.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "QueuedctlCommon.h"
|
||||
#include "version.h"
|
||||
|
||||
@ -29,6 +31,8 @@ extern "C" {
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QueuedDebug::applyLogFormat();
|
||||
|
||||
QCoreApplication app(argc, argv);
|
||||
app.setApplicationName(NAME);
|
||||
app.setApplicationVersion(VERSION);
|
||||
@ -81,13 +85,12 @@ int main(int argc, char *argv[])
|
||||
if (parser.isSet(infoOption)) {
|
||||
auto metadata = QueuedDebug::getBuildData();
|
||||
for (auto &string : metadata)
|
||||
QDebug(QtMsgType::QtInfoMsg).noquote() << string;
|
||||
std::cout << qPrintable(string) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
if (parser.isSet(commandsOption)) {
|
||||
QDebug(QtMsgType::QtInfoMsg).noquote() << parser.helpText();
|
||||
QDebug(QtMsgType::QtInfoMsg).noquote()
|
||||
<< QueuedctlCommon::commandsHelp();
|
||||
std::cout << qPrintable(parser.helpText()) << std::endl;
|
||||
std::cout << qPrintable(QueuedctlCommon::commandsHelp()) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user