refactor: propogate only only one StepProgress at a time

Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
This commit is contained in:
Rachel Powers 2023-04-02 21:51:07 -07:00
parent fe36471b8d
commit 0fb6a2836b
6 changed files with 80 additions and 74 deletions

View File

@ -35,13 +35,15 @@
*/
#include "ConcurrentTask.h"
#include <QDebug>
#include <QCoreApplication>
#include <QDebug>
#include "tasks/Task.h"
ConcurrentTask::ConcurrentTask(QObject* parent, QString task_name, int max_concurrent)
: Task(parent), m_name(task_name), m_total_max_size(max_concurrent)
{ setObjectName(task_name); }
{
setObjectName(task_name);
}
ConcurrentTask::~ConcurrentTask()
{
@ -126,18 +128,17 @@ void ConcurrentTask::startNext()
Task::Ptr next = m_queue.dequeue();
connect(next.get(), &Task::succeeded, this, [this, next](){ subTaskSucceeded(next); });
connect(next.get(), &Task::succeeded, this, [this, next]() { subTaskSucceeded(next); });
connect(next.get(), &Task::failed, this, [this, next](QString msg) { subTaskFailed(next, msg); });
connect(next.get(), &Task::status, this, [this, next](QString msg){ subTaskStatus(next, msg); });
connect(next.get(), &Task::details, this, [this, next](QString msg){ subTaskDetails(next, msg); });
connect(next.get(), &Task::stepProgress, this, [this, next](TaskStepProgressList tp){ subTaskStepProgress(next, tp); });
connect(next.get(), &Task::status, this, [this, next](QString msg) { subTaskStatus(next, msg); });
connect(next.get(), &Task::details, this, [this, next](QString msg) { subTaskDetails(next, msg); });
connect(next.get(), &Task::stepProgress, this, [this, next](TaskStepProgress tp) { subTaskStepProgress(next, tp); });
connect(next.get(), &Task::progress, this, [this, next](qint64 current, qint64 total){ subTaskProgress(next, current, total); });
connect(next.get(), &Task::progress, this, [this, next](qint64 current, qint64 total) { subTaskProgress(next, current, total); });
m_doing.insert(next.get(), next);
m_task_progress.insert(next->getUid(), std::make_shared<TaskStepProgress>(TaskStepProgress({next->getUid()})));
m_task_progress.insert(next->getUid(), std::make_shared<TaskStepProgress>(TaskStepProgress({ next->getUid() })));
updateState();
updateStepProgress();
@ -158,10 +159,12 @@ void ConcurrentTask::subTaskSucceeded(Task::Ptr task)
m_succeeded.insert(task.get(), task);
m_doing.remove(task.get());
m_task_progress.value(task->getUid())->state = TaskStepState::Succeeded;
auto task_progress = m_task_progress.value(task->getUid());
task_progress->state = TaskStepState::Succeeded;
disconnect(task.get(), 0, this, 0);
emit stepProgress(*task_progress.get());
updateState();
updateStepProgress();
startNext();
@ -173,10 +176,13 @@ void ConcurrentTask::subTaskFailed(Task::Ptr task, const QString& msg)
m_failed.insert(task.get(), task);
m_doing.remove(task.get());
m_task_progress.value(task->getUid())->state = TaskStepState::Failed;
auto task_progress = m_task_progress.value(task->getUid());
task_progress->state = TaskStepState::Failed;
disconnect(task.get(), 0, this, 0);
emit stepProgress(*task_progress.get());
updateState();
updateStepProgress();
startNext();
@ -184,68 +190,71 @@ void ConcurrentTask::subTaskFailed(Task::Ptr task, const QString& msg)
void ConcurrentTask::subTaskStatus(Task::Ptr task, const QString& msg)
{
auto taskProgress = m_task_progress.value(task->getUid());
taskProgress->status = msg;
taskProgress->state = TaskStepState::Running;
auto task_progress = m_task_progress.value(task->getUid());
task_progress->status = msg;
task_progress->state = TaskStepState::Running;
emit stepProgress(*task_progress.get());
updateStepProgress();
}
void ConcurrentTask::subTaskDetails(Task::Ptr task, const QString& msg)
{
auto taskProgress = m_task_progress.value(task->getUid());
taskProgress->details = msg;
taskProgress->state = TaskStepState::Running;
auto task_progress = m_task_progress.value(task->getUid());
task_progress->details = msg;
task_progress->state = TaskStepState::Running;
emit stepProgress(*task_progress.get());
updateStepProgress();
}
void ConcurrentTask::subTaskProgress(Task::Ptr task, qint64 current, qint64 total)
{
auto taskProgress = m_task_progress.value(task->getUid());
auto task_progress = m_task_progress.value(task->getUid());
taskProgress->current = current;
taskProgress->total = total;
taskProgress->state = TaskStepState::Running;
task_progress->current = current;
task_progress->total = total;
task_progress->state = TaskStepState::Running;
emit stepProgress(*task_progress.get());
updateStepProgress();
updateState();
}
void ConcurrentTask::subTaskStepProgress(Task::Ptr task, TaskStepProgressList task_step_progress)
void ConcurrentTask::subTaskStepProgress(Task::Ptr task, TaskStepProgress task_progress)
{
for (auto progress : task_step_progress) {
if (!m_task_progress.contains(progress->uid)) {
m_task_progress.insert(progress->uid, progress);
if (!m_task_progress.contains(task_progress.uid)) {
m_task_progress.insert(task_progress.uid, std::make_shared<TaskStepProgress>(task_progress));
} else {
auto tp = m_task_progress.value(progress->uid);
tp->current = progress->current;
tp->total = progress->total;
tp->status = progress->status;
tp->details = progress->details;
}
auto tp = m_task_progress.value(task_progress.uid);
tp->current = task_progress.current;
tp->total = task_progress.total;
tp->status = task_progress.status;
tp->details = task_progress.details;
}
emit stepProgress(task_progress);
updateStepProgress();
}
void ConcurrentTask::updateStepProgress()
{
qint64 current = 0, total = 0;
for ( auto taskProgress : m_task_progress ) {
for (auto taskProgress : m_task_progress) {
current += taskProgress->current;
total += taskProgress->total;
}
m_stepProgress = current;
m_stepTotalProgress = total;
emit stepProgress(m_task_progress.values());
}
void ConcurrentTask::updateState()
{
if (totalSize() > 1) {
setProgress(m_done.count(), totalSize());
setStatus(tr("Executing %1 task(s) (%2 out of %3 are done)").arg(QString::number(m_doing.count()), QString::number(m_done.count()), QString::number(totalSize())));
setStatus(tr("Executing %1 task(s) (%2 out of %3 are done)")
.arg(QString::number(m_doing.count()), QString::number(m_done.count()), QString::number(totalSize())));
} else {
setProgress(m_stepProgress, m_stepTotalProgress);
QString status = tr("Please wait ...");

View File

@ -77,7 +77,7 @@ slots:
void subTaskStatus(Task::Ptr task, const QString &msg);
void subTaskDetails(Task::Ptr task, const QString &msg);
void subTaskProgress(Task::Ptr task, qint64 current, qint64 total);
void subTaskStepProgress(Task::Ptr task, TaskStepProgressList task_step_progress);
void subTaskStepProgress(Task::Ptr task, TaskStepProgress task_step_progress);
protected:
// NOTE: This is not thread-safe.

View File

@ -161,7 +161,7 @@ void Task::emitSucceeded()
emit finished();
}
void Task::propogateStepProgress(TaskStepProgressList task_progress)
void Task::propogateStepProgress(TaskStepProgress task_progress)
{
emit stepProgress(task_progress);
}

View File

@ -48,8 +48,7 @@ enum class TaskStepState {
Waiting,
Running,
Failed,
Succeeded,
Finished
Succeeded
};
Q_DECLARE_METATYPE(TaskStepState)
@ -61,7 +60,7 @@ struct TaskStepProgress {
QString status = "";
QString details = "";
TaskStepState state = TaskStepState::Waiting;
bool isDone() { return (state == TaskStepState::Failed) || (state == TaskStepState::Succeeded) || (state == TaskStepState::Finished); }
bool isDone() { return (state == TaskStepState::Failed) || (state == TaskStepState::Succeeded); }
};
Q_DECLARE_METATYPE(TaskStepProgress)
@ -127,7 +126,7 @@ class Task : public QObject, public QRunnable {
void failed(QString reason);
void status(QString status);
void details(QString details);
void stepProgress(TaskStepProgressList task_progress); //
void stepProgress(TaskStepProgress task_progress); //
/** Emitted when the canAbort() status has changed.
*/
@ -150,7 +149,7 @@ class Task : public QObject, public QRunnable {
virtual void emitAborted();
virtual void emitFailed(QString reason = "");
virtual void propogateStepProgress(TaskStepProgressList task_progress);
virtual void propogateStepProgress(TaskStepProgress task_progress);
public slots:
void setStatus(const QString& status);

View File

@ -46,8 +46,8 @@
// map a value in a numaric range of an arbatray type to between 0 and INT_MAX
// for getting the best percision out of the qt progress bar
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
// for getting the best precision out of the qt progress bar
template<typename T, std::enable_if_t<std::is_arithmetic_v<T>, bool> = true>
std::tuple<int, int> map_int_zero_max(T current, T range_max, T range_min)
{
int int_max = std::numeric_limits<int>::max();
@ -213,7 +213,7 @@ void ProgressDialog::addTaskProgress(TaskStepProgress* progress)
ui->taskProgressLayout->addWidget(task_bar);
}
void ProgressDialog::changeStepProgress(TaskStepProgressList task_progress)
void ProgressDialog::changeStepProgress(TaskStepProgress task_progress)
{
m_is_multi_step = true;
if(ui->taskProgressScrollArea->isHidden()) {
@ -221,28 +221,26 @@ void ProgressDialog::changeStepProgress(TaskStepProgressList task_progress)
updateSize();
}
for (auto tp : task_progress) {
if (!taskProgress.contains(tp->uid))
addTaskProgress(tp.get());
auto task_bar = taskProgress.value(tp->uid);
if (!taskProgress.contains(task_progress.uid))
addTaskProgress(&task_progress);
auto task_bar = taskProgress.value(task_progress.uid);
auto const [mapped_current, mapped_total] = map_int_zero_max<qint64>(tp->current, tp->total, 0);
if (tp->total <= 0) {
auto const [mapped_current, mapped_total] = map_int_zero_max<qint64>(task_progress.current, task_progress.total, 0);
if (task_progress.total <= 0) {
task_bar->setRange(0, 0);
} else {
task_bar->setRange(0, mapped_total);
}
task_bar->setValue(mapped_current);
task_bar->setStatus(tp->status);
task_bar->setDetails(tp->details);
task_bar->setStatus(task_progress.status);
task_bar->setDetails(task_progress.details);
if (tp->isDone()) {
if (task_progress.isDone()) {
task_bar->setVisible(false);
}
}
}
void ProgressDialog::changeProgress(qint64 current, qint64 total)

View File

@ -80,7 +80,7 @@ slots:
void changeStatus(const QString &status);
void changeProgress(qint64 current, qint64 total);
void changeStepProgress(TaskStepProgressList task_progress);
void changeStepProgress(TaskStepProgress task_progress);
private