Skip to content

Commit

Permalink
Add time print for lgraph_cli (TuGraph-family#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljcui authored Nov 27, 2024
1 parent d214e4e commit d9a3e35
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/bolt/to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ nlohmann::json ToJsonObj(const bolt::Path& path) {
ret.append(forward ? "-" : "<-");
ret.append(ToJsonObj(rel).get<std::string>());
ret.append(forward ? "->" : "-");
ret.append(ToJsonObj(path.nodes[nodes_index.at(rel.endId)]).get<std::string>());
nodeId = rel.endId;
ret.append(ToJsonObj(path.nodes[nodes_index.at(
forward ? rel.endId : rel.startId)]).get<std::string>());
nodeId = forward ? rel.endId : rel.startId;
}
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_lgraph_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ p
WriteFile(file, statements);
std::string lgraph_cli =
"./lgraph_cli --ip 127.0.0.1 --port 7687 --graph default "
"--user admin --password 73@TuGraph";
"--user admin --password 73@TuGraph --print_time false";
lgraph::SubProcess cli(FMA_FMT("{} < {}", lgraph_cli, file));
cli.Wait();
UT_EXPECT_EQ(cli.Stdout(), expected);
Expand Down
25 changes: 21 additions & 4 deletions toolkits/lgraph_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ std::any ReadMessage(asio::ip::tcp::socket& socket, bolt::Hydrator& hydrator) {
return ret.first;
}

bool FetchRecords(asio::ip::tcp::socket& socket, bolt::Hydrator& hydrator, OutputFormat of) {
bool FetchRecords(asio::ip::tcp::socket& socket, bolt::Hydrator& hydrator,
OutputFormat of, bool print_time) {
auto start = std::chrono::high_resolution_clock::now();
std::string error;
std::optional<std::vector<std::string>> header;
tabulate::Table table;
Expand Down Expand Up @@ -122,12 +124,24 @@ bool FetchRecords(asio::ip::tcp::socket& socket, bolt::Hydrator& hydrator, Outpu
}

if (error.empty()) {
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
if (of == OutputFormat::TABLE) {
if (table.size() > 0) {
LOG_INFO() << table << "\n";
LOG_INFO() << FMA_FMT("{} rows", table.size() - 1) << "\n";
if (print_time) {
LOG_INFO() << FMA_FMT("{} rows ({} ms)", table.size() - 1,
static_cast<int>(elapsed.count())) << "\n";
} else {
LOG_INFO() << FMA_FMT("{} rows", table.size() - 1) << "\n";
}
} else {
LOG_INFO() << FMA_FMT("{} rows", table.size()) << "\n";
if (print_time) {
LOG_INFO() << FMA_FMT("{} rows ({} ms)", table.size(),
static_cast<int>(elapsed.count())) << "\n";
} else {
LOG_INFO() << FMA_FMT("{} rows", table.size()) << "\n";
}
}
}
return true;
Expand Down Expand Up @@ -184,6 +198,7 @@ int main(int argc, char** argv) {
std::string graph = "default";
std::string username = "admin";
std::string password = "73@TuGraph";
bool print_time = true;
config.Add(format, "format", true).
Comment("output format (table, csv, json)").
SetPossibleValues({"table", "csv", "json"});
Expand All @@ -192,6 +207,8 @@ int main(int argc, char** argv) {
config.Add(graph, "graph", true).Comment("Graph to use");
config.Add(username, "user", true).Comment("User to login");
config.Add(password, "password", true).Comment("Password to use when connecting to server");
config.Add(print_time, "print_time", true).Comment(
"Whether to print the execution time of the query");
try {
config.ExitAfterHelp(true);
config.ParseAndFinalize(argc, argv);
Expand Down Expand Up @@ -282,7 +299,7 @@ int main(int argc, char** argv) {
ps.AppendPullN(-1);
asio::write(socket,
asio::const_buffer(ps.ConstBuffer().data(), ps.ConstBuffer().size()));
bool ret = FetchRecords(socket, hydrator, of);
bool ret = FetchRecords(socket, hydrator, of, print_time);
if (!ret) {
// reset connection
ps.Reset();
Expand Down

0 comments on commit d9a3e35

Please sign in to comment.