mirror of https://github.com/Icinga/icinga2.git
Merge branch 'fix/column-headers-in-json-output-5317' into next
fixes #5317
This commit is contained in:
commit
dcd732905f
|
@ -105,7 +105,6 @@ Query::Query(const std::vector<String>& lines, const String& compat_log_path)
|
||||||
size_t col_index = line.FindFirstOf(":");
|
size_t col_index = line.FindFirstOf(":");
|
||||||
String header = line.SubStr(0, col_index);
|
String header = line.SubStr(0, col_index);
|
||||||
String params;
|
String params;
|
||||||
std::vector<String> separators;
|
|
||||||
|
|
||||||
if (line.GetLength() > col_index + 2)
|
if (line.GetLength() > col_index + 2)
|
||||||
params = line.SubStr(col_index + 2);
|
params = line.SubStr(col_index + 2);
|
||||||
|
@ -116,9 +115,12 @@ Query::Query(const std::vector<String>& lines, const String& compat_log_path)
|
||||||
m_OutputFormat = params;
|
m_OutputFormat = params;
|
||||||
else if (header == "KeepAlive")
|
else if (header == "KeepAlive")
|
||||||
m_KeepAlive = (params == "on");
|
m_KeepAlive = (params == "on");
|
||||||
else if (header == "Columns")
|
else if (header == "Columns") {
|
||||||
|
m_ColumnHeaders = false; // Might be explicitly re-enabled later on
|
||||||
boost::algorithm::split(m_Columns, params, boost::is_any_of(" "));
|
boost::algorithm::split(m_Columns, params, boost::is_any_of(" "));
|
||||||
else if (header == "Separators")
|
} else if (header == "Separators") {
|
||||||
|
std::vector<String> separators;
|
||||||
|
|
||||||
boost::algorithm::split(separators, params, boost::is_any_of(" "));
|
boost::algorithm::split(separators, params, boost::is_any_of(" "));
|
||||||
/* ugly ascii long to char conversion, but works */
|
/* ugly ascii long to char conversion, but works */
|
||||||
if (separators.size() > 0)
|
if (separators.size() > 0)
|
||||||
|
@ -129,7 +131,7 @@ Query::Query(const std::vector<String>& lines, const String& compat_log_path)
|
||||||
m_Separators[2] = String(1, static_cast<char>(Convert::ToLong(separators[2])));
|
m_Separators[2] = String(1, static_cast<char>(Convert::ToLong(separators[2])));
|
||||||
if (separators.size() > 3)
|
if (separators.size() > 3)
|
||||||
m_Separators[3] = String(1, static_cast<char>(Convert::ToLong(separators[3])));
|
m_Separators[3] = String(1, static_cast<char>(Convert::ToLong(separators[3])));
|
||||||
else if (header == "ColumnHeaders")
|
} else if (header == "ColumnHeaders")
|
||||||
m_ColumnHeaders = (params == "on");
|
m_ColumnHeaders = (params == "on");
|
||||||
else if (header == "Filter") {
|
else if (header == "Filter") {
|
||||||
Filter::Ptr filter = ParseFilter(params, m_LogTimeFrom, m_LogTimeUntil);
|
Filter::Ptr filter = ParseFilter(params, m_LogTimeFrom, m_LogTimeUntil);
|
||||||
|
@ -334,23 +336,8 @@ Filter::Ptr Query::ParseFilter(const String& params, unsigned long& from, unsign
|
||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Query::PrintResultSet(std::ostream& fp, const std::vector<String>& columns, const Array::Ptr& rs)
|
void Query::PrintResultSet(std::ostream& fp, const Array::Ptr& rs)
|
||||||
{
|
{
|
||||||
if (m_OutputFormat == "csv" && m_Columns.size() == 0 && m_ColumnHeaders) {
|
|
||||||
bool first = true;
|
|
||||||
|
|
||||||
BOOST_FOREACH(const String& column, columns) {
|
|
||||||
if (first)
|
|
||||||
first = false;
|
|
||||||
else
|
|
||||||
fp << m_Separators[1];
|
|
||||||
|
|
||||||
fp << column;
|
|
||||||
}
|
|
||||||
|
|
||||||
fp << m_Separators[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_OutputFormat == "csv") {
|
if (m_OutputFormat == "csv") {
|
||||||
ObjectLock olock(rs);
|
ObjectLock olock(rs);
|
||||||
|
|
||||||
|
@ -418,15 +405,25 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
|
||||||
Array::Ptr rs = make_shared<Array>();
|
Array::Ptr rs = make_shared<Array>();
|
||||||
|
|
||||||
if (m_Aggregators.empty()) {
|
if (m_Aggregators.empty()) {
|
||||||
|
Array::Ptr header = make_shared<Array>();
|
||||||
|
|
||||||
BOOST_FOREACH(const Value& object, objects) {
|
BOOST_FOREACH(const Value& object, objects) {
|
||||||
Array::Ptr row = make_shared<Array>();
|
Array::Ptr row = make_shared<Array>();
|
||||||
|
|
||||||
BOOST_FOREACH(const String& columnName, columns) {
|
BOOST_FOREACH(const String& columnName, columns) {
|
||||||
Column column = table->GetColumn(columnName);
|
Column column = table->GetColumn(columnName);
|
||||||
|
|
||||||
|
if (m_ColumnHeaders)
|
||||||
|
header->Add(columnName);
|
||||||
|
|
||||||
row->Add(column.ExtractValue(object));
|
row->Add(column.ExtractValue(object));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_ColumnHeaders) {
|
||||||
|
rs->Add(header);
|
||||||
|
m_ColumnHeaders = false;
|
||||||
|
}
|
||||||
|
|
||||||
rs->Add(row);
|
rs->Add(row);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -443,6 +440,21 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* add column headers both for raw and aggregated data */
|
||||||
|
if (m_ColumnHeaders) {
|
||||||
|
Array::Ptr header = make_shared<Array>();
|
||||||
|
|
||||||
|
BOOST_FOREACH(const String& columnName, m_Columns) {
|
||||||
|
header->Add(columnName);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 1; i < m_Aggregators.size(); i++) {
|
||||||
|
header->Add("stats_" + Convert::ToString(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
rs->Add(header);
|
||||||
|
}
|
||||||
|
|
||||||
Array::Ptr row = make_shared<Array>();
|
Array::Ptr row = make_shared<Array>();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -461,12 +473,10 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
|
||||||
row->Add(stats[i]);
|
row->Add(stats[i]);
|
||||||
|
|
||||||
rs->Add(row);
|
rs->Add(row);
|
||||||
|
|
||||||
m_ColumnHeaders = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostringstream result;
|
std::ostringstream result;
|
||||||
PrintResultSet(result, columns, rs);
|
PrintResultSet(result, rs);
|
||||||
|
|
||||||
SendResponse(stream, LivestatusErrorOK, result.str());
|
SendResponse(stream, LivestatusErrorOK, result.str());
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,7 +83,7 @@ private:
|
||||||
unsigned long m_LogTimeUntil;
|
unsigned long m_LogTimeUntil;
|
||||||
String m_CompatLogPath;
|
String m_CompatLogPath;
|
||||||
|
|
||||||
void PrintResultSet(std::ostream& fp, const std::vector<String>& columns, const Array::Ptr& rs);
|
void PrintResultSet(std::ostream& fp, const Array::Ptr& rs);
|
||||||
void PrintCsvArray(std::ostream& fp, const Array::Ptr& array, int level);
|
void PrintCsvArray(std::ostream& fp, const Array::Ptr& array, int level);
|
||||||
|
|
||||||
void ExecuteGetHelper(const Stream::Ptr& stream);
|
void ExecuteGetHelper(const Stream::Ptr& stream);
|
||||||
|
|
Loading…
Reference in New Issue