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

Measure manager fixups and improvements #5304

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

jmarrec
Copy link
Collaborator

@jmarrec jmarrec commented Nov 14, 2024

Pull request overview

  • Prompted by OpenStudio-PAT about to adopt the C++ CLI, made a few last minute changes
  • Renabled testing and comparison of Ruby versus C++ CLI
    • Modified the key in /internal_state to match ruby
  • Added some kind of feedback (logs) for the server itself, so in the terminal that runs it you see some kind of output showing the connections it receives, like WEBrick would do
color()(set -o pipefail;"$@" 2> >(sed $'s,.*,\e[31m&\e[m,'>&2))
export -f color
color $os_build3/Products/openstudio measure -s 1065

Screenshot from 2024-11-14 13-46-03

Pull Request Author

  • Model API Changes / Additions
  • Any new or modified fields have been implemented in the EnergyPlus ForwardTranslator (and ReverseTranslator as appropriate)
  • Model API methods are tested (in src/model/test)
  • EnergyPlus ForwardTranslator Tests (in src/energyplus/Test)
  • If a new object or method, added a test in NREL/OpenStudio-resources: Add Link
  • If needed, added VersionTranslation rules for the objects (src/osversion/VersionTranslator.cpp)
  • Verified that C# bindings built fine on Windows, partial classes used as needed, etc.
  • All new and existing tests passes
  • If methods have been deprecated, update rest of code to use the new methods

Labels:

  • If change to an IDD file, add the label IDDChange
  • If breaking existing API, add the label APIChange
  • If deemed ready, add label Pull Request - Ready for CI so that CI builds your PR

Review Checklist

This will not be exhaustively relevant to every PR.

  • Perform a Code Review on GitHub
  • Code Style, strip trailing whitespace, etc.
  • All related changes have been implemented: model changes, model tests, FT changes, FT tests, VersionTranslation, OS App
  • Labeling is ok
  • If defect, verify by running develop branch and reproducing defect, then running PR and reproducing fix
  • If feature, test running new feature, try creative ways to break it
  • CI status: all green or justified

Comment on lines +38 to +50
execute_process(COMMAND ${Python_EXECUTABLE} -c "import requests; print(requests.__version__)"
RESULT_VARIABLE _PyRequests_STATUS
OUTPUT_VARIABLE PyRequests_Version
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(_PyRequests_STATUS AND NOT _PyRequests_STATUS EQUAL 0)
message(AUTHOR_WARNING "requests isn't installed on your system python, so some tests won't be run. Run `pip install requests`")
set(PyRequests_AVAILABLE OFF)
else()
message("Found Python requests: ${PyRequests_Version}")
set(PyRequests_AVAILABLE ON)
endif()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Detect if requests is installed on the system python

Comment on lines +362 to +376
if (PyRequests_AVAILABLE)
add_test(NAME OpenStudioCLI.test_measure_manager
COMMAND ${Python_EXECUTABLE} -m pytest --verbose --os-cli-path $<TARGET_FILE:openstudio> "${CMAKE_CURRENT_SOURCE_DIR}/test/test_measure_manager.py"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/Testing/"
)
add_test(NAME OpenStudioCLI.Classic.test_measure_manager
COMMAND ${Python_EXECUTABLE} -m pytest --verbose --use-classic --os-cli-path $<TARGET_FILE:openstudio> "${CMAKE_CURRENT_SOURCE_DIR}/test/test_measure_manager.py"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/Testing/"
)
endif()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Register the Classic (Ruby) and C++ version of the test_measure_manager.py

@@ -12,11 +12,16 @@ def validate_file(arg):

def pytest_addoption(parser):
parser.addoption("--os-cli-path", type=validate_file, help="Path to the OS CLI") # , required=True

parser.addoption("--use-classic", action="store_true", help="Force use the Classic 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.

need addopt

Comment on lines +23 to +27

@pytest.fixture(scope="module")
def use_classic_cli(request):
use_classic = request.config.getoption("--use-classic")
return use_classic
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Set it as a fixture for use

}

BASE_INTERNAL_STATE_LABS: Dict[str, Any] = {
"idfs": [],
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the only key that the C++ CLI added in /internal_state

for (const auto& [measureDirPath, bclMeasureInfo] : m_measures) {
Json::Value mInfo(Json::objectValue);
measures.append(bclMeasureInfo.measure.toJSON());
}

auto& measureInfos = result["measure_info"];
measureInfos = Json::arrayValue;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

same

@@ -609,7 +616,7 @@ void MeasureManagerServer::handle_get(web::http::http_request message) {
return;
}

message.reply(web::http::status_codes::BadRequest, toWebJSON(fmt::format("Error, unknown path '{}'", uri)));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In Get, was returning a 400 instead of a 404 when unknown endpoint

Comment on lines +599 to +603
void MeasureManagerServer::unknown_endpoint(web::http::http_request& message) {
const std::string uri = toString(web::http::uri::decode(message.relative_uri().path()));
message.reply(web::http::status_codes::BadRequest, toWebJSON(fmt::format("Error, unknown path '{}'", uri)));
print_feedback(message, web::http::status_codes::NotFound);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

use a function for both GET and POST when 404

Comment on lines +1002 to +1009
void MeasureManagerServer::print_feedback(const web::http::http_request& message, web::http::status_code status_code) {
const std::string uri = toString(web::http::uri::decode(message.relative_uri().path()));
const std::string method = toString(message.method());
const std::string http_version = message.http_version().to_utf8string();
const std::string timestamp = openstudio::DateTime::now().toXsdDateTime();
fmt::print(status_code == web::http::status_codes::OK ? stdout : stderr, "[{}] \"{} {} {}\" {}\n", openstudio::DateTime::now().toXsdDateTime(),
method, uri, http_version, status_code);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Print log

@@ -119,7 +119,7 @@ def do_POST (request, response)

result = {}

data = JSON.parse(request.body, {:symbolize_names=>true})
data = JSON.parse(request.body || "{}", {:symbolize_names=>true})
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Minor tweaks in ruby CLI. When body is empty, you don't want to get bad error on JSON.parse with an ugly backtrace and No implicit conversion of nil into String isn't a good error message

@jmarrec jmarrec added component - CLI component - Measure Manager Pull Request - Ready for CI This pull request if finalized and is ready for continuous integration verification prior to merge. labels Nov 14, 2024
@jmarrec jmarrec self-assigned this Nov 14, 2024
@DavidGoldwasser DavidGoldwasser added this to the OpenStudio SDK 3.10.0 milestone Nov 14, 2024
@jmarrec jmarrec requested a review from kbenne December 19, 2024 15:06
@jmarrec jmarrec force-pushed the measure_manager_fixups_and_improvements branch from c4fb845 to 417a930 Compare December 19, 2024 15:06
…running, kinda like Webbrick was doing

success, on stdout:
[2024-11-14T10:21:46+01:00] "POST /reset HTTP/1.1" 200

failure, on stderr:
[2024-11-14T10:22:09+01:00] "GET /dsd HTTP/1.1" 400
…e on JSON.parse

No implicit conversion of nil into String isn't a good error message
@jmarrec jmarrec force-pushed the measure_manager_fixups_and_improvements branch from 417a930 to 27df95b Compare January 7, 2025 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component - CLI component - Measure Manager Pull Request - Ready for CI This pull request if finalized and is ready for continuous integration verification prior to merge.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants