Show remote progress in cli

This commit is contained in:
Martchus 2018-01-25 00:03:31 +01:00
parent 9017de6e1d
commit b9c87a9bba
7 changed files with 69 additions and 34 deletions

View File

@ -469,7 +469,7 @@ bool Application::findPwd()
return false;
}
void Application::printDir(const RelevantDir &relevantDir)
void Application::printDir(const RelevantDir &relevantDir) const
{
const SyncthingDir *const dir = relevantDir.dirObj;
cout << " - ";
@ -484,8 +484,18 @@ void Application::printDir(const RelevantDir &relevantDir)
printProperty("Last scan time", dir->lastScanTime);
printProperty("Last file time", dir->lastFileTime);
printProperty("Last file name", dir->lastFileName);
printProperty("Download progress", dir->downloadLabel);
printProperty("Shared with", dir->deviceNames.isEmpty() ? dir->deviceIds : dir->deviceNames);
printProperty("Download progress", dir->downloadLabel);
if (!dir->completionByDevice.empty()) {
printProperty("Remote progress", dir->areRemotesUpToDate() ? "all up-to-date" : "some need bytes");
for (const auto &completionForDev : dir->completionByDevice) {
printProperty(m_connection.deviceNameOrId(completionForDev.first).toLocal8Bit().data(),
argsToString(dataSizeToString(completionForDev.second.globalBytes - completionForDev.second.neededBytes), ' ', '/', ' ',
dataSizeToString(completionForDev.second.globalBytes), ' ', '(', static_cast<int>(completionForDev.second.percentage), " %)")
.data(),
nullptr, 6);
}
}
printProperty("Read-only", dir->readOnly);
printProperty("Ignore permissions", dir->ignorePermissions);
printProperty("Auto-normalize", dir->autoNormalize);
@ -502,7 +512,7 @@ void Application::printDir(const RelevantDir &relevantDir)
cout << '\n';
}
void Application::printDev(const SyncthingDev *dev)
void Application::printDev(const SyncthingDev *dev) const
{
cout << " - ";
setStyle(cout, TextAttribute::Bold);
@ -535,7 +545,7 @@ void Application::printStatus(const ArgumentOccurrence &)
setStyle(cout, TextAttribute::Bold);
cout << "Directories\n";
setStyle(cout);
for_each(m_relevantDirs.cbegin(), m_relevantDirs.cend(), &Application::printDir);
for_each(m_relevantDirs.cbegin(), m_relevantDirs.cend(), bind(&Application::printDir, this, placeholders::_1));
}
// display devs
@ -543,7 +553,7 @@ void Application::printStatus(const ArgumentOccurrence &)
setStyle(cout, TextAttribute::Bold);
cout << "Devices\n";
setStyle(cout);
for_each(m_relevantDevs.cbegin(), m_relevantDevs.cend(), &Application::printDev);
for_each(m_relevantDevs.cbegin(), m_relevantDevs.cend(), bind(&Application::printDev, this, placeholders::_1));
}
cout.flush();

View File

@ -60,8 +60,8 @@ private:
void requestRescan(const ArgumentOccurrence &occurrence);
void requestRescanAll(const ArgumentOccurrence &);
void requestPauseResume(bool pause);
static void printDir(const RelevantDir &relevantDir);
static void printDev(const Data::SyncthingDev *dev);
void printDir(const RelevantDir &relevantDir) const;
void printDev(const Data::SyncthingDev *dev) const;
void printStatus(const ArgumentOccurrence &);
static void printLog(const std::vector<Data::SyncthingLogEntry> &logEntries);
void waitForIdle(const ArgumentOccurrence &);

View File

@ -623,6 +623,19 @@ QStringList SyncthingConnection::deviceIds() const
return ids(m_devs);
}
/*!
* \brief Returns the device name for the specified \a deviceId if known; otherwise returns just the \a deviceId itself.
*/
QString SyncthingConnection::deviceNameOrId(const QString &deviceId) const
{
for (const auto &dev : devInfo()) {
if (dev.id == deviceId) {
return dev.name;
}
}
return deviceId;
}
/*!
* \brief Returns the number of devices Syncthing is currently connected to.
* \remarks Computed by looping devInfo().
@ -1161,10 +1174,8 @@ void SyncthingConnection::readConnections()
// read traffic, the conversion to double is neccassary because toInt() doesn't work for high values
const QJsonValue totalIncomingTrafficValue(totalObj.value(QStringLiteral("inBytesTotal")));
const QJsonValue totalOutgoingTrafficValue(totalObj.value(QStringLiteral("outBytesTotal")));
const uint64 totalIncomingTraffic
= totalIncomingTrafficValue.isDouble() ? static_cast<uint64>(totalIncomingTrafficValue.toDouble(0.0)) : unknownTraffic;
const uint64 totalOutgoingTraffic
= totalOutgoingTrafficValue.isDouble() ? static_cast<uint64>(totalOutgoingTrafficValue.toDouble(0.0)) : unknownTraffic;
const uint64 totalIncomingTraffic = totalIncomingTrafficValue.isDouble() ? jsonValueToInt(totalIncomingTrafficValue) : unknownTraffic;
const uint64 totalOutgoingTraffic = totalOutgoingTrafficValue.isDouble() ? jsonValueToInt(totalOutgoingTrafficValue) : unknownTraffic;
double transferTime;
const bool hasDelta
= !m_lastConnectionsUpdate.isNull() && ((transferTime = (DateTime::gmtNow() - m_lastConnectionsUpdate).totalSeconds()) != 0.0);
@ -1203,8 +1214,8 @@ void SyncthingConnection::readConnections()
}
}
dev.paused = connectionObj.value(QStringLiteral("paused")).toBool(false);
dev.totalIncomingTraffic = static_cast<uint64>(connectionObj.value(QStringLiteral("inBytesTotal")).toDouble(0));
dev.totalOutgoingTraffic = static_cast<uint64>(connectionObj.value(QStringLiteral("outBytesTotal")).toDouble(0));
dev.totalIncomingTraffic = jsonValueToInt(connectionObj.value(QStringLiteral("inBytesTotal")));
dev.totalOutgoingTraffic = jsonValueToInt(connectionObj.value(QStringLiteral("outBytesTotal")));
dev.connectionAddress = connectionObj.value(QStringLiteral("address")).toString();
dev.connectionType = connectionObj.value(QStringLiteral("type")).toString();
dev.clientVersion = connectionObj.value(QStringLiteral("clientVersion")).toString();
@ -1628,9 +1639,9 @@ void SyncthingConnection::readDirEvent(DateTime eventTime, const QString &eventT
} else if (eventType == QLatin1String("FolderSummary")) {
readDirSummary(eventTime, eventData.value(QStringLiteral("summary")).toObject(), *dirInfo, index);
} else if (eventType == QLatin1String("FolderCompletion")) {
const int percentage = static_cast<int>(eventData.value(QStringLiteral("completion")).toDouble());
dirInfo->globalBytes = static_cast<uint64>(eventData.value(QStringLiteral("globalBytes")).toDouble(dirInfo->globalBytes));
dirInfo->neededBytes = static_cast<uint64>(eventData.value(QStringLiteral("neededBytes")).toDouble(dirInfo->neededBytes));
const int percentage = jsonValueToInt<int>(eventData.value(QStringLiteral("completion")));
dirInfo->globalBytes = jsonValueToInt(eventData.value(QStringLiteral("globalBytes")), dirInfo->globalBytes);
dirInfo->neededBytes = jsonValueToInt(eventData.value(QStringLiteral("neededBytes")), dirInfo->neededBytes);
if (percentage > 0 && percentage < 100) {
dirInfo->completionPercentage = percentage;
emit dirStatusChanged(*dirInfo, index);
@ -1924,16 +1935,16 @@ bool SyncthingConnection::readDirSummary(DateTime eventTime, const QJsonObject &
}
// update statistics
dir.globalBytes = toUInt64(summary.value(QStringLiteral("globalBytes")));
dir.globalDeleted = toUInt64(summary.value(QStringLiteral("globalDeleted")));
dir.globalFiles = toUInt64(summary.value(QStringLiteral("globalFiles")));
dir.globalDirs = toUInt64(summary.value(QStringLiteral("globalDirectories")));
dir.localBytes = toUInt64(summary.value(QStringLiteral("localBytes")));
dir.localDeleted = toUInt64(summary.value(QStringLiteral("localDeleted")));
dir.localFiles = toUInt64(summary.value(QStringLiteral("localFiles")));
dir.localDirs = toUInt64(summary.value(QStringLiteral("localDirectories")));
dir.neededBytes = toUInt64(summary.value(QStringLiteral("needByted")));
dir.neededFiles = toUInt64(summary.value(QStringLiteral("needFiles")));
dir.globalBytes = jsonValueToInt(summary.value(QStringLiteral("globalBytes")));
dir.globalDeleted = jsonValueToInt(summary.value(QStringLiteral("globalDeleted")));
dir.globalFiles = jsonValueToInt(summary.value(QStringLiteral("globalFiles")));
dir.globalDirs = jsonValueToInt(summary.value(QStringLiteral("globalDirectories")));
dir.localBytes = jsonValueToInt(summary.value(QStringLiteral("localBytes")));
dir.localDeleted = jsonValueToInt(summary.value(QStringLiteral("localDeleted")));
dir.localFiles = jsonValueToInt(summary.value(QStringLiteral("localFiles")));
dir.localDirs = jsonValueToInt(summary.value(QStringLiteral("localDirectories")));
dir.neededBytes = jsonValueToInt(summary.value(QStringLiteral("needByted")));
dir.neededFiles = jsonValueToInt(summary.value(QStringLiteral("needFiles")));
dir.ignorePatterns = summary.value(QStringLiteral("ignorePatterns")).toBool();
dir.lastStatisticsUpdate = eventTime;
@ -1985,11 +1996,11 @@ void SyncthingConnection::readCompletion()
const auto replyObj(replyDoc.object());
auto &completion = dir->completionByDevice[devId];
completion.lastUpdate = DateTime::gmtNow();
completion.percentage = replyObj.value(QLatin1String("completion")).toInt();
completion.globalBytes = toUInt64(replyObj.value(QLatin1String("globalBytes")));
completion.neededBytes = toUInt64(replyObj.value(QLatin1String("needBytes")));
completion.neededItems = toUInt64(replyObj.value(QLatin1String("needItems")));
completion.neededDeletes = toUInt64(replyObj.value(QLatin1String("needDeletes")));
completion.percentage = replyObj.value(QLatin1String("completion")).toDouble();
completion.globalBytes = jsonValueToInt(replyObj.value(QLatin1String("globalBytes")));
completion.neededBytes = jsonValueToInt(replyObj.value(QLatin1String("needBytes")));
completion.neededItems = jsonValueToInt(replyObj.value(QLatin1String("needItems")));
completion.neededDeletes = jsonValueToInt(replyObj.value(QLatin1String("needDeletes")));
emit dirStatusChanged(*dir, index);
break;
}

View File

@ -128,6 +128,7 @@ public:
const std::vector<SyncthingDir *> &completedDirs() const;
QStringList directoryIds() const;
QStringList deviceIds() const;
QString deviceNameOrId(const QString &deviceId) const;
size_t connectedDevices() const;
public Q_SLOTS:

View File

@ -132,6 +132,16 @@ QStringRef SyncthingDir::pathWithoutTrailingSlash() const
return dirPath;
}
bool SyncthingDir::areRemotesUpToDate() const
{
for (const auto &completionForDev : completionByDevice) {
if (completionForDev.second.neededBytes) {
return false;
}
}
return true;
}
SyncthingItemDownloadProgress::SyncthingItemDownloadProgress(
const QString &containingDirPath, const QString &relativeItemPath, const QJsonObject &values)
: relativePath(relativeItemPath)

View File

@ -53,11 +53,11 @@ struct LIB_SYNCTHING_CONNECTOR_EXPORT SyncthingItemDownloadProgress {
struct LIB_SYNCTHING_CONNECTOR_EXPORT SyncthingCompletion {
ChronoUtilities::DateTime lastUpdate;
double percentage = 0;
quint64 globalBytes = 0;
quint64 neededBytes = 0;
quint64 neededItems = 0;
quint64 neededDeletes = 0;
int percentage = 0;
};
struct LIB_SYNCTHING_CONNECTOR_EXPORT SyncthingDir {
@ -67,6 +67,7 @@ struct LIB_SYNCTHING_CONNECTOR_EXPORT SyncthingDir {
const QString &displayName() const;
QString statusString() const;
QStringRef pathWithoutTrailingSlash() const;
bool areRemotesUpToDate() const;
QString id;
QString label;

View File

@ -4,6 +4,7 @@
#include "./global.h"
#include <c++utilities/conversion/types.h>
#include <c++utilities/misc/traits.h>
#include <QJsonValue>
#include <QStringList>
@ -30,9 +31,10 @@ bool LIB_SYNCTHING_CONNECTOR_EXPORT isLocal(const QUrl &url);
bool LIB_SYNCTHING_CONNECTOR_EXPORT setDirectoriesPaused(QJsonObject &syncthingConfig, const QStringList &dirIds, bool paused);
bool LIB_SYNCTHING_CONNECTOR_EXPORT setDevicesPaused(QJsonObject &syncthingConfig, const QStringList &dirs, bool paused);
inline quint64 LIB_SYNCTHING_CONNECTOR_EXPORT toUInt64(const QJsonValue &value, double defaultValue = 0.0)
template <typename IntType = quint64, Traits::EnableIf<std::is_integral<IntType>>...>
inline IntType LIB_SYNCTHING_CONNECTOR_EXPORT jsonValueToInt(const QJsonValue &value, double defaultValue = 0.0)
{
return static_cast<quint64>(value.toDouble(defaultValue));
return static_cast<IntType>(value.toDouble(defaultValue));
}
constexpr int LIB_SYNCTHING_CONNECTOR_EXPORT trQuandity(quint64 quandity)