Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit a95a5e3

Browse files
committed
Attempt to fix schedule sync issues
1 parent 965e055 commit a95a5e3

File tree

7 files changed

+99
-26
lines changed

7 files changed

+99
-26
lines changed

src/ramdatainterface/localdatainterface.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ void LocalDataInterface::createObject(QString uuid, QString table, QString data)
294294
)
295295
);
296296

297-
emit inserted(uuid, data, table);
297+
emit inserted(uuid, data, modified.toString("yyyy-MM-dd hh:mm:ss"), table);
298298
}
299299

300300
QString LocalDataInterface::objectData(QString uuid, QString table)
@@ -325,9 +325,10 @@ void LocalDataInterface::setObjectData(QString uuid, QString table, QString data
325325
"ON CONFLICT(uuid) DO UPDATE "
326326
"SET data=excluded.data, modified=excluded.modified ;";
327327

328-
query( q.arg(table, newData, modified.toString("yyyy-MM-dd hh:mm:ss"), uuid) );
328+
QString modifiedStr = modified.toString("yyyy-MM-dd hh:mm:ss");
329+
query( q.arg(table, newData, modifiedStr, uuid) );
329330

330-
emit dataChanged(uuid, data, table);
331+
emit dataChanged(uuid, data, modifiedStr, table);
331332
}
332333

333334
void LocalDataInterface::removeObject(QString uuid, QString table)
@@ -351,13 +352,14 @@ void LocalDataInterface::restoreObject(QString uuid, QString table)
351352
"removed = 0,"
352353
"modified = '%2' "
353354
"WHERE uuid = '%3';";
354-
query( q.arg(table, modified.toString("yyyy-MM-dd hh:mm:ss"), uuid) );
355+
QString modifiedStr = modified.toString("yyyy-MM-dd hh:mm:ss");
356+
query( q.arg(table, modifiedStr, uuid) );
355357

356358
// Get current data
357359
QString data = objectData(uuid, table);
358360

359361
// Emit inserted
360-
emit inserted(uuid, data, table);
362+
emit inserted(uuid, data, modifiedStr, table);
361363
}
362364

363365
bool LocalDataInterface::isRemoved(QString uuid, QString table)
@@ -439,7 +441,7 @@ void LocalDataInterface::updateUser(QString uuid, QString username, QString data
439441
q = "UPDATE RamUser SET `removed` = 1, `modified` = '%1' WHERE `userName` = '%2' AND `uuid` != '%3';";
440442
query( q.arg(modified, username, uuid) );
441443

442-
emit dataChanged(uuid, data, "RamUser");
444+
emit dataChanged(uuid, data, modified, "RamUser");
443445
}
444446

445447
ServerConfig LocalDataInterface::serverConfig()
@@ -727,7 +729,7 @@ void LocalDataInterface::saveSync(SyncData syncData)
727729
if (removed == 0)
728730
{
729731
QStringList ins;
730-
ins << uuid << incomingRow.data << tableName;
732+
ins << uuid << incomingRow.data << modified << tableName;
731733
insertedObjects << ins;
732734
}
733735
}
@@ -742,7 +744,7 @@ void LocalDataInterface::saveSync(SyncData syncData)
742744
// Emit insertions
743745
StateManager::i()->setState(StateManager::LoadingDataBase);
744746
foreach(QStringList io, insertedObjects ) {
745-
emit inserted( io.at(0), io.at(1), io.at(2) );
747+
emit inserted( io.at(0), io.at(1), io.at(2), io.at(3) );
746748
}
747749
}
748750

@@ -818,7 +820,7 @@ void LocalDataInterface::saveSync(SyncData syncData)
818820
}
819821

820822
QStringList cu;
821-
cu << uuid << data;
823+
cu << uuid << data << modified;
822824
changedUuids << cu;
823825
}
824826

@@ -835,7 +837,7 @@ void LocalDataInterface::saveSync(SyncData syncData)
835837
// Emit
836838
StateManager::i()->setState(StateManager::LoadingDataBase);
837839
foreach(QStringList cu, changedUuids) {
838-
emit dataChanged(cu.first(), cu.last(), tableName);
840+
emit dataChanged(cu.at(0), cu.at(1), cu.at(2), tableName);
839841
}
840842
}
841843

src/ramdatainterface/localdatainterface.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ public slots:
8787
void ramsesPathChanged(QString);
8888
// Sync result
8989
void syncFinished();
90-
void dataChanged(QString uuid, QString data, QString table);
90+
void dataChanged(const QString &uuid, const QString &data, const QString &modificationDate, const QString &table);
9191
void availabilityChanged(QString,bool);
92-
void inserted(QString uuid, QString data, QString table);
93-
void removed(QString uuid, QString table);
92+
void inserted(const QString &uuid, const QString &data, const QString &modificationDate, const QString &table);
93+
void removed(const QString &uuid, const QString &table);
9494

9595
protected:
9696
static LocalDataInterface *_instance;

src/ramobjectmodels/dbtablemodel.cpp

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "dbtablemodel.h"
2+
#include "duqf-app/app-config.h"
23
#include "localdatainterface.h"
34
#include "dbinterface.h"
45
#include "progressmanager.h"
@@ -126,10 +127,10 @@ bool DBTableModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int
126127
return true;
127128
}
128129

129-
void DBTableModel::insertObjects(int row, QVector<QStringList> data, QString table, bool silent)
130+
void DBTableModel::insertObjects(int row, const QVector<QStringList> &data, const QString &table, bool silent)
130131
{
131132
// Wrong table, not for us
132-
if (table != m_table) return;
133+
if (table != "" && table != m_table) return;
133134

134135
// Check row
135136
if (row < 0) row = 0;
@@ -151,7 +152,7 @@ void DBTableModel::insertObjects(int row, QVector<QStringList> data, QString tab
151152
if (!silent) endInsertRows();
152153
}
153154

154-
void DBTableModel::removeObjects(QStringList uuids, QString table)
155+
void DBTableModel::removeObjects(QStringList uuids, const QString &table)
155156
{
156157
// Not for us
157158
if (table != "" && table != m_table) return;
@@ -208,6 +209,43 @@ bool DBTableModel::checkFilters(QString data) const
208209
return true;
209210
}
210211

212+
QString DBTableModel::checkUnique(const QString &uuid, const QString &data, const QString &modifiedDate)
213+
{
214+
// Nothing to check
215+
if (m_uniqueDataKeys.isEmpty())
216+
return "";
217+
218+
QJsonDocument doc = QJsonDocument::fromJson( data.toUtf8() );
219+
QJsonObject dataObj = doc.object();
220+
221+
for (int i = 0; i < count(); i++) {
222+
RamObject *obj = get(i);
223+
if (!obj)
224+
continue;
225+
QJsonObject otherData = obj->data();
226+
bool ok = false;
227+
for(const QString &key: qAsConst(m_uniqueDataKeys)) {
228+
// If the key can't be found, consider it ok
229+
if (!otherData.contains(key) || !dataObj.contains(key)) {
230+
ok = true;
231+
break;
232+
}
233+
if (dataObj.value(key) == otherData.value(key))
234+
continue;
235+
ok = true;
236+
break;
237+
}
238+
if (!ok) {
239+
// Keep the most recent
240+
if (QDateTime::fromString(modifiedDate, DATETIME_DATA_FORMAT) >= obj->modificationDate())
241+
return obj->uuid();
242+
return uuid;
243+
}
244+
}
245+
246+
return "";
247+
}
248+
211249
void DBTableModel::saveOrder() const
212250
{
213251
if (!m_userOrder) return;
@@ -232,7 +270,7 @@ void DBTableModel::moveObject(int from, int to)
232270
endMoveRows();
233271
}
234272

235-
void DBTableModel::insertObject(QString uuid, QString data, QString table)
273+
void DBTableModel::insertObject(const QString &uuid, const QString &data, const QString &modificationDate, const QString &table)
236274
{
237275
if (table != m_table) return;
238276

@@ -242,6 +280,13 @@ void DBTableModel::insertObject(QString uuid, QString data, QString table)
242280
// Removed
243281
if (DBInterface::instance()->isRemoved(uuid, table)) return;
244282

283+
// Unique
284+
QString uuidToRemove = checkUnique(uuid, data, modificationDate);
285+
if (uuidToRemove == uuid) {
286+
LocalDataInterface::instance()->removeObject(uuid, table);
287+
return;
288+
}
289+
245290
// Filter
246291
if (!checkFilters(data)) return;
247292

@@ -253,6 +298,12 @@ void DBTableModel::insertObject(QString uuid, QString data, QString table)
253298
QStringList o;
254299
o << uuid << data;
255300
insertObjects( order, QVector<QStringList>() << o, table );
301+
302+
// Remove duplicate
303+
if (uuidToRemove != "") {
304+
removeObject(uuidToRemove, table);
305+
LocalDataInterface::instance()->removeObject(uuid, table);
306+
}
256307
}
257308

258309
void DBTableModel::removeObject(QString uuid, QString table)
@@ -291,7 +342,7 @@ void DBTableModel::reload()
291342
endResetModel();
292343
}
293344

294-
void DBTableModel::changeData(QString uuid, QString data, QString table)
345+
void DBTableModel::changeData(const QString &uuid, const QString &data, const QString &modificationDate, const QString &table)
295346
{
296347
// Not for us
297348
if (table != "" && table != m_table) return;
@@ -300,7 +351,7 @@ void DBTableModel::changeData(QString uuid, QString data, QString table)
300351
{
301352
// This may be a new object to insert according to the filters
302353
if (!checkFilters(data)) return;
303-
insertObject(uuid, data, table);
354+
insertObject(uuid, data, modificationDate, table);
304355
return;
305356
}
306357

src/ramobjectmodels/dbtablemodel.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,37 @@ class DBTableModel: public RamAbstractObjectModel
4141
// Support move rows
4242
virtual bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
4343

44+
protected:
45+
QStringList m_uniqueDataKeys;
46+
4447
protected slots:
4548
// Inserts a single object
46-
void insertObject(QString uuid, QString data, QString table);
49+
void insertObject(const QString &uuid, const QString &data, const QString &modificationDate, const QString &table);
4750
// Removes a single object
4851
void removeObject(QString uuid, QString table);
4952
// Clear and reload the data
5053
void reload();
5154
// Checks and changes the data
52-
void changeData(QString uuid, QString data, QString table = "");
55+
void changeData(const QString &uuid, const QString &data, const QString &modificationDate, const QString &table = "");
5356

5457
private:
5558

5659
// === METHODS ===
5760

5861
// Edit structure
59-
void insertObjects(int row, QVector<QStringList> data, QString table, bool silent = false);
60-
void removeObjects(QStringList uuids, QString table = "");
62+
void insertObjects(int row, const QVector<QStringList> &data, const QString &table = "", bool silent = false);
63+
void removeObjects(QStringList uuids, const QString &table = "");
6164

6265
// Gets the order from the data
6366
int getOrder(QString data);
6467

6568
// Checks the filters
6669
bool checkFilters(QString data) const;
70+
// Checks if this obj may be inserted
71+
// Returns a uuid to be removed from
72+
// the table after inserting the object
73+
// Or an empty string if there's nothing to remove
74+
QString checkUnique(const QString &uuid, const QString &data, const QString &modifiedDate);
6775

6876
// Save the order in the db
6977
void saveOrder() const;

src/ramobjectmodels/ramschedulemodel.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
RamScheduleModel::RamScheduleModel(QObject *parent)
66
: DBTableModel{RamAbstractObject::ScheduleEntry, true, false, parent}
77
{
8+
m_uniqueDataKeys = QStringList({
9+
"date",
10+
"user",
11+
"project"
12+
});
13+
814
connect( this, &DBTableModel::rowsInserted, this,&RamScheduleModel::countAll);
915
connect( this, &DBTableModel::rowsAboutToBeRemoved, this, &RamScheduleModel::countAll);
1016
connect( this, &DBTableModel::dataChanged, this, &RamScheduleModel::countAll);

src/ramobjectmodels/ramscheduletablemodel.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,13 @@ QVariant RamScheduleTableModel::data(const QModelIndex &index, int role) const
228228
if (!e) continue;
229229
if (!e->user()) continue;
230230
if (e->user()->uuid() == usrUuid) {
231-
entry = e;
232-
break;
231+
// Just in case there are multiple entries
232+
if (!entry) {
233+
entry = e;
234+
continue;
235+
}
236+
if (e->modificationDate() > entry->modificationDate())
237+
entry = e;
233238
}
234239
}
235240

src/ramobjects/ramscheduleentry.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ramscheduleentry.h"
2+
#include "duqf-app/app-config.h"
23
#include "ramproject.h"
34

45
// STATIC //
@@ -85,7 +86,7 @@ RamStep *RamScheduleEntry::step() const
8586

8687
const QDateTime RamScheduleEntry::date() const
8788
{
88-
QDateTime d = QDateTime::fromString( getData("date").toString(), "yyyy-MM-dd hh:mm:ss");
89+
QDateTime d = QDateTime::fromString( getData("date").toString(), DATETIME_DATA_FORMAT);
8990
return d;
9091
}
9192

0 commit comments

Comments
 (0)