2013-07-05 20:46:43 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2016-10-25 12:00:07 +03:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2013-07-05 20:46:43 +04:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "progressdispatcher.h"
|
|
|
|
|
2013-07-18 00:32:47 +04:00
|
|
|
#include <QObject>
|
2013-07-05 20:46:43 +04:00
|
|
|
#include <QMetaType>
|
2013-07-23 14:23:20 +04:00
|
|
|
#include <QDebug>
|
2013-08-17 13:20:17 +04:00
|
|
|
#include <QCoreApplication>
|
2013-07-05 20:46:43 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2013-07-05 20:46:43 +04:00
|
|
|
|
|
|
|
ProgressDispatcher* ProgressDispatcher::_instance = 0;
|
2014-03-14 16:03:16 +04:00
|
|
|
|
|
|
|
QString Progress::asResultString( const SyncFileItem& item)
|
2013-08-06 20:06:39 +04:00
|
|
|
{
|
2014-03-14 16:03:16 +04:00
|
|
|
switch(item._instruction) {
|
2014-04-15 18:54:49 +04:00
|
|
|
case CSYNC_INSTRUCTION_SYNC:
|
|
|
|
case CSYNC_INSTRUCTION_NEW:
|
2016-01-06 12:01:22 +03:00
|
|
|
case CSYNC_INSTRUCTION_TYPE_CHANGE:
|
2014-04-15 18:54:49 +04:00
|
|
|
if (item._direction != SyncFileItem::Up) {
|
|
|
|
return QCoreApplication::translate( "progress", "Downloaded");
|
|
|
|
} else {
|
|
|
|
return QCoreApplication::translate( "progress", "Uploaded");
|
|
|
|
}
|
2015-03-12 11:32:44 +03:00
|
|
|
case CSYNC_INSTRUCTION_CONFLICT:
|
2016-04-05 17:38:18 +03:00
|
|
|
return QCoreApplication::translate( "progress", "Server version downloaded, copied changed local file into conflict file");
|
2014-04-15 18:54:49 +04:00
|
|
|
case CSYNC_INSTRUCTION_REMOVE:
|
|
|
|
return QCoreApplication::translate( "progress", "Deleted");
|
|
|
|
case CSYNC_INSTRUCTION_EVAL_RENAME:
|
|
|
|
case CSYNC_INSTRUCTION_RENAME:
|
|
|
|
return QCoreApplication::translate( "progress", "Moved to %1").arg(item._renameTarget);
|
|
|
|
case CSYNC_INSTRUCTION_IGNORE:
|
|
|
|
return QCoreApplication::translate( "progress", "Ignored");
|
|
|
|
case CSYNC_INSTRUCTION_STAT_ERROR:
|
|
|
|
return QCoreApplication::translate( "progress", "Filesystem access error");
|
|
|
|
case CSYNC_INSTRUCTION_ERROR:
|
|
|
|
return QCoreApplication::translate( "progress", "Error");
|
2016-08-15 15:17:51 +03:00
|
|
|
case CSYNC_INSTRUCTION_UPDATE_METADATA:
|
|
|
|
return QCoreApplication::translate( "progress", "Updated local metadata");
|
2014-04-15 18:54:49 +04:00
|
|
|
case CSYNC_INSTRUCTION_NONE:
|
|
|
|
case CSYNC_INSTRUCTION_EVAL:
|
|
|
|
return QCoreApplication::translate( "progress", "Unknown");
|
|
|
|
|
2013-08-06 20:06:39 +04:00
|
|
|
}
|
2014-04-15 18:54:49 +04:00
|
|
|
return QCoreApplication::translate( "progress", "Unknown");
|
2013-08-06 20:06:39 +04:00
|
|
|
}
|
2013-07-05 20:46:43 +04:00
|
|
|
|
2014-03-14 16:03:16 +04:00
|
|
|
QString Progress::asActionString( const SyncFileItem &item )
|
2013-07-18 00:32:47 +04:00
|
|
|
{
|
2014-03-14 16:03:16 +04:00
|
|
|
switch(item._instruction) {
|
|
|
|
case CSYNC_INSTRUCTION_CONFLICT:
|
|
|
|
case CSYNC_INSTRUCTION_SYNC:
|
|
|
|
case CSYNC_INSTRUCTION_NEW:
|
2016-01-06 12:01:22 +03:00
|
|
|
case CSYNC_INSTRUCTION_TYPE_CHANGE:
|
2014-03-20 17:56:31 +04:00
|
|
|
if (item._direction != SyncFileItem::Up)
|
2014-03-14 16:03:16 +04:00
|
|
|
return QCoreApplication::translate( "progress", "downloading");
|
|
|
|
else
|
|
|
|
return QCoreApplication::translate( "progress", "uploading");
|
|
|
|
case CSYNC_INSTRUCTION_REMOVE:
|
|
|
|
return QCoreApplication::translate( "progress", "deleting");
|
|
|
|
case CSYNC_INSTRUCTION_EVAL_RENAME:
|
2014-04-15 18:54:49 +04:00
|
|
|
case CSYNC_INSTRUCTION_RENAME:
|
2014-03-14 16:03:16 +04:00
|
|
|
return QCoreApplication::translate( "progress", "moving");
|
2014-04-15 18:54:49 +04:00
|
|
|
case CSYNC_INSTRUCTION_IGNORE:
|
|
|
|
return QCoreApplication::translate( "progress", "ignoring");
|
|
|
|
case CSYNC_INSTRUCTION_STAT_ERROR:
|
|
|
|
return QCoreApplication::translate( "progress", "error");
|
|
|
|
case CSYNC_INSTRUCTION_ERROR:
|
|
|
|
return QCoreApplication::translate( "progress", "error");
|
2016-08-15 15:17:51 +03:00
|
|
|
case CSYNC_INSTRUCTION_UPDATE_METADATA:
|
|
|
|
return QCoreApplication::translate( "progress", "updating local metadata");
|
2014-04-15 18:54:49 +04:00
|
|
|
case CSYNC_INSTRUCTION_NONE:
|
|
|
|
case CSYNC_INSTRUCTION_EVAL:
|
2014-08-27 15:50:38 +04:00
|
|
|
break;
|
2013-07-18 00:32:47 +04:00
|
|
|
}
|
2014-08-27 15:50:38 +04:00
|
|
|
return QString();
|
2013-11-25 19:12:25 +04:00
|
|
|
}
|
|
|
|
|
2014-03-14 20:18:26 +04:00
|
|
|
bool Progress::isWarningKind( SyncFileItem::Status kind)
|
2013-11-25 19:12:25 +04:00
|
|
|
{
|
2014-03-14 20:18:26 +04:00
|
|
|
return kind == SyncFileItem::SoftError || kind == SyncFileItem::NormalError
|
|
|
|
|| kind == SyncFileItem::FatalError || kind == SyncFileItem::FileIgnored
|
2014-06-23 15:56:17 +04:00
|
|
|
|| kind == SyncFileItem::Conflict || kind == SyncFileItem::Restoration;
|
2014-03-14 20:18:26 +04:00
|
|
|
|
2013-07-18 00:32:47 +04:00
|
|
|
}
|
|
|
|
|
2015-01-08 13:42:14 +03:00
|
|
|
bool Progress::isIgnoredKind( SyncFileItem::Status kind)
|
|
|
|
{
|
|
|
|
return kind == SyncFileItem::FileIgnored;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-05 20:46:43 +04:00
|
|
|
ProgressDispatcher* ProgressDispatcher::instance() {
|
|
|
|
if (!_instance) {
|
|
|
|
_instance = new ProgressDispatcher();
|
|
|
|
}
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgressDispatcher::ProgressDispatcher(QObject *parent) :
|
2014-03-14 16:03:16 +04:00
|
|
|
QObject(parent)
|
2013-07-05 20:46:43 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgressDispatcher::~ProgressDispatcher()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-30 15:36:20 +03:00
|
|
|
void ProgressDispatcher::setProgressInfo(const QString& folder, const ProgressInfo& progress)
|
2013-07-05 20:46:43 +04:00
|
|
|
{
|
2014-08-15 17:00:10 +04:00
|
|
|
if( folder.isEmpty())
|
|
|
|
// The update phase now also has progress
|
|
|
|
// (progress._currentItems.size() == 0
|
|
|
|
// && progress._totalFileCount == 0) )
|
|
|
|
{
|
2013-07-26 15:44:38 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-03-14 16:03:16 +04:00
|
|
|
emit progressInfo( folder, progress );
|
2013-07-19 15:05:30 +04:00
|
|
|
}
|
|
|
|
|
2016-05-20 16:07:54 +03:00
|
|
|
ProgressInfo::ProgressInfo()
|
2015-01-30 15:36:20 +03:00
|
|
|
{
|
|
|
|
connect(&_updateEstimatesTimer, SIGNAL(timeout()), SLOT(updateEstimates()));
|
2016-05-20 16:07:54 +03:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressInfo::reset()
|
|
|
|
{
|
|
|
|
_currentItems.clear();
|
|
|
|
_currentDiscoveredFolder.clear();
|
|
|
|
_sizeProgress = Progress();
|
|
|
|
_fileProgress = Progress();
|
|
|
|
_totalSizeOfCompletedJobs = 0;
|
2016-12-20 15:23:58 +03:00
|
|
|
|
|
|
|
// Historically, these starting estimates were way lower, but that lead
|
|
|
|
// to gross overestimation of ETA when a good estimate wasn't available.
|
|
|
|
_maxBytesPerSecond = 2000000.0; // 2 MB/s
|
|
|
|
_maxFilesPerSecond = 10.0;
|
|
|
|
|
2016-05-20 16:07:54 +03:00
|
|
|
_updateEstimatesTimer.stop();
|
2016-10-18 13:29:52 +03:00
|
|
|
_lastCompletedItem = SyncFileItem();
|
2016-05-20 16:07:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressInfo::startEstimateUpdates()
|
|
|
|
{
|
2015-01-30 15:36:20 +03:00
|
|
|
_updateEstimatesTimer.start(1000);
|
|
|
|
}
|
|
|
|
|
2016-05-20 16:07:54 +03:00
|
|
|
bool ProgressInfo::isUpdatingEstimates() const
|
2015-01-30 15:36:20 +03:00
|
|
|
{
|
|
|
|
return _updateEstimatesTimer.isActive();
|
|
|
|
}
|
|
|
|
|
2016-05-27 11:52:00 +03:00
|
|
|
static bool shouldCountProgress(const SyncFileItem &item)
|
|
|
|
{
|
|
|
|
const auto instruction = item._instruction;
|
|
|
|
|
2016-08-15 15:17:51 +03:00
|
|
|
// Skip any ignored, error or non-propagated files and directories.
|
|
|
|
if (instruction == CSYNC_INSTRUCTION_NONE
|
|
|
|
|| instruction == CSYNC_INSTRUCTION_UPDATE_METADATA
|
|
|
|
|| instruction == CSYNC_INSTRUCTION_IGNORE
|
2016-05-27 11:52:00 +03:00
|
|
|
|| instruction == CSYNC_INSTRUCTION_ERROR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-30 15:36:20 +03:00
|
|
|
void ProgressInfo::adjustTotalsForFile(const SyncFileItem &item)
|
|
|
|
{
|
2016-05-27 11:52:00 +03:00
|
|
|
if (!shouldCountProgress(item)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_fileProgress._total += item._affectedItems;
|
|
|
|
if (isSizeDependent(item)) {
|
|
|
|
_sizeProgress._total += item._size;
|
2015-01-30 15:36:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 ProgressInfo::totalFiles() const
|
|
|
|
{
|
|
|
|
return _fileProgress._total;
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 ProgressInfo::completedFiles() const
|
|
|
|
{
|
|
|
|
return _fileProgress._completed;
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 ProgressInfo::currentFile() const
|
|
|
|
{
|
|
|
|
return completedFiles() + _currentItems.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 ProgressInfo::totalSize() const
|
|
|
|
{
|
|
|
|
return _sizeProgress._total;
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 ProgressInfo::completedSize() const
|
|
|
|
{
|
|
|
|
return _sizeProgress._completed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressInfo::setProgressComplete(const SyncFileItem &item)
|
|
|
|
{
|
2016-05-27 11:52:00 +03:00
|
|
|
if (!shouldCountProgress(item)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-30 15:36:20 +03:00
|
|
|
_currentItems.remove(item._file);
|
2015-08-11 11:38:04 +03:00
|
|
|
_fileProgress.setCompleted(_fileProgress._completed + item._affectedItems);
|
2015-01-30 15:36:20 +03:00
|
|
|
if (ProgressInfo::isSizeDependent(item)) {
|
|
|
|
_totalSizeOfCompletedJobs += item._size;
|
|
|
|
}
|
|
|
|
recomputeCompletedSize();
|
|
|
|
_lastCompletedItem = item;
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:38:04 +03:00
|
|
|
void ProgressInfo::setProgressItem(const SyncFileItem &item, quint64 completed)
|
2015-01-30 15:36:20 +03:00
|
|
|
{
|
2016-05-27 11:52:00 +03:00
|
|
|
if (!shouldCountProgress(item)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-30 15:36:20 +03:00
|
|
|
_currentItems[item._file]._item = item;
|
|
|
|
_currentItems[item._file]._progress._total = item._size;
|
2015-08-11 11:38:04 +03:00
|
|
|
_currentItems[item._file]._progress.setCompleted(completed);
|
2015-01-30 15:36:20 +03:00
|
|
|
recomputeCompletedSize();
|
|
|
|
|
|
|
|
// This seems dubious!
|
|
|
|
_lastCompletedItem = SyncFileItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgressInfo::Estimates ProgressInfo::totalProgress() const
|
|
|
|
{
|
|
|
|
Estimates file = _fileProgress.estimates();
|
|
|
|
if (_sizeProgress._total == 0) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
Estimates size = _sizeProgress.estimates();
|
|
|
|
|
|
|
|
// Ideally the remaining time would be modeled as:
|
|
|
|
// remaning_file_sizes / transfer_speed
|
|
|
|
// + remaining_file_count * per_file_overhead
|
|
|
|
// + remaining_chunked_file_sizes / chunked_reassembly_speed
|
|
|
|
// with us estimating the three parameters in conjunction.
|
|
|
|
//
|
|
|
|
// But we currently only model the bandwidth and the files per
|
|
|
|
// second independently, which leads to incorrect values. To slightly
|
|
|
|
// mitigate this problem, we combine the two models depending on
|
|
|
|
// which factor dominates (essentially big-file-upload vs.
|
|
|
|
// many-small-files)
|
|
|
|
//
|
|
|
|
// If we have size information, we prefer an estimate based
|
|
|
|
// on the upload speed. That's particularly relevant for large file
|
|
|
|
// up/downloads, where files per second will be close to 0.
|
|
|
|
//
|
|
|
|
// However, when many *small* files are transfered, the estimate
|
|
|
|
// can become very pessimistic as the transfered amount per second
|
|
|
|
// drops significantly.
|
|
|
|
//
|
2015-10-15 14:28:29 +03:00
|
|
|
// So, if we detect a high rate of files per second or a very low
|
|
|
|
// transfer rate (often drops hugely during a sequence of deletes,
|
|
|
|
// for instance), we gradually prefer an optimistic estimate and
|
|
|
|
// assume the remaining transfer will be done with the highest speed
|
|
|
|
// we've seen.
|
|
|
|
|
|
|
|
// Compute a value that is 0 when fps is <=L*max and 1 when fps is >=U*max
|
|
|
|
double fps = _fileProgress._progressPerSec;
|
|
|
|
double fpsL = 0.5;
|
|
|
|
double fpsU = 0.8;
|
|
|
|
double nearMaxFps =
|
|
|
|
qBound(0.0,
|
|
|
|
(fps - fpsL * _maxFilesPerSecond) /
|
|
|
|
((fpsU - fpsL) * _maxFilesPerSecond),
|
|
|
|
1.0);
|
|
|
|
|
|
|
|
// Compute a value that is 0 when transfer is >= U*max and
|
|
|
|
// 1 when transfer is <= L*max
|
|
|
|
double trans = _sizeProgress._progressPerSec;
|
|
|
|
double transU = 0.1;
|
|
|
|
double transL = 0.01;
|
|
|
|
double slowTransfer = 1.0 -
|
|
|
|
qBound(0.0,
|
|
|
|
(trans - transL * _maxBytesPerSecond) /
|
|
|
|
((transU - transL) * _maxBytesPerSecond),
|
|
|
|
1.0);
|
|
|
|
|
|
|
|
double beOptimistic = nearMaxFps * slowTransfer;
|
|
|
|
size.estimatedEta = (1.0 - beOptimistic) * size.estimatedEta
|
2016-08-15 14:36:53 +03:00
|
|
|
+ beOptimistic * optimisticEta();
|
2015-10-15 14:28:29 +03:00
|
|
|
|
2015-01-30 15:36:20 +03:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-08-15 14:36:53 +03:00
|
|
|
quint64 ProgressInfo::optimisticEta() const
|
|
|
|
{
|
|
|
|
// This assumes files and transfers finish as quickly as possible
|
|
|
|
// *but* note that maxPerSecond could be serious underestimate
|
|
|
|
// (if we never got to fully excercise transfer or files/second)
|
|
|
|
|
|
|
|
return _fileProgress.remaining() / _maxFilesPerSecond * 1000
|
|
|
|
+ _sizeProgress.remaining() / _maxBytesPerSecond * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProgressInfo::trustEta() const
|
|
|
|
{
|
|
|
|
return totalProgress().estimatedEta < 100 * optimisticEta();
|
|
|
|
}
|
|
|
|
|
2015-01-30 15:36:20 +03:00
|
|
|
ProgressInfo::Estimates ProgressInfo::fileProgress(const SyncFileItem &item) const
|
|
|
|
{
|
|
|
|
return _currentItems[item._file]._progress.estimates();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressInfo::updateEstimates()
|
|
|
|
{
|
|
|
|
_sizeProgress.update();
|
|
|
|
_fileProgress.update();
|
|
|
|
|
|
|
|
// Update progress of all running items.
|
|
|
|
QMutableHashIterator<QString, ProgressItem> it(_currentItems);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
it.value()._progress.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
_maxFilesPerSecond = qMax(_fileProgress._progressPerSec,
|
|
|
|
_maxFilesPerSecond);
|
|
|
|
_maxBytesPerSecond = qMax(_sizeProgress._progressPerSec,
|
|
|
|
_maxBytesPerSecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressInfo::recomputeCompletedSize()
|
|
|
|
{
|
|
|
|
quint64 r = _totalSizeOfCompletedJobs;
|
|
|
|
foreach(const ProgressItem &i, _currentItems) {
|
|
|
|
if (isSizeDependent(i._item))
|
|
|
|
r += i._progress._completed;
|
|
|
|
}
|
2015-08-11 11:38:04 +03:00
|
|
|
_sizeProgress.setCompleted(r);
|
2015-01-30 15:36:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ProgressInfo::Estimates ProgressInfo::Progress::estimates() const
|
|
|
|
{
|
|
|
|
Estimates est;
|
|
|
|
est.estimatedBandwidth = _progressPerSec;
|
|
|
|
if (_progressPerSec != 0) {
|
|
|
|
est.estimatedEta = (_total - _completed) / _progressPerSec * 1000.0;
|
|
|
|
} else {
|
|
|
|
est.estimatedEta = 0; // looks better than quint64 max
|
|
|
|
}
|
|
|
|
return est;
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 ProgressInfo::Progress::completed() const
|
|
|
|
{
|
|
|
|
return _completed;
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 ProgressInfo::Progress::remaining() const
|
|
|
|
{
|
|
|
|
return _total - _completed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressInfo::Progress::update()
|
|
|
|
{
|
|
|
|
// A good way to think about the smoothing factor:
|
|
|
|
// If we make progress P per sec and then stop making progress at all,
|
|
|
|
// after N calls to this function (and thus seconds) the _progressPerSec
|
|
|
|
// will have reduced to P*smoothing^N.
|
|
|
|
// With a value of 0.9, only 4% of the original value is left after 30s
|
|
|
|
//
|
|
|
|
// In the first few updates we want to go to the correct value quickly.
|
|
|
|
// Therefore, smoothing starts at 0 and ramps up to its final value over time.
|
|
|
|
const double smoothing = 0.9 * (1.0 - _initialSmoothing);
|
|
|
|
_initialSmoothing *= 0.7; // goes from 1 to 0.03 in 10s
|
|
|
|
_progressPerSec = smoothing * _progressPerSec + (1.0 - smoothing) * (_completed - _prevCompleted);
|
|
|
|
_prevCompleted = _completed;
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:38:04 +03:00
|
|
|
void ProgressInfo::Progress::setCompleted(quint64 completed)
|
|
|
|
{
|
2015-08-11 11:44:27 +03:00
|
|
|
_completed = qMin(completed, _total);
|
|
|
|
_prevCompleted = qMin(_prevCompleted, _completed);
|
2015-08-11 11:38:04 +03:00
|
|
|
}
|
|
|
|
|
2013-08-13 17:22:03 +04:00
|
|
|
|
2013-07-05 20:46:43 +04:00
|
|
|
}
|