Compute overall statistics

This commit is contained in:
Martchus 2018-07-30 23:00:22 +02:00
parent 59924f431c
commit 3549a4cdef
4 changed files with 99 additions and 4 deletions

View File

@ -262,6 +262,7 @@ void SyncthingConnection::continueReconnecting()
m_lastConnectionsUpdate = DateTime();
m_lastFileTime = DateTime();
m_lastErrorTime = DateTime();
m_startTime = DateTime();
m_lastFileName.clear();
m_lastFileDeleted = false;
if (m_apiKey.isEmpty() || m_syncthingUrl.isEmpty()) {
@ -1176,15 +1177,16 @@ void SyncthingConnection::readStatus()
case QNetworkReply::NoError: {
const QByteArray response(reply->readAll());
QJsonParseError jsonError;
const QJsonDocument replyDoc = QJsonDocument::fromJson(response, &jsonError);
const auto replyDoc(QJsonDocument::fromJson(response, &jsonError));
if (jsonError.error != QJsonParseError::NoError) {
emitError(tr("Unable to parse Syncthing status: "), jsonError, reply, response);
handleFatalConnectionError();
return;
}
emitMyIdChanged(replyDoc.object().value(QLatin1String("myID")).toString());
// other values are currently not interesting
const auto replyObj(replyDoc.object());
emitMyIdChanged(replyObj.value(QLatin1String("myID")).toString());
m_startTime = DateTime::fromIsoStringGmt(replyObj.value(QLatin1String("startTime")).toString().toLocal8Bit().data());
m_hasStatus = true;
if (m_keepPolling) {
@ -1826,7 +1828,7 @@ void SyncthingConnection::readItemFinished(DateTime eventTime, const QJsonObject
return;
}
// handle unsuccessfull operation
// handle unsuccessful operation
const auto error(eventData.value(QLatin1String("error")).toString()), item(eventData.value(QLatin1String("item")).toString());
if (!error.isEmpty()) {
if (dirInfo->status == SyncthingDirStatus::OutOfSync) {

View File

@ -82,6 +82,8 @@ class LIB_SYNCTHING_CONNECTOR_EXPORT SyncthingConnection : public QObject {
Q_PROPERTY(int totalOutgoingTraffic READ totalOutgoingTraffic NOTIFY trafficChanged)
Q_PROPERTY(double totalIncomingRate READ totalIncomingRate NOTIFY trafficChanged)
Q_PROPERTY(double totalOutgoingRate READ totalOutgoingRate NOTIFY trafficChanged)
Q_PROPERTY(QString lastSyncedFile READ lastSyncedFile)
Q_PROPERTY(ChronoUtilities::DateTime lastSyncTime READ lastSyncTime)
Q_PROPERTY(QList<QSslError> expectedSslErrors READ expectedSslErrors)
Q_PROPERTY(std::vector<const SyncthingDev *> connectedDevices READ connectedDevices)
Q_PROPERTY(QStringList directoryIds READ directoryIds)
@ -126,6 +128,11 @@ public:
static constexpr uint64 unknownTraffic = std::numeric_limits<uint64>::max();
const std::vector<SyncthingDir> &dirInfo() const;
const std::vector<SyncthingDev> &devInfo() const;
SyncthingOverallDirStatistics computeOverallDirStatistics() const;
const QString &lastSyncedFile() const;
ChronoUtilities::DateTime lastSyncTime() const;
ChronoUtilities::DateTime startTime() const;
ChronoUtilities::TimeSpan uptime() const;
QMetaObject::Connection requestQrCode(const QString &text, std::function<void(const QByteArray &)> callback);
QMetaObject::Connection requestLog(std::function<void(const std::vector<SyncthingLogEntry> &)> callback);
const QList<QSslError> &expectedSslErrors() const;
@ -299,6 +306,7 @@ private:
ChronoUtilities::DateTime m_lastConnectionsUpdate;
ChronoUtilities::DateTime m_lastFileTime;
ChronoUtilities::DateTime m_lastErrorTime;
ChronoUtilities::DateTime m_startTime;
QString m_lastFileName;
bool m_lastFileDeleted;
QList<QSslError> m_expectedSslErrors;
@ -573,6 +581,46 @@ inline const std::vector<SyncthingDev> &SyncthingConnection::devInfo() const
return m_devs;
}
/*!
* \brief Computes overall directory statistics based on the currently available directory information.
*/
inline SyncthingOverallDirStatistics SyncthingConnection::computeOverallDirStatistics() const
{
return SyncthingOverallDirStatistics(dirInfo());
}
/*!
* \brief Returns the name of the file which has been synced most recently.
*/
inline const QString &SyncthingConnection::lastSyncedFile() const
{
return m_lastFileName;
}
/*!
* \brief Returns the time of the most recent sync.
*/
inline ChronoUtilities::DateTime SyncthingConnection::lastSyncTime() const
{
return m_lastFileTime;
}
/*!
* \brief Returns when Syncthing has been started.
*/
inline ChronoUtilities::DateTime SyncthingConnection::startTime() const
{
return m_startTime;
}
/*!
* \brief Returns how long Syncthing has been running.
*/
inline ChronoUtilities::TimeSpan SyncthingConnection::uptime() const
{
return ChronoUtilities::DateTime::gmtNow() - m_startTime;
}
/*!
* \brief Returns a list of all expected certificate errors. This is meant to allow self-signed certificates.
* \remarks This list is updated via loadSelfSignedCertificate().

View File

@ -193,4 +193,26 @@ SyncthingItemDownloadProgress::SyncthingItemDownloadProgress(
{
}
SyncthingStatistics &SyncthingStatistics::operator+=(const SyncthingStatistics &other)
{
bytes += other.bytes;
deletes += other.deletes;
dirs += other.dirs;
files += other.files;
symlinks += other.symlinks;
return *this;
}
/*!
* \brief Computes overall statistics for the specified \a directories.
*/
SyncthingOverallDirStatistics::SyncthingOverallDirStatistics(const std::vector<SyncthingDir> &directories)
{
for (const auto &dir : directories) {
local += dir.localStats;
global += dir.globalStats;
needed += dir.neededStats;
}
}
} // namespace Data

View File

@ -94,6 +94,7 @@ struct LIB_SYNCTHING_CONNECTOR_EXPORT SyncthingStatistics {
constexpr bool isNull() const;
constexpr bool operator==(const SyncthingStatistics &other) const;
constexpr bool operator!=(const SyncthingStatistics &other) const;
SyncthingStatistics &operator+=(const SyncthingStatistics &other);
};
constexpr bool SyncthingStatistics::isNull() const
@ -197,10 +198,32 @@ inline bool SyncthingDir::assignStatus(SyncthingDirStatus newStatus, ChronoUtili
return checkWhetherStatusUpdateRelevant(time) && finalizeStatusUpdate(newStatus);
}
struct LIB_SYNCTHING_CONNECTOR_EXPORT SyncthingOverallDirStatistics {
SyncthingOverallDirStatistics();
SyncthingOverallDirStatistics(const std::vector<SyncthingDir> &directories);
SyncthingStatistics local;
SyncthingStatistics global;
SyncthingStatistics needed;
bool isNull() const;
};
inline SyncthingOverallDirStatistics::SyncthingOverallDirStatistics()
{
}
inline bool SyncthingOverallDirStatistics::isNull() const
{
return local.isNull() && global.isNull();
}
} // namespace Data
Q_DECLARE_METATYPE(Data::SyncthingItemError)
Q_DECLARE_METATYPE(Data::SyncthingItemDownloadProgress)
Q_DECLARE_METATYPE(Data::SyncthingStatistics)
Q_DECLARE_METATYPE(Data::SyncthingOverallDirStatistics)
Q_DECLARE_METATYPE(Data::SyncthingDir)
#endif // DATA_SYNCTHINGDIR_H