Skip to content

Commit

Permalink
Add new column to display speed
Browse files Browse the repository at this point in the history
  • Loading branch information
thesues committed Jun 21, 2019
1 parent 43ce43c commit 8cc9baf
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 59 deletions.
25 changes: 13 additions & 12 deletions src/S3ClientGUI/helper.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#include "helper.h"
#include <QStringList>

QString helper::formattedDataSize(qlonglong s, int precision) {
static QStringList units = {"B", "KB", "MB", "GB", "TB", "PB", "EB"};
int power=0;
int base=1000;
if (s) {
//math.log(s, 1000)
power = int(std::log10(qAbs(s)) / 3);
}
QString helper::formattedDataSize(qlonglong s, int precision)
{
static QStringList units = {"B", "KB", "MB", "GB", "TB", "PB", "EB"};
int power = 0;
int base = 1000;
if (s) {
//math.log(s, 1000)
power = int(std::log10(qAbs(s)) / 3);
}

const QString number = power
? QString::number(s/ std::pow(double(base), power), 'f', qMin(precision, 3 * power))
: QString::number(s);
const QString number = power
? QString::number(s / std::pow(double(base), power), 'f', qMin(precision, 3 * power))
: QString::number(s);

return number + QLatin1Char(' ') + units[power];
return number + QLatin1Char(' ') + units[power];
}

2 changes: 1 addition & 1 deletion src/S3ClientGUI/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <QString>
#include <complex>
namespace helper {
QString formattedDataSize(qlonglong s, int precision=2);
QString formattedDataSize(qlonglong s, int precision = 2);
}
#endif

67 changes: 36 additions & 31 deletions src/S3ClientGUI/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ void MainWindow::on_LocalContextMenuRequest(const QPoint &point)
if (info.isFile() && !m_s3model->getRootBucket().isEmpty()) {
menu.addAction("Upload", this, SLOT(on_upload()));
menu.exec(m_fsview->mapToGlobal(point));
} else if (info.isDir() && index.data().toString() != ".." && !m_s3model->getRootBucket().isEmpty()) {
} else if (info.isDir() && index.data().toString() != ".."
&& !m_s3model->getRootBucket().isEmpty()) {
menu.addAction("Upload", this, SLOT(OnUploadDir()));
menu.exec(m_fsview->mapToGlobal(point));
} else {
Expand All @@ -366,27 +367,31 @@ void MainWindow::on_LocalContextMenuRequest(const QPoint &point)
}


void MainWindow::addTask(QString bucketName, QString localFilePath, QString remoteFilePath,
QString key, bool taskStatus)
void MainWindow::createTask(QString bucketName, QString localFilePath, QString remoteFilePath,
QString key, bool isUpload)
{
QSharedPointer<TransferTask> t = QSharedPointer<TransferTask>(new TransferTask);

if (taskStatus) {
if (isUpload) {
t->transferType = TaskDirection::Upload;
t->size = "unknown";
t->size = tr("");
UploadObjectHandler *pHandler = m_s3client->UploadFile(localFilePath, bucketName,
remoteFilePath, "");
remoteFilePath, "");
t->pInstance = pHandler;
} else {
t->transferType = TaskDirection::Download;
DownloadObjectHandler *pHandler = m_s3client->DownloadFile(bucketName, remoteFilePath,
localFilePath);
localFilePath);
t->pInstance = pHandler;
}

t->localFileName = localFilePath;
t->remoteFileName = m_s3model->getRootPath() + key;
t->status = TaskStatus::Queueing;
t->currentSpeed = tr("");
t->lastTransfered = 0;
t->transfered = 0;


m_transferTabWidget->addTask(t);
}
Expand All @@ -403,11 +408,11 @@ void MainWindow::OnUploadDir()
QString remoteFilePath;

QDirIterator it(info.absoluteFilePath(), QStringList(), QDir::Files,
QDirIterator::Subdirectories);
QDirIterator::Subdirectories);
while (it.hasNext()) {
fileList.append(it.next());
}
for (auto& file : fileList) {
for (auto &file : fileList) {
QString localFilePath = file.absoluteFilePath();
QString keyName = localFilePath;
keyName.remove(0, parentDirPath.length());
Expand All @@ -417,7 +422,7 @@ void MainWindow::OnUploadDir()
qDebug() << localFilePath;
qDebug() << info.fileName();

addTask(remoteBucketName, localFilePath, remoteFilePath, keyName, true);
createTask(remoteBucketName, localFilePath, remoteFilePath, keyName, true);
}
}

Expand All @@ -442,7 +447,7 @@ void MainWindow::on_upload()
//can not upload;
return;
}
addTask(remoteBucketName, localFile, KeyName, info.fileName(), true);
createTask(remoteBucketName, localFile, KeyName, info.fileName(), true);
}

void MainWindow::on_open()
Expand Down Expand Up @@ -510,7 +515,7 @@ void MainWindow::on_download()
QMessageBox::Yes);
return;
}
addTask(item->bucketName, downloadFile, item->objectPath, item->objectPath, false);
createTask(item->bucketName, downloadFile, item->objectPath, item->objectPath, false);

qDebug() << "inside UI thread:" << QThread::currentThread();

Expand All @@ -523,7 +528,7 @@ void MainWindow::OnDownloadDir()
if (!proxyIndex.isValid())
return;
QModelIndex index = static_cast<const QSortFilterProxyModel *>(proxyIndex.model())->mapToSource(
proxyIndex);
proxyIndex);
SimpleItem *item = static_cast<SimpleItem *>(index.internalPointer());
QString bucketName = item->bucketName;
QString prefix = item->objectPath;
Expand All @@ -539,25 +544,25 @@ void MainWindow::OnDownloadDir()
//must be a dir
if (path.isDir() && path.isWritable() == false) {
QMessageBox::warning(this,
windowTitle(),
tr("\"%1\" is not writable").arg(m_fsview->currentPath()),
QMessageBox::Yes);
windowTitle(),
tr("\"%1\" is not writable").arg(m_fsview->currentPath()),
QMessageBox::Yes);
return;
}

if (!prefix.endsWith(seperator))
prefix.append(seperator);
ListObjectAction *loAction = m_s3client->ListObjects(bucketName, "", prefix, "");
connect(loAction, &ListObjectAction::ListObjectInfo, this, [=](s3object object,
QString bucketName) {
connect(loAction, &ListObjectAction::ListObjectInfo, this, [ = ](s3object object,
QString bucketName) {
downloadList << AwsString2QString(object.GetKey());
});
connect(loAction, &ListObjectAction::ListPrefixInfo, this, [=](s3prefix prefix,
QString bucketName) {
connect(loAction, &ListObjectAction::ListPrefixInfo, this, [ = ](s3prefix prefix,
QString bucketName) {
downloadList << AwsString2QString(prefix.GetPrefix());
});
connect(loAction, &ListObjectAction::ListObjectFinished, this, [=](bool success, s3error error,
bool truncated, QString nextMarker) {
connect(loAction, &ListObjectAction::ListObjectFinished, this, [ = ](bool success, s3error error,
bool truncated, QString nextMarker) {
for (auto remoteFilePath : downloadList) {
qDebug() << "fileName" << m_fsview->currentPath() << endl;
qDebug() << "bucket=" << bucketName << endl;
Expand All @@ -579,7 +584,7 @@ void MainWindow::OnDownloadDir()
if (localFilePath.endsWith("/"))
continue;
qDebug() << "downloadFile=" << localFilePath << endl;
addTask(item->bucketName, localFilePath, remoteFilePath, localKeyName, false);
createTask(item->bucketName, localFilePath, remoteFilePath, localKeyName, false);
}
downloadList.clear();
});
Expand Down Expand Up @@ -621,24 +626,24 @@ void MainWindow::on_taskFinished(QSharedPointer<TransferTask> t)
} else {
if (fileExists > 0) {
QMessageBox::warning(this,
tr("Download"),
tr("Some File Already Exists"));
tr("Download"),
tr("Some File Already Exists"));
fileExists = 0;
}
if (fileFailed > 0) {
QMessageBox::warning(this,
tr("Download"),
tr("Some File Download Failed"));
tr("Download"),
tr("Some File Download Failed"));
fileFailed = 0;
}
if (t->status == TaskStatus::ObjectAlreadyExists) {
QMessageBox::warning(this,
tr("Download"),
tr("File Already Exists"));
tr("Download"),
tr("File Already Exists"));
} else if (t->status != TaskStatus::SuccessCompleted) {
QMessageBox::warning(this,
tr("Download"),
tr("File Download Failed"));
tr("Download"),
tr("File Download Failed"));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/S3ClientGUI/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ private slots:
void on_upload();
void OnUploadDir();
void on_delete();
void addTask(QString bucketName, QString localFilePath, QString remoteFilePath,
QString key, bool taskStatus);
void createTask(QString bucketName, QString localFilePath, QString remoteFilePath,
QString key, bool isUpload);

void on_taskFinished(QSharedPointer<TransferTask> t);
void on_cmdFinished(bool, s3error);
Expand Down
43 changes: 34 additions & 9 deletions src/S3ClientGUI/qtaskmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ QTaskModel::QTaskModel(QObject *parent): QAbstractTableModel(parent)
int availableSlot = QThreadPool::globalInstance()->maxThreadCount() - 1;
m_scheduler = new QTaskScheduler(this, QThreadPool::globalInstance(), availableSlot);
m_scheduler->start();
int interval = 1; //display speed interval, unit: seconds
m_timer.setInterval(interval * 1000);
m_timer.start();

QObject::connect(&m_timer, &QTimer::timeout, this, [ = ]() {
for (auto task : this->m_tasks) {
//speed is Byte/s
int speed = (task->transfered - task->lastTransfered) / interval;
if (speed > 0 ) {
const QModelIndex &speedIndex = this->indexOfTask(SPEED_COLUMN, task);
task->currentSpeed = QString("%1/s").arg(helper::formattedDataSize(speed));
emit dataChanged(speedIndex, speedIndex);
task->lastTransfered = task->transfered;
}


}
});
}

int QTaskModel::rowCount(const QModelIndex &parent) const
Expand Down Expand Up @@ -87,6 +105,9 @@ QVariant QTaskModel::data(const QModelIndex &index, int role) const
return helper::formattedDataSize(task->size.toLongLong());
case STATUS_COLUMN:
return taskStatusStringMapper(task->status);
case SPEED_COLUMN:
return task->currentSpeed;

}
// use qt::userrole to filter model
}
Expand Down Expand Up @@ -162,6 +183,7 @@ void QTaskModel::addTask(QSharedPointer<TransferTask> t)
emit dataChanged(sizeIndex, sizeIndex);
}

t->transfered = transfered;

if (total == 0)
t->progress = 100;
Expand All @@ -171,10 +193,10 @@ void QTaskModel::addTask(QSharedPointer<TransferTask> t)
return;
}

//Update the progress column
const QModelIndex &progressIndex = this->indexOfTask(PROGRESS_COLUMN, t);
emit dataChanged(progressIndex, progressIndex);


emit TaskUpdateProgress(t);
});
}
Expand All @@ -184,14 +206,15 @@ QVariant QTaskModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
QString titles[DISPLAY_TASK_COLUNM] = {
"localfile",
"direction",
"remotefilename",
"progress",
"size",
"status"
}
;
tr("localfile"),
tr("direction"),
tr("remotefilename"),
tr("progress"),
tr("size"),
tr("status"),
tr("speed")
};

if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
return titles[section];
}
Expand Down Expand Up @@ -235,8 +258,10 @@ int QTaskModel::runningJobs()

QTaskModel::~QTaskModel()
{
//
m_scheduler->stopme();
m_scheduler->wait();
delete m_scheduler;
}


Expand Down
16 changes: 14 additions & 2 deletions src/S3ClientGUI/qtaskmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <QQueue>
#include <QWaitCondition>
#include <QDebug>
#include <QTime>
#include <QTimer>


using namespace qlibs3;
Expand Down Expand Up @@ -36,6 +38,11 @@ QString taskStatusStringMapper(TaskStatus s);
struct TransferTask {
QString uuid;
ObjectHandlerInterface *pInstance;
/*
* speedTimer runs every 5 seconds,
*/
uint64_t transfered;
uint64_t lastTransfered;

//for display
QString localFileName;
Expand All @@ -44,14 +51,18 @@ struct TransferTask {
TaskStatus status;
QString size;
int progress;
//From this->transfered to caculate currentSpeed
QString currentSpeed;
};

#define DISPLAY_TASK_COLUNM 6

#define DISPLAY_TASK_COLUNM 7


#define SIZE_COLUMN 4
#define PROGRESS_COLUMN 3
#define STATUS_COLUMN 5
#define SPEED_COLUMN 6

class QTaskModel;

Expand Down Expand Up @@ -82,7 +93,7 @@ class QTaskModel: public QAbstractTableModel
Q_OBJECT
public:
QTaskModel(QObject *parent = 0);
~QTaskModel();
~QTaskModel() override;
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE ;
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
Expand All @@ -103,6 +114,7 @@ class QTaskModel: public QAbstractTableModel
private:
QList<QSharedPointer<TransferTask>> m_tasks;
QTaskScheduler *m_scheduler;
QTimer m_timer;
};


Expand Down
3 changes: 2 additions & 1 deletion src/S3ClientGUI/s3treemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ void S3TreeModel::listObjectInfo(s3object object, QString bucketName)
}

data << name << AwsString2QString(object.GetLastModified().ToLocalTimeString("%Y/%m/%d %R"))
<< AwsString2QString(object.GetOwner().GetDisplayName()) << helper::formattedDataSize(object.GetSize(),1);
<< AwsString2QString(object.GetOwner().GetDisplayName()) << helper::formattedDataSize(
object.GetSize(), 1);

//
data << AwsString2QString(object.GetETag()) << AwsString2QString(
Expand Down
2 changes: 1 addition & 1 deletion src/s3util
Submodule s3util updated 1 files
+0 −1 s3consolemanager.cpp

0 comments on commit 8cc9baf

Please sign in to comment.