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
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PROGRESSDISPATCHER_H
|
|
|
|
#define PROGRESSDISPATCHER_H
|
|
|
|
|
2014-04-29 17:30:19 +04:00
|
|
|
#include "owncloudlib.h"
|
2013-07-05 20:46:43 +04:00
|
|
|
#include <QObject>
|
2013-07-26 15:44:38 +04:00
|
|
|
#include <QHash>
|
2013-07-31 00:22:43 +04:00
|
|
|
#include <QTime>
|
|
|
|
#include <QQueue>
|
2013-10-08 16:07:46 +04:00
|
|
|
#include <QElapsedTimer>
|
2014-12-06 14:27:50 +03:00
|
|
|
#include <QDebug>
|
|
|
|
|
2014-03-14 16:03:16 +04:00
|
|
|
#include "syncfileitem.h"
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The FolderScheduler class schedules folders for sync
|
|
|
|
*/
|
2013-08-17 13:21:32 +04:00
|
|
|
namespace Progress
|
2013-07-05 20:46:43 +04:00
|
|
|
{
|
2014-03-14 16:03:16 +04:00
|
|
|
/** Return true is the size need to be taken in account in the total amount of time */
|
|
|
|
inline bool isSizeDependent(csync_instructions_e instruction) {
|
|
|
|
return instruction == CSYNC_INSTRUCTION_CONFLICT || instruction == CSYNC_INSTRUCTION_SYNC
|
|
|
|
|| instruction == CSYNC_INSTRUCTION_NEW;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct Info {
|
2014-04-28 19:49:27 +04:00
|
|
|
Info() : _totalFileCount(0), _totalSize(0), _completedFileCount(0), _completedSize(0) {}
|
2014-03-14 16:03:16 +04:00
|
|
|
|
2014-08-15 17:00:10 +04:00
|
|
|
// Used during local and remote update phase
|
|
|
|
QString _currentDiscoveredFolder;
|
|
|
|
|
2014-03-14 16:03:16 +04:00
|
|
|
quint64 _totalFileCount;
|
|
|
|
quint64 _totalSize;
|
|
|
|
quint64 _completedFileCount;
|
|
|
|
quint64 _completedSize;
|
2014-04-28 19:49:27 +04:00
|
|
|
// Should this be in a separate file?
|
|
|
|
struct EtaEstimate {
|
2014-05-14 11:55:04 +04:00
|
|
|
EtaEstimate() : _startedTime(QDateTime::currentMSecsSinceEpoch()), _agvEtaMSecs(0),_effectivProgressPerSec(0),_sampleCount(1) {}
|
2014-04-28 19:49:27 +04:00
|
|
|
|
2014-05-14 17:57:14 +04:00
|
|
|
static const int MAX_AVG_DIVIDER=60;
|
2014-05-12 01:26:17 +04:00
|
|
|
static const int INITAL_WAIT_TIME=5;
|
2014-04-28 19:49:27 +04:00
|
|
|
|
2014-05-14 11:55:04 +04:00
|
|
|
quint64 _startedTime ;
|
|
|
|
quint64 _agvEtaMSecs;
|
|
|
|
quint64 _effectivProgressPerSec;
|
2014-05-14 17:57:14 +04:00
|
|
|
float _sampleCount;
|
2014-04-28 19:49:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* reset the estiamte.
|
|
|
|
*/
|
|
|
|
void reset() {
|
|
|
|
_startedTime = QDateTime::currentMSecsSinceEpoch();
|
2014-05-14 11:55:04 +04:00
|
|
|
_sampleCount =1;
|
2014-04-28 19:49:27 +04:00
|
|
|
_effectivProgressPerSec = _agvEtaMSecs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update the estimated eta time with more current data.
|
|
|
|
* @param quint64 completed the amount the was completed.
|
|
|
|
* @param quint64 total the total amout that should be completed.
|
|
|
|
*/
|
|
|
|
void updateTime(quint64 completed, quint64 total) {
|
2014-05-12 01:26:17 +04:00
|
|
|
quint64 elapsedTime = QDateTime::currentMSecsSinceEpoch() - this->_startedTime ;
|
|
|
|
//don't start until you have some good data to process, prevents jittring estiamtes at the start of the syncing process
|
2014-05-14 11:55:04 +04:00
|
|
|
if(total != 0 && completed != 0 && elapsedTime > INITAL_WAIT_TIME ) {
|
2014-05-14 17:57:14 +04:00
|
|
|
if(_sampleCount < MAX_AVG_DIVIDER) { _sampleCount+=0.01f; }
|
2014-04-29 03:31:27 +04:00
|
|
|
// (elapsedTime-1) is an hack to avoid float "rounding" issue (ie. 0.99999999999999999999....)
|
|
|
|
_agvEtaMSecs = _agvEtaMSecs + (((static_cast<float>(total) / completed) * elapsedTime) - (elapsedTime-1)) - this->getEtaEstimate();
|
2014-04-28 19:49:27 +04:00
|
|
|
_effectivProgressPerSec = ( total - completed ) / (1+this->getEtaEstimate()/1000);
|
|
|
|
}
|
|
|
|
}
|
2014-04-29 03:31:27 +04:00
|
|
|
|
2014-04-28 19:49:27 +04:00
|
|
|
/**
|
|
|
|
* Get the eta estimate in milliseconds
|
|
|
|
* @return quint64 the estimate amount of milliseconds to end the process.
|
|
|
|
*/
|
|
|
|
quint64 getEtaEstimate() const {
|
2014-05-14 11:55:04 +04:00
|
|
|
return _agvEtaMSecs / _sampleCount;
|
2014-04-28 19:49:27 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 01:48:12 +04:00
|
|
|
/**
|
2014-04-28 19:49:27 +04:00
|
|
|
* Get the estimated average bandwidth usage.
|
|
|
|
* @return quint64 the estimated bandwidth usage in bytes.
|
|
|
|
*/
|
|
|
|
quint64 getEstimatedBandwidth() const {
|
|
|
|
return _effectivProgressPerSec;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
EtaEstimate _totalEtaEstimate;
|
2014-03-14 16:03:16 +04:00
|
|
|
|
|
|
|
struct ProgressItem {
|
|
|
|
ProgressItem() : _completedSize(0) {}
|
|
|
|
SyncFileItem _item;
|
|
|
|
quint64 _completedSize;
|
2014-04-28 19:49:27 +04:00
|
|
|
EtaEstimate _etaEstimate;
|
2014-03-14 16:03:16 +04:00
|
|
|
};
|
|
|
|
QHash<QString, ProgressItem> _currentItems;
|
|
|
|
SyncFileItem _lastCompletedItem;
|
|
|
|
|
|
|
|
void setProgressComplete(const SyncFileItem &item) {
|
|
|
|
_currentItems.remove(item._file);
|
2014-12-06 14:27:50 +03:00
|
|
|
_completedFileCount += item._affectedItems;
|
2014-04-30 14:53:32 +04:00
|
|
|
if (!item._isDirectory) {
|
2014-12-06 14:27:50 +03:00
|
|
|
if (Progress::isSizeDependent(item._instruction)) {
|
|
|
|
_completedSize += item._size;
|
|
|
|
}
|
2014-12-06 14:37:53 +03:00
|
|
|
}
|
2014-03-14 16:03:16 +04:00
|
|
|
_lastCompletedItem = item;
|
2014-05-14 11:55:04 +04:00
|
|
|
this->updateEstimation();
|
2014-03-14 16:03:16 +04:00
|
|
|
}
|
|
|
|
void setProgressItem(const SyncFileItem &item, quint64 size) {
|
|
|
|
_currentItems[item._file]._item = item;
|
|
|
|
_currentItems[item._file]._completedSize = size;
|
|
|
|
_lastCompletedItem = SyncFileItem();
|
2014-05-14 11:55:04 +04:00
|
|
|
this->updateEstimation();
|
2014-04-28 19:49:27 +04:00
|
|
|
_currentItems[item._file]._etaEstimate.updateTime(size,item._size);
|
|
|
|
}
|
2014-05-14 11:55:04 +04:00
|
|
|
|
|
|
|
void updateEstimation() {
|
|
|
|
if(this->_totalSize > 0) {
|
|
|
|
_totalEtaEstimate.updateTime(this->completedSize(),this->_totalSize);
|
|
|
|
} else {
|
|
|
|
_totalEtaEstimate.updateTime(this->_completedFileCount,this->_totalFileCount);
|
|
|
|
}
|
|
|
|
}
|
2014-03-14 16:03:16 +04:00
|
|
|
|
|
|
|
quint64 completedSize() const {
|
|
|
|
quint64 r = _completedSize;
|
|
|
|
foreach(const ProgressItem &i, _currentItems) {
|
2014-04-30 14:53:32 +04:00
|
|
|
if (!i._item._isDirectory)
|
|
|
|
r += i._completedSize;
|
2014-03-14 16:03:16 +04:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2014-04-25 02:08:25 +04:00
|
|
|
/**
|
2014-04-28 19:49:27 +04:00
|
|
|
* Get the total completion estimate structure
|
|
|
|
* @return EtaEstimate a structure containing the total completion information.
|
|
|
|
*/
|
2014-04-27 01:48:12 +04:00
|
|
|
EtaEstimate totalEstimate() const {
|
2014-04-28 19:49:27 +04:00
|
|
|
return _totalEtaEstimate;
|
2014-04-25 02:08:25 +04:00
|
|
|
}
|
2014-04-27 01:48:12 +04:00
|
|
|
|
2014-04-25 02:08:25 +04:00
|
|
|
/**
|
2014-04-28 19:49:27 +04:00
|
|
|
* Get the current file completion estimate structure
|
|
|
|
* @return EtaEstimate a structure containing the current file completion information.
|
|
|
|
*/
|
|
|
|
EtaEstimate getFileEstimate(const SyncFileItem &item) const {
|
|
|
|
return _currentItems[item._file]._etaEstimate;
|
2014-04-27 01:48:12 +04:00
|
|
|
}
|
2014-03-14 16:03:16 +04:00
|
|
|
};
|
|
|
|
|
2014-04-29 17:30:19 +04:00
|
|
|
OWNCLOUDSYNC_EXPORT QString asActionString( const SyncFileItem& item );
|
|
|
|
OWNCLOUDSYNC_EXPORT QString asResultString( const SyncFileItem& item );
|
2013-11-25 19:16:33 +04:00
|
|
|
|
2014-04-29 17:30:19 +04:00
|
|
|
OWNCLOUDSYNC_EXPORT bool isWarningKind( SyncFileItem::Status );
|
2015-01-08 13:42:14 +03:00
|
|
|
OWNCLOUDSYNC_EXPORT bool isIgnoredKind( SyncFileItem::Status );
|
2014-03-14 16:03:16 +04:00
|
|
|
|
2013-08-17 13:21:32 +04:00
|
|
|
}
|
2013-07-05 20:46:43 +04:00
|
|
|
|
2013-07-23 15:32:24 +04:00
|
|
|
/**
|
|
|
|
* @file progressdispatcher.h
|
|
|
|
* @brief A singleton class to provide sync progress information to other gui classes.
|
|
|
|
*
|
|
|
|
* How to use the ProgressDispatcher:
|
|
|
|
* Just connect to the two signals either to progress for every individual file
|
|
|
|
* or the overall sync progress.
|
|
|
|
*
|
|
|
|
*/
|
2014-04-29 17:30:19 +04:00
|
|
|
class OWNCLOUDSYNC_EXPORT ProgressDispatcher : public QObject
|
2013-07-05 20:46:43 +04:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
2013-07-23 15:32:24 +04:00
|
|
|
|
|
|
|
friend class Folder; // only allow Folder class to access the setting slots.
|
2013-07-05 20:46:43 +04:00
|
|
|
public:
|
|
|
|
static ProgressDispatcher* instance();
|
|
|
|
~ProgressDispatcher();
|
|
|
|
|
|
|
|
signals:
|
2013-07-23 15:32:24 +04:00
|
|
|
/**
|
2013-07-26 15:44:38 +04:00
|
|
|
@brief Signals the progress of data transmission.
|
2013-07-23 15:32:24 +04:00
|
|
|
|
|
|
|
@param[out] folder The folder which is being processed
|
2014-03-14 16:03:16 +04:00
|
|
|
@param[out] progress A struct with all progress info.
|
2013-07-23 15:32:24 +04:00
|
|
|
|
|
|
|
*/
|
2013-07-31 00:22:43 +04:00
|
|
|
void progressInfo( const QString& folder, const Progress::Info& progress );
|
2014-06-06 17:52:55 +04:00
|
|
|
/**
|
|
|
|
* @brief: the item's job is completed
|
|
|
|
*/
|
|
|
|
void jobCompleted(const QString &folder, const SyncFileItem & item);
|
2013-07-05 20:46:43 +04:00
|
|
|
|
2014-08-27 21:05:26 +04:00
|
|
|
void syncItemDiscovered(const QString &folder, const SyncFileItem & item);
|
|
|
|
|
2013-07-23 15:32:24 +04:00
|
|
|
protected:
|
2013-11-25 19:16:33 +04:00
|
|
|
void setProgressInfo(const QString& folder, const Progress::Info& progress);
|
2013-07-05 20:46:43 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
ProgressDispatcher(QObject* parent = 0);
|
2013-10-08 16:07:46 +04:00
|
|
|
|
|
|
|
QElapsedTimer _timer;
|
2013-07-05 20:46:43 +04:00
|
|
|
static ProgressDispatcher* _instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // PROGRESSDISPATCHER_H
|