Web interface: support diplaying tables generated on transport's side

This commit is contained in:
Jan Kaluza 2016-03-06 20:13:52 +01:00
parent e386fe7258
commit fe69cbd7a1
6 changed files with 112 additions and 29 deletions

View file

@ -60,12 +60,13 @@ class AdminInterfaceCommand {
class Arg {
public:
Arg(const std::string &_name, const std::string &_label, const std::string &_example) :
name(_name), label(_label), example(_example) {}
Arg(const std::string &_name, const std::string &_label, const std::string &_type, const std::string &_example) :
name(_name), label(_label), type(_type), example(_example) {}
~Arg() {}
std::string name;
std::string label;
std::string type;
std::string example;
};
@ -103,8 +104,8 @@ class AdminInterfaceCommand {
return m_accessMode;
}
void addArg(const std::string &name, const std::string &label, const std::string &example = "") {
Arg arg(name, label, example);
void addArg(const std::string &name, const std::string &label, const std::string &type = "string", const std::string &example = "") {
Arg arg(name, label, type, example);
m_args.push_back(arg);
}

View file

@ -112,24 +112,25 @@ class OnlineUsersCommand : public AdminInterfaceCommand {
AdminInterfaceCommand::Users,
AdminInterfaceCommand::GlobalContext,
AdminInterfaceCommand::AdminMode,
AdminInterfaceCommand::Get) {
AdminInterfaceCommand::Execute,
"Online users") {
m_userManager = userManager;
setDescription("Returns list of all online users");
}
virtual std::string handleGetRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
std::string ret = AdminInterfaceCommand::handleGetRequest(uinfo, user, args);
virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
if (!ret.empty()) {
return ret;
}
const std::map<std::string, User *> &users = m_userManager->getUsers();
if (users.empty()) {
ret = "0";
ret = "hanzz@njs.netlab.cz \"3rd-party network username:\" \"me\"\n";
}
for (std::map<std::string, User *>::const_iterator it = users.begin(); it != users.end(); it ++) {
ret += (*it).first + "\n";
ret += (*it).first + " \"3rd-party network username:\" \"" + user->getUserInfo().uin + "\"\n";
}
return ret;
}
@ -282,7 +283,7 @@ class HasOnlineUserCommand : public AdminInterfaceCommand {
AdminInterfaceCommand::Execute, "Has online user") {
m_userManager = userManager;
setDescription("Returns 1 if user is online");
addArg("username", "Username", "user@domain.tld");
addArg("username", "Username", "string", "user@domain.tld");
}
virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
@ -717,7 +718,7 @@ class RegisterCommand : public AdminInterfaceCommand {
addArg("legacy_username", args[1]);
}
if (fields.size() > 2) {
addArg("legacy_password", args[2]);
addArg("legacy_password", args[2], "password");
}
}
@ -844,7 +845,7 @@ class GetOAuth2URLCommand : public AdminInterfaceCommand {
addArg("legacy_username", args[1]);
}
if (fields.size() > 2) {
addArg("legacy_password", args[2]);
addArg("legacy_password", args[2], "password");
}
}
@ -1051,7 +1052,7 @@ class ArgsCommand : public AdminInterfaceCommand {
AdminInterfaceCommand::Execute, "Command's arguments") {
m_commands = commands;
setDescription("Shows descripton of arguments for command");
addArg("command", "Command", "register");
addArg("command", "Command", "string", "register");
}
virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
@ -1067,7 +1068,7 @@ class ArgsCommand : public AdminInterfaceCommand {
AdminInterfaceCommand *command = it->second;
BOOST_FOREACH(const AdminInterfaceCommand::Arg &arg, command->getArgs()) {
ret += arg.name + " - \"" + arg.label + "\" " + "Example: \"" + arg.example + "\"\n";
ret += arg.name + " - \"" + arg.label + "\" " + "Example: \"" + arg.example + "\" Type: \"" + arg.type + "\"\n";
}
return ret;
@ -1092,17 +1093,17 @@ AdminInterface::AdminInterface(Component *component, UserManager *userManager, N
addCommand(new OnlineUsersCommand(m_userManager));
addCommand(new OnlineUsersCountCommand(m_userManager));
addCommand(new ReloadCommand(m_component));
addCommand(new OnlineUsersPerBackendCommand(m_server));
// addCommand(new OnlineUsersPerBackendCommand(m_server));
addCommand(new HasOnlineUserCommand(m_userManager));
addCommand(new BackendsCountCommand(m_server));
addCommand(new ResMemoryCommand(m_server));
addCommand(new ShrMemoryCommand(m_server));
addCommand(new UsedMemoryCommand(m_server));
addCommand(new AverageMemoryPerUserCommand(m_server, m_userManager));
addCommand(new ResMemoryPerBackendCommand(m_server));
addCommand(new ShrMemoryPerBackendCommand(m_server));
addCommand(new UsedMemoryPerBackendCommand(m_server));
addCommand(new AverageMemoryPerUserPerBackendCommand(m_server));
// addCommand(new ResMemoryPerBackendCommand(m_server));
// addCommand(new ShrMemoryPerBackendCommand(m_server));
// addCommand(new UsedMemoryPerBackendCommand(m_server));
// addCommand(new AverageMemoryPerUserPerBackendCommand(m_server));
addCommand(new CrashedBackendsCountCommand(m_server));
addCommand(new CrashedBackendsCommand(m_server));
addCommand(new MessagesFromXMPPCommand(m_userManager));

View file

@ -99,12 +99,14 @@ class JoinRoomCommand : public AdminInterfaceCommand {
addArg("nickname",
CONFIG_STRING_DEFAULTED(cfg, "service.join_room_nickname_label", "Nickname in 3rd-party room"),
"string",
CONFIG_STRING_DEFAULTED(cfg, "service.join_room_nickname_example", "BotNickname"));
addArg("legacy_room", legacyRoomLabel, legacyRoomExample);
addArg("legacy_room", legacyRoomLabel, "string", legacyRoomExample);
addArg("legacy_server",
CONFIG_STRING_DEFAULTED(cfg, "service.join_room_server_label", "3rd-party server"),
"string",
CONFIG_STRING_DEFAULTED(cfg, "service.join_room_server_example", "3rd.party.server.org"));
addArg("slack_channel", "Slack Chanel", "mychannel");
addArg("slack_channel", "Slack Chanel", "string", "mychannel");
}
virtual std::string handleExecuteRequest(UserInfo &uinfo, User *u, std::vector<std::string> &args) {
@ -147,7 +149,7 @@ class LeaveRoomCommand : public AdminInterfaceCommand {
m_storageBackend = storageBackend;
setDescription("Leave the room");
addArg("slack_channel", "Slack Chanel", "mychannel");
addArg("slack_channel", "Slack Chanel", "string", "mychannel");
}
virtual std::string handleExecuteRequest(UserInfo &uinfo, User *u, std::vector<std::string> &args) {

View file

@ -485,7 +485,7 @@ void APIServer::serve_instances_command_args(Server *server, Server::session *se
tokens.push_back(*beg);
}
if (tokens.size() != 5) {
if (tokens.size() != 7) {
continue;
}
@ -496,6 +496,7 @@ void APIServer::serve_instances_command_args(Server *server, Server::session *se
arg.AddMember("name", tokens[0].c_str(), json.GetAllocator());
arg.AddMember("label", tokens[2].c_str(), json.GetAllocator());
arg.AddMember("example", tokens[4].c_str(), json.GetAllocator());
arg.AddMember("type", tokens[6].c_str(), json.GetAllocator());
argList.PushBack(arg, json.GetAllocator());
}
@ -574,11 +575,64 @@ void APIServer::serve_instances_execute(Server *server, Server::session *session
}
response = server->send_command(instance, command);
boost::replace_all(response, "\n", "<br/>");
if (response.find("Error:") == 0) {
send_ack(conn, false, response);
}
std::vector<std::string> fields;
boost::split(fields, response, boost::is_any_of("\n"));
if (!fields.empty() && /*fields[0].find(" - ") != std::string::npos &&*/ (fields[0].find(": ") != std::string::npos || fields[0].find(":\"") != std::string::npos)) {
Document json;
json.SetObject();
json.AddMember("error", 0, json.GetAllocator());
std::vector<std::string> tmp;
std::vector<std::string> tmp2;
Value table(kArrayType);
BOOST_FOREACH(const std::string &line, fields) {
escaped_list_separator<char> els('\\', ' ', '\"');
tokenizer<escaped_list_separator<char> > tok(line, els);
Value arg;
arg.SetObject();
std::string key;
int i = 0;
bool hasDesc = true;
for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg, ++i) {
if (i == 1 && *beg != "-") {
hasDesc = false;
}
if (i == 0) {
tmp.push_back(*beg);
arg.AddMember("Key", tmp.back().c_str(), json.GetAllocator());
}
else if (i == 2 && hasDesc) {
tmp.push_back(*beg);
arg.AddMember("Description", tmp.back().c_str(), json.GetAllocator());
}
else if (i > 1 || (!hasDesc && i > 0)) {
if (key.empty()) {
key = *beg;
}
else {
tmp.push_back(key);
tmp2.push_back(*beg);
arg.AddMember(tmp.back().c_str(), tmp2.back().c_str(), json.GetAllocator());
key = "";
}
}
}
table.PushBack(arg, json.GetAllocator());
}
json.AddMember("table", table, json.GetAllocator());
json.AddMember("message", response.c_str(), json.GetAllocator());
send_json(conn, json);
}
else {
boost::replace_all(response, "\n", "<br/>");
send_ack(conn, true, response);
}
}

View file

@ -135,7 +135,12 @@ function execute_command(instance, command) {
form += '<div class="form-group">';
form += '<label class="col-md-4 control-label" for="' + arg.name + '">' + arg.label + ':</label>';
form += '<div class="col-md-4">';
form += '<input id="command_arg' + i + '" name="command_arg' + i + '" type="text" placeholder="' + arg.example + '" class="form-control input-md"/>';
if (arg.type == "password") {
form += '<input id="command_arg' + i + '" name="command_arg' + i + '" type="password" placeholder="' + arg.example + '" class="form-control input-md"/>';
}
else {
form += '<input id="command_arg' + i + '" name="command_arg' + i + '" type="text" placeholder="' + arg.example + '" class="form-control input-md"/>';
}
form += '</div></div>';
console.log('command_arg' + i );
});
@ -211,6 +216,26 @@ function execute_command(instance, command) {
}
}
$.post(posturl, postdata, function(data) {
if (data.table) {
data.message = "<table>";
data.message += "<tr>";
for (var key in data.table[0]) {
if (data.table[0].hasOwnProperty(key)) {
data.message += "<th>" + key + "</th>";
}
}
data.message += "</tr>";
$.each(data.table, function(i, line) {
data.message += "<tr>";
for (var key in line) {
if (line.hasOwnProperty(key)) {
data.message += "<td>" + line[key] + "</td>";
}
}
data.message += "</tr>";
})
data.message += "</table>";
}
var dialog = bootbox.dialog({
title: "Command result: " + command + ".",
message: "<pre>" + data.message + "</pre>",

View file

@ -69,10 +69,10 @@ class AdminInterfaceTest : public CPPUNIT_NS :: TestFixture, public BasicSlackTe
void joinRoomArgs() {
std::string resp = sendAdminMessage("args join_room");
CPPUNIT_ASSERT_EQUAL(std::string("nickname - \"Nickname in 3rd-party room\" Example: \"BotNickname\"\n"
"legacy_room - \"3rd-party room name\" Example: \"3rd-party room name\"\n"
"legacy_server - \"3rd-party server\" Example: \"3rd.party.server.org\"\n"
"slack_channel - \"Slack Chanel\" Example: \"mychannel\"\n"), resp);
CPPUNIT_ASSERT_EQUAL(std::string("nickname - \"Nickname in 3rd-party room\" Example: \"BotNickname\" Type: \"string\"\n"
"legacy_room - \"3rd-party room name\" Example: \"3rd-party room name\" Type: \"string\"\n"
"legacy_server - \"3rd-party server\" Example: \"3rd.party.server.org\" Type: \"string\"\n"
"slack_channel - \"Slack Chanel\" Example: \"mychannel\" Type: \"string\"\n"), resp);
}
void getOAuth2URLCommand() {