mirror of
https://gitlab.com/spaceti-app/integrations/acs/colnod-connector.git
synced 2026-07-12 13:00:41 +02:00
Merge branch 'finalizing' into 'master'
Finalizing See merge request spaceti-app/integrations/acs/colnod-connector!34
This commit is contained in:
commit
400f3c8b40
@ -1 +1 @@
|
||||
Subproject commit 3640c0db5d1e24b314b00cc2b0dd61915080381b
|
||||
Subproject commit d4c4d75b49fe7e0399a6c4e57a7bb32f82bee59d
|
||||
@ -30,7 +30,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
|
||||
// Logger initializiaton
|
||||
serversdk::initAppLogger(); // <-- then Logger::outputMessage will handle all messages
|
||||
serversdk::initAppLogger();
|
||||
|
||||
// Log messge Filtering
|
||||
QStringList filters;
|
||||
|
||||
@ -30,8 +30,8 @@ bool ColnodAPI::downloadData(const qint64 &time)
|
||||
return false;
|
||||
}
|
||||
connect(login.get(), &serversdk::Request::finished, this, [=](){
|
||||
// Get login token
|
||||
if (login->status() != 200) {
|
||||
// Get login token and check if is correct
|
||||
if (!checkRequest(login)) {
|
||||
emit downloadDataFinished(nullptr);
|
||||
return false;
|
||||
}
|
||||
@ -55,6 +55,7 @@ bool ColnodAPI::downloadData(const qint64 &time)
|
||||
Timestamp timestamp;
|
||||
timestamp.setTimestamp(time);
|
||||
|
||||
// emit signal when with all data when it is ready
|
||||
auto checkData = [=]() {
|
||||
int dataCount = data->incrementCounter();
|
||||
if ( dataCount > 5) {
|
||||
@ -69,16 +70,16 @@ bool ColnodAPI::downloadData(const qint64 &time)
|
||||
!data->updatedSubjects.isEmpty() || !data->deletedSubjects.isEmpty()) {
|
||||
qInfo(colnod) << dash;
|
||||
}
|
||||
for (auto token : data->updatedTokens) {
|
||||
for (const auto &token : data->updatedTokens) {
|
||||
qInfo(colnod) << Message::massageTemplate("Download updated", "token", "colnod", token->name());
|
||||
}
|
||||
for (auto id : data->deletedTokens) {
|
||||
for (const auto &id : data->deletedTokens) {
|
||||
qInfo(colnod) << Message::massageTemplate("Download deleted", "token", "colnod", id);
|
||||
}
|
||||
for (auto subject : data->updatedSubjects) {
|
||||
for (const auto &subject : data->updatedSubjects) {
|
||||
qInfo(colnod) << Message::massageTemplate("Download updated", "subject", "colnod", subject->name());
|
||||
}
|
||||
for (auto id : data->deletedSubjects) {
|
||||
for (const auto &id : data->deletedSubjects) {
|
||||
qInfo(colnod) << Message::massageTemplate("Download deleted", "token", "colnod", id);
|
||||
}
|
||||
if (!data->updatedTokens.isEmpty() || !data->deletedTokens.isEmpty() ||
|
||||
@ -91,7 +92,13 @@ bool ColnodAPI::downloadData(const qint64 &time)
|
||||
|
||||
auto tokensRequest = getTokensUpdated(timestamp);
|
||||
connect(tokensRequest.get(), &serversdk::Request::finished, [=](){
|
||||
if (!checkErrors(tokensRequest)) {
|
||||
qCritical(colnod) << "Error during call getTokensUpdated endpoint";
|
||||
emit downloadDataFinished(nullptr);
|
||||
return;
|
||||
}
|
||||
if (!saveUpdatedTokens(data, tokensRequest->responseData())) {
|
||||
qCritical(colnod) << "Error during saving getTokensUpdated";
|
||||
emit downloadDataFinished(nullptr);
|
||||
return;
|
||||
}
|
||||
@ -100,7 +107,13 @@ bool ColnodAPI::downloadData(const qint64 &time)
|
||||
|
||||
auto deletedTokensRequest = getTokensDeleted(timestamp);
|
||||
connect(deletedTokensRequest.get(), &serversdk::Request::finished, [=](){
|
||||
if (!checkErrors(deletedTokensRequest)) {
|
||||
qCritical(colnod) << "Error during call getTokensDeleted endpoint";
|
||||
emit downloadDataFinished(nullptr);
|
||||
return;
|
||||
}
|
||||
if (!saveDeletedTokens(data, deletedTokensRequest->responseData())) {
|
||||
qCritical(colnod) << "Error during saving getTokensDeleted";
|
||||
emit downloadDataFinished(nullptr);
|
||||
return;
|
||||
}
|
||||
@ -109,7 +122,13 @@ bool ColnodAPI::downloadData(const qint64 &time)
|
||||
|
||||
auto subjectsRequest = getSubjectsUpdated(timestamp);
|
||||
connect(subjectsRequest.get(), &serversdk::Request::finished, [=](){
|
||||
if (!checkErrors(subjectsRequest)) {
|
||||
qCritical(colnod) << "Error during call getSubjectsUpdated endpoint";
|
||||
emit downloadDataFinished(nullptr);
|
||||
return;
|
||||
}
|
||||
if (!saveUpdatedSubjects(data, subjectsRequest->responseData())) {
|
||||
qCritical(colnod) << "Error during saving getSubjectsUpdated";
|
||||
emit downloadDataFinished(nullptr);
|
||||
return;
|
||||
}
|
||||
@ -118,7 +137,13 @@ bool ColnodAPI::downloadData(const qint64 &time)
|
||||
|
||||
auto deletedSubjectsRequest = getSubjectsDeleted(timestamp);
|
||||
connect(deletedSubjectsRequest.get(), &serversdk::Request::finished, [=](){
|
||||
if (!checkErrors(deletedSubjectsRequest)) {
|
||||
qCritical(colnod) << "Error during call getSubjectsDeleted endpoint";
|
||||
emit downloadDataFinished(nullptr);
|
||||
return;
|
||||
}
|
||||
if (!saveDeletedSubjects(data, deletedSubjectsRequest->responseData())) {
|
||||
qCritical(colnod) << "Error during saving getSubjectsDeleted";
|
||||
emit downloadDataFinished(nullptr);
|
||||
return;
|
||||
}
|
||||
@ -132,26 +157,67 @@ bool ColnodAPI::downloadData(const qint64 &time)
|
||||
|
||||
void ColnodAPI::processData(const std::shared_ptr<Data> &data)
|
||||
{
|
||||
qInfo(colnod) << tab << "- push col:" << tab << "tokens [" << data->updatedTokens.size() << ":" << data->deletedTokens.size() <<
|
||||
"] [ added/updated : deleted ]";
|
||||
// emit signal when finished
|
||||
auto checkData = [=]() {
|
||||
int dataCount = data->incrementCounter();
|
||||
if ( dataCount > 3) {
|
||||
qCCritical(colnod) << "check data cannnot be higher then 2";
|
||||
}
|
||||
Q_ASSERT(dataCount < 3);
|
||||
if (dataCount == 2) {
|
||||
qInfo(colnod) << tab << "- push col:" << tab << "tokens [" << data->updatedTokens.size() << ":" << data->deletedTokens.size() <<
|
||||
"] [ added/updated : deleted ]";
|
||||
|
||||
connect(this, &ColnodAPI::partOfDataProcessFinished, this, &ColnodAPI::isDataProcessFinished);
|
||||
if (!data->updatedTokens.isEmpty() || !data->deletedTokens.isEmpty() ||
|
||||
!data->updatedSubjects.isEmpty() || !data->deletedSubjects.isEmpty()) {
|
||||
qInfo(colnod) << dash;
|
||||
}
|
||||
|
||||
if (!data->updatedTokens.isEmpty() || !data->deletedTokens.isEmpty()) {
|
||||
for (const auto &token : data->updatedTokens) {
|
||||
qInfo(colnod).noquote() << Message::massageTemplate("Updated", "token", "colnod", token->name());
|
||||
}
|
||||
|
||||
for (const auto &id : data->deletedTokens) {
|
||||
qInfo(colnod).noquote() << Message::massageTemplate("Deleted", "token", "colnod", id);
|
||||
}
|
||||
|
||||
if (!data->updatedTokens.isEmpty() || !data->deletedTokens.isEmpty() ||
|
||||
!data->updatedSubjects.isEmpty() || !data->deletedSubjects.isEmpty()) {
|
||||
qInfo(colnod) << dash;
|
||||
}
|
||||
emit dataProcessFinished(mValid);
|
||||
}
|
||||
};
|
||||
|
||||
if ( !data->updatedTokens.isEmpty() || !data->deletedTokens.isEmpty() ) {
|
||||
qInfo(colnod) << dash;
|
||||
}
|
||||
if ( !data->updatedTokens.isEmpty() && !mDryMode) {
|
||||
setTokens(data->updatedTokens);
|
||||
if ( !data->updatedTokens.isEmpty() && !mDryMode ) {
|
||||
auto setTokensRequest = setTokens(data->updatedTokens);
|
||||
connect(setTokensRequest.get(), &serversdk::Request::finished, [=](){
|
||||
if (!checkErrors(setTokensRequest)) {
|
||||
data->updatedTokens.clear();
|
||||
mValid = false;
|
||||
return;
|
||||
}
|
||||
checkData();
|
||||
});
|
||||
} else {
|
||||
emit partOfDataProcessFinished();
|
||||
checkData();
|
||||
}
|
||||
if ( !data->deletedTokens.isEmpty() && !mDryMode) {
|
||||
deleteTokens(data->deletedTokens);
|
||||
|
||||
if ( !data->deletedTokens.isEmpty() && !mDryMode ) {
|
||||
auto deletedTokensRequest = deleteTokens(data->deletedTokens);
|
||||
connect(deletedTokensRequest.get(), &serversdk::Request::finished, [=](){
|
||||
if (!checkErrors(deletedTokensRequest)) {
|
||||
data->deletedTokens.clear();
|
||||
mValid = false;
|
||||
return;
|
||||
}
|
||||
checkData();
|
||||
});
|
||||
} else {
|
||||
emit partOfDataProcessFinished();
|
||||
}
|
||||
if (!data->updatedTokens.isEmpty() || !data->deletedTokens.isEmpty()) {
|
||||
qInfo(colnod) << dash;
|
||||
checkData();
|
||||
}
|
||||
}
|
||||
|
||||
@ -213,22 +279,6 @@ std::shared_ptr<serversdk::Request> ColnodAPI::setTokens(const QHash<QString, st
|
||||
auto request = composeRequest("/TokenManager/setTokens", Token::tokensToColnodJson(tokens));
|
||||
request->setUserName(authToken);
|
||||
mReqMan->processRequest(request);
|
||||
connect(request.get(), &serversdk::Request::finished, [=](){
|
||||
if (!checkRequest(request)) {
|
||||
qCritical(colnod) << "ERROR during calling /TokenManager/setTokens, request unsuccessful. EXIT";
|
||||
}
|
||||
if (checkResponse(request->responseData())) {
|
||||
for (const auto &token : tokens) {
|
||||
qInfo(colnod).noquote() << Message::massageTemplate("Updated", "token", "colnod", token->name());
|
||||
}
|
||||
emit partOfDataProcessFinished();
|
||||
}
|
||||
else {
|
||||
qWarning(colnod) << "Tokens wasn't updated";
|
||||
mValid = false;
|
||||
emit partOfDataProcessFinished(false);
|
||||
}
|
||||
});
|
||||
return request;
|
||||
}
|
||||
|
||||
@ -237,22 +287,6 @@ std::shared_ptr<serversdk::Request> ColnodAPI::deleteTokens(const QList<QString>
|
||||
auto request = composeRequest("/TokenManager/deleteTokens", Token::idsToJSON(ids));
|
||||
request->setUserName(authToken);
|
||||
mReqMan->processRequest(request);
|
||||
connect(request.get(), &serversdk::Request::finished, [=](){
|
||||
if (!checkRequest(request)) {
|
||||
qCritical(colnod) << "ERROR during calling /TokenManager/deleteTokens, request unsuccessful. EXIT";
|
||||
}
|
||||
if (checkResponse(request->responseData())) {
|
||||
for (const auto &id : ids) {
|
||||
qInfo(colnod).noquote() << Message::massageTemplate("Deleted", "token", "colnod", id);
|
||||
}
|
||||
emit partOfDataProcessFinished();
|
||||
}
|
||||
else {
|
||||
qWarning(colnod) << "Tokens wasn't deleted";
|
||||
mValid = false;
|
||||
emit partOfDataProcessFinished(false);
|
||||
}
|
||||
});
|
||||
return request;
|
||||
}
|
||||
|
||||
@ -267,6 +301,19 @@ bool ColnodAPI::checkRequest(const std::shared_ptr<serversdk::Request> &request)
|
||||
return request->status() == 200;
|
||||
}
|
||||
|
||||
bool ColnodAPI::checkErrors(const std::shared_ptr<serversdk::Request> &request)
|
||||
{
|
||||
if (!checkRequest(request)) {
|
||||
qCWarning(colnod) << "Status is not 200";
|
||||
return false;
|
||||
}
|
||||
if (!checkResponse(request->responseData())) {
|
||||
qCWarning(colnod) << "Empty response data";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ColnodAPI::isValid()
|
||||
{
|
||||
return mValid;
|
||||
@ -292,36 +339,6 @@ void ColnodAPI::setUserName(const QString &userName)
|
||||
mUserName = userName;
|
||||
}
|
||||
|
||||
void ColnodAPI::areDataReady(const std::shared_ptr<Data> &data)
|
||||
{
|
||||
if (++amountData == 4) {
|
||||
|
||||
// new
|
||||
qInfo(colnod) << "- src colnod: tokens [" << data->updatedTokens.size() << ":" << data->deletedTokens.size() <<
|
||||
"] subjects [" << data->updatedSubjects.size() << ":" << data->deletedSubjects.size() << "] [ added/updated : deleted ]";
|
||||
//OLD
|
||||
// qInfo(colnod) << tab << "-> COLNOD DATA:";
|
||||
// qInfo(colnod) << tab << tab << data->updatedTokens.size() << "TOKENS WERE ADDED/UPDATED";
|
||||
// qInfo(colnod) << tab << tab << data->deletedTokens.size() << "TOKENS DELETED";
|
||||
// qInfo(colnod) << tab << tab << data->updatedSubjects.size() << "SUBJECTS WERE ADDED/UPDATED";
|
||||
// qInfo(colnod) << tab << tab << data->deletedSubjects.size() << "SUBJECTS DELETED";
|
||||
// qInfo(colnod) << dash;
|
||||
|
||||
amountData = 0;
|
||||
emit downloadDataFinished(data);
|
||||
}
|
||||
}
|
||||
|
||||
void ColnodAPI::isDataProcessFinished(bool success)
|
||||
{
|
||||
if (!success) {
|
||||
mSuccess = false;
|
||||
}
|
||||
if (++amountProccessesFinished == 2) {
|
||||
emit dataProcessFinished(mSuccess);
|
||||
}
|
||||
}
|
||||
|
||||
bool ColnodAPI::saveUpdatedTokens(const std::shared_ptr<Data>& data, const QByteArray &responseData)
|
||||
{
|
||||
Q_ASSERT(data);
|
||||
|
||||
@ -8,28 +8,43 @@
|
||||
|
||||
Q_DECLARE_LOGGING_CATEGORY(colnod)
|
||||
|
||||
/**
|
||||
* @brief Helper class for simply work with Colnod API
|
||||
*/
|
||||
class LoginResp : public serversdk::DatabaseTable {
|
||||
Q_OBJECT
|
||||
TABLE_PROPERTY(QString, username, setUsername, "")
|
||||
TABLE_PROPERTY(QString, password, setPassword, "")
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Helper class for simply work with Colnod API
|
||||
*/
|
||||
class AuthToken : public serversdk::DatabaseTable {
|
||||
Q_OBJECT
|
||||
TABLE_PROPERTY(QString, id, setId, QString())
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Class for easy switching date time formats
|
||||
* Especially between from and to miliseconds
|
||||
*/
|
||||
class Timestamp : public serversdk::DatabaseTable {
|
||||
Q_OBJECT
|
||||
TABLE_PROPERTY(qint64, timestamp, setTimestamp, 0)
|
||||
public:
|
||||
Timestamp() = default;
|
||||
|
||||
void setTimestamp(QDateTime time)
|
||||
void setTimestamp(const QDateTime &time)
|
||||
{
|
||||
setTimestamp(time.toMSecsSinceEpoch());
|
||||
}
|
||||
|
||||
/**
|
||||
* @breif Function for output date in readable form
|
||||
*
|
||||
* @return String in format: 2019-11-12 16:34:03.932
|
||||
*
|
||||
*/
|
||||
QString toDateString()
|
||||
{
|
||||
return QDateTime::fromMSecsSinceEpoch(a_timestamp).toString("yyyy-MM-dd hh:mm:ss.zzz");
|
||||
@ -39,22 +54,31 @@ class Timestamp : public serversdk::DatabaseTable {
|
||||
class ColnodAPI: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
friend class ColnodTest;
|
||||
public:
|
||||
ColnodAPI() = default;
|
||||
ColnodAPI(serversdk::RequestManager* reqManconst, const bool &dryMode = false, QString userName = "spaceti", QString password = "spaceti1");
|
||||
|
||||
/**
|
||||
* @brief Downloading changed data from Colnod since last Synchronization
|
||||
*
|
||||
* @param time Time of last sync
|
||||
*
|
||||
* @return Function returns data changed after this time
|
||||
*/
|
||||
bool downloadData(const qint64 &time);
|
||||
|
||||
/**
|
||||
* @brief Push all data prepare during synchronization to Colnod
|
||||
* @param data Data which are pushing to Colnod
|
||||
*/
|
||||
void processData(const std::shared_ptr<Data> &data);
|
||||
|
||||
std::shared_ptr<serversdk::Request> requestLogin();
|
||||
|
||||
/**
|
||||
* @brief Endpoint of Colnod API
|
||||
* @param time Returns changed tokens after time
|
||||
* @return Return request, than waiting for finished
|
||||
*/
|
||||
std::shared_ptr<serversdk::Request> getTokensUpdated(const Timestamp &time);
|
||||
std::shared_ptr<serversdk::Request> getTokensDeleted(const Timestamp &time);
|
||||
std::shared_ptr<serversdk::Request> getSubjectsUpdated(const Timestamp &time);
|
||||
std::shared_ptr<serversdk::Request> getSubjectsDeleted(const Timestamp &time);
|
||||
|
||||
std::shared_ptr<serversdk::Request> setTokens(const QHash<QString, std::shared_ptr<Token>> &tokens);
|
||||
std::shared_ptr<serversdk::Request> deleteTokens(const QList<QString> &tokenIds);
|
||||
|
||||
// Attributes
|
||||
QString userName() const;
|
||||
@ -65,42 +89,49 @@ public:
|
||||
bool isValid();
|
||||
|
||||
private:
|
||||
Q_SLOT void areDataReady(const std::shared_ptr<Data>& data);
|
||||
Q_SLOT void isDataProcessFinished(bool success);
|
||||
std::shared_ptr<serversdk::Request> requestLogin();
|
||||
|
||||
private:
|
||||
// All endpoints of Colnod API that we need to use, simply returns request
|
||||
// than we wait for finish
|
||||
std::shared_ptr<serversdk::Request> getTokensDeleted(const Timestamp &time);
|
||||
std::shared_ptr<serversdk::Request> getSubjectsUpdated(const Timestamp &time);
|
||||
std::shared_ptr<serversdk::Request> getSubjectsDeleted(const Timestamp &time);
|
||||
|
||||
std::shared_ptr<serversdk::Request> setTokens(const QHash<QString, std::shared_ptr<Token>> &tokens);
|
||||
std::shared_ptr<serversdk::Request> deleteTokens(const QList<QString> &tokenIds);
|
||||
|
||||
// Functions which saves response data from Colnod to variable data
|
||||
bool saveUpdatedTokens(const std::shared_ptr<Data>& data, const QByteArray &responseData);
|
||||
bool saveUpdatedSubjects(const std::shared_ptr<Data>& data, const QByteArray &responseData);
|
||||
bool saveDeletedTokens(const std::shared_ptr<Data>& data, const QByteArray &responseData);
|
||||
bool saveDeletedSubjects(const std::shared_ptr<Data>& data, const QByteArray &responseData);
|
||||
|
||||
// Helper func for check format of response
|
||||
bool checkResponse(const QByteArray &responseData);
|
||||
|
||||
std::shared_ptr<serversdk::Request> composeRequest(const QString &endpoint, const QByteArray &data);
|
||||
|
||||
// Helper func for check status of request
|
||||
bool checkRequest(const std::shared_ptr<serversdk::Request> &request);
|
||||
|
||||
bool checkErrors(const std::shared_ptr<serversdk::Request> &request);
|
||||
|
||||
// Prepares request to be sent
|
||||
std::shared_ptr<serversdk::Request> composeRequest(const QString &endpoint, const QByteArray &data);
|
||||
|
||||
signals:
|
||||
/**
|
||||
* @brief Signal which transfer data to Sync Manager
|
||||
*/
|
||||
void downloadDataFinished(std::shared_ptr<Data> = std::shared_ptr<Data>());
|
||||
void partOfDataReady(std::shared_ptr<Data> = std::shared_ptr<Data>());
|
||||
void partOfDataProcessFinished(bool success = true);
|
||||
void dataProcessFinished(bool success = true);
|
||||
|
||||
void error();
|
||||
|
||||
private:
|
||||
serversdk::RequestManager* mReqMan = nullptr;
|
||||
QString mHost = "https://192.168.1.3:8443";
|
||||
QString mUserName;
|
||||
QString mPassword;
|
||||
Timestamp mLastSync;
|
||||
bool mDryMode = false;
|
||||
bool mValid = true;
|
||||
bool mSuccess = true;
|
||||
|
||||
QString authToken;
|
||||
qint16 amountData = 0;
|
||||
qint16 amountProccessesFinished = 0;
|
||||
};
|
||||
|
||||
|
||||
@ -1,14 +1,5 @@
|
||||
#include "data.h"
|
||||
|
||||
|
||||
void Data::clear()
|
||||
{
|
||||
updatedTokens.clear();
|
||||
updatedSubjects.clear();
|
||||
deletedTokens.clear();
|
||||
deletedSubjects.clear();
|
||||
}
|
||||
|
||||
bool Data::loadTokensFromJson(const QByteArray &data)
|
||||
{
|
||||
if (data.isEmpty())
|
||||
|
||||
@ -27,10 +27,18 @@ class Data
|
||||
friend class DatabaseAPI;
|
||||
public:
|
||||
Data() = default;
|
||||
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* @brief Fill data with tokens from JSON
|
||||
* @param data Response data in JSON format
|
||||
* @return True if success otherwise False
|
||||
*/
|
||||
bool loadTokensFromJson(const QByteArray &data);
|
||||
|
||||
/**
|
||||
* @brief Fill data with subjects from JSON
|
||||
* @param data Response data in JSON format
|
||||
* @return True if success otherwise False
|
||||
*/
|
||||
bool loadSubjectsFormJson(const QByteArray &data);
|
||||
|
||||
QHash<QString, std::shared_ptr<Token>> updatedTokens;
|
||||
@ -38,9 +46,12 @@ public:
|
||||
QHash<QString, std::shared_ptr<Subject>> updatedSubjects;
|
||||
QList<QString> deletedSubjects;
|
||||
|
||||
/**
|
||||
* @brief Helper function for check if all endpoints calls are processed
|
||||
* @return Returns acctual amout of processed endpoints
|
||||
*/
|
||||
int incrementCounter();
|
||||
|
||||
|
||||
private:
|
||||
int counter = 0;
|
||||
};
|
||||
|
||||
@ -71,16 +71,16 @@ bool DatabaseAPI::processData(const std::shared_ptr<Data> &data)
|
||||
!data->updatedSubjects.isEmpty() || !data->deletedSubjects.isEmpty()) {
|
||||
qInfo(database) << dash;
|
||||
}
|
||||
for (auto token : data->updatedTokens) {
|
||||
for (const auto &token : data->updatedTokens) {
|
||||
qInfo(database) << Message::massageTemplate("Updated", "token", "database", token->name());
|
||||
}
|
||||
for (auto id : data->deletedTokens) {
|
||||
for (const auto &id : data->deletedTokens) {
|
||||
qInfo(database) << Message::massageTemplate("Deleted", "token", "database", id);
|
||||
}
|
||||
for (auto subject : data->updatedSubjects) {
|
||||
for (const auto &subject : data->updatedSubjects) {
|
||||
qInfo(database) << Message::massageTemplate("Updated", "subject", "database", subject->name());
|
||||
}
|
||||
for (auto id : data->deletedSubjects) {
|
||||
for (const auto &id : data->deletedSubjects) {
|
||||
qInfo(database) << Message::massageTemplate("Deleted", "token", "database", id);
|
||||
}
|
||||
if (!data->updatedTokens.isEmpty() || !data->deletedTokens.isEmpty() ||
|
||||
@ -91,7 +91,7 @@ bool DatabaseAPI::processData(const std::shared_ptr<Data> &data)
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
// save data to structure Data to be sent to syncManager
|
||||
std::shared_ptr<Data> DatabaseAPI::composeData(const QSqlQuery &updatedQuery, QSqlQuery &deletedQuery)
|
||||
{
|
||||
auto data = std::make_shared<Data>();
|
||||
@ -109,10 +109,10 @@ std::shared_ptr<Data> DatabaseAPI::composeData(const QSqlQuery &updatedQuery, QS
|
||||
if (!data->updatedTokens.isEmpty() || !data->deletedTokens.isEmpty()) {
|
||||
qInfo(database) << dash;
|
||||
}
|
||||
for (auto token : data->updatedTokens) {
|
||||
for (const auto &token : data->updatedTokens) {
|
||||
qInfo(database) << Message::massageTemplate("Download updated", "token", "database", token->name());
|
||||
}
|
||||
for (auto id : data->deletedTokens) {
|
||||
for (const auto &id : data->deletedTokens) {
|
||||
qInfo(database) << Message::massageTemplate("Download deleted", "token", "database", id);
|
||||
}
|
||||
if (!data->updatedTokens.isEmpty() || !data->deletedTokens.isEmpty()) {
|
||||
@ -122,12 +122,13 @@ std::shared_ptr<Data> DatabaseAPI::composeData(const QSqlQuery &updatedQuery, QS
|
||||
return data;
|
||||
}
|
||||
|
||||
// Time of last synchronization
|
||||
qint64 DatabaseAPI::getLastSync()
|
||||
{
|
||||
auto query = mDbMan->createQuery();
|
||||
if (!executeQuery(query, "SELECT last_sync FROM config")) {
|
||||
qCritical(database) << "ERROR during getting time of last sync. Exit.";
|
||||
emit error();
|
||||
return -1;
|
||||
}
|
||||
if (query.next()){
|
||||
return query.value(0).toLongLong();
|
||||
@ -135,17 +136,19 @@ qint64 DatabaseAPI::getLastSync()
|
||||
return -1;
|
||||
}
|
||||
|
||||
void DatabaseAPI::setLastSync(qint64 timestamp)
|
||||
bool DatabaseAPI::setLastSync(qint64 timestamp)
|
||||
{
|
||||
auto query = mDbMan->createQuery();
|
||||
query.prepare("UPDATE config SET last_sync = :last_sync");
|
||||
query.bindValue(":last_sync", timestamp);
|
||||
if (!executeQuery(query)) {
|
||||
qCritical(database) << "ERROR during set time of this synchronization as last sync time";
|
||||
emit error();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// It update last_update time in db of deleted tokens, because in colnod is later time (time of real deletion from Colnod)
|
||||
void DatabaseAPI::reupdateDeleted(const QList<QString>& ids)
|
||||
{
|
||||
auto query = mDbMan->createQuery();
|
||||
@ -172,18 +175,16 @@ qint64 DatabaseAPI::getNow()
|
||||
return query.value(0).toLongLong();
|
||||
}
|
||||
|
||||
// All tokens which are pushed to Colnad are update back to db, because of diffrent last_update and init of new tokens
|
||||
bool DatabaseAPI::uploadTokens(const QHash<QString, std::shared_ptr<Token>> &tokens, const QSet<QString> &filters)
|
||||
{
|
||||
auto query = mDbMan->createQuery();
|
||||
for (const auto &token : tokens) {
|
||||
if (exists(token->id(), "colnod_token")) {
|
||||
query = utils->updateTable(token.get(), "colnod_token", filters);
|
||||
if (filters.contains("last_modified"))
|
||||
qInfo(database).noquote() << Message::massageTemplate("Updating", "token", "database", token->name());
|
||||
}
|
||||
else {
|
||||
query = utils->insertIntoTable(token.get(), "colnod_token");
|
||||
qInfo(database).noquote() << Message::massageTemplate("Adding", "token", "database", token->name());
|
||||
}
|
||||
if (!query.isActive()) {
|
||||
mValid = false;
|
||||
@ -213,11 +214,9 @@ bool DatabaseAPI::uploadSubjects(const QHash<QString, std::shared_ptr<Subject>>
|
||||
}
|
||||
if (exists(subject->id(), "colnod_subject")) {
|
||||
query = utils->updateTable(subject.get(), "colnod_subject");
|
||||
qInfo(database).noquote() << Message::massageTemplate("Updating", "subject", "database", subject->name());
|
||||
}
|
||||
else {
|
||||
query = utils->insertIntoTable(subject.get(), "colnod_subject");
|
||||
qInfo(database).noquote() << Message::massageTemplate("Adding", "subject", "database", subject->name());
|
||||
}
|
||||
if (!query.isActive()) {
|
||||
mValid = false;
|
||||
@ -246,8 +245,6 @@ bool DatabaseAPI::deleteItems(const QList<QString> &ids, const QString &tableNam
|
||||
}
|
||||
|
||||
for (const auto &id : ids) {
|
||||
qInfo(database).noquote() << Message::massageTemplate("Deleting", tableName.mid(tableName.indexOf("_") + 1, tableName.length() - 1), "database", id);
|
||||
|
||||
query.prepare(QString("UPDATE %1 SET deleted = true, last_update = :now, last_modified = :now WHERE id = :id").arg(tableName));
|
||||
query.bindValue(":id", id);
|
||||
query.bindValue(":now", nowString);
|
||||
|
||||
@ -20,41 +20,104 @@ class DatabaseAPI : public QObject
|
||||
public:
|
||||
explicit DatabaseAPI(serversdk::DatabaseManager *dbMan, bool dryMode);
|
||||
|
||||
/**
|
||||
* @brief Connecting to database
|
||||
* @param host
|
||||
* @param name
|
||||
* @param user
|
||||
* @param password
|
||||
* @return True if success False otherwise
|
||||
*/
|
||||
bool open(const QString& host = "192.168.1.94",
|
||||
const QString& name = "deloitte",
|
||||
const QString& user = "postgres",
|
||||
const QString& password = "34rjkciea12");
|
||||
|
||||
bool executeQuery(QSqlQuery &query, const QString &statement = QString());
|
||||
|
||||
std::shared_ptr<Data> downloadData();
|
||||
bool processData(const std::shared_ptr<Data> &data);
|
||||
|
||||
/**
|
||||
* @brief Get time of last synchronization
|
||||
*
|
||||
* LastSync time is stored in database, config table
|
||||
*/
|
||||
qint64 getLastSync();
|
||||
void setLastSync(qint64 timestamp);
|
||||
|
||||
/**
|
||||
* @brief Set time of acctual synchronization
|
||||
* @param timestamp
|
||||
*/
|
||||
bool setLastSync(qint64 timestamp);
|
||||
|
||||
/**
|
||||
* @brief Update time of tokens which were deleted in actual sync
|
||||
* @param ids Ids of deleted items
|
||||
*/
|
||||
void reupdateDeleted(const QList<QString>& ids);
|
||||
|
||||
/**
|
||||
* @brief Get time from database server
|
||||
* @return Time in miliseconds
|
||||
*/
|
||||
qint64 getNow();
|
||||
|
||||
|
||||
// process data
|
||||
/**
|
||||
* @brief Push tokens prepared in this sync to database
|
||||
*
|
||||
* @param tokens Tokens to be pushed to database
|
||||
* @param filters Can be used to omit tokens properties (Properties in filters won't be pushed to database
|
||||
*
|
||||
* @return True if success, False otherwise
|
||||
*/
|
||||
bool uploadTokens(const QHash<QString, std::shared_ptr<Token> > &tokens, const QSet<QString> &filters = {});
|
||||
|
||||
/**
|
||||
* @brief Push subjects prepared in this sync to database
|
||||
*
|
||||
* @param subjects Subjects prepared to be pushed to database
|
||||
*
|
||||
* @return True if success, False otherwise
|
||||
*/
|
||||
bool uploadSubjects(const QHash<QString, std::shared_ptr<Subject> > &subjects);
|
||||
|
||||
/**
|
||||
* @brief Set deleted of tokens/subjects to true
|
||||
*
|
||||
* @param ids Items which was prepared to be deleted from database
|
||||
* @param tableName Name of table with subjects or tokens
|
||||
*
|
||||
* @return True if success, False otherwise
|
||||
*/
|
||||
bool deleteItems(const QList<QString> &ids, const QString &tableName);
|
||||
|
||||
/**
|
||||
* @brief Get token from database
|
||||
*
|
||||
* @param id Id of token accessing
|
||||
*
|
||||
* @return Return token
|
||||
*/
|
||||
std::shared_ptr<Token> getToken(const QString &id);
|
||||
|
||||
/**
|
||||
* @brief Check if this sync was without any problem
|
||||
*/
|
||||
bool isValid();
|
||||
|
||||
// Helper functions for naming new tokens
|
||||
qint16 getNumOfTokensOfSubject(const QString &id);
|
||||
QString getSubjectNameByTokenId(const QString &tokenId);
|
||||
|
||||
private:
|
||||
int exists(const QString &id, const QString &tableName);
|
||||
bool executeQuery(QSqlQuery &query, const QString &statement = QString());
|
||||
|
||||
std::shared_ptr<Data> composeData(const QSqlQuery &updatedQuery, QSqlQuery &deletedQuery);
|
||||
|
||||
signals:
|
||||
void downloadDataFinished(std::shared_ptr<Data> = std::shared_ptr<Data>());
|
||||
void error();
|
||||
|
||||
private:
|
||||
bool mValid = true;
|
||||
|
||||
@ -28,7 +28,7 @@ bool SyncManager::databaseInitialization()
|
||||
}
|
||||
|
||||
// here -> nothing can fail
|
||||
mlastSync.setTimestamp(database->getLastSync());
|
||||
mlastSync.setTimestamp(lastsync_in_ms);
|
||||
colnod = std::make_shared<ColnodAPI>(&reqMan, mDryMode);
|
||||
return true;
|
||||
}
|
||||
@ -36,7 +36,7 @@ bool SyncManager::databaseInitialization()
|
||||
|
||||
bool SyncManager::startSynchronization()
|
||||
{
|
||||
// I have to current time from database
|
||||
// Need current time from db server
|
||||
qint64 syncBegin = database->getNow();
|
||||
if (syncBegin == -1) {
|
||||
qCritical(syncmanager) << "Not possible to get last sync date";
|
||||
@ -55,20 +55,21 @@ bool SyncManager::startSynchronization()
|
||||
|
||||
colnod->downloadData(mlastSync.timestamp());
|
||||
connect(colnod.get(), &ColnodAPI::downloadDataFinished, [=](const std::shared_ptr<Data> &colnodData) {
|
||||
|
||||
// NOW I HAVE DATA FROM COLNOD
|
||||
if (!colnodData) {
|
||||
qCritical(syncmanager) << "Not possible to get correct data from colnod";
|
||||
QCoreApplication::exit(-1);
|
||||
return;
|
||||
}
|
||||
|
||||
std::unique_ptr<PreparedData> preparedData = prepareDataNew(databaseData, colnodData);
|
||||
std::unique_ptr<PreparedData> preparedData = prepareData(databaseData, colnodData);
|
||||
if (!preparedData) {
|
||||
qCritical(syncmanager) << "Not possible to compose data";
|
||||
QCoreApplication::exit(-1);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!database->processData(preparedData->colnodData)) {
|
||||
qCritical(syncmanager) << "Not possible to upload data to database";
|
||||
}
|
||||
@ -108,7 +109,10 @@ bool SyncManager::startSynchronization()
|
||||
qWarning(syncmanager) << "Unsuccessful FINISH, check warnings";
|
||||
}
|
||||
else {
|
||||
database->setLastSync(syncBegin);
|
||||
// set new time of last sync to this time of this run
|
||||
if (!database->setLastSync(syncBegin)) {
|
||||
qWarning(syncmanager) << "Unsuccessful FINISH, check warnings";
|
||||
}
|
||||
qInfo(syncmanager) << "Successful FINISH";
|
||||
}
|
||||
QCoreApplication::exit();
|
||||
@ -117,12 +121,11 @@ bool SyncManager::startSynchronization()
|
||||
});
|
||||
colnod->processData(preparedData->databaseData);
|
||||
});
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<PreparedData> SyncManager::prepareDataNew(const std::shared_ptr<Data> &databaseData, const std::shared_ptr<Data> &colnodData)
|
||||
// goes through downloaded data and check if consist conflicts (both sides edit), already sync/deleted tokens or new tokens
|
||||
std::unique_ptr<PreparedData> SyncManager::prepareData(const std::shared_ptr<Data> &databaseData, const std::shared_ptr<Data> &colnodData)
|
||||
{
|
||||
auto preparedColnodData = std::make_shared<Data>();
|
||||
auto preparedDatabaseData = std::make_shared<Data>();
|
||||
|
||||
@ -15,19 +15,20 @@ public:
|
||||
bool databaseInitialization();
|
||||
bool startSynchronization();
|
||||
|
||||
// New approach
|
||||
private:
|
||||
/**
|
||||
* @brief Preapre data to be pushed to database and colnod from actual downloaded data
|
||||
* @param data from database
|
||||
* @param data from colnod
|
||||
* @return prepared data to be pushed into colnod and db
|
||||
*
|
||||
* @todo rename when removed old aproach
|
||||
*/
|
||||
std::unique_ptr<PreparedData> prepareDataNew(const std::shared_ptr<Data> &databaseData, const std::shared_ptr<Data> &colnodData);
|
||||
std::unique_ptr<PreparedData> prepareData(const std::shared_ptr<Data> &databaseData, const std::shared_ptr<Data> &colnodData);
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Set default values to new tokens from db
|
||||
* @param token New token in this run
|
||||
*/
|
||||
void initNewToken(const std::shared_ptr<Token>& token);
|
||||
|
||||
private:
|
||||
|
||||
@ -24,9 +24,7 @@ class Token : public serversdk::DatabaseTable {
|
||||
TABLE_PROPERTY(QDateTime, lastModified, setLastModified, QDateTime())
|
||||
TABLE_PROPERTY(bool, deleted, setDeleted, false)
|
||||
|
||||
|
||||
|
||||
public:
|
||||
public:
|
||||
/**
|
||||
* # Needed values to proper sync
|
||||
*
|
||||
@ -37,7 +35,7 @@ class Token : public serversdk::DatabaseTable {
|
||||
*
|
||||
*/
|
||||
|
||||
explicit Token() = default;
|
||||
explicit Token() = default;
|
||||
~Token() override;
|
||||
|
||||
QByteArray jsonData(const QSet<QString> &filters = {}) const override;
|
||||
@ -49,6 +47,11 @@ class Token : public serversdk::DatabaseTable {
|
||||
friend bool operator>(const std::shared_ptr<Token> &lToken, const std::shared_ptr<Token> &rToken){
|
||||
return lToken->lastModified() > rToken->lastUpdate();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Compare of two tokens
|
||||
* @return Returns true, if tokens are same
|
||||
*/
|
||||
friend bool operator==(const std::shared_ptr<Token> &lToken, const std::shared_ptr<Token> &rToken)
|
||||
{
|
||||
return lToken->id() == rToken->id() &&
|
||||
|
||||
@ -56,7 +56,7 @@ void SyncManagerTest::prepareData_test()
|
||||
token->setId("id1");
|
||||
dbData->updatedTokens.insert("id1", token);
|
||||
|
||||
auto newPreparedData = sync.prepareDataNew(dbData, colnodData);
|
||||
auto newPreparedData = sync.prepareData(dbData, colnodData);
|
||||
QVERIFY(newPreparedData->databaseData->updatedTokens.contains("id1"));
|
||||
QVERIFY(newPreparedData->databaseData->updatedSubjects.isEmpty());
|
||||
QVERIFY(newPreparedData->databaseData->deletedTokens.isEmpty());
|
||||
@ -99,7 +99,7 @@ void SyncManagerTest::prepareData_tokens()
|
||||
if ( colnod_dtoken )
|
||||
colnodData->deletedTokens.append(colnod_dtoken->id());
|
||||
|
||||
auto newPreparedData = sync.prepareDataNew(dbData, colnodData);
|
||||
auto newPreparedData = sync.prepareData(dbData, colnodData);
|
||||
|
||||
|
||||
auto testTokens = [=](const QSet<QString> &set, const QString &expectedId) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user