From 64db2b58519ba08bd295ffa75bd1481b0acc1362 Mon Sep 17 00:00:00 2001 From: t-horikawa Date: Wed, 17 Apr 2024 15:41:18 +0900 Subject: [PATCH] fix tgctl session list command --- src/tateyama/session/session.cpp | 72 +++++++++++++------------- test/tateyama/session/session_test.cpp | 31 +++++------ 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/src/tateyama/session/session.cpp b/src/tateyama/session/session.cpp index 5f26870..a123411 100644 --- a/src/tateyama/session/session.cpp +++ b/src/tateyama/session/session.cpp @@ -15,7 +15,6 @@ */ #include -#include #include @@ -30,24 +29,20 @@ DECLARE_string(monitor); DECLARE_bool(force); +DEFINE_bool(verbose, false, "show session list in verbose format"); // NOLINT namespace tateyama::session { static std::string to_timepoint_string(std::uint64_t msu) { - auto ms = static_cast(msu); - std::timespec ts{ms / 1000, (ms % 1000) * 1000000}; - - auto* when = std::localtime(&ts.tv_sec); - constexpr int array_size = 32; - std::array buf{}; - if (std::strftime(buf.data(), array_size - 1, "%Y-%m-%d %H:%M:%S", when) == 0) { - return {}; - } - std::array output{}; - const int msec = static_cast(ts.tv_nsec) / 1000000; - auto len = snprintf(output.data(), array_size, "%s.%03d %s", buf.data(), msec, when->tm_zone); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - std::string rv{output.data(), static_cast(len)}; - return rv; + std::chrono::time_point e0{}; + std::chrono::time_point t = e0 + std::chrono::milliseconds(static_cast(msu)); + + std::stringstream stream; + time_t epoch_seconds = std::chrono::system_clock::to_time_t(t); + struct tm buf{}; + gmtime_r(&epoch_seconds, &buf); + stream << std::put_time(&buf, "%FT%TZ"); + return stream.str(); } tgctl::return_code session_list() { //NOLINT(readability-function-cognitive-complexity) @@ -83,7 +78,7 @@ tgctl::return_code session_list() { //NOLINT(readability-function-cognitive-comp } if (rtnv == tgctl::return_code::ok) { - auto session_list = response.success().entries(); + auto& session_list = response.success().entries(); std::size_t id_max{2}; std::size_t label_max{5}; @@ -125,33 +120,40 @@ tgctl::return_code session_list() { //NOLINT(readability-function-cognitive-comp type_max += 2; remote_max += 2; - std::cout << std::left; - std::cout << std::setw(static_cast(id_max)) << "id"; - std::cout << std::setw(static_cast(label_max)) << "label"; - std::cout << std::setw(static_cast(application_max)) << "application"; - std::cout << std::setw(static_cast(user_max)) << "user"; - std::cout << std::setw(static_cast(start_max)) << "start"; - std::cout << std::setw(static_cast(type_max)) << "type"; - std::cout << std::setw(static_cast(remote_max)) << "remote"; - std::cout << std::endl; - + if (FLAGS_verbose) { + std::cout << std::left; + std::cout << std::setw(static_cast(id_max)) << "id"; + std::cout << std::setw(static_cast(label_max)) << "label"; + std::cout << std::setw(static_cast(application_max)) << "application"; + std::cout << std::setw(static_cast(user_max)) << "user"; + std::cout << std::setw(static_cast(start_max)) << "start"; + std::cout << std::setw(static_cast(type_max)) << "type"; + std::cout << std::setw(static_cast(remote_max)) << "remote"; + std::cout << std::endl; + } for( auto& e : session_list ) { auto e_session_id = e.session_id(); if (e_session_id.substr(1) != std::to_string(session_id)) { - std::cout << std::setw(static_cast(id_max)) << e_session_id; - std::cout << std::setw(static_cast(label_max)) << e.label(); - std::cout << std::setw(static_cast(application_max)) << e.application(); - std::cout << std::setw(static_cast(user_max)) << e.user(); - std::cout << std::setw(static_cast(start_max)) << to_timepoint_string(e.start_at()); - std::cout << std::setw(static_cast(type_max)) << e.connection_type(); - std::cout << std::setw(static_cast(remote_max)) << e.connection_info(); - std::cout << std::endl; + if (FLAGS_verbose) { + std::cout << std::setw(static_cast(id_max)) << e_session_id; + std::cout << std::setw(static_cast(label_max)) << e.label(); + std::cout << std::setw(static_cast(application_max)) << e.application(); + std::cout << std::setw(static_cast(user_max)) << e.user(); + std::cout << std::setw(static_cast(start_max)) << to_timepoint_string(e.start_at()); + std::cout << std::setw(static_cast(type_max)) << e.connection_type(); + std::cout << std::setw(static_cast(remote_max)) << e.connection_info(); + std::cout << std::endl; + } else { + std::cout << (e.label().empty() ? e_session_id : e.label()) << " "; + } if (monitor_output) { monitor_output->session_info(e_session_id, e.label(), e.application(), e.user(), to_timepoint_string(e.start_at()), e.connection_type(), e.connection_info()); } } } - + if (!FLAGS_verbose) { + std::cout << std::endl; + } if (monitor_output) { monitor_output->finish(true); } diff --git a/test/tateyama/session/session_test.cpp b/test/tateyama/session/session_test.cpp index a162503..6e85dce 100644 --- a/test/tateyama/session/session_test.cpp +++ b/test/tateyama/session/session_test.cpp @@ -104,18 +104,15 @@ class session_test : public ::testing::Test { } std::string to_timepoint_string(std::uint64_t msu) { - auto ms = static_cast(msu); - std::timespec ts{ms / 1000, (ms % 1000) * 1000000}; - - auto* when = std::localtime(&ts.tv_sec); - constexpr int array_size = 32; - std::array buf{}; - std::strftime(buf.data(), array_size - 1, "%Y-%m-%d %H:%M:%S", when); - std::array output{}; - const int msec = ts.tv_nsec / 1000000; - auto len = snprintf(output.data(), array_size, "%s.%03d %s", buf.data(), msec, when->tm_zone); - std::string rv{output.data(), static_cast(len)}; - return rv; + std::chrono::time_point e0{}; + std::chrono::time_point t = e0 + std::chrono::milliseconds(static_cast(msu)); + + std::stringstream stream; + time_t epoch_seconds = std::chrono::system_clock::to_time_t(t); + struct tm buf; + gmtime_r(&epoch_seconds, &buf); + stream << std::put_time(&buf, "%FT%TZ"); + return stream.str(); } }; @@ -133,12 +130,12 @@ TEST_F(session_test, session_list) { entry->set_application("session_test"); entry->set_user("test_user"); entry->set_start_at(now_stamp); - entry->set_connection_type(std::string("IPC")); + entry->set_connection_type(std::string("ipc")); entry->set_connection_info(std::to_string(getpid())); server_mock_->push_response(session_list.SerializeAsString()); } - command = "tgctl session list --conf "; + command = "tgctl session list --verbose --conf "; command += helper_->conf_file_path(); std::cout << command << std::endl; if((fp = popen(command.c_str(), "r")) == nullptr){ @@ -151,7 +148,7 @@ TEST_F(session_test, session_list) { EXPECT_NE(std::string::npos, result.find("session_test")); EXPECT_NE(std::string::npos, result.find("test_user")); EXPECT_NE(std::string::npos, result.find(to_timepoint_string(now_stamp))); - EXPECT_NE(std::string::npos, result.find("IPC")); + EXPECT_NE(std::string::npos, result.find("ipc")); EXPECT_NE(std::string::npos, result.find(std::to_string(getpid()))); EXPECT_EQ(tateyama::framework::service_id_session, server_mock_->component_id()); @@ -173,7 +170,7 @@ TEST_F(session_test, session_show) { entry->set_application("session_test"); entry->set_user("test_user"); entry->set_start_at(now_stamp); - entry->set_connection_type(std::string("IPC")); + entry->set_connection_type(std::string("ipc")); entry->set_connection_info(std::to_string(getpid())); server_mock_->push_response(session_get.SerializeAsString()); } @@ -191,7 +188,7 @@ TEST_F(session_test, session_show) { EXPECT_NE(std::string::npos, result.find("session_test")); EXPECT_NE(std::string::npos, result.find("test_user")); EXPECT_NE(std::string::npos, result.find(to_timepoint_string(now_stamp))); - EXPECT_NE(std::string::npos, result.find("IPC")); + EXPECT_NE(std::string::npos, result.find("ipc")); EXPECT_NE(std::string::npos, result.find(std::to_string(getpid()))); EXPECT_EQ(tateyama::framework::service_id_session, server_mock_->component_id());