forked from ondra/colnod-connector
comment rest of code
This commit is contained in:
parent
2bc1301fcd
commit
d49db3f6b0
@ -30,7 +30,7 @@ bool ColnodAPI::downloadData(const qint64 &time)
|
||||
return false;
|
||||
}
|
||||
connect(login.get(), &serversdk::Request::finished, this, [=](){
|
||||
// Get login token
|
||||
// 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) {
|
||||
@ -156,7 +157,8 @@ bool ColnodAPI::downloadData(const qint64 &time)
|
||||
|
||||
void ColnodAPI::processData(const std::shared_ptr<Data> &data)
|
||||
{
|
||||
auto checkData = [=]() {
|
||||
// emit signal when finished
|
||||
auto checkData = [=]() {
|
||||
int dataCount = data->incrementCounter();
|
||||
if ( dataCount > 3) {
|
||||
qCCritical(colnod) << "check data cannnot be higher then 2";
|
||||
|
||||
@ -27,8 +27,18 @@ class Data
|
||||
friend class DatabaseAPI;
|
||||
public:
|
||||
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);
|
||||
|
||||
/**
|
||||
* @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;
|
||||
@ -36,6 +46,10 @@ 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:
|
||||
|
||||
@ -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>();
|
||||
@ -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,35 +20,99 @@ 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);
|
||||
|
||||
|
||||
@ -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,6 +55,8 @@ 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);
|
||||
@ -68,7 +70,6 @@ bool SyncManager::startSynchronization()
|
||||
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,11 +121,10 @@ bool SyncManager::startSynchronization()
|
||||
});
|
||||
colnod->processData(preparedData->databaseData);
|
||||
});
|
||||
|
||||
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)
|
||||
{
|
||||
auto preparedColnodData = std::make_shared<Data>();
|
||||
|
||||
@ -25,6 +25,10 @@ private:
|
||||
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:
|
||||
|
||||
@ -47,6 +47,11 @@ public:
|
||||
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() &&
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user