mirror of
https://gitlab.com/spaceti-app/integrations/acs/colnod-connector.git
synced 2026-07-12 18:10:37 +02:00
Merge branch 'transform-app-using-serversdk' into 'suggest/different-approach'
# Conflicts: # src/colnod/databaseapi.cpp # src/colnod/databaseapi.h # src/colnod/syncmanager.cpp # src/colnod/syncmanager.h
This commit is contained in:
commit
c5cff0f216
@ -39,7 +39,15 @@ void ColnodAPI::downloadData()
|
|||||||
|
|
||||||
void ColnodAPI::processData(const std::shared_ptr<Data> &data)
|
void ColnodAPI::processData(const std::shared_ptr<Data> &data)
|
||||||
{
|
{
|
||||||
|
for (const auto &token : data->updatedTokens) {
|
||||||
|
qInfo(colnod).noquote() << Message::massageTemplate("Updating", "token", "colnod", token->name());
|
||||||
|
}
|
||||||
|
for (const auto &id : data->deletedTokens) {
|
||||||
|
qInfo(colnod).noquote() << Message::massageTemplate("Deleting", "token", "colnod", id);
|
||||||
|
}
|
||||||
|
if (!data->updatedTokens.isEmpty() || !data->deletedTokens.isEmpty()) {
|
||||||
|
qInfo(colnod) << dash;
|
||||||
|
}
|
||||||
qInfo(colnod) << tab << "-> DATA PROCESSED TO COLNOD:";
|
qInfo(colnod) << tab << "-> DATA PROCESSED TO COLNOD:";
|
||||||
qInfo(colnod) << tab << tab << data->updatedTokens.size() << "TOKENS UPDATED";
|
qInfo(colnod) << tab << tab << data->updatedTokens.size() << "TOKENS UPDATED";
|
||||||
qInfo(colnod) << tab << tab << data->deletedTokens.size() << "TOKENS DELETED";
|
qInfo(colnod) << tab << tab << data->deletedTokens.size() << "TOKENS DELETED";
|
||||||
|
|||||||
@ -38,6 +38,7 @@ bool DatabaseAPI::executeQuery(QSqlQuery &query, const QString &statement)
|
|||||||
|
|
||||||
std::shared_ptr<Data> DatabaseAPI::downloadData()
|
std::shared_ptr<Data> DatabaseAPI::downloadData()
|
||||||
{
|
{
|
||||||
|
bool res = true;
|
||||||
auto updatedQuery = mDbMan->createQuery();
|
auto updatedQuery = mDbMan->createQuery();
|
||||||
auto deletedQuery = mDbMan->createQuery();
|
auto deletedQuery = mDbMan->createQuery();
|
||||||
if (!executeQuery(updatedQuery, "SELECT * FROM colnod_token WHERE last_modified > last_update AND deleted = false")) {
|
if (!executeQuery(updatedQuery, "SELECT * FROM colnod_token WHERE last_modified > last_update AND deleted = false")) {
|
||||||
@ -55,13 +56,18 @@ std::shared_ptr<Data> DatabaseAPI::downloadData()
|
|||||||
//emit downloadDataFinished(composeData(updatedQuery, deletedQuery));
|
//emit downloadDataFinished(composeData(updatedQuery, deletedQuery));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DatabaseAPI::processData(const std::shared_ptr<Data> &data)
|
bool DatabaseAPI::processData(const std::shared_ptr<Data> &data)
|
||||||
{
|
{
|
||||||
uploadTokens(data->updatedTokens);
|
uploadTokens(data->updatedTokens);
|
||||||
uploadSubjects(data->updatedSubjects);
|
uploadSubjects(data->updatedSubjects);
|
||||||
deleteItems(data->deletedTokens, "colnod_token");
|
deleteItems(data->deletedTokens, "colnod_token");
|
||||||
deleteItems(data->deletedSubjects, "colnod_subject");
|
deleteItems(data->deletedSubjects, "colnod_subject");
|
||||||
|
if ( !data->updatedTokens.isEmpty() ||
|
||||||
|
!data->updatedSubjects.isEmpty() ||
|
||||||
|
!data->deletedTokens.isEmpty() ||
|
||||||
|
!data->deletedSubjects.isEmpty()) {
|
||||||
|
qInfo(database) << dash;
|
||||||
|
}
|
||||||
qInfo(database) << tab << "-> DATA PROCESSED TO DATABASE:";
|
qInfo(database) << tab << "-> DATA PROCESSED TO DATABASE:";
|
||||||
qInfo(database) << tab << tab << data->updatedTokens.size() << "TOKENS ADDED/UPDATED";
|
qInfo(database) << tab << tab << data->updatedTokens.size() << "TOKENS ADDED/UPDATED";
|
||||||
qInfo(database) << tab << tab << data->deletedTokens.size() << "TOKENS DELETED";
|
qInfo(database) << tab << tab << data->deletedTokens.size() << "TOKENS DELETED";
|
||||||
@ -155,6 +161,8 @@ qint64 DatabaseAPI::getNow()
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
query.next();
|
query.next();
|
||||||
|
qDebug() << "Database time:" << QDateTime::fromMSecsSinceEpoch(query.value(0).toLongLong()).toString("yyyy-MM-dd hh:mm:ss.zzz");
|
||||||
|
qDebug() << "Current time:" << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
|
||||||
return query.value(0).toLongLong();
|
return query.value(0).toLongLong();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +186,7 @@ void DatabaseAPI::initNewTokens()
|
|||||||
uploadTokens(tokensToInit);
|
uploadTokens(tokensToInit);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DatabaseAPI::uploadTokens(const QHash<QString, std::shared_ptr<Token>> &tokens)
|
bool DatabaseAPI::uploadTokens(const QHash<QString, std::shared_ptr<Token>> &tokens)
|
||||||
{
|
{
|
||||||
// TODO: first check if OK, then message
|
// TODO: first check if OK, then message
|
||||||
// OK -> token was updated
|
// OK -> token was updated
|
||||||
@ -209,11 +217,13 @@ void DatabaseAPI::uploadTokens(const QHash<QString, std::shared_ptr<Token>> &tok
|
|||||||
if (!query.isActive()) {
|
if (!query.isActive()) {
|
||||||
mValid = false;
|
mValid = false;
|
||||||
qWarning(database) << "ERROR durning inserting/updating of token:" << token->name();
|
qWarning(database) << "ERROR durning inserting/updating of token:" << token->name();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DatabaseAPI::uploadSubjects(const QHash<QString, std::shared_ptr<Subject>> &subjects)
|
bool DatabaseAPI::uploadSubjects(const QHash<QString, std::shared_ptr<Subject>> &subjects)
|
||||||
{
|
{
|
||||||
auto query = mDbMan->createQuery();
|
auto query = mDbMan->createQuery();
|
||||||
for (const auto &subject : subjects) {
|
for (const auto &subject : subjects) {
|
||||||
@ -241,11 +251,13 @@ void DatabaseAPI::uploadSubjects(const QHash<QString, std::shared_ptr<Subject>>
|
|||||||
if (!query.isActive()) {
|
if (!query.isActive()) {
|
||||||
mValid = false;
|
mValid = false;
|
||||||
qWarning(database) << "ERROR durning inserting/updating of subject attribute:" << subject->name();
|
qWarning(database) << "ERROR durning inserting/updating of subject attribute:" << subject->name();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DatabaseAPI::deleteItems(const QList<QString> &ids, const QString &tableName)
|
bool DatabaseAPI::deleteItems(const QList<QString> &ids, const QString &tableName)
|
||||||
{
|
{
|
||||||
auto query = mDbMan->createQuery();
|
auto query = mDbMan->createQuery();
|
||||||
QString nowString = QDateTime::fromMSecsSinceEpoch(getLastSync()).toString("yyyy-MM-dd hh:mm:ss.zzz");
|
QString nowString = QDateTime::fromMSecsSinceEpoch(getLastSync()).toString("yyyy-MM-dd hh:mm:ss.zzz");
|
||||||
@ -257,18 +269,21 @@ void DatabaseAPI::deleteItems(const QList<QString> &ids, const QString &tableNam
|
|||||||
query.bindValue(":now", nowString);
|
query.bindValue(":now", nowString);
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
qWarning(database) << "ERROR during marking token as deleted:" << id;
|
qWarning(database) << "ERROR during marking token as deleted:" << id;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Token> DatabaseAPI::getToken(const QString &id)
|
std::shared_ptr<Token> DatabaseAPI::getToken(const QString &id)
|
||||||
{
|
{
|
||||||
auto query = mDbMan->createQuery();
|
auto query = mDbMan->createQuery();
|
||||||
query.prepare("SELECT * FROM colnod_token WHERE id = :id");
|
query.prepare("SELECT * FROM colnod_token WHERE d = :id");
|
||||||
query.bindValue(":id", id);
|
query.bindValue(":id", id);
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
qCritical(database) << "ERROR during accessing existing tokens";
|
qCritical(database) << "ERROR during accessing existing tokens. EXIT";
|
||||||
emit error();
|
emit error();
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
auto list = utils->tableFromQuery<Token>(query);
|
auto list = utils->tableFromQuery<Token>(query);
|
||||||
if (list.isEmpty())
|
if (list.isEmpty())
|
||||||
@ -280,6 +295,9 @@ bool DatabaseAPI::isAlreadyDeleted(const QString &id)
|
|||||||
{
|
{
|
||||||
auto token = getToken(id);
|
auto token = getToken(id);
|
||||||
if (!token) {
|
if (!token) {
|
||||||
|
if (id.isEmpty()) {
|
||||||
|
|
||||||
|
}
|
||||||
qWarning(database) << "Token deleted in colnod doesn't exists in db, will be noted as already deleted in stats";
|
qWarning(database) << "Token deleted in colnod doesn't exists in db, will be noted as already deleted in stats";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -294,15 +312,14 @@ bool DatabaseAPI::isValid()
|
|||||||
return mValid;
|
return mValid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int DatabaseAPI::exists(const QString &id, const QString &tableName)
|
||||||
bool DatabaseAPI::exists(const QString &id, const QString &tableName)
|
|
||||||
{
|
{
|
||||||
auto query = mDbMan->createQuery();
|
auto query = mDbMan->createQuery();
|
||||||
query.prepare(QString("SELECT 1 FROM %1 WHERE id = :id").arg(tableName));
|
query.prepare(QString("SELECT 1 FROM %1 WHERE id = :id").arg(tableName));
|
||||||
query.bindValue(":id", id);
|
query.bindValue(":id", id);
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)";
|
qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)";
|
||||||
emit error();
|
return -1;
|
||||||
}
|
}
|
||||||
return query.next();
|
return query.next();
|
||||||
}
|
}
|
||||||
@ -314,7 +331,7 @@ qint16 DatabaseAPI::getNumOfTokensOfSubject(const QString &id)
|
|||||||
query.bindValue(":id", id);
|
query.bindValue(":id", id);
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)";
|
qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)";
|
||||||
emit error();
|
return -1;
|
||||||
}
|
}
|
||||||
return static_cast<qint16>(query.size());
|
return static_cast<qint16>(query.size());
|
||||||
}
|
}
|
||||||
@ -326,7 +343,7 @@ QString DatabaseAPI::getSubjectNameByTokenId(const QString &tokenId)
|
|||||||
query.bindValue(":tokenId", tokenId);
|
query.bindValue(":tokenId", tokenId);
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
qCritical(database) << "ERROR during accessing existing subject. EXIT(1)";
|
qCritical(database) << "ERROR during accessing existing subject. EXIT(1)";
|
||||||
emit error();
|
return "error";
|
||||||
}
|
}
|
||||||
if (query.next())
|
if (query.next())
|
||||||
return query.value(0).toString();
|
return query.value(0).toString();
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include "serversdk/databasetable.h"
|
#include "serversdk/databasetable.h"
|
||||||
#include "serversdk/databaseutils.h"
|
#include "serversdk/databaseutils.h"
|
||||||
#include "serversdk/logger.h"
|
#include "serversdk/logger.h"
|
||||||
|
#include <QCoreApplication>
|
||||||
|
|
||||||
Q_DECLARE_LOGGING_CATEGORY(database)
|
Q_DECLARE_LOGGING_CATEGORY(database)
|
||||||
|
|
||||||
@ -34,9 +35,9 @@ public:
|
|||||||
void initNewTokens();
|
void initNewTokens();
|
||||||
|
|
||||||
// process data
|
// process data
|
||||||
void uploadTokens(const QHash<QString, std::shared_ptr<Token> > &tokens);
|
bool uploadTokens(const QHash<QString, std::shared_ptr<Token> > &tokens);
|
||||||
void uploadSubjects(const QHash<QString, std::shared_ptr<Subject> > &subjects);
|
bool uploadSubjects(const QHash<QString, std::shared_ptr<Subject> > &subjects);
|
||||||
void deleteItems(const QList<QString> &ids, const QString &tableName);
|
bool deleteItems(const QList<QString> &ids, const QString &tableName);
|
||||||
|
|
||||||
std::shared_ptr<Token> getToken(const QString &id);
|
std::shared_ptr<Token> getToken(const QString &id);
|
||||||
|
|
||||||
@ -45,7 +46,7 @@ public:
|
|||||||
bool isValid();
|
bool isValid();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool exists(const QString &id, const QString &tableName);
|
int exists(const QString &id, const QString &tableName);
|
||||||
|
|
||||||
QSqlQuery getUpdatedTokens();
|
QSqlQuery getUpdatedTokens();
|
||||||
std::shared_ptr<Data> composeData(const QSqlQuery &updatedQuery, QSqlQuery &deletedQuery);
|
std::shared_ptr<Data> composeData(const QSqlQuery &updatedQuery, QSqlQuery &deletedQuery);
|
||||||
|
|||||||
@ -115,8 +115,9 @@ bool SyncManager::startSynchronization()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SyncManager::downloadData()
|
bool SyncManager::downloadData()
|
||||||
{
|
{
|
||||||
|
bool res = true;
|
||||||
qInfo(syncmanager) << equals;
|
qInfo(syncmanager) << equals;
|
||||||
qInfo(syncmanager) << "DOWNLOADING DATA -> BEGIN";
|
qInfo(syncmanager) << "DOWNLOADING DATA -> BEGIN";
|
||||||
qInfo(syncmanager) << dash;
|
qInfo(syncmanager) << dash;
|
||||||
@ -127,10 +128,12 @@ void SyncManager::downloadData()
|
|||||||
//connect(database.get(), &DatabaseAPI::downloadDataFinished, this, &SyncManager::saveDatabaseData);
|
//connect(database.get(), &DatabaseAPI::downloadDataFinished, this, &SyncManager::saveDatabaseData);
|
||||||
//database->downloadData();
|
//database->downloadData();
|
||||||
colnod->downloadData();
|
colnod->downloadData();
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// finds conflicts -> changes on both sides (colnod and DB), apply newer change
|
// finds conflicts -> changes on both sides (colnod and DB), apply newer change
|
||||||
void SyncManager::prepareData()
|
bool SyncManager::prepareData()
|
||||||
{
|
{
|
||||||
qInfo(syncmanager) << equals;
|
qInfo(syncmanager) << equals;
|
||||||
qInfo(syncmanager) << "PREPARING DATA -> BEGIN";
|
qInfo(syncmanager) << "PREPARING DATA -> BEGIN";
|
||||||
@ -217,9 +220,10 @@ void SyncManager::prepareData()
|
|||||||
|
|
||||||
qInfo(syncmanager) << "PREPARING DATA -> FINISHED";
|
qInfo(syncmanager) << "PREPARING DATA -> FINISHED";
|
||||||
qInfo(syncmanager) << equals;
|
qInfo(syncmanager) << equals;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SyncManager::processData()
|
bool SyncManager::processData()
|
||||||
{
|
{
|
||||||
qInfo(syncmanager) << equals;
|
qInfo(syncmanager) << equals;
|
||||||
qInfo(syncmanager) << "PROCESSING DATA -> BEGIN";
|
qInfo(syncmanager) << "PROCESSING DATA -> BEGIN";
|
||||||
@ -230,6 +234,7 @@ void SyncManager::processData()
|
|||||||
database->processData(colnodDataToProcess);
|
database->processData(colnodDataToProcess);
|
||||||
connect(colnod.get(), &ColnodAPI::dataProcessFinished, this, &SyncManager::dataProcessFinished);
|
connect(colnod.get(), &ColnodAPI::dataProcessFinished, this, &SyncManager::dataProcessFinished);
|
||||||
colnod->processData(databaseDataToProcess);
|
colnod->processData(databaseDataToProcess);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SyncManager::updateLastUpdate(qint64 syncBegin)
|
void SyncManager::updateLastUpdate(qint64 syncBegin)
|
||||||
|
|||||||
@ -8,7 +8,6 @@ Q_DECLARE_LOGGING_CATEGORY(syncmanager)
|
|||||||
class SyncManager : public QObject
|
class SyncManager : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit SyncManager(bool dryMode);
|
explicit SyncManager(bool dryMode);
|
||||||
|
|
||||||
@ -16,9 +15,9 @@ public:
|
|||||||
bool startSynchronization();
|
bool startSynchronization();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void downloadData();
|
bool downloadData();
|
||||||
void prepareData();
|
bool prepareData();
|
||||||
void processData();
|
bool processData();
|
||||||
void updateLastUpdate(qint64 syncBegin);
|
void updateLastUpdate(qint64 syncBegin);
|
||||||
|
|
||||||
|
|
||||||
@ -32,6 +31,8 @@ signals:
|
|||||||
void dataProcessFinished();
|
void dataProcessFinished();
|
||||||
void syncFinished();
|
void syncFinished();
|
||||||
|
|
||||||
|
void error();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<ColnodAPI> colnod;
|
std::shared_ptr<ColnodAPI> colnod;
|
||||||
std::shared_ptr<DatabaseAPI> database;
|
std::shared_ptr<DatabaseAPI> database;
|
||||||
@ -47,4 +48,6 @@ private:
|
|||||||
|
|
||||||
int dataAmount = 0;
|
int dataAmount = 0;
|
||||||
bool mDryMode;
|
bool mDryMode;
|
||||||
|
|
||||||
|
std::function<void()> onError = nullptr;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user