Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to enable Verbose output to run tab + styled fallback when no socket #513

Merged
merged 13 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/openstudio_lib/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ void MainWindow::readSettings() {
restoreGeometry(settings.value("geometry").toByteArray());
restoreState(settings.value("state").toByteArray());
m_displayIP = settings.value("displayIP").toBool();
m_verboseOutput = settings.value("verboseOutput").toBool();
}

void MainWindow::writeSettings() {
Expand All @@ -272,6 +273,7 @@ void MainWindow::writeSettings() {
settings.setValue("geometry", saveGeometry());
settings.setValue("state", saveState());
settings.setValue("displayIP", m_displayIP);
settings.setValue("verboseOutput", m_verboseOutput);
}

QString MainWindow::lastPath() const {
Expand All @@ -282,6 +284,14 @@ void MainWindow::toggleUnits(bool displayIP) {
m_displayIP = displayIP;
}

bool MainWindow::verboseOutput() const {
return m_verboseOutput;
}

void MainWindow::toggleVerboseOutput(bool verboseOutput) {
m_verboseOutput = verboseOutput;
}

void MainWindow::configureProxyClicked() {
QString organizationName = QCoreApplication::organizationName();
QString applicationName = QCoreApplication::applicationName();
Expand Down
8 changes: 8 additions & 0 deletions src/openstudio_lib/MainWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class MainWindow : public QMainWindow

bool displayIP();

bool verboseOutput() const;

void enableRevertToSavedAction(bool enable);

void enableFileImportActions(bool enable);
Expand Down Expand Up @@ -165,6 +167,10 @@ class MainWindow : public QMainWindow

void enableComponentsMeasures(bool enable);

public slots:

void toggleVerboseOutput(bool verboseOutput);

protected:
void closeEvent(QCloseEvent* event) override;

Expand All @@ -191,6 +197,8 @@ class MainWindow : public QMainWindow

bool m_displayIP;

bool m_verboseOutput = false;

QString m_lastPath;

private slots:
Expand Down
253 changes: 230 additions & 23 deletions src/openstudio_lib/RunTabView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

#include "../model_editor/Application.hpp"
#include "../model_editor/Utilities.hpp"
#include "MainWindow.hpp"

#include <QButtonGroup>
#include <QDir>
Expand All @@ -92,6 +93,7 @@
#include <QDesktopServices>
#include <QTcpServer>
#include <QTcpSocket>
#include <QCheckBox>

namespace openstudio {

Expand Down Expand Up @@ -131,19 +133,30 @@ RunView::RunView() : QWidget(), m_runSocket(nullptr) {
progressbarlayout->addWidget(m_progressBar);
mainLayout->addLayout(progressbarlayout, 0, 1);

auto* mainWindow = OSAppBase::instance()->currentDocument()->mainWindow();
bool verboseOutput = mainWindow->verboseOutput();
m_verboseOutputBox = new QCheckBox();
m_verboseOutputBox->setText("Verbose Output");
m_verboseOutputBox->setChecked(verboseOutput);
connect(m_verboseOutputBox, &QCheckBox::clicked, mainWindow, &MainWindow::toggleVerboseOutput);
mainLayout->addWidget(m_verboseOutputBox, 0, 2);

m_openSimDirButton = new QPushButton();
m_openSimDirButton->setText("Show Simulation");
m_openSimDirButton->setFlat(true);
m_openSimDirButton->setObjectName("StandardGrayButton");
connect(m_openSimDirButton, &QPushButton::clicked, this, &RunView::onOpenSimDirClicked);
mainLayout->addWidget(m_openSimDirButton, 0, 2);
mainLayout->addWidget(m_openSimDirButton, 0, 3);

m_textInfo = new QTextEdit();
m_textInfo->setReadOnly(true);
mainLayout->addWidget(m_textInfo, 1, 0, 1, 3);
mainLayout->addWidget(m_textInfo, 1, 0, 1, 4);

m_runProcess = new QProcess(this);
connect(m_runProcess, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &RunView::onRunProcessFinished);
connect(m_runProcess, &QProcess::finished, this, &RunView::onRunProcessFinished);
connect(m_runProcess, &QProcess::errorOccurred, this, &RunView::onRunProcessErrored);
connect(m_runProcess, &QProcess::readyReadStandardError, this, &RunView::readyReadStandardError);
connect(m_runProcess, &QProcess::readyReadStandardOutput, this, &RunView::readyReadStandardOutput);

QProcessEnvironment env = QProcessEnvironment::systemEnvironment();

Expand Down Expand Up @@ -178,8 +191,37 @@ void RunView::onOpenSimDirClicked() {
}
}

void RunView::onRunProcessErrored(QProcess::ProcessError error) {
m_textInfo->setTextColor(Qt::red);
m_textInfo->setFontPointSize(18);
QString text = tr("onRunProcessErrored: Simulation failed to run, QProcess::ProcessError: ") + QString::number(error);
m_textInfo->append(text);
}

void RunView::onRunProcessFinished(int exitCode, QProcess::ExitStatus status) {
LOG(Debug, "run finished");
if (status == QProcess::NormalExit) {
LOG(Debug, "run finished, exit code = " << exitCode);
}

if (exitCode != 0 || status == QProcess::CrashExit) {
m_textInfo->setTextColor(Qt::red);
m_textInfo->setFontPointSize(18);
m_textInfo->append(tr("Simulation failed to run, with exit code ") + QString::number(exitCode));

//m_textInfo->setTextColor(Qt::black);
//m_textInfo->setFontPointSize(15);
//m_textInfo->append("Stderr:");
//m_textInfo->setFontPointSize(12);
//QString errorString = QString(m_runProcess->readAllStandardError());
//m_textInfo->append(errorString);

//m_textInfo->setFontPointSize(15);
//m_textInfo->append("Stdout:");
//m_textInfo->setFontPointSize(12);
//QString outString = QString(m_runProcess->readAllStandardOutput());
//m_textInfo->append(outString);
}

m_playButton->setChecked(false);
m_state = State::stopped;
m_progressBar->setMaximum(State::complete);
Expand Down Expand Up @@ -231,19 +273,33 @@ void RunView::playButtonClicked(bool t_checked) {
auto workflowJSONPath = QString::fromStdString(workflowPath.string());

unsigned port = m_runTcpServer->serverPort();
bool haveConnection = (port != 0);
m_hasSocketConnexion = (port != 0);
// NOTE: temp test, uncomment to see fallback to stdout
// m_hasSocketConnexion = false;

QStringList arguments;
if (haveConnection) {
LOG(Debug, "Checkbox is checked? " << std::boolalpha << m_verboseOutputBox->isChecked());
if (m_verboseOutputBox->isChecked()) {
arguments << "--verbose";
} else {
// If not verbose, we save the stdout/stderr to a file, like historical
// Actually, we don't, we just read it
// m_runProcess->setStandardOutputFile(toQString(stdoutPath));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will E+ stdout and stderr still be in files in the sim dir for the user to read later? I see stdout-energyplus and stderr-energyplus files but I'm not entirely sure which process was writing those, the app or the cli.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

stdout-energyplus stderr-energyplus are written by workflow gem.

What I am no longer outputing is the model_companion_dir/stdout and stderr files. I see no value in keeping those, when we are seeing them live. I don't see any use case for seeing them afterwards, but perhaps there is one. In which case the RunView::readyReadStandardOutput and RunView::readyReadStandardError should be writting to these files in append mode in addition of displaying them live. thoughts @macumber ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok yeah, I don't think those two files are useful. Thanks Julian!

Copy link
Collaborator Author

@jmarrec jmarrec May 3, 2022

Choose a reason for hiding this comment

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

cf dd97abe

// m_runProcess->setStandardErrorFile(toQString(stderrPath));
}

if (m_hasSocketConnexion) {
arguments << "run"
<< "-s" << QString::number(port) << "-w" << workflowJSONPath;
} else {
arguments << "run"
<< "--show-stdout"
// << "--style-stdout"
// << "--add-timings"
<< "-w" << workflowJSONPath;
}

LOG(Debug, "openstudioExePath='" << toString(openstudioExePath) << "'");
LOG(Debug, "run arguments" << arguments.join(";").toStdString());
LOG(Debug, "run arguments = " << arguments.join(";").toStdString());

osdocument->disableTabsDuringRun();
m_openSimDirButton->setEnabled(false);
Expand All @@ -257,30 +313,28 @@ void RunView::playButtonClicked(bool t_checked) {
m_state = State::stopped;
m_textInfo->clear();

if (haveConnection) {
m_progressBar->setMinimum(0);
m_progressBar->setMaximum(State::complete);
m_progressBar->setValue(0);
} else {
m_progressBar->setMinimum(0);
m_progressBar->setMaximum(0);
macumber marked this conversation as resolved.
Show resolved Hide resolved
m_progressBar->setValue(0);
m_progressBar->setMinimum(0);
m_progressBar->setMaximum(State::complete);
m_progressBar->setValue(0);

m_textInfo->setTextColor(Qt::black);
m_textInfo->setFontPointSize(18);
m_textInfo->append("Could not open socket connection to OpenStudio CLI.");
if (!m_hasSocketConnexion) {
m_textInfo->setTextColor(Qt::red);
m_textInfo->setFontPointSize(15);
m_textInfo->append("Live simulation feedback during run not available.");
m_textInfo->append("View simulation directory when run is complete.");
m_textInfo->append("Could not open socket connection to OpenStudio CLI.");
m_textInfo->setFontPointSize(12);
m_textInfo->append("Falling back to stdout/stderr parsing, live updates might be slower.");
}

m_runProcess->setStandardOutputFile(toQString(stdoutPath));
m_runProcess->setStandardErrorFile(toQString(stderrPath));
m_runProcess->start(openstudioExePath, arguments);
} else {
// stop running
LOG(Debug, "Kill Simulation");
m_textInfo->setTextColor(Qt::red);
m_textInfo->setFontPointSize(18);
m_textInfo->append("Aborted");
m_runProcess->blockSignals(true);
m_runProcess->kill();
m_runProcess->blockSignals(false);
}
}

Expand Down Expand Up @@ -389,4 +443,157 @@ void RunView::onRunDataReady() {
}
}

void RunView::readyReadStandardOutput() {

auto appendErrorText = [&](const QString& text) {
m_textInfo->setTextColor(Qt::red);
m_textInfo->setFontPointSize(18);
m_textInfo->append(text);
};

auto appendNormalText = [&](const QString& text) {
m_textInfo->setTextColor(Qt::black);
m_textInfo->setFontPointSize(12);
m_textInfo->append(text);
};

auto appendH1Text = [&](const QString& text) {
m_textInfo->setTextColor(Qt::black);
m_textInfo->setFontPointSize(18);
m_textInfo->append(text);
};

auto appendH2Text = [&](const QString& text) {
m_textInfo->setTextColor(Qt::black);
m_textInfo->setFontPointSize(15);
m_textInfo->append(text);
};

QString data = m_runProcess->readAllStandardOutput();
QStringList lines = data.split("\n");

for (const auto& line : lines) {
//std::cout << data.toStdString() << std::endl;

QString trimmedLine = line.trimmed();

bool b = trimmedLine.contains("DEBUG");

// DLM: coordinate with openstudio-workflow-gem\lib\openstudio\workflow\adapters\output\socket.rb
if (trimmedLine.isEmpty()) {
continue;
} else if ((trimmedLine.contains("DEBUG")) || (trimmedLine.contains("] <-2>"))) {
m_textInfo->setFontPointSize(10);
m_textInfo->setTextColor(Qt::lightGray);
m_textInfo->append(line);
} else if ((trimmedLine.contains("INFO")) || (trimmedLine.contains("] <-1>"))) {
m_textInfo->setFontPointSize(10);
m_textInfo->setTextColor(Qt::gray);
m_textInfo->append(line);
} else if ((trimmedLine.contains("WARN")) || (trimmedLine.contains("] <0>"))) {
m_textInfo->setFontPointSize(12);
m_textInfo->setTextColor(Qt::darkYellow);
m_textInfo->append(line);
} else if ((trimmedLine.contains("ERROR")) || (trimmedLine.contains("] <1>"))) {
m_textInfo->setFontPointSize(12);
m_textInfo->setTextColor(Qt::darkRed);
m_textInfo->append(line);
} else if ((trimmedLine.contains("FATAL")) || (trimmedLine.contains("] <1>"))) {
m_textInfo->setFontPointSize(14);
m_textInfo->setTextColor(Qt::red);
m_textInfo->append(line);

} else if (!m_hasSocketConnexion) {
// For socket fall back. Avoid doing all these compare if we know we don't need to
if (QString::compare(trimmedLine, "Starting state initialization", Qt::CaseInsensitive) == 0) {
appendH1Text("Initializing workflow.");
m_state = State::initialization;
m_progressBar->setValue(m_state);
} else if (QString::compare(trimmedLine, "Started", Qt::CaseInsensitive) == 0) {
// no-op
} else if (QString::compare(trimmedLine, "Returned from state initialization", Qt::CaseInsensitive) == 0) {
// no-op
} else if (QString::compare(trimmedLine, "Starting state os_measures", Qt::CaseInsensitive) == 0) {
appendH1Text("Processing OpenStudio Measures.");
m_state = State::os_measures;
m_progressBar->setValue(m_state);
} else if (QString::compare(trimmedLine, "Returned from state os_measures", Qt::CaseInsensitive) == 0) {
// no-op
} else if (QString::compare(trimmedLine, "Starting state translator", Qt::CaseInsensitive) == 0) {
appendH1Text("Translating the OpenStudio Model to EnergyPlus.");
m_state = State::translator;
m_progressBar->setValue(m_state);
} else if (QString::compare(trimmedLine, "Returned from state translator", Qt::CaseInsensitive) == 0) {
// no-op
} else if (QString::compare(trimmedLine, "Starting state ep_measures", Qt::CaseInsensitive) == 0) {
appendH1Text("Processing EnergyPlus Measures.");
m_state = State::ep_measures;
m_progressBar->setValue(m_state);
} else if (QString::compare(trimmedLine, "Returned from state ep_measures", Qt::CaseInsensitive) == 0) {
// no-op
} else if (QString::compare(trimmedLine, "Starting state preprocess", Qt::CaseInsensitive) == 0) {
// ignore this state
m_state = State::preprocess;
m_progressBar->setValue(m_state);
} else if (QString::compare(trimmedLine, "Returned from state preprocess", Qt::CaseInsensitive) == 0) {
// ignore this state
} else if (QString::compare(trimmedLine, "Starting state simulation", Qt::CaseInsensitive) == 0) {
appendH1Text("Starting Simulation.");
m_state = State::simulation;
m_progressBar->setValue(m_state);
} else if (QString::compare(trimmedLine, "Returned from state simulation", Qt::CaseInsensitive) == 0) {
// no-op
} else if (QString::compare(trimmedLine, "Starting state reporting_measures", Qt::CaseInsensitive) == 0) {
appendH1Text("Processing Reporting Measures.");
m_state = State::reporting_measures;
m_progressBar->setValue(m_state);
} else if (QString::compare(trimmedLine, "Returned from state reporting_measures", Qt::CaseInsensitive) == 0) {
// no-op
} else if (QString::compare(trimmedLine, "Starting state postprocess", Qt::CaseInsensitive) == 0) {
appendH1Text("Gathering Reports.");
m_state = State::postprocess;
m_progressBar->setValue(m_state);
} else if (QString::compare(trimmedLine, "Returned from state postprocess", Qt::CaseInsensitive) == 0) {
// no-op
} else if (QString::compare(trimmedLine, "Failure", Qt::CaseInsensitive) == 0) {
appendErrorText("Failed.");
} else if (QString::compare(trimmedLine, "Complete", Qt::CaseInsensitive) == 0) {
appendH1Text("Completed.");
} else if (trimmedLine.startsWith("Applying", Qt::CaseInsensitive)) {
appendH2Text(line);
} else if (trimmedLine.startsWith("Applied", Qt::CaseInsensitive)) {
// no-op
} else {
appendNormalText(line);
}
} else { // m_hasSocketConnexion: we know it's stdout and not important socket info, so we put that in gray
m_textInfo->setFontPointSize(10);
m_textInfo->setTextColor(Qt::gray);
m_textInfo->append(line);
}
}
}

void RunView::readyReadStandardError() {
auto appendErrorText = [&](const QString& text) {
m_textInfo->setTextColor(Qt::darkRed);
m_textInfo->setFontPointSize(18);
m_textInfo->append(text);
};

QString data = m_runProcess->readAllStandardError();
QStringList lines = data.split("\n");

for (const auto& line : lines) {

QString trimmedLine = line.trimmed();

if (trimmedLine.isEmpty()) {
continue;
} else {
appendErrorText("stderr: " + line);
}
}
}

} // namespace openstudio
Loading