Skip to content

Commit

Permalink
submit logs to dropbox
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterPetrik authored and wonder-sk committed Sep 28, 2020
1 parent 704d09a commit c3dab89
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 109 deletions.
124 changes: 123 additions & 1 deletion app/inputhelp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@
***************************************************************************/

#include "inputhelp.h"
#include "merginuserauth.h"
#include "merginuserinfo.h"
#include "merginsubscriptionstatus.h"
#include "merginapi.h"
#include "inpututils.h"

#include "qgsquickutils.h"

#include <QNetworkReply>
#include <QSysInfo>

const QString inputHelpRoot = "https://help.inputapp.io";
const QString merginHelpRoot = "https://help.cloudmergin.com";
const QString reportLogUrl = QStringLiteral( "https://opl1bkwxhg.execute-api.us-east-1.amazonaws.com/md_test_function" );
const QString helpDeskMail = QStringLiteral( "info@lutraconsulting.co.uk" );

InputHelp::InputHelp()
InputHelp::InputHelp( MerginApi *merginApi, InputUtils *utils ):
mMerginApi( merginApi ),
mInputUtils( utils )
{
emit linkChanged();
}
Expand Down Expand Up @@ -51,3 +65,111 @@ QString InputHelp::howToDownloadProjectLink() const
{
return inputHelpRoot + "/howto/data_sync";
}

bool InputHelp::submitReportPending() const
{
return mSubmitReportPending;
}

QString InputHelp::fullLog( bool isHtml )
{
qint64 limit = 500000;
QVector<QString> retLines;
QFile file( InputUtils::logFilename() );
if ( file.open( QIODevice::ReadOnly ) )
{
qint64 fileSize = file.size();
if ( fileSize > limit )
file.seek( file.size() - limit );

QString line = file.readLine();
while ( !line.isNull() )
{
retLines.push_back( line );
line = file.readLine();
}

file.close();
}
else
{
return QString( "Unable to open log file %1" ).arg( InputUtils::logFilename() );
}

QString ret;
// now add some extra info
retLines.push_back( QStringLiteral( "------------------------------------------" ) );
retLines.append( QgsQuickUtils().dumpScreenInfo().split( "\n" ).toVector() );
retLines.push_back( QStringLiteral( "Screen Info:" ) );
if ( !mMerginApi->userInfo()->email().isEmpty() )
{
retLines.push_back( QStringLiteral( "Mergin Data: %1/%2 Bytes" )
.arg( InputUtils::bytesToHumanSize( mMerginApi->userInfo()->diskUsage() ) )
.arg( InputUtils::bytesToHumanSize( mMerginApi->userInfo()->storageLimit() ) ) );
retLines.push_back( QStringLiteral( "Subscription plan: %1" ).arg( mMerginApi->userInfo()->planAlias() ) );
retLines.push_back( QStringLiteral( "Subscription Status: %1" ).arg( MerginSubscriptionStatus::toString( static_cast<MerginSubscriptionStatus::SubscriptionStatus>( mMerginApi->userInfo()->subscriptionStatus() ) ) ) );
}
else
{
retLines.push_back( QStringLiteral( "%1Mergin User Profile not available. To include it, open you Profile Page in InputApp%2" ).arg( isHtml ? "<b>" : "" ).arg( isHtml ? "</b>" : "" ) );
}
retLines.push_back( QStringLiteral( "Mergin User: %1" ).arg( mMerginApi->userAuth()->username() ) );
retLines.push_back( QStringLiteral( "System: %1" ).arg( QSysInfo::prettyProductName() ) );
retLines.push_back( QStringLiteral( "Mergin URL: %1" ).arg( mMerginApi->apiRoot() ) );
retLines.push_back( QStringLiteral( "InputApp: %1 - %2" ).arg( InputUtils::appVersion() ).arg( InputUtils::appPlatform() ) );

// now reverse so the most recent messages are on top and add separators
std::reverse( std::begin( retLines ), std::end( retLines ) );
int i = 0;
for ( const QString &str : retLines )
{
++i;
if ( isHtml )
ret += QStringLiteral( "<p class=\"%1\">" ).arg( i % 2 == 0 ? "odd" : "even" ) + str.trimmed() + "</p>";
else
ret += QString( i ) + str.trimmed() + "\n";
}

return ret;
}

void InputHelp::submitReport()
{
// There is a limit of 1MB on the remote service, send less, let say half of that
QString log = fullLog( false );
QByteArray logArr = log.toUtf8();
QString app = QStringLiteral( "input-%1-%2" ).arg( InputUtils::appPlatform() ).arg( InputUtils::appVersion() );
QString username = mMerginApi->userAuth()->username().toHtmlEscaped();
if ( username.isEmpty() )
username = "unknown";
QString params = QStringLiteral( "?app=%1&username=%2" ).arg( app ).arg( username );
QNetworkRequest req( reportLogUrl + params );
req.setRawHeader( "User-Agent", "InputApp" );
req.setRawHeader( "Content-Type", "text/plain" );
QNetworkReply *reply = mManager.post( req, logArr );
qDebug() << "Report to " << reportLogUrl << endl;

mSubmitReportPending = true;
emit submitReportPendingChanged();
connect( reply, &QNetworkReply::finished, this, &InputHelp::onSubmitReportReplyFinished );
}

void InputHelp::onSubmitReportReplyFinished()
{
mSubmitReportPending = false;
emit submitReportPendingChanged();

QNetworkReply *r = qobject_cast<QNetworkReply *>( sender() );
Q_ASSERT( r );

if ( r->error() == QNetworkReply::NoError )
{
InputUtils::log( "submit report", "Report submitted!" );
emit mInputUtils->showNotification( tr( "Report submitted.%1Please contact us on%1%2" ).arg( "<br />" ).arg( helpDeskMail ) );
}
else
{
InputUtils::log( "submit report", QStringLiteral( "FAILED - %1" ).arg( r->errorString() ) );
emit mInputUtils->showNotification( tr( "Failed to submit report.%1Please check your internet connection." ).arg( "<br>" ) );
}
}
33 changes: 32 additions & 1 deletion app/inputhelp.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

#include <QObject>
#include <QString>
#include <QNetworkAccessManager>

class MerginApi;
class InputUtils;

class InputHelp: public QObject
{
Expand All @@ -26,11 +30,17 @@ class InputHelp: public QObject
Q_PROPERTY( QString howToCreateNewProjectLink READ howToCreateNewProjectLink NOTIFY linkChanged )
Q_PROPERTY( QString howToDownloadProjectLink READ howToDownloadProjectLink NOTIFY linkChanged )

Q_PROPERTY( bool submitReportPending READ submitReportPending NOTIFY submitReportPendingChanged )

signals:
void linkChanged();
void submitReportPendingChanged();

public slots:
void onSubmitReportReplyFinished();

public:
explicit InputHelp();
explicit InputHelp( MerginApi *merginApi, InputUtils *utils );

QString privacyPolicyLink() const;
QString merginSubscriptionDetailsLink() const;
Expand All @@ -39,6 +49,27 @@ class InputHelp: public QObject
QString howToSetupThemesLink() const;
QString howToCreateNewProjectLink() const;
QString howToDownloadProjectLink() const;

bool submitReportPending() const;
/**
* Reads and returns the internal text log file content.
*
* The latest messages in the log come at the beginning. Only last 0.5MB are read.
* Prepends the information about screen, device, logged user and application
*
* \see log()
*/
Q_INVOKABLE QString fullLog( bool isHtml );

/** Submit user log*/
Q_INVOKABLE void submitReport( );


private:
MerginApi *mMerginApi = nullptr;
InputUtils *mInputUtils = nullptr;
QNetworkAccessManager mManager;
bool mSubmitReportPending = false;
};

#endif // INPUTHELP_H
103 changes: 33 additions & 70 deletions app/inpututils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "qgslinestring.h"
#include "qgspolygon.h"
#include "qgsvectorlayer.h"
#include "qgsquickutils.h"

#include "qgsquickmaptransform.h"

Expand All @@ -22,12 +23,9 @@
#include <QFileInfo>
#include <QRegularExpression>
#include <algorithm>
#include <QNetworkReply>

QString InputUtils::sLogFile = QStringLiteral();
static const QString DATE_TIME_FORMAT = QStringLiteral( "yyMMdd-hhmmss" );
static const QString reportLogUrl = QStringLiteral("https://opl1bkwxhg.execute-api.us-east-1.amazonaws.com/default/md_test_function");
static const QString helpDeskMail = QStringLiteral("info@lutraconsulting.co.uk");

InputUtils::InputUtils( QObject *parent ): QObject( parent )
{
Expand Down Expand Up @@ -265,6 +263,11 @@ void InputUtils::setLogFilename( const QString &value )
sLogFile = value;
}

QString InputUtils::logFilename()
{
return sLogFile;
}

QString InputUtils::filesToString( QList<MerginFile> files )
{
QStringList resultList;
Expand Down Expand Up @@ -334,6 +337,33 @@ QString InputUtils::localizedDateFromUTFString( QString timestamp )
}
}

QString InputUtils::appVersion()
{
QString version;
#ifdef INPUT_VERSION
version = STR( INPUT_VERSION );
#endif
return version;
}

QString InputUtils::appPlatform()
{
#if defined( ANDROID )
const QString platform = "android";
#elif defined( Q_OS_IOS )
const QString platform = "ios";
#elif defined( Q_OS_WIN32 )
const QString platform = "win";
#elif defined( Q_OS_LINUX )
const QString platform = "linux";
#elif defined( Q_OS_MAC )
const QString platform = "macos";
#else
const QString platform = "unknown";
#endif
return platform;
}

void InputUtils::onQgsLogMessageReceived( const QString &message, const QString &tag, Qgis::MessageLevel level )
{
QString levelStr;
Expand Down Expand Up @@ -403,73 +433,6 @@ bool InputUtils::cpDir( const QString &srcPath, const QString &dstPath, bool onl
return result;
}

QString InputUtils::fullLog( int limit )
{
QVector<QString> retLines;
QString ret;

QFile file( sLogFile );
if ( file.open( QIODevice::ReadOnly ) )
{
file.seek( file.size() - 1 );
int count = 0;
while ( ( count < limit ) && ( file.pos() > 0 ) )
{
QString ch = file.read( 1 );
file.seek( file.pos() - 2 );
if ( ch == "\n" )
count++;
}

QString line = file.readLine();
while ( !line.isNull() )
{
retLines.push_back( line );
line = file.readLine();
}

std::reverse( std::begin( retLines ), std::end( retLines ) );

QString ret;
for ( const QString &str : retLines )
{
ret += str.trimmed() + "<br/>" + "<br/>";
}

file.close();
return ret;
}
else
{
ret = QString( "Unable to open log file %1" ).arg( sLogFile );
}

return ret;
}

void InputUtils::submitReport()
{
reportLogUrl
}

void InputUtils::onSubmitReportReplyFinished()
{
QNetworkReply *r = qobject_cast<QNetworkReply *>( sender() );
Q_ASSERT( r );

if ( r->error() == QNetworkReply::NoError )
{
QString remoteLogName = "TODO";
InputUtils::log( "submit report", "Report submitted " + remoteLogName );
emit showNotification( tr("Report submitted.%1Please contact out help-desk on email %1%2%1 with the details of your problem.").arg(helpDeskMail) );
}
else
{
InputUtils::log("submit report", QStringLiteral( "FAILED - %1" ).arg( r->errorString() ) );
emit showNotification( tr("Failed to submit report, please check your internet connection.") );
}
}

QString InputUtils::renameWithDateTime( const QString &srcPath, const QDateTime &dateTime )
{
if ( QFile::exists( srcPath ) )
Expand Down
Loading

3 comments on commit c3dab89

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

win-apk: x86_64 (SDK: win-8)

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signed apk: armeabi-v7a (SDK: android-15)

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signed apk: arm64-v8a (SDK: android-15)

Please sign in to comment.