Skip to content

Commit

Permalink
fix tgctl session list command
Browse files Browse the repository at this point in the history
  • Loading branch information
t-horikawa committed May 15, 2024
1 parent 4fe0e11 commit 64db2b5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 52 deletions.
72 changes: 37 additions & 35 deletions src/tateyama/session/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

#include <ctime>
#include <array>

#include <gflags/gflags.h>

Expand All @@ -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<std::int64_t>(msu);
std::timespec ts{ms / 1000, (ms % 1000) * 1000000};

auto* when = std::localtime(&ts.tv_sec);
constexpr int array_size = 32;
std::array<char, array_size> buf{};
if (std::strftime(buf.data(), array_size - 1, "%Y-%m-%d %H:%M:%S", when) == 0) {
return {};
}
std::array<char, array_size> output{};
const int msec = static_cast<std::int32_t>(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<std::size_t>(len)};
return rv;
std::chrono::time_point<std::chrono::system_clock> e0{};
std::chrono::time_point<std::chrono::system_clock> t = e0 + std::chrono::milliseconds(static_cast<std::int64_t>(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)
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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<int>(id_max)) << "id";
std::cout << std::setw(static_cast<int>(label_max)) << "label";
std::cout << std::setw(static_cast<int>(application_max)) << "application";
std::cout << std::setw(static_cast<int>(user_max)) << "user";
std::cout << std::setw(static_cast<int>(start_max)) << "start";
std::cout << std::setw(static_cast<int>(type_max)) << "type";
std::cout << std::setw(static_cast<int>(remote_max)) << "remote";
std::cout << std::endl;

if (FLAGS_verbose) {
std::cout << std::left;
std::cout << std::setw(static_cast<int>(id_max)) << "id";
std::cout << std::setw(static_cast<int>(label_max)) << "label";
std::cout << std::setw(static_cast<int>(application_max)) << "application";
std::cout << std::setw(static_cast<int>(user_max)) << "user";
std::cout << std::setw(static_cast<int>(start_max)) << "start";
std::cout << std::setw(static_cast<int>(type_max)) << "type";
std::cout << std::setw(static_cast<int>(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<int>(id_max)) << e_session_id;
std::cout << std::setw(static_cast<int>(label_max)) << e.label();
std::cout << std::setw(static_cast<int>(application_max)) << e.application();
std::cout << std::setw(static_cast<int>(user_max)) << e.user();
std::cout << std::setw(static_cast<int>(start_max)) << to_timepoint_string(e.start_at());
std::cout << std::setw(static_cast<int>(type_max)) << e.connection_type();
std::cout << std::setw(static_cast<int>(remote_max)) << e.connection_info();
std::cout << std::endl;
if (FLAGS_verbose) {
std::cout << std::setw(static_cast<int>(id_max)) << e_session_id;
std::cout << std::setw(static_cast<int>(label_max)) << e.label();
std::cout << std::setw(static_cast<int>(application_max)) << e.application();
std::cout << std::setw(static_cast<int>(user_max)) << e.user();
std::cout << std::setw(static_cast<int>(start_max)) << to_timepoint_string(e.start_at());
std::cout << std::setw(static_cast<int>(type_max)) << e.connection_type();
std::cout << std::setw(static_cast<int>(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);
}
Expand Down
31 changes: 14 additions & 17 deletions test/tateyama/session/session_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,15 @@ class session_test : public ::testing::Test {
}

std::string to_timepoint_string(std::uint64_t msu) {
auto ms = static_cast<std::int64_t>(msu);
std::timespec ts{ms / 1000, (ms % 1000) * 1000000};

auto* when = std::localtime(&ts.tv_sec);
constexpr int array_size = 32;
std::array<char, array_size> buf{};
std::strftime(buf.data(), array_size - 1, "%Y-%m-%d %H:%M:%S", when);
std::array<char, array_size> 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<std::size_t>(len)};
return rv;
std::chrono::time_point<std::chrono::system_clock> e0{};
std::chrono::time_point<std::chrono::system_clock> t = e0 + std::chrono::milliseconds(static_cast<std::int64_t>(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();
}
};

Expand All @@ -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){
Expand All @@ -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());
Expand All @@ -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());
}
Expand All @@ -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());
Expand Down

0 comments on commit 64db2b5

Please sign in to comment.