mirror of
https://gitlab.com/spaceti-app/integrations/acs/colnod-connector.git
synced 2026-07-12 14:10:40 +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)
|
||||
{
|
||||
|
||||
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 << tab << data->updatedTokens.size() << "TOKENS UPDATED";
|
||||
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()
|
||||
{
|
||||
bool res = true;
|
||||
auto updatedQuery = mDbMan->createQuery();
|
||||
auto deletedQuery = mDbMan->createQuery();
|
||||
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));
|
||||
}
|
||||
|
||||
void DatabaseAPI::processData(const std::shared_ptr<Data> &data)
|
||||
bool DatabaseAPI::processData(const std::shared_ptr<Data> &data)
|
||||
{
|
||||
uploadTokens(data->updatedTokens);
|
||||
uploadSubjects(data->updatedSubjects);
|
||||
deleteItems(data->deletedTokens, "colnod_token");
|
||||
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 << tab << data->updatedTokens.size() << "TOKENS ADDED/UPDATED";
|
||||
qInfo(database) << tab << tab << data->deletedTokens.size() << "TOKENS DELETED";
|
||||
@ -155,6 +161,8 @@ qint64 DatabaseAPI::getNow()
|
||||
return -1;
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
@ -178,7 +186,7 @@ void DatabaseAPI::initNewTokens()
|
||||
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
|
||||
// OK -> token was updated
|
||||
@ -209,11 +217,13 @@ void DatabaseAPI::uploadTokens(const QHash<QString, std::shared_ptr<Token>> &tok
|
||||
if (!query.isActive()) {
|
||||
mValid = false;
|
||||
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();
|
||||
for (const auto &subject : subjects) {
|
||||
@ -241,11 +251,13 @@ void DatabaseAPI::uploadSubjects(const QHash<QString, std::shared_ptr<Subject>>
|
||||
if (!query.isActive()) {
|
||||
mValid = false;
|
||||
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();
|
||||
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);
|
||||
if (!executeQuery(query)) {
|
||||
qWarning(database) << "ERROR during marking token as deleted:" << id;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<Token> DatabaseAPI::getToken(const QString &id)
|
||||
{
|
||||
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);
|
||||
if (!executeQuery(query)) {
|
||||
qCritical(database) << "ERROR during accessing existing tokens";
|
||||
qCritical(database) << "ERROR during accessing existing tokens. EXIT";
|
||||
emit error();
|
||||
return nullptr;
|
||||
}
|
||||
auto list = utils->tableFromQuery<Token>(query);
|
||||
if (list.isEmpty())
|
||||
@ -280,6 +295,9 @@ bool DatabaseAPI::isAlreadyDeleted(const QString &id)
|
||||
{
|
||||
auto token = getToken(id);
|
||||
if (!token) {
|
||||
if (id.isEmpty()) {
|
||||
|
||||
}
|
||||
qWarning(database) << "Token deleted in colnod doesn't exists in db, will be noted as already deleted in stats";
|
||||
return true;
|
||||
}
|
||||
@ -294,15 +312,14 @@ bool DatabaseAPI::isValid()
|
||||
return mValid;
|
||||
}
|
||||
|
||||
|
||||
bool DatabaseAPI::exists(const QString &id, const QString &tableName)
|
||||
int DatabaseAPI::exists(const QString &id, const QString &tableName)
|
||||
{
|
||||
auto query = mDbMan->createQuery();
|
||||
query.prepare(QString("SELECT 1 FROM %1 WHERE id = :id").arg(tableName));
|
||||
query.bindValue(":id", id);
|
||||
if (!executeQuery(query)) {
|
||||
qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)";
|
||||
emit error();
|
||||
return -1;
|
||||
}
|
||||
return query.next();
|
||||
}
|
||||
@ -314,7 +331,7 @@ qint16 DatabaseAPI::getNumOfTokensOfSubject(const QString &id)
|
||||
query.bindValue(":id", id);
|
||||
if (!executeQuery(query)) {
|
||||
qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)";
|
||||
emit error();
|
||||
return -1;
|
||||
}
|
||||
return static_cast<qint16>(query.size());
|
||||
}
|
||||
@ -326,7 +343,7 @@ QString DatabaseAPI::getSubjectNameByTokenId(const QString &tokenId)
|
||||
query.bindValue(":tokenId", tokenId);
|
||||
if (!executeQuery(query)) {
|
||||
qCritical(database) << "ERROR during accessing existing subject. EXIT(1)";
|
||||
emit error();
|
||||
return "error";
|
||||
}
|
||||
if (query.next())
|
||||
return query.value(0).toString();
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
#include "serversdk/databasetable.h"
|
||||
#include "serversdk/databaseutils.h"
|
||||
#include "serversdk/logger.h"
|
||||
#include <QCoreApplication>
|
||||
|
||||
Q_DECLARE_LOGGING_CATEGORY(database)
|
||||
|
||||
@ -34,9 +35,9 @@ public:
|
||||
void initNewTokens();
|
||||
|
||||
// process data
|
||||
void uploadTokens(const QHash<QString, std::shared_ptr<Token> > &tokens);
|
||||
void uploadSubjects(const QHash<QString, std::shared_ptr<Subject> > &subjects);
|
||||
void deleteItems(const QList<QString> &ids, const QString &tableName);
|
||||
bool uploadTokens(const QHash<QString, std::shared_ptr<Token> > &tokens);
|
||||
bool uploadSubjects(const QHash<QString, std::shared_ptr<Subject> > &subjects);
|
||||
bool deleteItems(const QList<QString> &ids, const QString &tableName);
|
||||
|
||||
std::shared_ptr<Token> getToken(const QString &id);
|
||||
|
||||
@ -45,7 +46,7 @@ public:
|
||||
bool isValid();
|
||||
|
||||
private:
|
||||
bool exists(const QString &id, const QString &tableName);
|
||||
int exists(const QString &id, const QString &tableName);
|
||||
|
||||
QSqlQuery getUpdatedTokens();
|
||||
std::shared_ptr<Data> composeData(const QSqlQuery &updatedQuery, QSqlQuery &deletedQuery);
|
||||
|
||||
@ -115,8 +115,9 @@ bool SyncManager::startSynchronization()
|
||||
return true;
|
||||
}
|
||||
|
||||
void SyncManager::downloadData()
|
||||
bool SyncManager::downloadData()
|
||||
{
|
||||
bool res = true;
|
||||
qInfo(syncmanager) << equals;
|
||||
qInfo(syncmanager) << "DOWNLOADING DATA -> BEGIN";
|
||||
qInfo(syncmanager) << dash;
|
||||
@ -127,10 +128,12 @@ void SyncManager::downloadData()
|
||||
//connect(database.get(), &DatabaseAPI::downloadDataFinished, this, &SyncManager::saveDatabaseData);
|
||||
//database->downloadData();
|
||||
colnod->downloadData();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// finds conflicts -> changes on both sides (colnod and DB), apply newer change
|
||||
void SyncManager::prepareData()
|
||||
bool SyncManager::prepareData()
|
||||
{
|
||||
qInfo(syncmanager) << equals;
|
||||
qInfo(syncmanager) << "PREPARING DATA -> BEGIN";
|
||||
@ -217,9 +220,10 @@ void SyncManager::prepareData()
|
||||
|
||||
qInfo(syncmanager) << "PREPARING DATA -> FINISHED";
|
||||
qInfo(syncmanager) << equals;
|
||||
return true;
|
||||
}
|
||||
|
||||
void SyncManager::processData()
|
||||
bool SyncManager::processData()
|
||||
{
|
||||
qInfo(syncmanager) << equals;
|
||||
qInfo(syncmanager) << "PROCESSING DATA -> BEGIN";
|
||||
@ -230,6 +234,7 @@ void SyncManager::processData()
|
||||
database->processData(colnodDataToProcess);
|
||||
connect(colnod.get(), &ColnodAPI::dataProcessFinished, this, &SyncManager::dataProcessFinished);
|
||||
colnod->processData(databaseDataToProcess);
|
||||
return true;
|
||||
}
|
||||
|
||||
void SyncManager::updateLastUpdate(qint64 syncBegin)
|
||||
|
||||
@ -8,7 +8,6 @@ Q_DECLARE_LOGGING_CATEGORY(syncmanager)
|
||||
class SyncManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SyncManager(bool dryMode);
|
||||
|
||||
@ -16,9 +15,9 @@ public:
|
||||
bool startSynchronization();
|
||||
|
||||
private:
|
||||
void downloadData();
|
||||
void prepareData();
|
||||
void processData();
|
||||
bool downloadData();
|
||||
bool prepareData();
|
||||
bool processData();
|
||||
void updateLastUpdate(qint64 syncBegin);
|
||||
|
||||
|
||||
@ -32,6 +31,8 @@ signals:
|
||||
void dataProcessFinished();
|
||||
void syncFinished();
|
||||
|
||||
void error();
|
||||
|
||||
private:
|
||||
std::shared_ptr<ColnodAPI> colnod;
|
||||
std::shared_ptr<DatabaseAPI> database;
|
||||
@ -47,4 +48,6 @@ private:
|
||||
|
||||
int dataAmount = 0;
|
||||
bool mDryMode;
|
||||
|
||||
std::function<void()> onError = nullptr;
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user