Skip to content

Commit

Permalink
STYLE: clean set/get methods and allow/deny servers API
Browse files Browse the repository at this point in the history
Co-authored-by: Andras Lasso <[email protected]>
  • Loading branch information
Punzo committed Apr 16, 2024
1 parent faf2d61 commit 51648d6
Show file tree
Hide file tree
Showing 35 changed files with 513 additions and 1,552 deletions.
67 changes: 25 additions & 42 deletions Libs/Core/ctkJobScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ void ctkJobSchedulerPrivate::removeJobs(const QStringList &jobUIDs)

QList<QVariant> datas;
{
// The QMutexLocker is enclosed within brackets to restrict its scope and
// prevent conflicts with other QMutexLockers within the scheduler's methods.
QMutexLocker locker(&this->QueueMutex);

foreach (QString jobUID, jobUIDs)
Expand Down Expand Up @@ -231,6 +233,8 @@ void ctkJobSchedulerPrivate::removeAllJobs()
Q_Q(ctkJobScheduler);

{
// The QMutexLocker is enclosed within brackets to restrict its scope and
// prevent conflicts with other QMutexLockers within the scheduler's methods.
QMutexLocker locker(&this->QueueMutex);
foreach (QSharedPointer<ctkAbstractJob> job, this->JobsQueue)
{
Expand Down Expand Up @@ -308,12 +312,22 @@ ctkJobScheduler::~ctkJobScheduler()
this->waitForFinish(true);
}

//------------------------------------------------------------------------------
CTK_SET_CPP(ctkJobScheduler, const bool&, setFreezeJobsScheduling, FreezeJobsScheduling);
CTK_GET_CPP(ctkJobScheduler, bool, freezeJobsScheduling, FreezeJobsScheduling)
CTK_SET_CPP(ctkJobScheduler, const int&, setMaximumNumberOfRetry, MaximumNumberOfRetry);
CTK_GET_CPP(ctkJobScheduler, int, maximumNumberOfRetry, MaximumNumberOfRetry)
CTK_SET_CPP(ctkJobScheduler, const int&, setRetryDelay, RetryDelay);
CTK_GET_CPP(ctkJobScheduler, int, retryDelay, RetryDelay)

//----------------------------------------------------------------------------
int ctkJobScheduler::numberOfJobs()
{
Q_D(ctkJobScheduler);
int numberOfJobs = 0;
{
// The QMutexLocker is enclosed within brackets to restrict its scope and
// prevent conflicts with other QMutexLockers within the scheduler's methods.
QMutexLocker locker(&d->QueueMutex);
numberOfJobs = d->JobsQueue.count();
}
Expand All @@ -326,6 +340,8 @@ int ctkJobScheduler::numberOfPersistentJobs()
Q_D(ctkJobScheduler);
int numberOfPersistentJobs = 0;
{
// The QMutexLocker is enclosed within brackets to restrict its scope and
// prevent conflicts with other QMutexLockers within the scheduler's methods.
QMutexLocker locker(&d->QueueMutex);
foreach (QSharedPointer<ctkAbstractJob> job, d->JobsQueue)
{
Expand All @@ -346,6 +362,8 @@ int ctkJobScheduler::numberOfRunningJobs()

int numberOfRunningJobs = 0;
{
// The QMutexLocker is enclosed within brackets to restrict its scope and
// prevent conflicts with other QMutexLockers within the scheduler's methods.
QMutexLocker locker(&d->QueueMutex);
foreach (QSharedPointer<ctkAbstractJob> job, d->JobsQueue)
{
Expand Down Expand Up @@ -396,6 +414,8 @@ QSharedPointer<ctkAbstractJob> ctkJobScheduler::getJobSharedByUID(const QString&

QSharedPointer<ctkAbstractJob> job = nullptr;
{
// The QMutexLocker is enclosed within brackets to restrict its scope and
// prevent conflicts with other QMutexLockers within the scheduler's methods.
QMutexLocker locker(&d->QueueMutex);
QMap<QString, QSharedPointer<ctkAbstractJob>>::iterator it = d->JobsQueue.find(jobUID);
if (it == d->JobsQueue.end())
Expand Down Expand Up @@ -449,6 +469,8 @@ void ctkJobScheduler::stopAllJobs(bool stopPersistentJobs)

QStringList initializedStoppedJobsUIDs;
{
// The QMutexLocker is enclosed within brackets to restrict its scope and
// prevent conflicts with other QMutexLockers within the scheduler's methods.
QMutexLocker locker(&d->QueueMutex);

// Stops jobs without a worker (in waiting, still in main thread).
Expand Down Expand Up @@ -524,6 +546,8 @@ void ctkJobScheduler::stopJobsByJobUIDs(const QStringList &jobUIDs)

QStringList initializedStoppedJobsUIDs;
{
// The QMutexLocker is enclosed within brackets to restrict its scope and
// prevent conflicts with other QMutexLockers within the scheduler's methods.
QMutexLocker locker(&d->QueueMutex);

// Stops jobs without a worker (in waiting, still in main thread)
Expand Down Expand Up @@ -596,19 +620,6 @@ void ctkJobScheduler::stopJobsByJobUIDs(const QStringList &jobUIDs)
}
}

//----------------------------------------------------------------------------
bool ctkJobScheduler::freezeJobsScheduling() const
{
Q_D(const ctkJobScheduler);
return d->FreezeJobsScheduling;
}

//----------------------------------------------------------------------------
void ctkJobScheduler::setFreezeJobsScheduling(bool freezeJobsScheduling)
{
Q_D(ctkJobScheduler);
d->FreezeJobsScheduling = freezeJobsScheduling;
}

//----------------------------------------------------------------------------
int ctkJobScheduler::maximumThreadCount() const
Expand All @@ -618,40 +629,12 @@ int ctkJobScheduler::maximumThreadCount() const
}

//----------------------------------------------------------------------------
void ctkJobScheduler::setMaximumThreadCount(int maximumThreadCount)
void ctkJobScheduler::setMaximumThreadCount(const int& maximumThreadCount)
{
Q_D(ctkJobScheduler);
d->ThreadPool->setMaxThreadCount(maximumThreadCount);
}

//----------------------------------------------------------------------------
int ctkJobScheduler::maximumNumberOfRetry() const
{
Q_D(const ctkJobScheduler);
return d->MaximumNumberOfRetry;
}

//----------------------------------------------------------------------------
void ctkJobScheduler::setMaximumNumberOfRetry(int maximumNumberOfRetry)
{
Q_D(ctkJobScheduler);
d->MaximumNumberOfRetry = maximumNumberOfRetry;
}

//----------------------------------------------------------------------------
int ctkJobScheduler::retryDelay() const
{
Q_D(const ctkJobScheduler);
return d->RetryDelay;
}

//----------------------------------------------------------------------------
void ctkJobScheduler::setRetryDelay(int retryDelay)
{
Q_D(ctkJobScheduler);
d->RetryDelay = retryDelay;
}

//----------------------------------------------------------------------------
QThreadPool* ctkJobScheduler::threadPool() const
{
Expand Down
8 changes: 4 additions & 4 deletions Libs/Core/ctkJobScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,28 @@ class CTK_CORE_EXPORT ctkJobScheduler : public QObject
/// if set to true, new jobs will not be queued
/// default: false
bool freezeJobsScheduling() const;
void setFreezeJobsScheduling(bool freezeJobsScheduling);
void setFreezeJobsScheduling(const bool& freezeJobsScheduling);
///@}

///@{
/// Maximum number of concurrent QThreads spawned by the threadPool in the Job pool
/// default: 20
int maximumThreadCount() const;
void setMaximumThreadCount(int maximumThreadCount);
void setMaximumThreadCount(const int& maximumThreadCount);
///@}

///@{
/// Maximum number of retries that the Job pool will try on each failed Job
/// default: 3
int maximumNumberOfRetry() const;
void setMaximumNumberOfRetry(int maximumNumberOfRetry);
void setMaximumNumberOfRetry(const int& maximumNumberOfRetry);
///@}

///@{
/// Retry delay in millisec
/// default: 100 msec
int retryDelay() const;
void setRetryDelay(int retryDelay);
void setRetryDelay(const int& retryDelay);
///@}

/// Return the threadPool.
Expand Down
134 changes: 51 additions & 83 deletions Libs/DICOM/Core/ctkDICOMDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,47 +70,6 @@ static QString ValueIsNotStored("__VALUE_IS_NOT_STORED__");
/// Separator character for table and field names to be used in display rules manager
static QString TableFieldSeparator(":");

//------------------------------------------------------------------------------
// Convert QJsonArray to QStringList
QStringList jsonArrayToStringList(const QJsonArray& jsonArray)
{
QStringList stringList;
for (const QJsonValue& jsonValue : jsonArray)
{
if (jsonValue.isString())
{
stringList << jsonValue.toString();
}
}
return stringList;
}

//------------------------------------------------------------------------------
// Convert QStringList to QJsonArray
QJsonArray stringListToJsonArray(const QStringList& stringList)
{
QJsonArray jsonArray;
for (const QString& str : stringList)
{
jsonArray.append(str);
}
return jsonArray;
}

//------------------------------------------------------------------------------
// Convert allowList and denyList to a JSON string
QString convertListsToJson(const QStringList& allowList, const QStringList& denyList)
{
// Create a JSON object
QJsonObject jsonObject;
jsonObject["allow"] = stringListToJsonArray(allowList);
jsonObject["deny"] = stringListToJsonArray(denyList);

// Serialize the JSON object to a string
QJsonDocument jsonDoc(jsonObject);
return jsonDoc.toJson(QJsonDocument::Compact);
}

//------------------------------------------------------------------------------
// ctkDICOMDatabasePrivate methods

Expand Down Expand Up @@ -421,8 +380,9 @@ bool ctkDICOMDatabasePrivate::insertConnectionName(const int& dbPatientID,
Q_Q(ctkDICOMDatabase);

// check if connection name is already stored
QStringList allowList = q->enabledConnectionsForPatient(QString::number(dbPatientID));
QStringList denyList = q->disabledConnectionsForPatient(QString::number(dbPatientID));
QMap<QString, QStringList> connectionsInformation = q->connectionsInformationForPatient(QString::number(dbPatientID));
QStringList allowList = connectionsInformation["allow"];
QStringList denyList = connectionsInformation["deny"];

bool connectionNameFound = allowList.contains(connectionName);
if (connectionNameFound)
Expand All @@ -431,10 +391,10 @@ bool ctkDICOMDatabasePrivate::insertConnectionName(const int& dbPatientID,
logger.debug("Found connection name in the patient database as UID: " + QString::number(dbPatientID));
logger.debug("New connection name ID cache item: " + QString::number(dbPatientID) + "->" + connectionName);
}
else
else if (!denyList.contains(connectionName))
{
allowList.append(connectionName);
QString connectionsData = convertListsToJson(allowList, denyList);
QString connectionsData = this->convertConnectionInfoToJson(allowList, denyList);

// insert connection name
QSqlQuery updateConnectionsStatement(this->Database);
Expand Down Expand Up @@ -469,7 +429,7 @@ bool ctkDICOMDatabasePrivate::updateConnections(const QString& dbPatientID,
const QStringList& allowList,
const QStringList& denyList)
{
QString connectionsData = convertListsToJson(allowList, denyList);
QString connectionsData = this->convertConnectionInfoToJson(allowList, denyList);

QSqlQuery updateConnectionsStatement(this->Database);
updateConnectionsStatement.prepare("UPDATE Patients SET Connections = :connectionsData WHERE UID = :uid");
Expand Down Expand Up @@ -1412,12 +1372,50 @@ QString ctkDICOMDatabasePrivate::internalPathFromAbsolute(const QString& filenam
return ctk::internalPathFromAbsolute(filename, q->databaseDirectory());
}

//------------------------------------------------------------------------------
QStringList ctkDICOMDatabasePrivate::jsonArrayToStringList(const QJsonArray& jsonArray)
{
QStringList stringList;
for (const QJsonValue& jsonValue : jsonArray)
{
if (jsonValue.isString())
{
stringList << jsonValue.toString();
}
}
return stringList;
}

//------------------------------------------------------------------------------
QJsonArray ctkDICOMDatabasePrivate::stringListToJsonArray(const QStringList& stringList)
{
QJsonArray jsonArray;
for (const QString& str : stringList)
{
jsonArray.append(str);
}
return jsonArray;
}

//------------------------------------------------------------------------------
QString ctkDICOMDatabasePrivate::convertConnectionInfoToJson(const QStringList& allowList,
const QStringList& denyList)
{
// Create a JSON object
QJsonObject jsonObject;
jsonObject["allow"] = this->stringListToJsonArray(allowList);
jsonObject["deny"] = this->stringListToJsonArray(denyList);

// Serialize the JSON object to a string
QJsonDocument jsonDoc(jsonObject);
return jsonDoc.toJson(QJsonDocument::Compact);
}

//------------------------------------------------------------------------------
CTK_GET_CPP(ctkDICOMDatabase, bool, isDisplayedFieldsTableAvailable, DisplayedFieldsTableAvailable);
CTK_GET_CPP(ctkDICOMDatabase, bool, useShortStoragePath, UseShortStoragePath);
CTK_SET_CPP(ctkDICOMDatabase, bool, setUseShortStoragePath, UseShortStoragePath);


//------------------------------------------------------------------------------
// ctkDICOMDatabase methods
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -1916,50 +1914,19 @@ QString ctkDICOMDatabase::nameForPatient(const QString patientUID)
}

//------------------------------------------------------------------------------
QStringList ctkDICOMDatabase::enabledConnectionsForPatient(const QString patientUID)
{
Q_D(ctkDICOMDatabase);

QString result;
QStringList allowList;

QSqlQuery query(d->Database);
query.prepare("SELECT Connections FROM Patients WHERE UID= ?");
query.addBindValue(patientUID);
if (!d->loggedExec(query))
{
return allowList;
}
if (query.next())
{
result.append(query.value(0).toString());
}

// Parse the JSON string
QJsonDocument jsonDoc = QJsonDocument::fromJson(result.toUtf8());
if (!jsonDoc.isNull() && jsonDoc.isObject())
{
QJsonObject jsonObject = jsonDoc.object();
allowList = jsonArrayToStringList(jsonObject["allow"].toArray());
}

return allowList;
}

//------------------------------------------------------------------------------
QStringList ctkDICOMDatabase::disabledConnectionsForPatient(const QString patientUID)
QMap<QString, QStringList> ctkDICOMDatabase::connectionsInformationForPatient(const QString patientUID)
{
Q_D(ctkDICOMDatabase);

QString result;
QStringList denyList;
QMap<QString, QStringList> connectionsInformation;

QSqlQuery query(d->Database);
query.prepare("SELECT Connections FROM Patients WHERE UID= ?");
query.addBindValue(patientUID);
if (!d->loggedExec(query))
{
return denyList;
return connectionsInformation;
}
if (query.next())
{
Expand All @@ -1971,10 +1938,11 @@ QStringList ctkDICOMDatabase::disabledConnectionsForPatient(const QString patien
if (!jsonDoc.isNull() && jsonDoc.isObject())
{
QJsonObject jsonObject = jsonDoc.object();
denyList = jsonArrayToStringList(jsonObject["deny"].toArray());
connectionsInformation["allow"] = d->jsonArrayToStringList(jsonObject["allow"].toArray());
connectionsInformation["deny"] = d->jsonArrayToStringList(jsonObject["deny"].toArray());
}

return denyList;
return connectionsInformation;
}

//------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 51648d6

Please sign in to comment.