comment rest of code

This commit is contained in:
Ondřej Fischer 2019-12-16 09:18:06 +01:00
parent 2bc1301fcd
commit d49db3f6b0
7 changed files with 112 additions and 23 deletions

View File

@ -30,7 +30,7 @@ bool ColnodAPI::downloadData(const qint64 &time)
return false; return false;
} }
connect(login.get(), &serversdk::Request::finished, this, [=](){ connect(login.get(), &serversdk::Request::finished, this, [=](){
// Get login token // Get login token and check if is correct
if (!checkRequest(login)) { if (!checkRequest(login)) {
emit downloadDataFinished(nullptr); emit downloadDataFinished(nullptr);
return false; return false;
@ -55,6 +55,7 @@ bool ColnodAPI::downloadData(const qint64 &time)
Timestamp timestamp; Timestamp timestamp;
timestamp.setTimestamp(time); timestamp.setTimestamp(time);
// emit signal when with all data when it is ready
auto checkData = [=]() { auto checkData = [=]() {
int dataCount = data->incrementCounter(); int dataCount = data->incrementCounter();
if ( dataCount > 5) { if ( dataCount > 5) {
@ -156,7 +157,8 @@ bool ColnodAPI::downloadData(const qint64 &time)
void ColnodAPI::processData(const std::shared_ptr<Data> &data) void ColnodAPI::processData(const std::shared_ptr<Data> &data)
{ {
auto checkData = [=]() { // emit signal when finished
auto checkData = [=]() {
int dataCount = data->incrementCounter(); int dataCount = data->incrementCounter();
if ( dataCount > 3) { if ( dataCount > 3) {
qCCritical(colnod) << "check data cannnot be higher then 2"; qCCritical(colnod) << "check data cannnot be higher then 2";

View File

@ -27,8 +27,18 @@ class Data
friend class DatabaseAPI; friend class DatabaseAPI;
public: public:
Data() = default; Data() = default;
/**
* @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); 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); bool loadSubjectsFormJson(const QByteArray &data);
QHash<QString, std::shared_ptr<Token>> updatedTokens; QHash<QString, std::shared_ptr<Token>> updatedTokens;
@ -36,6 +46,10 @@ public:
QHash<QString, std::shared_ptr<Subject>> updatedSubjects; QHash<QString, std::shared_ptr<Subject>> updatedSubjects;
QList<QString> deletedSubjects; QList<QString> deletedSubjects;
/**
* @brief Helper function for check if all endpoints calls are processed
* @return Returns acctual amout of processed endpoints
*/
int incrementCounter(); int incrementCounter();
private: private:

View File

@ -91,7 +91,7 @@ bool DatabaseAPI::processData(const std::shared_ptr<Data> &data)
return res; return res;
} }
// save data to structure Data to be sent to syncManager
std::shared_ptr<Data> DatabaseAPI::composeData(const QSqlQuery &updatedQuery, QSqlQuery &deletedQuery) std::shared_ptr<Data> DatabaseAPI::composeData(const QSqlQuery &updatedQuery, QSqlQuery &deletedQuery)
{ {
auto data = std::make_shared<Data>(); auto data = std::make_shared<Data>();
@ -122,12 +122,13 @@ std::shared_ptr<Data> DatabaseAPI::composeData(const QSqlQuery &updatedQuery, QS
return data; return data;
} }
// Time of last synchronization
qint64 DatabaseAPI::getLastSync() qint64 DatabaseAPI::getLastSync()
{ {
auto query = mDbMan->createQuery(); auto query = mDbMan->createQuery();
if (!executeQuery(query, "SELECT last_sync FROM config")) { if (!executeQuery(query, "SELECT last_sync FROM config")) {
qCritical(database) << "ERROR during getting time of last sync. Exit."; qCritical(database) << "ERROR during getting time of last sync. Exit.";
emit error(); return -1;
} }
if (query.next()){ if (query.next()){
return query.value(0).toLongLong(); return query.value(0).toLongLong();
@ -135,17 +136,19 @@ qint64 DatabaseAPI::getLastSync()
return -1; return -1;
} }
void DatabaseAPI::setLastSync(qint64 timestamp) bool DatabaseAPI::setLastSync(qint64 timestamp)
{ {
auto query = mDbMan->createQuery(); auto query = mDbMan->createQuery();
query.prepare("UPDATE config SET last_sync = :last_sync"); query.prepare("UPDATE config SET last_sync = :last_sync");
query.bindValue(":last_sync", timestamp); query.bindValue(":last_sync", timestamp);
if (!executeQuery(query)) { if (!executeQuery(query)) {
qCritical(database) << "ERROR during set time of this synchronization as last sync time"; 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) void DatabaseAPI::reupdateDeleted(const QList<QString>& ids)
{ {
auto query = mDbMan->createQuery(); auto query = mDbMan->createQuery();
@ -172,18 +175,16 @@ qint64 DatabaseAPI::getNow()
return query.value(0).toLongLong(); 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) bool DatabaseAPI::uploadTokens(const QHash<QString, std::shared_ptr<Token>> &tokens, const QSet<QString> &filters)
{ {
auto query = mDbMan->createQuery(); auto query = mDbMan->createQuery();
for (const auto &token : tokens) { for (const auto &token : tokens) {
if (exists(token->id(), "colnod_token")) { if (exists(token->id(), "colnod_token")) {
query = utils->updateTable(token.get(), "colnod_token", filters); query = utils->updateTable(token.get(), "colnod_token", filters);
if (filters.contains("last_modified"))
qInfo(database).noquote() << Message::massageTemplate("Updating", "token", "database", token->name());
} }
else { else {
query = utils->insertIntoTable(token.get(), "colnod_token"); query = utils->insertIntoTable(token.get(), "colnod_token");
qInfo(database).noquote() << Message::massageTemplate("Adding", "token", "database", token->name());
} }
if (!query.isActive()) { if (!query.isActive()) {
mValid = false; mValid = false;
@ -213,11 +214,9 @@ bool DatabaseAPI::uploadSubjects(const QHash<QString, std::shared_ptr<Subject>>
} }
if (exists(subject->id(), "colnod_subject")) { if (exists(subject->id(), "colnod_subject")) {
query = utils->updateTable(subject.get(), "colnod_subject"); query = utils->updateTable(subject.get(), "colnod_subject");
qInfo(database).noquote() << Message::massageTemplate("Updating", "subject", "database", subject->name());
} }
else { else {
query = utils->insertIntoTable(subject.get(), "colnod_subject"); query = utils->insertIntoTable(subject.get(), "colnod_subject");
qInfo(database).noquote() << Message::massageTemplate("Adding", "subject", "database", subject->name());
} }
if (!query.isActive()) { if (!query.isActive()) {
mValid = false; mValid = false;
@ -246,8 +245,6 @@ bool DatabaseAPI::deleteItems(const QList<QString> &ids, const QString &tableNam
} }
for (const auto &id : ids) { 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.prepare(QString("UPDATE %1 SET deleted = true, last_update = :now, last_modified = :now WHERE id = :id").arg(tableName));
query.bindValue(":id", id); query.bindValue(":id", id);
query.bindValue(":now", nowString); query.bindValue(":now", nowString);

View File

@ -20,35 +20,99 @@ class DatabaseAPI : public QObject
public: public:
explicit DatabaseAPI(serversdk::DatabaseManager *dbMan, bool dryMode); 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", bool open(const QString& host = "192.168.1.94",
const QString& name = "deloitte", const QString& name = "deloitte",
const QString& user = "postgres", const QString& user = "postgres",
const QString& password = "34rjkciea12"); const QString& password = "34rjkciea12");
bool executeQuery(QSqlQuery &query, const QString &statement = QString());
std::shared_ptr<Data> downloadData(); std::shared_ptr<Data> downloadData();
bool processData(const std::shared_ptr<Data> &data); bool processData(const std::shared_ptr<Data> &data);
/**
* @brief Get time of last synchronization
*
* LastSync time is stored in database, config table
*/
qint64 getLastSync(); 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); void reupdateDeleted(const QList<QString>& ids);
/**
* @brief Get time from database server
* @return Time in miliseconds
*/
qint64 getNow(); 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 = {}); 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); 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); 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); std::shared_ptr<Token> getToken(const QString &id);
/**
* @brief Check if this sync was without any problem
*/
bool isValid(); bool isValid();
// Helper functions for naming new tokens
qint16 getNumOfTokensOfSubject(const QString &id); qint16 getNumOfTokensOfSubject(const QString &id);
QString getSubjectNameByTokenId(const QString &tokenId); QString getSubjectNameByTokenId(const QString &tokenId);
private: private:
int exists(const QString &id, const QString &tableName); 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); std::shared_ptr<Data> composeData(const QSqlQuery &updatedQuery, QSqlQuery &deletedQuery);

View File

@ -28,7 +28,7 @@ bool SyncManager::databaseInitialization()
} }
// here -> nothing can fail // here -> nothing can fail
mlastSync.setTimestamp(database->getLastSync()); mlastSync.setTimestamp(lastsync_in_ms);
colnod = std::make_shared<ColnodAPI>(&reqMan, mDryMode); colnod = std::make_shared<ColnodAPI>(&reqMan, mDryMode);
return true; return true;
} }
@ -36,7 +36,7 @@ bool SyncManager::databaseInitialization()
bool SyncManager::startSynchronization() bool SyncManager::startSynchronization()
{ {
// I have to current time from database // Need current time from db server
qint64 syncBegin = database->getNow(); qint64 syncBegin = database->getNow();
if (syncBegin == -1) { if (syncBegin == -1) {
qCritical(syncmanager) << "Not possible to get last sync date"; qCritical(syncmanager) << "Not possible to get last sync date";
@ -55,6 +55,8 @@ bool SyncManager::startSynchronization()
colnod->downloadData(mlastSync.timestamp()); colnod->downloadData(mlastSync.timestamp());
connect(colnod.get(), &ColnodAPI::downloadDataFinished, [=](const std::shared_ptr<Data> &colnodData) { connect(colnod.get(), &ColnodAPI::downloadDataFinished, [=](const std::shared_ptr<Data> &colnodData) {
// NOW I HAVE DATA FROM COLNOD
if (!colnodData) { if (!colnodData) {
qCritical(syncmanager) << "Not possible to get correct data from colnod"; qCritical(syncmanager) << "Not possible to get correct data from colnod";
QCoreApplication::exit(-1); QCoreApplication::exit(-1);
@ -68,7 +70,6 @@ bool SyncManager::startSynchronization()
return; return;
} }
if (!database->processData(preparedData->colnodData)) { if (!database->processData(preparedData->colnodData)) {
qCritical(syncmanager) << "Not possible to upload data to database"; qCritical(syncmanager) << "Not possible to upload data to database";
} }
@ -108,7 +109,10 @@ bool SyncManager::startSynchronization()
qWarning(syncmanager) << "Unsuccessful FINISH, check warnings"; qWarning(syncmanager) << "Unsuccessful FINISH, check warnings";
} }
else { 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"; qInfo(syncmanager) << "Successful FINISH";
} }
QCoreApplication::exit(); QCoreApplication::exit();
@ -117,11 +121,10 @@ bool SyncManager::startSynchronization()
}); });
colnod->processData(preparedData->databaseData); colnod->processData(preparedData->databaseData);
}); });
return true; return true;
} }
// 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) 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 preparedColnodData = std::make_shared<Data>();

View File

@ -25,6 +25,10 @@ private:
std::unique_ptr<PreparedData> prepareData(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: 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); void initNewToken(const std::shared_ptr<Token>& token);
private: private:

View File

@ -47,6 +47,11 @@ public:
friend bool operator>(const std::shared_ptr<Token> &lToken, const std::shared_ptr<Token> &rToken){ friend bool operator>(const std::shared_ptr<Token> &lToken, const std::shared_ptr<Token> &rToken){
return lToken->lastModified() > rToken->lastUpdate(); 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) friend bool operator==(const std::shared_ptr<Token> &lToken, const std::shared_ptr<Token> &rToken)
{ {
return lToken->id() == rToken->id() && return lToken->id() == rToken->id() &&