[Fix](profile) fix /rest/v1/query_profile action. (#15981)

Co-authored-by: wangxiangyu@360shuke.com <wangxiangyu@360shuke.com>
This commit is contained in:
wxy
2023-01-17 20:21:48 +08:00
committed by GitHub
parent 02a7995171
commit 061b28b32e
2 changed files with 35 additions and 16 deletions

View File

@ -32,8 +32,8 @@ import org.apache.commons.lang3.tuple.Triple;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
@ -85,7 +85,7 @@ public class ProfileManager {
LOAD,
}
public static final ArrayList<String> PROFILE_HEADERS = new ArrayList(
public static final List<String> PROFILE_HEADERS = Collections.unmodifiableList(
Arrays.asList(JOB_ID, QUERY_ID, USER, DEFAULT_DB, SQL_STATEMENT, QUERY_TYPE,
START_TIME, END_TIME, TOTAL_TIME, QUERY_STATE, TRACE_ID));

View File

@ -41,13 +41,14 @@ import java.util.Map;
public class QueryProfileController extends BaseController {
private static final Logger LOG = LogManager.getLogger(QueryProfileController.class);
private static final String QUERY_ID = "query_id";
private static final String ID = "id";
private static final String DETAIL_COL = "Detail";
@RequestMapping(path = "/query_profile/{" + QUERY_ID + "}", method = RequestMethod.GET)
public Object profile(@PathVariable(value = QUERY_ID) String queryId) {
String profile = ProfileManager.getInstance().getProfile(queryId);
@RequestMapping(path = "/query_profile/{" + ID + "}", method = RequestMethod.GET)
public Object profile(@PathVariable(value = ID) String id) {
String profile = ProfileManager.getInstance().getProfile(id);
if (profile == null) {
return ResponseEntityBuilder.okWithCommonError("Query " + queryId + " does not exist");
return ResponseEntityBuilder.okWithCommonError("ID " + id + " does not exist");
}
profile = profile.replaceAll("\n", "</br>");
profile = profile.replaceAll(" ", "&nbsp;&nbsp;");
@ -65,33 +66,51 @@ public class QueryProfileController extends BaseController {
private void addFinishedQueryInfo(Map<String, Object> result) {
List<List<String>> finishedQueries = ProfileManager.getInstance().getAllQueries();
List<String> columnHeaders = ProfileManager.PROFILE_HEADERS;
int queryIdIndex = 0; // the first column is 'Query ID' by default
List<String> columnHeaders = Lists.newLinkedList(ProfileManager.PROFILE_HEADERS);
int jobIdIndex = -1;
int queryIdIndex = -1;
int queryTypeIndex = -1;
for (int i = 0; i < columnHeaders.size(); ++i) {
if (columnHeaders.get(i).equals(ProfileManager.JOB_ID)) {
jobIdIndex = i;
continue;
}
if (columnHeaders.get(i).equals(ProfileManager.QUERY_ID)) {
queryIdIndex = i;
break;
continue;
}
if (columnHeaders.get(i).equals(ProfileManager.QUERY_TYPE)) {
queryTypeIndex = i;
continue;
}
}
// set href as the first column
columnHeaders.add(0, DETAIL_COL);
result.put("column_names", columnHeaders);
result.put("href_column", Lists.newArrayList(ProfileManager.QUERY_ID));
result.put("href_column", Lists.newArrayList(DETAIL_COL));
List<Map<String, Object>> list = Lists.newArrayList();
result.put("rows", list);
for (List<String> row : finishedQueries) {
String queryId = row.get(queryIdIndex);
List<String> realRow = Lists.newLinkedList(row);
String queryType = realRow.get(queryTypeIndex);
String id = "Query".equals(queryType) ? realRow.get(queryIdIndex) : realRow.get(jobIdIndex);
realRow.add(0, id);
Map<String, Object> rowMap = new HashMap<>();
for (int i = 0; i < row.size(); ++i) {
rowMap.put(columnHeaders.get(i), row.get(i));
for (int i = 0; i < realRow.size(); ++i) {
rowMap.put(columnHeaders.get(i), realRow.get(i));
}
// add hyper link
if (Strings.isNullOrEmpty(queryId)) {
if (Strings.isNullOrEmpty(id)) {
rowMap.put("__hrefPaths", Lists.newArrayList("/query_profile/-1"));
} else {
rowMap.put("__hrefPaths", Lists.newArrayList("/query_profile/" + queryId));
rowMap.put("__hrefPaths", Lists.newArrayList("/query_profile/" + id));
}
list.add(rowMap);
}
}