From 7e8f04ff2047cee0f98ee306ee5d2d65d8917a44 Mon Sep 17 00:00:00 2001 From: sakshikukreja14 Date: Fri, 25 Dec 2020 17:25:59 +0530 Subject: [PATCH] [ref] Preparation of implementing license for peakML --- src/gui/mzroll/peakdetectiondialog.cpp | 74 +++++------------- src/gui/mzroll/peakdetectiondialog.h | 8 +- src/gui/mzroll/pollyelmaveninterface.cpp | 95 ++++++++++++++++++++++++ src/gui/mzroll/pollyelmaveninterface.h | 27 ++++++- 4 files changed, 141 insertions(+), 63 deletions(-) diff --git a/src/gui/mzroll/peakdetectiondialog.cpp b/src/gui/mzroll/peakdetectiondialog.cpp index 9b096dc1a6..0fed410eaa 100644 --- a/src/gui/mzroll/peakdetectiondialog.cpp +++ b/src/gui/mzroll/peakdetectiondialog.cpp @@ -262,27 +262,39 @@ void PeakDetectionDialog::getLoginForPeakMl() } } +void PeakDetectionDialog::handleAuthorization(QStringList models, QString status) { + if (status != "OK") { + unsuccessfulLogin(); + // TODO: Message needs to change once license part is done. + auto htmlText = QString("

Something went wrong.

"); + htmlText += "

Please contact tech support at elmaven@elucidata.io if the problem persists.

"; + mainwindow->showWarning(htmlText); + } else { + modelTypes->clear(); + modelTypes->addItem("Global Model Elucidata"); + for (auto model : models) + modelTypes->addItem(model); + } +} + void PeakDetectionDialog::loginSuccessful() -{ +{ _peakMlSet = true; peakMl->setChecked(true); mainwindow->mavenParameters->classifyUsingPeakMl = true; modelTypes->setEnabled(true); - getModelsList(); - modelTypes->clear(); - modelTypes->addItem("Global Model Elucidata"); - for (auto model : _modelsList) - modelTypes->addItem(QString::fromStdString(model)); + mainwindow->pollyElmavenInterfaceDialog->getModelsForPeakML(); } void PeakDetectionDialog::unsuccessfulLogin() -{ +{ _peakMlSet = false; peakMl->setChecked(false); modelTypes->setEnabled(false); if(mainwindow) mainwindow->mavenParameters->classifyUsingPeakMl = false; } + void PeakDetectionDialog::onReset() { emit resetSettings(peakSettings->getSettings().keys()); @@ -486,8 +498,6 @@ void PeakDetectionDialog::inputInitialValuesPeakDetectionDialog() { matchFragmentationOptions->setChecked(fragmentationWasEnabled); modelTypes->clear(); modelTypes->addItem("Global Model Elucidata"); - for (auto model : _modelsList) - modelTypes->addItem(QString::fromStdString(model)); // selecting the compound database that is selected by the user in the // ligand widget QString selectedDB = mainwindow->ligandWidget->getDatabaseName(); @@ -515,52 +525,6 @@ void PeakDetectionDialog::inputInitialValuesPeakDetectionDialog() { } } -void PeakDetectionDialog::getModelsList() -{ - QStringList args; - _modelsList.clear(); - auto _dlManager = new DownloadManager(); - auto _pollyIntegration = new PollyIntegration(_dlManager); - - auto cookieFile = QStandardPaths::writableLocation( - QStandardPaths::GenericConfigLocation) - + QDir::separator() - + "El-MAVEN_cookie.json" ; - - ifstream readCookie(cookieFile.toStdString()); - if(!readCookie.is_open()) - return; - - json cookieInput = json::parse(readCookie); - string refreshToken = cookieInput["refreshToken"]; - string idToken = cookieInput["idToken"]; - - QString cookies; - cookies += "refreshToken="; - cookies += QString::fromStdString(refreshToken); - cookies += ";"; - cookies += "idToken="; - cookies += QString::fromStdString(idToken); - - args << cookies; - - auto res = _pollyIntegration->runQtProcess("listBucketObjects", args); - QString str(res[0].constData()); - if (str.isEmpty()) - return; - auto splitString = mzUtils::split(str.toStdString(), "\n"); - auto data = splitString[splitString.size() - 2]; - json dataObject = json::parse(data); - - if (!dataObject["data"]["error"].is_null()){ - return; - } - - for (int i = 1; i < dataObject["data"]["attributes"]["models"].size(); i++) { - _modelsList.push_back(dataObject["data"]["attributes"]["models"][i].get()); - } -} - void PeakDetectionDialog::refreshCompoundDatabases() { map::iterator itr; diff --git a/src/gui/mzroll/peakdetectiondialog.h b/src/gui/mzroll/peakdetectiondialog.h index b758a1646b..1fbb5f8c65 100644 --- a/src/gui/mzroll/peakdetectiondialog.h +++ b/src/gui/mzroll/peakdetectiondialog.h @@ -25,13 +25,6 @@ class PeakDetectionDialog : public QDialog, public Ui_PeakDetectionDialog void displayAppropriatePeakDetectionDialog(FeatureDetectionType type); //TODO: Sahil - Kiran, Added while merging mainwindow void setMavenParameters(QSettings *settings); bool databaseSearchEnabled(); - /** - * @brief Runs QtProcess to hit Polly API that returns the - * list of Models that is stored in S3 bucket for that - * particular organisation of the current user. - * @return vector of model names. - */ - void getModelsList(); public Q_SLOTS: void findPeaks(); @@ -71,6 +64,7 @@ class PeakDetectionDialog : public QDialog, public Ui_PeakDetectionDialog void setQuantType(QString type); void triggerSettingsUpdate(); void onReset(); + void handleAuthorization(QStringList models, QString status); Q_SIGNALS: void updateSettings(PeakDetectionSettings* pd); diff --git a/src/gui/mzroll/pollyelmaveninterface.cpp b/src/gui/mzroll/pollyelmaveninterface.cpp index 91a83672fd..e1230498b9 100644 --- a/src/gui/mzroll/pollyelmaveninterface.cpp +++ b/src/gui/mzroll/pollyelmaveninterface.cpp @@ -13,6 +13,10 @@ #include "pollywaitdialog.h" #include "projectdockwidget.h" #include "tabledockwidget.h" +#include "peakdetectiondialog.h" +#include "json.hpp" + +using json = nlohmann::json; PollyElmavenInterfaceDialog::PollyElmavenInterfaceDialog(MainWindow* mw) : QDialog(mw), _mainwindow(mw), _loginform(nullptr) @@ -30,6 +34,7 @@ PollyElmavenInterfaceDialog::PollyElmavenInterfaceDialog(MainWindow* mw) _activeTable = nullptr; _pollyIntegration = _mainwindow->getController()->iPolly; _loadingDialog = new PollyWaitDialog(this); + _loadingDialogForPeakML = new PollyWaitDialog(_mainwindow->peakDetectionDialog); _uploadInProgress = false; _lastCohortFileWasValid = false; @@ -191,10 +196,19 @@ PollyElmavenInterfaceDialog::PollyElmavenInterfaceDialog(MainWindow* mw) SIGNAL(filesUploaded(QStringList, QString, QString)), this, SLOT(_performPostFilesUploadTasks(QStringList, QString, QString))); + connect(_worker, + &EPIWorkerThread::peakMLAuthenticationFinished, + this, + &PollyElmavenInterfaceDialog::peakMLAccessControl); connect(sendModeTab, &QTabWidget::currentChanged, this, &PollyElmavenInterfaceDialog::_changeMode); + + connect(this, + &PollyElmavenInterfaceDialog::peakMLAccess, + _mainwindow->peakDetectionDialog, + &PeakDetectionDialog::handleAuthorization); } PollyElmavenInterfaceDialog::~PollyElmavenInterfaceDialog() { @@ -217,6 +231,9 @@ void EPIWorkerThread::run() case RunMethod::SendEmail: _sendEmail(); break; + case RunMethod::FetchPeakMLModels: + _getModels(); + break; default: break; } @@ -430,6 +447,84 @@ void PollyElmavenInterfaceDialog::loginForPeakMl() } } +void PollyElmavenInterfaceDialog::getModelsForPeakML() +{ + _worker->wait(); + _worker->setMethodToRun(EPIWorkerThread::RunMethod::FetchPeakMLModels); + _worker->start(); + + _loadingDialogForPeakML->setWindowFlag(Qt::WindowTitleHint, true); + _loadingDialogForPeakML->open(); + _loadingDialogForPeakML->statusLabel->setVisible(true); + _loadingDialogForPeakML->statusLabel->setStyleSheet("QLabel {color : green;}"); + _loadingDialogForPeakML->statusLabel->setText("Fetching user data..."); + _loadingDialogForPeakML->label->setVisible(true); + _loadingDialogForPeakML->label->setMovie(_loadingDialogForPeakML->movie); + _loadingDialogForPeakML->label->setAlignment(Qt::AlignCenter); + QCoreApplication::processEvents(); +} + +void PollyElmavenInterfaceDialog::peakMLAccessControl(QStringList models, QString status) { + _loadingDialogForPeakML->close(); + emit peakMLAccess(models, status); +} + +void EPIWorkerThread::_getModels() { + QStringList args; + QStringList models; + + auto cookieFile = QStandardPaths::writableLocation( + QStandardPaths::GenericConfigLocation) + + QDir::separator() + + "El-MAVEN_cookie.json" ; + + ifstream readCookie(cookieFile.toStdString()); + if(!readCookie.is_open()) { + emit peakMLAuthenticationFinished(models, "Error"); + return; + } + + json cookieInput = json::parse(readCookie); + string refreshToken = cookieInput["refreshToken"]; + string idToken = cookieInput["idToken"]; + + QString cookies; + cookies += "refreshToken="; + cookies += QString::fromStdString(refreshToken); + cookies += ";"; + cookies += "idToken="; + cookies += QString::fromStdString(idToken); + + args << cookies; + + auto res = _pollyIntegration->runQtProcess("listBucketObjects", args); + if (!res.size()) { + emit peakMLAuthenticationFinished(models, "Error"); + return; + } + + QString str(res[0].constData()); + if (str.isEmpty()) { + emit peakMLAuthenticationFinished(models, "Error"); + return; + } + + auto splitString = mzUtils::split(str.toStdString(), "\n"); + auto data = splitString[splitString.size() - 2]; + json dataObject = json::parse(data); + + if (!dataObject["data"]["error"].is_null()){ + emit peakMLAuthenticationFinished(models, "Error"); + } + + for (int i = 1; i < dataObject["data"]["attributes"]["models"].size(); i++) { + QString modelName = QString::fromStdString(dataObject["data"]["attributes"]["models"][i].get()); + models.push_back(modelName); + } + + emit peakMLAuthenticationFinished(models, "OK"); +} + void PollyElmavenInterfaceDialog::initialSetup() { int nodeStatus = _pollyIntegration->checkNodeExecutable(); diff --git a/src/gui/mzroll/pollyelmaveninterface.h b/src/gui/mzroll/pollyelmaveninterface.h index c2d5b23444..63f2700558 100644 --- a/src/gui/mzroll/pollyelmaveninterface.h +++ b/src/gui/mzroll/pollyelmaveninterface.h @@ -26,7 +26,8 @@ class EPIWorkerThread : public QThread { AuthenticateAndFetchData, UploadFiles, - SendEmail + SendEmail, + FetchPeakMLModels }; EPIWorkerThread(PollyIntegration *pi) : _pollyIntegration(pi) {} @@ -67,6 +68,7 @@ class EPIWorkerThread : public QThread void licensesReady(QMap licenseMap); void projectsReady(QVariantMap projectNamesId); void authenticationFinished(QString username, QString status); + void peakMLAuthenticationFinished(QStringList models, QString status); private: @@ -101,6 +103,8 @@ class EPIWorkerThread : public QThread void _sendEmail(); void _removeFilesFromDir(QDir dir, QStringList files); + + void _getModels(); }; /** @@ -176,6 +180,14 @@ class PollyElmavenInterfaceDialog : public QDialog, */ void loginForPeakMl(); + /** + * @brief Runs QtProcess to hit Polly API that returns the + * list of Models that is stored in S3 bucket for that + * particular organisation of the current user. + * @return vector of model names. + */ + void getModelsForPeakML(); + public Q_SLOTS: /** @@ -196,6 +208,8 @@ public Q_SLOTS: void showEPIError(QString errorMessage); + void peakMLAccessControl(QStringList models, QString status); + Q_SIGNALS: /** @@ -204,6 +218,11 @@ public Q_SLOTS: */ void uploadFinished(bool success); + /** + * + */ + void peakMLAccess(QStringList models, QString status); + private: /** @@ -238,6 +257,12 @@ public Q_SLOTS: */ PollyWaitDialog* _loadingDialog; + /** + * @brief A pointer to the loading dialog that fetches user data for peakML + * and appear on the top of peak detection dialog. + */ + PollyWaitDialog* _loadingDialogForPeakML; + /** * @brief A pointer to loginform class. */