Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 60 additions & 25 deletions App/Server/clsRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ clsRequestHandler::clsRequestHandler(QHttpRequest *_req, QHttpResponse *_res, QO
}

void clsRequestHandler::process(const QString& _api) {
this->Request->onData([this](QByteArray _data ) {
this->Request->onData([this](QByteArray _data) {
try {
TargomanLogInfo(7, "posted data: " << _data);

Expand Down Expand Up @@ -264,6 +264,37 @@ const qhttp::TStatusCode StatusCodeOnMethod[] = {
qhttp::ESTATUS_EXPECTATION_FAILED, ///< EHTTP_UNLINK = 32,
};

void clsRequestHandler::addToTimings(const QString &_label, quint64 _nanoSecs) {
if (this->ServerTimings.contains(_label))
this->ServerTimings[_label] += _nanoSecs;
else
this->ServerTimings[_label] = _nanoSecs;
}

void clsRequestHandler::sendTimingsToResponse() {
this->addToTimings("total", this->ElapsedTimer.nsecsElapsed());

// this->Response->addHeaderValue("Access-Control-Expose-Headers", QStringLiteral("x-debug-time-elapsed"));
// this->Response->addHeaderValue("x-debug-time-elapsed", QString::number(ceil(this->ElapsedTimer.nsecsElapsed() / 1000.0) / 1000) + " ms");

QStringList Output;

for (QMap<QString, quint64>::const_iterator it = this->ServerTimings.constBegin();
it != this->ServerTimings.constEnd();
it ++
) {
Output.append(QString("%1;dur=%2").arg(it.key()).arg(ceil(it.value() / 1000.0) / 1000));
}

this->Response->addHeaderValue("Server-Timing", Output.join(", "));

TargomanLogInfo(7, " "
<< "Server-Timing"
<< " : "
<< Output.join(", ")
);
}

clsRequestHandler::stuResult clsRequestHandler::run(
clsAPIObject* _apiObject,
QStringList& _queries,
Expand All @@ -272,11 +303,15 @@ clsRequestHandler::stuResult clsRequestHandler::run(
) {
// QVariantMap ResponseHeaders;

auto FNTiming = [=](const QString &_label, quint64 _nanoSecs) {
this->addToTimings(_label, _nanoSecs);
};

QScopedPointer<intfAPICallBoom> APICALLBOOM;
if (_apiObject->requiresJWT())
APICALLBOOM.reset(new APICALLBOOM_TYPE_JWT_DECL());
APICALLBOOM.reset(new APICALLBOOM_TYPE_JWT_DECL(FNTiming));
else
APICALLBOOM.reset(new APICALLBOOM_TYPE_NO_JWT_DECL());
APICALLBOOM.reset(new APICALLBOOM_TYPE_NO_JWT_DECL(FNTiming));

try {
for (auto QueryIter = _queries.begin(); QueryIter != _queries.end(); ++QueryIter)
Expand Down Expand Up @@ -511,8 +546,7 @@ void clsRequestHandler::sendFile(QString _basePath, const QString &_path) {
this->Response->setStatusCode(qhttp::ESTATUS_OK);

#ifdef QT_DEBUG
this->Response->addHeaderValue("Access-Control-Expose-Headers", QStringLiteral("x-debug-time-elapsed"));
this->Response->addHeaderValue("x-debug-time-elapsed", QString::number(ceil(this->ElapsedTimer.nsecsElapsed() / 1000.0) / 1000) + " ms");
this->sendTimingsToResponse();
#endif

QTimer::singleShot(10, this, &clsRequestHandler::slotSendFileData);
Expand Down Expand Up @@ -575,18 +609,19 @@ void clsRequestHandler::sendResponse(qhttp::TStatusCode _code,
this->Response->addHeaderValue("Access-Control-Allow-Origin", QStringLiteral("*"));
this->Response->addHeaderValue("Connection", QStringLiteral("keep-alive"));

auto funcAddToHeaderArray = [&_responseHeaders](const QString &_header) {
if (_responseHeaders.contains("Access-Control-Expose-Headers"))
_responseHeaders["Access-Control-Expose-Headers"] = _responseHeaders["Access-Control-Expose-Headers"].toString() + "," + _header;
else
_responseHeaders["Access-Control-Expose-Headers"] = _header;
};
// auto funcAddToHeaderArray = [&_responseHeaders](const QString &_header) {
// if (_responseHeaders.contains("Access-Control-Expose-Headers"))
// _responseHeaders["Access-Control-Expose-Headers"] = _responseHeaders["Access-Control-Expose-Headers"].toString() + "," + _header;
// else
// _responseHeaders["Access-Control-Expose-Headers"] = _header;
// };

#ifdef QT_DEBUG
funcAddToHeaderArray("x-debug-time-elapsed");
this->sendTimingsToResponse();
// funcAddToHeaderArray("x-debug-time-elapsed");

if (_responseHeaders.contains("x-debug-time-elapsed") == false)
_responseHeaders["x-debug-time-elapsed"] = QString::number(ceil(this->ElapsedTimer.nsecsElapsed() / 1000.0) / 1000) + " ms";
// if (_responseHeaders.contains("x-debug-time-elapsed") == false)
// _responseHeaders["x-debug-time-elapsed"] = QString::number(ceil(this->ElapsedTimer.nsecsElapsed() / 1000.0) / 1000) + " ms";
#endif

this->addHeaderValues(_responseHeaders);
Expand Down Expand Up @@ -631,8 +666,7 @@ void clsRequestHandler::sendCORSOptions() {
this->Response->setStatusCode(qhttp::ESTATUS_NO_CONTENT);

#ifdef QT_DEBUG
this->Response->addHeaderValue("Access-Control-Expose-Headers", QStringLiteral("x-debug-time-elapsed"));
this->Response->addHeaderValue("x-debug-time-elapsed", QString::number(ceil(this->ElapsedTimer.nsecsElapsed() / 1000.0) / 1000) + " ms");
this->sendTimingsToResponse();
#endif

this->Response->end();
Expand Down Expand Up @@ -686,18 +720,19 @@ void clsRequestHandler::sendResponseBase(
this->Response->addHeaderValue("Access-Control-Allow-Origin", QStringLiteral("*"));
this->Response->addHeaderValue("Connection", QStringLiteral("keep-alive"));

auto funcAddToHeaderArray = [&_responseHeaders](const QString &_header) {
if (_responseHeaders.contains("Access-Control-Expose-Headers"))
_responseHeaders["Access-Control-Expose-Headers"] = _responseHeaders["Access-Control-Expose-Headers"].toString() + "," + _header;
else
_responseHeaders["Access-Control-Expose-Headers"] = _header;
};
// auto funcAddToHeaderArray = [&_responseHeaders](const QString &_header) {
// if (_responseHeaders.contains("Access-Control-Expose-Headers"))
// _responseHeaders["Access-Control-Expose-Headers"] = _responseHeaders["Access-Control-Expose-Headers"].toString() + "," + _header;
// else
// _responseHeaders["Access-Control-Expose-Headers"] = _header;
// };

#ifdef QT_DEBUG
funcAddToHeaderArray("x-debug-time-elapsed");
this->sendTimingsToResponse();
// funcAddToHeaderArray("x-debug-time-elapsed");

if (_responseHeaders.contains("x-debug-time-elapsed") == false)
_responseHeaders["x-debug-time-elapsed"] = QString::number(ceil(this->ElapsedTimer.nsecsElapsed() / 1000.0) / 1000) + " ms";
// if (_responseHeaders.contains("x-debug-time-elapsed") == false)
// _responseHeaders["x-debug-time-elapsed"] = QString::number(ceil(this->ElapsedTimer.nsecsElapsed() / 1000.0) / 1000) + " ms";
#endif

this->addHeaderValues(_responseHeaders);
Expand Down
9 changes: 6 additions & 3 deletions App/Server/clsRequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ class clsRequestHandler : public QObject
clsRequestHandler(qhttp::server::QHttpRequest* _req,
qhttp::server::QHttpResponse* _res,
QObject *_parent = nullptr);
void process(const QString& _api);

//private:
// bool callStaticAPI(QString _api);
void process(const QString& _api);
void addToTimings(const QString &_label, quint64 _nanoSecs);
void sendTimingsToResponse();

public:
void findAndCallAPI(QString _api);
Expand Down Expand Up @@ -160,6 +160,9 @@ private slots:
QScopedPointer<QFile> FileHandler;
QElapsedTimer ElapsedTimer;

//QString _label, quint64 _nanoSecs
QMap<QString, quint64> ServerTimings;

friend class clsMultipartFormDataRequestHandler;
};

Expand Down
31 changes: 16 additions & 15 deletions Interfaces/AAA/Accounting_Interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,19 @@ QVariant IMPL_ORMGET(intfAccountProducts) {
quint32 IMPL_ORMCREATE(intfAccountProducts) {
Authorization::checkPriv(_APICALLBOOM.getJWT(), this->privOn(EHTTP_PUT, this->moduleBaseName()));

return this->Create(*this, CREATE_METHOD_ARGS_CALL_INTERNAL_BOOM2USER);
return this->Create(*this, CREATE_METHOD_ARGS_CALL_INTERNAL_BOOM);
}

bool IMPL_ORMUPDATE(intfAccountProducts) {
Authorization::checkPriv(_APICALLBOOM.getJWT(), this->privOn(EHTTP_PATCH, this->moduleBaseName()));

return this->Update(*this, UPDATE_METHOD_ARGS_CALL_INTERNAL_BOOM2USER);
return this->Update(*this, UPDATE_METHOD_ARGS_CALL_INTERNAL_BOOM);
}

bool IMPL_ORMDELETE(intfAccountProducts) {
Authorization::checkPriv(_APICALLBOOM.getJWT(), this->privOn(EHTTP_DELETE, this->moduleBaseName()));

return this->DeleteByPks(*this, DELETE_METHOD_ARGS_CALL_INTERNAL_BOOM2USER);
return this->DeleteByPks(*this, DELETE_METHOD_ARGS_CALL_INTERNAL_BOOM);
}

/******************************************************************/
Expand Down Expand Up @@ -350,19 +350,19 @@ QVariant IMPL_ORMGET(intfAccountSaleables) {
quint32 IMPL_ORMCREATE(intfAccountSaleables) {
Authorization::checkPriv(_APICALLBOOM.getJWT(), this->privOn(EHTTP_PUT, this->moduleBaseName()));

return this->Create(*this, CREATE_METHOD_ARGS_CALL_INTERNAL_BOOM2USER);
return this->Create(*this, CREATE_METHOD_ARGS_CALL_INTERNAL_BOOM);
}

bool IMPL_ORMUPDATE(intfAccountSaleables) {
Authorization::checkPriv(_APICALLBOOM.getJWT(), this->privOn(EHTTP_PATCH, this->moduleBaseName()));

return this->Update(*this, UPDATE_METHOD_ARGS_CALL_INTERNAL_BOOM2USER);
return this->Update(*this, UPDATE_METHOD_ARGS_CALL_INTERNAL_BOOM);
}

bool IMPL_ORMDELETE(intfAccountSaleables) {
Authorization::checkPriv(_APICALLBOOM.getJWT(), this->privOn(EHTTP_DELETE, this->moduleBaseName()));

return this->DeleteByPks(*this, DELETE_METHOD_ARGS_CALL_INTERNAL_BOOM2USER);
return this->DeleteByPks(*this, DELETE_METHOD_ARGS_CALL_INTERNAL_BOOM);
}

/******************************************************************/
Expand Down Expand Up @@ -432,9 +432,10 @@ bool IMPL_REST_UPDATE(intfAccountUserAssets, setAsPrefered, (
if (!Ok || !UserPackageID )
throw exHTTPBadRequest("Invalid UserPackageID provided");

this->callSP("spUserAsset_SetAsPrefered", {
{"iUserID", _APICALLBOOM.getUserID()},
{"iUASID", UserPackageID},
this->callSP(APICALLBOOM_PARAM,
"spUserAsset_SetAsPrefered", {
{ "iUserID", _APICALLBOOM.getUserID() },
{ "iUASID", UserPackageID },
});
return false;
}
Expand Down Expand Up @@ -549,19 +550,19 @@ QVariant IMPL_ORMGET(intfAccountCoupons) {
quint32 IMPL_ORMCREATE(intfAccountCoupons) {
Authorization::checkPriv(_APICALLBOOM.getJWT(), this->privOn(EHTTP_PUT, this->moduleBaseName()));

return this->Create(*this, CREATE_METHOD_ARGS_CALL_INTERNAL_BOOM2USER);
return this->Create(*this, CREATE_METHOD_ARGS_CALL_INTERNAL_BOOM);
}

bool IMPL_ORMUPDATE(intfAccountCoupons) {
Authorization::checkPriv(_APICALLBOOM.getJWT(), this->privOn(EHTTP_PATCH, this->moduleBaseName()));

return this->Update(*this, UPDATE_METHOD_ARGS_CALL_INTERNAL_BOOM2USER);
return this->Update(*this, UPDATE_METHOD_ARGS_CALL_INTERNAL_BOOM);
}

bool IMPL_ORMDELETE(intfAccountCoupons) {
Authorization::checkPriv(_APICALLBOOM.getJWT(), this->privOn(EHTTP_DELETE, this->moduleBaseName()));

return this->DeleteByPks(*this, DELETE_METHOD_ARGS_CALL_INTERNAL_BOOM2USER);
return this->DeleteByPks(*this, DELETE_METHOD_ARGS_CALL_INTERNAL_BOOM);
}

/******************************************************************/
Expand Down Expand Up @@ -591,19 +592,19 @@ QVariant IMPL_ORMGET(intfAccountPrizes) {
quint32 IMPL_ORMCREATE(intfAccountPrizes) {
Authorization::checkPriv(_APICALLBOOM.getJWT(), this->privOn(EHTTP_PUT, this->moduleBaseName()));

return this->Create(*this, CREATE_METHOD_ARGS_CALL_INTERNAL_BOOM2USER);
return this->Create(*this, CREATE_METHOD_ARGS_CALL_INTERNAL_BOOM);
}

bool IMPL_ORMUPDATE(intfAccountPrizes) {
Authorization::checkPriv(_APICALLBOOM.getJWT(), this->privOn(EHTTP_PATCH, this->moduleBaseName()));

return this->Update(*this, UPDATE_METHOD_ARGS_CALL_INTERNAL_BOOM2USER);
return this->Update(*this, UPDATE_METHOD_ARGS_CALL_INTERNAL_BOOM);
}

bool IMPL_ORMDELETE(intfAccountPrizes) {
Authorization::checkPriv(_APICALLBOOM.getJWT(), this->privOn(EHTTP_DELETE, this->moduleBaseName()));

return this->DeleteByPks(*this, DELETE_METHOD_ARGS_CALL_INTERNAL_BOOM2USER);
return this->DeleteByPks(*this, DELETE_METHOD_ARGS_CALL_INTERNAL_BOOM);
}

//} //namespace Targoman::API::AAA {
Expand Down
Loading