CJsonNode g_LegacyStatToJson(CNetServer server, bool verbose) { const string stat_cmd(verbose ? "STAT ALL" : "STAT"); CNetServerMultilineCmdOutput output(server.ExecWithRetry(stat_cmd, true)); CJsonNode stat_info(CJsonNode::NewObjectNode()); CJsonNode jobs_by_status(CJsonNode::NewObjectNode());; stat_info.SetByKey("JobsByStatus", jobs_by_status); CJsonNode section_entries; string line; CTempString key, value; while (output.ReadLine(line)) { if (line.empty() || isspace(line[0])) continue; if (line[0] == '[') { size_t section_name_len = line.length(); while (--section_name_len > 0 && (line[section_name_len] == ':' || line[section_name_len] == ']' || isspace(line[section_name_len]))) ; line.erase(0, 1); line.resize(section_name_len); stat_info.SetByKey(line, section_entries = CJsonNode::NewArrayNode()); } else if (section_entries) { section_entries.AppendString(line); } else if (NStr::SplitInTwo(line, ":", key, value)) { value = NStr::TruncateSpaces_Unsafe(value, NStr::eTrunc_Begin); if (CNetScheduleAPI::StringToStatus(key) != CNetScheduleAPI::eJobNotFound) jobs_by_status.SetInteger(key, NStr::StringToInt8(value)); else { if (key == "Executable path" && g_FixMisplacedPID(stat_info, value, "PID")) { if (!stat_info.HasKey("Version")) stat_info.SetString("Version", "Unknown"); if (!stat_info.HasKey("Build date")) stat_info.SetString("Build date", "Unknown"); } stat_info.SetByKey(key, CJsonNode::GuessType(value)); } } } return stat_info; }
CJsonNode g_LegacyStatToJson(CNetServer server, bool verbose) { const string stat_cmd(verbose ? "STAT ALL" : "STAT"); CNetServerMultilineCmdOutput output(server.ExecWithRetry(stat_cmd)); CJsonNode stat_info(CJsonNode::NewObjectNode()); CJsonNode jobs_by_status(CJsonNode::NewObjectNode());; stat_info.SetNode("JobsByStatus", jobs_by_status); CJsonNode section_entries; string line; CTempString key, value; while (output.ReadLine(line)) { if (line.empty() || isspace(line[0])) continue; if (line[0] == '[') { size_t section_name_len = line.length(); while (--section_name_len > 0 && (line[section_name_len] == ':' || line[section_name_len] == ']' || isspace(line[section_name_len]))) ; line.erase(0, 1); line.resize(section_name_len); stat_info.SetNode(line, section_entries = CJsonNode::NewArrayNode()); } else if (section_entries) section_entries.PushString(line); else if (NStr::SplitInTwo(line, ":", key, value)) { value = NStr::TruncateSpaces(value, NStr::eTrunc_Begin); if (CNetScheduleAPI::StringToStatus(key) == CNetScheduleAPI::eJobNotFound) stat_info.SetString(key, value); else jobs_by_status.SetNumber(key, NStr::StringToInt8(value)); } } return stat_info; }